honeypot working for #210 and cleaned up the form error handling.
authorBryan <btbonval@gmail.com>
Tue, 25 Feb 2014 05:53:16 +0000 (00:53 -0500)
committerBryan <btbonval@gmail.com>
Tue, 25 Feb 2014 05:53:16 +0000 (00:53 -0500)
karmaworld/apps/courses/forms.py
karmaworld/apps/courses/views.py
karmaworld/settings/common.py
karmaworld/templates/partial/add_course.html

index bde2cfccd7821b944adb5c6aa250f65719d9ad04..35f3a6ce36bf4f06ef75087e901fa19ed804bd31 100644 (file)
@@ -2,13 +2,38 @@
 # -*- coding:utf8 -*-
 # Copyright (C) 2012  FinalsClub Foundation
 
+from django.conf import settings
 from django.forms import ModelForm
+from django.forms import CharField
 
 from karmaworld.apps.courses.models import Course
 
 class CourseForm(ModelForm):
+
+    def __init__(self, *args, **kwargs):
+        """ Add a dynamically named field. """
+        super(CourseForm, self).__init__(*args, **kwargs)
+        self.fields[settings.HONEYPOT_FIELD_NAME] = CharField(required=False)
+
     class Meta:
         model = Course
         fields = ('name', 'school', 'url', 'instructor_name', \
                   'instructor_email')
 
+    def clean(self, *args, **kwargs):
+        """ Additional form validation. """
+
+        # Call ModelFormMixin or whoever normally cleans house.
+        cleaned_data = super(CourseForm, self).clean(*args, **kwargs)
+
+        # parts of this code borrow from
+        # https://github.com/sunlightlabs/django-honeypot
+        hfn = settings.HONEYPOT_FIELD_NAME
+        formhoneypot = cleaned_data.get(hfn, None)
+        if formhoneypot and (formhoneypot != settings.HONEYPOT_VALUE):
+            # Highlight a failure to follow instructions.
+            # When the template dynamically generates the form, replace
+            # 'honeypot' with hfn
+            self._errors['honeypot'] = [u'You did not follow directions.']
+            del cleaned_data[hfn]
+        return cleaned_data
index 91a05779352b01660e84e100957b5197f80e9fd9..332bd7c6fef3dcdbf64626f267d8eeebcc7f4725 100644 (file)
@@ -60,25 +60,16 @@ class CourseListView(ListView, ModelFormMixin, ProcessFormView):
         """ 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
         kwargs['course_form'] = form
         self.object_list = self.get_queryset()
         kwargs['object_list'] = self.object_list
+        # hard code errors for the dynamically named honeypot field.
+        # This bit will not be necessary when the form is dynamically
+        # generated by Django rather than hard coded in the template.
+        kwargs['honeypot_errors'] = [x for x in form.errors['honeypot']]
         return self.render_to_response(self.get_context_data(**kwargs))
 
 
index 506ccf9c474c2dfc41cd6e3900a0b20d74f1cdd0..440dd2af95ff50848ee67638e2f0d228c3926652 100644 (file)
@@ -375,7 +375,7 @@ TAGGIT_STOPWORDS = [u'a', u'an', u'and', u'be', u'from', u'of']
 ########## HONEYPOT CONFIGURATION
 # parts of this code borrow from
 # https://github.com/sunlightlabs/django-honeypot
-HONEYPOT_FIELD_NAME = "settings_field_name"
+HONEYPOT_FIELD_NAME = "instruction_url" # see that "_url"? bots gotta want that.
 HONEYPOT_VALUE = ""
 ########## END HONEYPOT CONFIGURATION
 
index 693a563fe3108335131292365535b6544ec14de1..47af638bd8b34b3a7866690c4322e4693779a71a 100644 (file)
   <form method="POST" action="{% url 'home' %}">
   {% csrf_token %}
 
-    {% if course_form.non_field_errors %}
+    {% for error in course_form.non_field_errors %}
     <div class="row">
       <div class="small-12 columns">
-        {{ course_form.non_field_errors }}
+        {{ error }}
       </div>
     </div>
-    {% endif %}
+    {% endfor %}
 
     <div class="row">
       <div class="small-12 columns">
         <legend>
           School
-         {% if course_form.school.errors %}
+          {% for error in course_form.school.errors %}
           <span style="color:red">
-            Please select a school from the drop-down
+            {{ error }}
           </span>
-         {% endif %}
+          {% endfor %}
         </legend>
         <div>
           <input id="str_school" class="" type="text" name="str_school" placeholder="Select a school"/>
     <div class="row">
       <div class="small-12 columns">
         <legend>Do not fill in this field:
-          {% if course_form.honeypot.errors %}
+          {% for error in honeypot_errors %}
             <span style="color:red">
-            {{ course_form.honeypot.errors }}
+            {{ error }}
             </span>
-          {% endif %}
+          {% endfor %}
         </legend>
         <input type="text" name="{{HONEYPOT_FIELD_NAME}}" value="{{HONEYPOT_VALUE}}" />
       </div>
       <div class="small-12 columns large-6">
         <legend class="">
           Instructor Name:
-          {% if course_form.instructor_name.errors %}
+          {% for error in course_form.instructor_name.errors %}
             <span style="color:red">
-            {{ course_form.instructor_name.errors }}
+            {{ error }}
             </span>
-          {% endif %}
+          {% endfor %}
         </legend><!-- -->
         <input id="id_instructor_name" class="" type="text" name="instructor_name" maxlength="75" />
       </div>
       <div class="small-12 columns large-6">
         <legend class="">
           Instructor Email:
-          {% if course_form.instructor_email.errors %}
+          {% for error in course_form.instructor_email.errors %}
             <span style="color:red">
-            {{ course_form.instructor_email.errors }}
+            {{ error }}
             </span>
-          {% endif %}
+          {% endfor %}
         </legend>
         <input id="id_instructor_email" class="" type="text" name="instructor_email" maxlength="75" />
       </div><!--  -->
     <div class="row">
       <div class="small-12 columns">
         <legend>Course url:
-          {% if course_form.url.errors %}
+          {% for error in course_form.url.errors %}
             <span style="color:red">
-            {{ course_form.url.errors }}
+            {{ error }}
             </span>
-          {% endif %}
+          {% endfor %}
         </legend>
         <input id="id_url" class="" type="text" name="url" maxlength="255" />
       </div>