# -*- 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
""" 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))
########## 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
<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>