<s> from functools import wraps <EOL> from django . conf import settings <EOL> from django . http import HttpResponseBadRequest <EOL> from django . template . loader import render_to_string <EOL> from django . utils import six <EOL> from django . utils . decorators import available_attrs <EOL> def honeypot_equals ( val ) : <EOL> """<STR_LIT>""" <EOL> expected = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if callable ( expected ) : <EOL> expected = expected ( ) <EOL> return val == expected <EOL> def verify_honeypot_value ( request , field_name ) : <EOL> """<STR_LIT>""" <EOL> verifier = getattr ( settings , '<STR_LIT>' , honeypot_equals ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> field = field_name or settings . HONEYPOT_FIELD_NAME <EOL> if field not in request . POST or not verifier ( request . POST [ field ] ) : <EOL> resp = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : field } ) <EOL> return HttpResponseBadRequest ( resp ) <EOL> def check_honeypot ( func = None , field_name = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( func , six . string_types ) : <EOL> func , field_name = field_name , func <EOL> def decorated ( func ) : <EOL> def inner ( request , * args , ** kwargs ) : <EOL> response = verify_honeypot_value ( request , field_name ) <EOL> if response : <EOL> return response <EOL> else : <EOL> return func ( request , * args , ** kwargs ) <EOL> return wraps ( func , assigned = available_attrs ( func ) ) ( inner ) <EOL> if func is None : <EOL> def decorator ( func ) : <EOL> return decorated ( func ) <EOL> return decorator <EOL> return decorated ( func ) <EOL> def honeypot_exempt ( view_func ) : <EOL> """<STR_LIT>""" <EOL> def wrapped ( * args , ** kwargs ) : <EOL> return view_func ( * args , ** kwargs ) <EOL> wrapped . honeypot_exempt = True <EOL> return wraps ( view_func , assigned = available_attrs ( view_func ) ) ( wrapped ) </s>
