if request.POST:
# get the right field for secret question from the form model
secret_field = page.form.fields.filter(label="What is Jen or Danny's last name?")
if secret_field.exists():
secret_field = secret_field[0]
secret_value = request.POST.get(secret_field_post_key)
# answer should be one of our last names
prog = re.compile('sanchez|flack', re.I)
if request.POST.get(secret_field_post_key) and not prog.match(secret_value):
# wrong answer to the secret question so invalidate form by removing the secret value
# from the post data and reassigning the data back to the request object's POST
form_data = request.POST.copy()
del form_data[secret_field_post_key]
request.POST = form_data
# add the error to the form
form = FormForForm(page.form, RequestContext(request), form_data, None)
form_error = {
secret_field_post_key: form.error_class(['Are you sure you know these folks?'])
}
if form.errors:
form._errors.update(form_error)
else:
form._errors = form_error
return {'form': form}
else:
raise forms.ValidationError('Missing expected secret field')
return {}