Skip to content

Commit

Permalink
added captcha option to password reset form
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Feb 14, 2024
1 parent a088697 commit 57d7bda
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
16 changes: 14 additions & 2 deletions cookbook/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from allauth.account.forms import ResetPasswordForm, SignupForm
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
Expand All @@ -9,6 +10,8 @@
from django_scopes.forms import SafeModelChoiceField, SafeModelMultipleChoiceField
from hcaptcha.fields import hCaptchaField



from .models import (Comment, Food, InviteLink, Keyword, Recipe, RecipeBook, RecipeBookEntry,
SearchPreference, Space, Storage, Sync, User, UserPreference)

Expand Down Expand Up @@ -313,12 +316,12 @@ class SpaceJoinForm(forms.Form):
token = forms.CharField()


class AllAuthSignupForm(forms.Form):
class AllAuthSignupForm(SignupForm):
captcha = hCaptchaField()
terms = forms.BooleanField(label=_('Accept Terms and Privacy'))

def __init__(self, **kwargs):
super(AllAuthSignupForm, self).__init__(**kwargs)
super().__init__(**kwargs)
if settings.PRIVACY_URL == '' and settings.TERMS_URL == '':
self.fields.pop('terms')
if settings.HCAPTCHA_SECRET == '':
Expand All @@ -328,6 +331,15 @@ def signup(self, request, user):
pass


class CustomPasswordResetForm(ResetPasswordForm):
captcha = hCaptchaField()

def __init__(self, **kwargs):
super(CustomPasswordResetForm, self).__init__(**kwargs)
if settings.HCAPTCHA_SECRET == '':
self.fields.pop('captcha')


class UserCreateForm(forms.Form):
name = forms.CharField(label='Username')
password = forms.CharField(
Expand Down
9 changes: 9 additions & 0 deletions cookbook/templates/account/password_reset.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ <h3>{% trans "Password Reset" %}</h3>
</div>
</div>

<div class="row mt-3">
<div class="col-sm-12 col-lg-6 col-md-6 offset-lg-3 offset-md-3 text-center">
<a href="{% url 'account_login' %}">{% trans "Sign In" %}</a>
{% if SIGNUP_ENABLED %}
- <a href="{% url 'account_signup' %}">{% trans "Sign Up" %}</a>
{% endif %}
</div>
</div>


{% endblock %}
27 changes: 24 additions & 3 deletions cookbook/templates/account/password_reset_done.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@
{% block title %}{% trans "Password Reset" %}{% endblock %}

{% block content %}
<h3>{% trans "Password Reset" %}</h3>


{% if user.is_authenticated %}
{% include "account/snippets/already_logged_in.html" %}
{% include "account/snippets/already_logged_in.html" %}
{% endif %}

<p>{% blocktrans %}We have sent you an e-mail. Please contact us if you do not receive it within a few minutes.{% endblocktrans %}</p>
<div class="row">
<div class="col-12" style="text-align: center">
<h3>{% trans "Password Reset" %}</h3>
</div>
</div>

<div class="row">
<div class="col-sm-12 col-lg-6 col-md-6 offset-lg-3 offset-md-3">
<hr>
<p>{% blocktrans %}We have sent you an e-mail. Please contact us if you do not receive it within a few minutes.{% endblocktrans %}</p>
</div>
</div>

<div class="row mt-3">
<div class="col-sm-12 col-lg-6 col-md-6 offset-lg-3 offset-md-3 text-center">
<a href="{% url 'account_login' %}">{% trans "Sign In" %}</a>
{% if SIGNUP_ENABLED %}
- <a href="{% url 'account_signup' %}">{% trans "Sign Up" %}</a>
{% endif %}
</div>
</div>

{% endblock %}
8 changes: 6 additions & 2 deletions recipes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@
SHARING_ABUSE = bool(int(os.getenv('SHARING_ABUSE', False)))
SHARING_LIMIT = int(os.getenv('SHARING_LIMIT', 0))

ACCOUNT_SIGNUP_FORM_CLASS = 'cookbook.forms.AllAuthSignupForm'

DRF_THROTTLE_RECIPE_URL_IMPORT = os.getenv('DRF_THROTTLE_RECIPE_URL_IMPORT', '60/hour')

TERMS_URL = os.getenv('TERMS_URL', '')
Expand Down Expand Up @@ -556,6 +554,12 @@
ACCOUNT_EMAIL_SUBJECT_PREFIX = os.getenv(
'ACCOUNT_EMAIL_SUBJECT_PREFIX', '[Tandoor Recipes] ') # allauth sender prefix

# ACCOUNT_SIGNUP_FORM_CLASS = 'cookbook.forms.AllAuthSignupForm'
ACCOUNT_FORMS = {
'signup': 'cookbook.forms.AllAuthSignupForm',
'reset_password': 'cookbook.forms.CustomPasswordResetForm'
}

ACCOUNT_EMAIL_UNKNOWN_ACCOUNTS = False
ACCOUNT_RATE_LIMITS = {
"change_password": "1/m/user",
Expand Down

0 comments on commit 57d7bda

Please sign in to comment.