-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add reCAPTCHA * Update translations * Add forms for login, register, etc.
- Loading branch information
1 parent
fb944b4
commit 0e4b581
Showing
19 changed files
with
412 additions
and
282 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from django import forms | ||
from django.contrib.auth import get_user_model | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from donations.forms.common import ReCaptchaMixin, TwoPasswordMixin | ||
|
||
|
||
class LoginForm(forms.Form, ReCaptchaMixin): | ||
email = forms.EmailField(required=True) | ||
password = forms.CharField(widget=forms.PasswordInput(), required=True, max_length=150) | ||
|
||
class Meta: | ||
fields = ["email", "password"] | ||
|
||
def clean_email(self): | ||
email = self.cleaned_data["email"] | ||
if not email: | ||
raise forms.ValidationError(_("Email is required")) | ||
return email | ||
|
||
def clean_password(self): | ||
password = self.cleaned_data["password"] | ||
if not password: | ||
raise forms.ValidationError(_("Password is required")) | ||
return password | ||
|
||
|
||
class RegisterForm(forms.ModelForm, ReCaptchaMixin, TwoPasswordMixin): | ||
password_confirm = forms.CharField(widget=forms.PasswordInput(), required=True, max_length=150) | ||
|
||
class Meta: | ||
model = get_user_model() | ||
fields = [ | ||
"first_name", | ||
"last_name", | ||
"email", | ||
"password", | ||
"password_confirm", | ||
] | ||
|
||
def clean_first_name(self): | ||
first_name = self.cleaned_data["first_name"] | ||
if not first_name: | ||
raise forms.ValidationError(_("First name is required")) | ||
return first_name | ||
|
||
def clean_last_name(self): | ||
last_name = self.cleaned_data["last_name"] | ||
if not last_name: | ||
raise forms.ValidationError(_("Last name is required")) | ||
return last_name | ||
|
||
def clean_email(self): | ||
email = self.cleaned_data["email"] | ||
if not email: | ||
raise forms.ValidationError(_("Email is required")) | ||
return email | ||
|
||
|
||
class ForgotPasswordForm(forms.Form, ReCaptchaMixin): | ||
email = forms.EmailField(required=True) | ||
|
||
class Meta: | ||
fields = ["email"] | ||
|
||
def clean_email(self): | ||
email = self.cleaned_data["email"] | ||
if not email: | ||
raise forms.ValidationError(_("Email is required")) | ||
return email | ||
|
||
|
||
class ResetPasswordForm(forms.Form, ReCaptchaMixin, TwoPasswordMixin): | ||
password = forms.CharField(widget=forms.PasswordInput(), required=True, max_length=150) | ||
password_confirm = forms.CharField(widget=forms.PasswordInput(), required=True, max_length=150) | ||
|
||
class Meta: | ||
fields = ["password", "password_confirm"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from secrets import compare_digest | ||
from typing import Dict | ||
|
||
from django import forms | ||
from django.conf import settings | ||
from django.utils.translation import gettext_lazy as _ | ||
from django_recaptcha.fields import ReCaptchaField | ||
from django_recaptcha.widgets import ReCaptchaV2Invisible | ||
|
||
|
||
class ReCaptchaMixin: | ||
fields: Dict | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.init_captcha() | ||
|
||
def init_captcha(self): | ||
if settings.RECAPTCHA_ENABLED: | ||
self.fields["g-recaptcha-response"] = ReCaptchaField(widget=ReCaptchaV2Invisible) | ||
|
||
|
||
class TwoPasswordMixin: | ||
cleaned_data: Dict | ||
password: str | ||
password_confirm: str | ||
|
||
def clean_password(self): | ||
password = self.cleaned_data["password"] | ||
if not password: | ||
raise forms.ValidationError(_("Password is required")) | ||
return password | ||
|
||
def clean_password_confirm(self): | ||
password = self.cleaned_data.get("password") | ||
password_confirm = self.cleaned_data.get("password_confirm") | ||
|
||
if not compare_digest(password, password_confirm): | ||
raise forms.ValidationError(_("Passwords do not match")) | ||
|
||
return password_confirm |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from urllib.error import HTTPError | ||
|
||
from django.conf import settings | ||
from django_recaptcha import client as captcha | ||
|
||
|
||
def validate_captcha(request): | ||
""" | ||
Validates the captcha | ||
""" | ||
if request.method != "POST": | ||
return False | ||
|
||
captcha_response = request.POST.get("g-recaptcha-response") | ||
if not captcha_response: | ||
return False | ||
|
||
try: | ||
check_captcha = captcha.submit(captcha_response, settings.RECAPTCHA_PRIVATE_KEY, {}) | ||
except HTTPError: | ||
return False | ||
|
||
if not check_captcha.is_valid: | ||
return False | ||
|
||
return True |
Oops, something went wrong.