decorators.py 740 B

123456789101112131415161718192021
  1. from django.http import HttpResponseForbidden
  2. from .forms import PublicContribForm
  3. def prevent_robots(field_name='human_field'):
  4. """
  5. this decorator returns a HTTP 403 Forbidden error on POST requests
  6. if a given field has been set
  7. Keyword arguments :
  8. field_name -- the name of the field to search for (default 'human_field')
  9. """
  10. def _dec(func):
  11. def _wrapped_func(request, *args, **kwargs):
  12. if request.method == 'POST':
  13. form = PublicContribForm(request.POST)
  14. if field_name in form.data and form.data[field_name]:
  15. return HttpResponseForbidden()
  16. return func(request, *args, **kwargs)
  17. return _wrapped_func
  18. return _dec