import json
+from django.conf import settings
from django.core import serializers
from django.core.exceptions import MultipleObjectsReturned
from django.core.exceptions import ObjectDoesNotExist
# 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
########## 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
-
<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">
<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><!-- -->
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>
<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>