|
1 | 1 | """Form models for the users app.""" |
2 | 2 |
|
3 | | -from typing import cast |
| 3 | +from importlib.util import find_spec |
| 4 | +from typing import Optional, cast |
4 | 5 |
|
5 | 6 | from django import forms |
6 | 7 | from django.contrib.auth.models import User |
7 | 8 | from django.contrib.auth.validators import UnicodeUsernameValidator |
8 | 9 |
|
| 10 | +from allauth.account.forms import ResetPasswordForm, SignupForm |
| 11 | + |
9 | 12 | from MangAdventure.validators import FileSizeValidator |
10 | 13 |
|
11 | 14 | from .models import UserProfile |
12 | 15 |
|
| 16 | +if find_spec('sentry_sdk'): # pragma: no cover |
| 17 | + from sentry_sdk import capture_message, configure_scope |
| 18 | + |
| 19 | + def _log_honeypot(message: str, username: Optional[str], email: str): |
| 20 | + with configure_scope() as scope: |
| 21 | + scope.set_tag('username', username) |
| 22 | + scope.set_tag('email', email) |
| 23 | + capture_message(message, 'warning', scope) |
| 24 | +else: # pragma: no cover |
| 25 | + from django.core.mail import mail_admins |
| 26 | + |
| 27 | + def _log_honeypot(message: str, username: Optional[str], email: str): |
| 28 | + body = f'Username: {username or "N/A"}\nE-mail: {email}' |
| 29 | + mail_admins(message, body, fail_silently=True) |
| 30 | + |
| 31 | + |
| 32 | +class RegistrationForm(SignupForm): # pragma: no cover |
| 33 | + """Registration form with a honeypot field.""" |
| 34 | + email2 = forms.EmailField( |
| 35 | + label='Email (again)', |
| 36 | + required=False, |
| 37 | + widget=forms.EmailInput( |
| 38 | + attrs={ |
| 39 | + 'placeholder': 'Email address confirmation' |
| 40 | + } |
| 41 | + ) |
| 42 | + ) |
| 43 | + |
| 44 | + def clean(self): |
| 45 | + result = super().clean() |
| 46 | + if self.cleaned_data.get('email2'): |
| 47 | + msg = 'Possible spam bot detected' |
| 48 | + username = self.cleaned_data['username'] |
| 49 | + email = self.cleaned_data['email'] |
| 50 | + _log_honeypot(msg, username, email) |
| 51 | + raise forms.ValidationError('Nope!') |
| 52 | + return result |
| 53 | + |
| 54 | + |
| 55 | +class PasswordResetForm(ResetPasswordForm): # pragma: no cover |
| 56 | + """Password reset form with a honeypot field.""" |
| 57 | + email2 = forms.EmailField( |
| 58 | + label='Email (again)', |
| 59 | + required=False, |
| 60 | + widget=forms.EmailInput( |
| 61 | + attrs={ |
| 62 | + 'placeholder': 'Email address confirmation' |
| 63 | + } |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + def clean(self): |
| 68 | + result = super().clean() |
| 69 | + if self.cleaned_data.get('email2'): |
| 70 | + msg = 'Possible spam bot detected' |
| 71 | + email = self.cleaned_data['email'] |
| 72 | + _log_honeypot(msg, None, email) |
| 73 | + raise forms.ValidationError('Nope!') |
| 74 | + return result |
| 75 | + |
13 | 76 |
|
14 | 77 | class UserProfileForm(forms.ModelForm): |
15 | 78 | """Form used for editing a :class:`~users.models.UserProfile` model.""" |
16 | 79 | #: The user's e-mail address. |
17 | 80 | email = forms.EmailField( |
18 | 81 | max_length=254, min_length=5, label='E-mail', |
19 | | - widget=forms.TextInput(attrs={ |
| 82 | + widget=forms.EmailInput(attrs={ |
20 | 83 | 'placeholder': 'E-mail address' |
21 | 84 | }) |
22 | 85 | ) |
@@ -166,4 +229,4 @@ class Meta: |
166 | 229 | ) |
167 | 230 |
|
168 | 231 |
|
169 | | -__all__ = ['UserProfileForm'] |
| 232 | +__all__ = ['RegistrationForm', 'PasswordResetForm', 'UserProfileForm'] |
0 commit comments