Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple captcha #451

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions coderedcms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import csv
import os
import re
from captcha.fields import CaptchaField
from django import forms
from django.core.exceptions import ValidationError
from django.db import models
Expand Down Expand Up @@ -107,10 +108,21 @@ class CoderedTimeField(forms.TimeField):


class CoderedFormBuilder(FormBuilder):
CAPTCHA_FIELD_NAME = 'wagtailcaptcha'
"""
Enhance wagtail FormBuilder with additional custom fields.
"""

# The following function is sourced from https://github.com/acarasimon96/wagtail-django-simple-captcha
# with small tweeks to integrate with the CodeRedCMS codebase (under the MIT license)
@property
def formfields(self):
# Add wagtailcaptcha to formfields property
fields = super(CoderedFormBuilder, self).formfields
fields[self.CAPTCHA_FIELD_NAME] = CaptchaField(label='')

return fields

def create_file_field(self, field, options):
return SecureFileField(**options)

Expand All @@ -123,6 +135,12 @@ def create_datetime_field(self, field, options):
def create_time_field(self, field, options):
return CoderedTimeField(**options)

# The following function is sourced from https://github.com/acarasimon96/wagtail-django-simple-captcha
# with small tweeks to integrate with the CodeRedCMS codebase (under the MIT license)
@staticmethod
def remove_captcha_field(form):
form.fields.pop(CoderedFormBuilder.CAPTCHA_FIELD_NAME, None)
form.cleaned_data.pop(CoderedFormBuilder.CAPTCHA_FIELD_NAME, None)

class CoderedSubmissionsListView(WagtailSubmissionsListView):
def get_csv_response(self, context):
Expand Down
12 changes: 11 additions & 1 deletion coderedcms/models/page_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,16 @@ def __init__(self, *args, **kwargs):
name, ext = os.path.splitext(self.template)
self.landing_page_template = name + '_landing' + ext

# The following function is sourced from https://github.com/acarasimon96/wagtail-django-simple-captcha
# with small tweeks to integrate with the CodeRedCMS codebase (under the MIT License)
def process_form_submission(self, request, form, form_submission, processed_data):
"""
Change process_form_submission function such that
captcha is removed upon submission
"""
CoderedFormBuilder.remove_captcha_field(form)
return super(CoderedFormPage, self).process_form_submission(request, form, form_submission, processed_data)

def get_form_fields(self):
"""
Form page expects `form_fields` to be declared.
Expand Down Expand Up @@ -1685,7 +1695,7 @@ class Meta:
CoderedFormMixin.body_content_panels + [
InlinePanel('confirmation_emails', label=_('Confirmation Emails'))
]

def process_form_post(self, form, request):
if form.is_valid():
is_complete = self.steps.update_data()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
# Application definition

INSTALLED_APPS = [
#django-simple-catpcha
'captcha',

# This project
'website',

Expand Down
4 changes: 4 additions & 0 deletions coderedcms/project_template/basic/project_name/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
# Search
path('search/', include(coderedsearch_urls)),

# Captcha
path('captcha/', include('captcha.urls')),

# For anything not caught by a more specific rule above, hand over to
# the page serving mechanism. This should be the last pattern in
# the list:
Expand All @@ -25,6 +28,7 @@
# Alternatively, if you want CMS pages to be served from a subpath
# of your site, rather than the site root:
# re_path(r"^pages/", include(codered_urls)),

]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""
{% endcomment %}
{% verbatim %}
# Generated by Django 3.2.7 on 2021-10-01 17:27

import coderedcms.blocks.base_blocks
import coderedcms.blocks.html_blocks
from django.conf import settings
Expand Down Expand Up @@ -136,4 +138,5 @@ class Migration(migrations.Migration):
bases=('coderedcms.coderedpage',),
),
]

{% endverbatim %}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# Application definition

INSTALLED_APPS = [
#django-simple-captcha
'captcha',

# This project
'website',

Expand Down Expand Up @@ -61,6 +64,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
"django.contrib.sitemaps",

]

MIDDLEWARE = [
Expand Down
3 changes: 3 additions & 0 deletions coderedcms/project_template/sass/project_name/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
# Search
path('search/', include(coderedsearch_urls)),

# Captcha
path('captcha/', include('captcha.urls')),

# For anything not caught by a more specific rule above, hand over to
# the page serving mechanism. This should be the last pattern in
# the list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""
{% endcomment %}
{% verbatim %}
# Generated by Django 3.2.7 on 2021-10-01 17:27

import coderedcms.blocks.base_blocks
import coderedcms.blocks.html_blocks
from django.conf import settings
Expand Down Expand Up @@ -136,4 +138,5 @@ class Migration(migrations.Migration):
bases=('coderedcms.coderedpage',),
),
]

{% endverbatim %}
3 changes: 2 additions & 1 deletion coderedcms/templates/coderedcms/pages/form_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
<form class='{{ page.form_css_class }}' id='{{ page.form_id }}' action="{% pageurl self %}" method="POST" {% if form|is_file_form %}enctype="multipart/form-data"{% endif %}>
{% csrf_token %}

{% bootstrap_form form layout='horizontal' %}
{% bootstrap_form form layout='horizontal' exclude='wagtailcaptcha'%}

{% block captcha %}
{% if page.spam_protection %}
{% include 'coderedcms/includes/form_honeypot.html' %}
{% bootstrap_field form.wagtailcaptcha layout='horizontal' %}
{% endif %}
{% endblock %}

Expand Down
3 changes: 3 additions & 0 deletions coderedcms/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
'django.contrib.messages',
'django.contrib.staticfiles',
"django.contrib.sitemaps",

#django-simple-catpcha
'captcha',
]

MIDDLEWARE = [
Expand Down
3 changes: 3 additions & 0 deletions coderedcms/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# Search
path('search/', include(coderedsearch_urls)),

# Captcha
path('captcha/', include('captcha.urls')),

# For anything not caught by a more specific rule above, hand over to
# the page serving mechanism. This should be the last pattern in
# the list:
Expand Down
2 changes: 1 addition & 1 deletion docs/features/page_types/form_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ Settings Tab

* **Form go live date/time**: The optional date/time the form will start appearing on the page.
* **Form expiry date/time**: The optional date/time the form will stop appearing on the page.
* **Spam Protection**: When toggled on, this will engage spam protection techniques to attempt to reduce spam form submissions.
* **Spam Protection**: When toggled on, this will engage spam protection techniques to attempt to reduce spam form submissions and add a captcha to the form.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
libsass
twine
wheel
django-simple-captcha