-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforms.py
More file actions
52 lines (42 loc) · 1.81 KB
/
forms.py
File metadata and controls
52 lines (42 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
from django import forms
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm as _AuthenticationForm
from api.utils import send_activation_email
User = get_user_model()
class AuthenticationForm(_AuthenticationForm):
def confirm_login_allowed(self, user):
if not user.is_active and user.last_login is None:
send_activation_email(self.request, user)
raise ValidationError(
_('The account is not activated, please check the activation email.'),
code="inactive",
)
return super().confirm_login_allowed(user)
class RegistrationForm(UserCreationForm):
h_captcha_token = forms.CharField(
label=_("h-captcha token"),
widget=forms.HiddenInput(attrs={"name": "h-captcha-response"}),
strip=False,
required=False,
help_text=_("Google recaptcha token hidden field"),
)
def clean_h_captcha_token(self):
h_captcha_token = self.data.get("h-captcha-response")
if settings.H_CAPTCHA_KEY and settings.H_CAPTCHA_SECRET:
success = requests.post('https://hcaptcha.com/siteverify', data={
'secret': settings.H_CAPTCHA_SECRET,
'response': h_captcha_token,
}).json()["success"]
if not success:
raise ValidationError(
_('Error verifying HCAPTCHA, please try again.'),
code="captcha_invalid",
)
return h_captcha_token
class Meta(UserCreationForm.Meta):
model = User
fields = ('username', 'email', 'password1', 'password2')