failed attempt to check for a blank honeypot, form doesn't clean()
authorBryan <btbonval@gmail.com>
Sun, 23 Feb 2014 04:16:41 +0000 (23:16 -0500)
committerBryan <btbonval@gmail.com>
Sun, 23 Feb 2014 04:16:41 +0000 (23:16 -0500)
karmaworld/apps/courses/views.py
karmaworld/settings/common.py
karmaworld/templates/partial/add_course.html

index b88fe7b5a2be99601bac918a044ddb0a4c71114f..91a05779352b01660e84e100957b5197f80e9fd9 100644 (file)
@@ -5,6 +5,7 @@
 
 import json
 
+from django.conf import settings
 from django.core import serializers
 from django.core.exceptions import MultipleObjectsReturned
 from django.core.exceptions import ObjectDoesNotExist
@@ -49,12 +50,29 @@ class CourseListView(ListView, ModelFormMixin, ProcessFormView):
         # Include "Add Course" button in header
         context['display_add_course'] = True
 
+        # Include settings constants for honeypot
+        for key in ('HONEYPOT_FIELD_NAME', 'HONEYPOT_VALUE'):
+            context[key] = getattr(settings, key)
+
         return context
 
     def get_success_url(self):
         """ On success, return url based on urls.py definition. """
         return self.object.get_absolute_url()
 
+    def clean(self, *args, **kwargs):
+        """ Additional form validation. """
+        # Call ModelFormMixin or whoever normally cleans house.
+        cleaned_data = super(CourseListView, self).clean(*args, **kwargs)
+        # parts of this code borrow from
+        # https://github.com/sunlightlabs/django-honeypot
+        formhoneypot = cleaned_data.get(settings.HONEYPOT_FIELD_NAME, None)
+        if formhoneypot and (formhoneypot != settings.HONEYPOT_VALUE):
+            # Highlight a failure to follow instructions.
+            self._errors['honeypot'] = 'You did not follow directions.'
+            del cleaned_data[hfn]
+        return cleaned_data
+
     def form_invalid(self, form, **kwargs):
         """ override form_invalid to populate object_list on redirect """
         kwargs['is_error'] = True
index 8993be64f3a87bb8b1356d93e5f116e5df00e10c..506ccf9c474c2dfc41cd6e3900a0b20d74f1cdd0 100644 (file)
@@ -371,7 +371,15 @@ TAGGIT_STOPWORDS = [u'a', u'an', u'and', u'be', u'from', u'of']
 
 ########## END TAGGIT CONFIGURATION
 
+
+########## HONEYPOT CONFIGURATION
+# parts of this code borrow from
+# https://github.com/sunlightlabs/django-honeypot
+HONEYPOT_FIELD_NAME = "settings_field_name"
+HONEYPOT_VALUE = ""
+########## END HONEYPOT CONFIGURATION
+
+
 ########## TESTING CONFIGURATION
 TESTING = 'test' in sys.argv
 ########## END TESTING CONFIGURATION
-
index 2938f5be9188af3c395584e64dc497fcaba3bfe2..693a563fe3108335131292365535b6544ec14de1 100644 (file)
   <form method="POST" action="{% url 'home' %}">
   {% csrf_token %}
 
+    {% if course_form.non_field_errors %}
     <div class="row">
       <div class="small-12 columns">
         {{ course_form.non_field_errors }}
       </div>
     </div>
-
+    {% endif %}
 
     <div class="row">
       <div class="small-12 columns">
@@ -42,7 +43,7 @@
         <legend>Course Name:
           {% if course_form.name.errors %}
             <span style="color:red">
-            * there was an error with this field
+            {{ course_form.name.errors }}
             </span>
           {% endif %}
         </legend>
       </div>
     </div> <!-- .row -->
 
+    <div class="row">
+      <div class="small-12 columns">
+        <legend>Do not fill in this field:
+          {% if course_form.honeypot.errors %}
+            <span style="color:red">
+            {{ course_form.honeypot.errors }}
+            </span>
+          {% endif %}
+        </legend>
+        <input type="text" name="{{HONEYPOT_FIELD_NAME}}" value="{{HONEYPOT_VALUE}}" />
+      </div>
+    </div>
+
     <div class="row">
       <div class="small-12 columns large-6">
         <legend class="">
           Instructor Name:
           {% if course_form.instructor_name.errors %}
             <span style="color:red">
-            * there was an error with this field
+            {{ course_form.instructor_name.errors }}
             </span>
           {% endif %}
         </legend><!-- -->
@@ -68,7 +82,7 @@
           Instructor Email:
           {% if course_form.instructor_email.errors %}
             <span style="color:red">
-            * there was an error with this field
+            {{ course_form.instructor_email.errors }}
             </span>
           {% endif %}
         </legend>
@@ -81,7 +95,7 @@
         <legend>Course url:
           {% if course_form.url.errors %}
             <span style="color:red">
-            * there was an error with this field
+            {{ course_form.url.errors }}
             </span>
           {% endif %}
         </legend>