Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Some fixes to make the anonymous forms work again with recaptcha

Roland Geider6c1b2fa3a0fd
diff --git a/extras/docker/development/settings.py b/extras/docker/development/settings.py
index 660c2a90d..e17484cd9 100644
--- a/extras/docker/development/settings.py
+++ b/extras/docker/development/settings.py
@@ -42,7 +42,6 @@ SECRET_KEY = os.environ.get("SECRET_KEY", 'wger-django-secret-key')
# Your reCaptcha keys
RECAPTCHA_PUBLIC_KEY = os.environ.get('RECAPTCHA_PUBLIC_KEY', '')
RECAPTCHA_PRIVATE_KEY = os.environ.get('RECAPTCHA_PRIVATE_KEY', '')
-NOCAPTCHA = os.environ.get('NOCAPTCHA', '')
# The site's URL (e.g. http://www.my-local-gym.com or http://localhost:8000)
# This is needed for uploaded files and images (exercise images, etc.) to be
diff --git a/requirements.txt b/requirements.txt
index 8f9326ea5..2bcba35c5 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,7 +6,7 @@
bleach~=3.3
django-bootstrap-breadcrumbs~=0.9
django-formtools~=2.2
-django-recaptcha==2.0.6
+django-recaptcha~=2.0.6
Django~=3.1.8
django-crispy-forms~=1.11
django_compressor~=2.4
diff --git a/wger/core/forms.py b/wger/core/forms.py
index 3ec666a91..8cc70c1e6 100644
--- a/wger/core/forms.py
+++ b/wger/core/forms.py
@@ -33,6 +33,7 @@ from django.utils.translation import ugettext as _
# Third Party
from captcha.fields import ReCaptchaField
+from captcha.widgets import ReCaptchaV3
from crispy_forms.bootstrap import (
Accordion,
AccordionGroup
@@ -205,15 +206,28 @@ class PasswordConfirmationForm(Form):
class RegistrationForm(UserCreationForm, UserEmailForm):
"""
- Registration form
+ Registration form with recaptcha field
"""
- # Manually set the language to 'en', otherwise the language used seems to
- # randomly one of the application languages. This also appears to happen
- # only on wger.de, perhaps because there the application is behind a reverse
- # proxy. See #281.
- captcha = ReCaptchaField(label=_('Confirmation text'),
- help_text=_('As a security measure, please enter the previous words'))
+ captcha = ReCaptchaField(widget=ReCaptchaV3,
+ label='reCaptcha',
+ help_text=_('Form is secured with reCaptcha'))
+
+ def __init__(self, *args, **kwargs):
+ super(RegistrationForm, self).__init__(*args, **kwargs)
+ self.helper = FormHelper()
+ self.helper.form_class = 'wger-form'
+ self.helper.layout = Layout(
+ 'username',
+ 'email',
+ Row(
+ Column('password1', css_class='form-group col-6 mb-0'),
+ Column('password2', css_class='form-group col-6 mb-0'),
+ css_class='form-row'
+ ),
+ 'captcha',
+ ButtonHolder(Submit('submit', _("Register"), css_class='btn-success btn-block'))
+ )
class RegistrationFormNoCaptcha(UserCreationForm, UserEmailForm):
@@ -259,5 +273,6 @@ class FeedbackAnonymousForm(FeedbackRegisteredForm):
"""
Feedback form used for anonymous users (has additionally a reCaptcha field)
"""
- captcha = ReCaptchaField(label=_('Confirmation text'),
- help_text=_('As a security measure, please enter the previous words'),)
+ captcha = ReCaptchaField(widget=ReCaptchaV3,
+ label='reCaptcha',
+ help_text=_('Form is secured with reCaptcha'))
diff --git a/wger/core/views/misc.py b/wger/core/views/misc.py
index 93614bb2e..cc6ad8d2e 100644
--- a/wger/core/views/misc.py
+++ b/wger/core/views/misc.py
@@ -31,10 +31,15 @@ from django.urls import (
reverse,
reverse_lazy
)
+from django.utils.text import slugify
from django.utils.translation import ugettext as _
from django.views.generic import TemplateView
from django.views.generic.edit import FormView
+# Third Party
+from crispy_forms.helper import FormHelper
+from crispy_forms.layout import Submit
+
# wger
from wger.core.demo import (
create_demo_entries,
@@ -178,10 +183,6 @@ class FeedbackClass(FormView):
"""
context = super(FeedbackClass, self).get_context_data(**kwargs)
context['title'] = _('Feedback')
- # TODO: change template so it iterates through form and not formfields
- context['form_fields'] = context['form']
- context['submit_text'] = _('Send')
- context['contribute_url'] = reverse('software:contribute')
return context
def get_form_class(self):
@@ -194,16 +195,23 @@ class FeedbackClass(FormView):
else:
return FeedbackRegisteredForm
+ def get_form(self, form_class=None):
+ """Return an instance of the form to be used in this view."""
+
+ form = super(FeedbackClass, self).get_form(form_class)
+ form.helper = FormHelper()
+ form.helper.form_id = slugify(self.request.path)
+ form.helper.add_input(Submit('submit', _('Submit'), css_class='btn-success btn-block'))
+ form.helper.form_class = 'wger-form'
+ return form
+
def form_valid(self, form):
"""
Send the feedback to the administrators
"""
messages.success(self.request, _('Your feedback was successfully sent. Thank you!'))
- context = {}
- context['user'] = self.request.user
- context['form_data'] = form.cleaned_data
-
+ context = {'user': self.request.user, 'form_data': form.cleaned_data}
subject = 'New feedback'
message = render_to_string('user/email_feedback.html', context)
mail.mail_admins(subject, message)
diff --git a/wger/manager/forms.py b/wger/manager/forms.py
index 578bdd803..68540050c 100644
--- a/wger/manager/forms.py
+++ b/wger/manager/forms.py
@@ -37,7 +37,6 @@ from django.utils.translation import (
)
# Third Party
-from captcha.fields import ReCaptchaField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import (
Column,
@@ -67,11 +66,6 @@ from wger.utils.widgets import (
)
-class DemoUserForm(Form):
- captcha = ReCaptchaField(label=_('Confirmation text'),
- help_text=_('As a security measure, please enter the previous words'),)
-
-
class WorkoutForm(ModelForm):
class Meta:
model = Workout