From 4b3aad2cea7424b7b40e07f4db0984f5c7cf6d89 Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Thu, 28 Feb 2013 15:33:18 +0000 Subject: [PATCH] Add CAPTCHA to submission form This should prevent spamming even though this is less likely with this kind of site. The CAPTCHA does not show when editing, only submitting, and is also not shown for authenticated users. Signed-off-by: Paul Eggleton --- README | 1 + TODO | 2 -- layerindex/editlayer.html | 33 ++++++++++++++++++++++++---- layerindex/forms.py | 7 +++++- layerindex/static/css/additional.css | 10 +++++++++ layerindex/views.py | 4 ++-- settings.py | 3 ++- urls.py | 1 + 8 files changed, 51 insertions(+), 10 deletions(-) diff --git a/README b/README index 6661a96..51e63b5 100644 --- a/README +++ b/README @@ -29,6 +29,7 @@ In order to make use of this application you will need: * django-registration * django-reversion * django-reversion-compare +* django-simple-captcha Setup instructions: diff --git a/TODO b/TODO index 5380931..1d26136 100644 --- a/TODO +++ b/TODO @@ -9,8 +9,6 @@ TODO: * Breadcrumbs / fix tab navigation? * Need an admin contact in footer * Show recipes from non-software/base layers differently in recipes list -* Submit layer form fixes: - * Captcha Later: * Usage links in list page? diff --git a/layerindex/editlayer.html b/layerindex/editlayer.html index 54cfe0d..5f6b691 100644 --- a/layerindex/editlayer.html +++ b/layerindex/editlayer.html @@ -45,6 +45,7 @@
{% for field in form.visible_fields %} + {% if field.name != 'captcha' %} {% if field.errors %}
{{ field.errors }} @@ -92,6 +93,7 @@
+ {% endif %} {% endfor %}

Maintainers

@@ -115,16 +117,39 @@
{{ field.label_tag }}
- {{ field }} - - {{ field.help_text }} - +
+ {{ field }} + + {{ field.help_text }} + +
{% endfor %} {% endfor %}

Add maintainer

+ + + {% if form.captcha %} +

+

Verification

+ {% if form.captcha.errors %} +
+ {{ form.captcha.errors }} + {% else %} +
+ {% endif %} +
+ {{ form.captcha }} + + {{ form.captcha.help_text }} + +
+
+
+ {% endif %} +
{% block submitbuttons %} diff --git a/layerindex/forms.py b/layerindex/forms.py index d3518e4..954d37c 100644 --- a/layerindex/forms.py +++ b/layerindex/forms.py @@ -8,6 +8,7 @@ from layerindex.models import LayerItem, LayerMaintainer, LayerNote from django import forms from django.core.validators import URLValidator, RegexValidator, email_re from django.forms.models import inlineformset_factory +from captcha.fields import CaptchaField import re @@ -46,17 +47,21 @@ LayerMaintainerFormSet = inlineformset_factory(LayerItem, LayerMaintainer, form= class SubmitLayerForm(forms.ModelForm): # Additional form fields deps = forms.ModelMultipleChoiceField(label='Other layers this layer depends upon', queryset=LayerItem.objects.all(), required=False) + captcha = CaptchaField(label='Verification', help_text='Please enter the letters displayed for verification purposes', error_messages={'invalid':'Incorrect entry, please try again'}) class Meta: model = LayerItem fields = ('name', 'layer_type', 'summary', 'description', 'vcs_url', 'vcs_subdir', 'vcs_web_url', 'vcs_web_tree_base_url', 'vcs_web_file_base_url', 'usage_url', 'mailing_list_url') - def __init__(self, *args, **kwargs): + def __init__(self, user, *args, **kwargs): super(self.__class__, self).__init__(*args, **kwargs) if self.instance.pk: self.fields['deps'].initial = [d.dependency.pk for d in self.instance.dependencies_set.all()] + del self.fields['captcha'] else: self.fields['deps'].initial = [l.pk for l in LayerItem.objects.filter(name='openembedded-core')] + if user.is_authenticated(): + del self.fields['captcha'] self.was_saved = False def checked_deps(self): diff --git a/layerindex/static/css/additional.css b/layerindex/static/css/additional.css index 5d36df4..3818a79 100644 --- a/layerindex/static/css/additional.css +++ b/layerindex/static/css/additional.css @@ -115,6 +115,16 @@ padding: 8px; padding-right: 3px; } +.captchafield { + padding-top: 1em; + padding-bottom: 1em; +} + +.captcha { + padding-right: 10px; + padding-bottom: 10px; +} + .submitbuttons { margin-top: 2em; padding-top: 1em; diff --git a/layerindex/views.py b/layerindex/views.py index f0f5960..f4c84a2 100644 --- a/layerindex/views.py +++ b/layerindex/views.py @@ -88,7 +88,7 @@ def edit_layer_view(request, template_name, slug=None): layeritem = LayerItem() if request.method == 'POST': - form = SubmitLayerForm(request.POST, instance=layeritem) + form = SubmitLayerForm(request.user, request.POST, instance=layeritem) maintainerformset = LayerMaintainerFormSet(request.POST, instance=layeritem) if form.is_valid() and maintainerformset.is_valid(): with transaction.commit_on_success(): @@ -132,7 +132,7 @@ def edit_layer_view(request, template_name, slug=None): return HttpResponseRedirect(reverse('submit_layer_thanks')) form.was_saved = True else: - form = SubmitLayerForm(instance=layeritem) + form = SubmitLayerForm(request.user, instance=layeritem) maintainerformset = LayerMaintainerFormSet(instance=layeritem) return render(request, template_name, { diff --git a/settings.py b/settings.py index 90bdac0..3dbe232 100644 --- a/settings.py +++ b/settings.py @@ -140,7 +140,8 @@ INSTALLED_APPS = ( 'layerindex', 'registration', 'reversion', - 'reversion_compare' + 'reversion_compare', + 'captcha' ) # A sample logging configuration. The only tangible logging diff --git a/urls.py b/urls.py index e9294d4..174a370 100644 --- a/urls.py +++ b/urls.py @@ -14,5 +14,6 @@ urlpatterns = patterns('', url(r'^layerindex/', include('layerindex.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^accounts/', include('registration.urls')), + url(r'^captcha/', include('captcha.urls')), )