Skip to content

Commit

Permalink
[0.5.0] Add settings module, option for invisible recaptcha.
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Rogers <luke@dmptr.com>
  • Loading branch information
dmptrluke committed Jan 11, 2020
1 parent 62f5ce5 commit 86e0afa
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion foxtail_contact/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" A simple contact form for Django """
__version__ = '0.4.1'
__version__ = '0.5.0'

default_app_config = "foxtail_contact.apps.ContactConfig"
17 changes: 10 additions & 7 deletions foxtail_contact/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from django.conf import settings
from django.forms import CharField, EmailField, Form, Textarea

from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Invisible
from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Column, Layout, Row
from csp_helpers.mixins import CSPFormMixin

from . import settings as app_settings


class ContactForm(CSPFormMixin, Form):
if settings.RECAPTCHA_ENABLED:
captcha = ReCaptchaField()
if app_settings.RECAPTCHA_ENABLED:
if app_settings.RECAPTCHA_INVISIBLE:
captcha = ReCaptchaField(widget=ReCaptchaV2Invisible)
else:
captcha = ReCaptchaField()

name = CharField(required=True)
email = EmailField(required=True)
Expand All @@ -24,7 +29,7 @@ def __init__(self, *args, **kwargs):
self.helper.form_tag = False
self.helper.error_text_inline = False

if settings.RECAPTCHA_ENABLED:
if app_settings.RECAPTCHA_ENABLED:
self.fields['captcha'].label = False

self.helper.layout = Layout(
Expand All @@ -35,7 +40,5 @@ def __init__(self, *args, **kwargs):
Row(
Column('message', css_class='col-md-12'),
),
Row(
Column('captcha', css_class='col-md-12'),
) if settings.RECAPTCHA_ENABLED else HTML('<!-- security! -->')
'captcha' if app_settings.RECAPTCHA_ENABLED else HTML('<!-- security! -->')
)
4 changes: 4 additions & 0 deletions foxtail_contact/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.conf import settings

RECAPTCHA_ENABLED = getattr(settings, 'RECAPTCHA_ENABLED', True)
RECAPTCHA_INVISIBLE = getattr(settings, 'RECAPTCHA_INVISIBLE', False)
5 changes: 5 additions & 0 deletions foxtail_contact/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class ContactView(CSPViewMixin, FormView):
form_class = ContactForm
success_url = reverse_lazy('contact:contact')

def get_initial(self):
initial = super().get_initial()
initial['email'] = self.request.GET.get('email', None)
return initial

def form_valid(self, form):
context = {
'name': form.cleaned_data['name'],
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ requires = [
'django>=2.2',
'django-mail-templated-simple',
'django-crispy-forms',
'django-recaptcha',
'django-recaptcha-foxtail',
'django-csp-helpers>=0.5.0'
]

Expand Down

0 comments on commit 86e0afa

Please sign in to comment.