1 from functools import wraps
3 from django.http import (HttpResponse, HttpResponseRedirect,
5 from django.shortcuts import render_to_response
6 from django.template import RequestContext
7 from django.utils.encoding import force_unicode
8 from django.utils.functional import Promise
9 from django.utils.http import urlquote_plus
11 from django.utils.translation import ugettext_lazy as _
12 from django.views.decorators.vary import vary_on_headers
13 from honeypot.decorators import verify_honeypot_value
16 class LazyEncoder(json.JSONEncoder):
17 def default(self, obj):
18 if isinstance(obj, Promise):
19 return force_unicode(obj)
22 # shortcut for JSON reponses
23 class JSONResponse(HttpResponse):
24 def __init__(self, data={}, callback=None, **kwargs):
26 kwargs.pop('mimetype', None)
27 data = json.dumps(data)
29 data = callback + "(" + data + ");"
30 super(JSONResponse, self).__init__(data, mimetype="application/json", **kwargs)
33 def method_decorator(function_decorator):
34 """Converts a function decorator to a method decorator.
36 It just makes it ignore first argument.
38 def decorator(method):
40 def wrapped_method(self, *args, **kwargs):
41 def function(*fargs, **fkwargs):
42 return method(self, *fargs, **fkwargs)
43 return function_decorator(function)(*args, **kwargs)
48 def require_login(request):
49 """Return 403 if request is AJAX. Redirect to login page if not."""
51 return HttpResponseForbidden('Not logged in')
53 return HttpResponseRedirect('/uzytkownicy/zaloguj')# next?=request.build_full_path())
56 def placeholdized(form):
57 for field in form.fields.values():
58 field.widget.attrs['placeholder'] = field.label
62 class AjaxableFormView(object):
63 """Subclass this to create an ajaxable view for any form.
65 In the subclass, provide at least form_class.
70 # override to customize form look
71 template = "ajaxable/form.html"
79 full_template = "ajaxable/form_on_page.html"
82 @method_decorator(vary_on_headers('X-Requested-With'))
83 def __call__(self, request, *args, **kwargs):
84 """A view displaying a form, or JSON if request is AJAX."""
85 obj = self.get_object(request, *args, **kwargs)
86 form_args, form_kwargs = self.form_args(request, obj)
88 form_kwargs['prefix'] = self.form_prefix
90 if request.method == "POST":
92 response = verify_honeypot_value(request, None)
96 # do I need to be logged in?
97 if self.POST_login and not request.user.is_authenticated():
98 return require_login(request)
100 form_kwargs['data'] = request.POST
101 form = self.form_class(*form_args, **form_kwargs)
103 add_args = self.success(form, request)
106 'message': self.success_message,
107 'redirect': request.GET.get('next')
110 response_data.update(add_args)
111 if not request.is_ajax() and response_data['redirect']:
112 return HttpResponseRedirect(urlquote_plus(
113 response_data['redirect'], safe='/?=&'))
114 elif request.is_ajax():
115 # Form was sent with errors. Send them back.
118 for key, value in form.errors.items():
119 errors["%s-%s" % (self.form_prefix, key)] = value
122 response_data = {'success': False, 'errors': errors}
125 if request.is_ajax():
126 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
128 if (self.POST_login and not request.user.is_authenticated()
129 and not request.is_ajax()):
130 return require_login(request)
132 form = self.form_class(*form_args, **form_kwargs)
136 if request.is_ajax():
137 template = self.template
139 template = self.full_template
140 cd = self.context_description(request, obj)
143 if self.placeholdize:
144 form = placeholdized(form)
148 "honeypot": self.honeypot,
149 "placeholdize": self.placeholdize,
150 "submit": self.submit,
151 "response_data": response_data,
152 "ajax_template": self.template,
154 "view_kwargs": kwargs,
156 context.update(self.extra_context(request, obj))
157 return render_to_response(template, context,
158 context_instance=RequestContext(request))
160 def redirect_or_refresh(self, request, path, message=None):
161 """If the form is AJAX, refresh the page. If not, go to `path`."""
162 if request.is_ajax():
163 output = "<script>window.location.reload()</script>"
165 output = "<div class='normal-text'>" + message + "</div>" + output
166 return HttpResponse(output);
168 return HttpResponseRedirect(path)
170 def get_object(self, request, *args, **kwargs):
171 """Override to parse view args and get some associated data."""
174 def form_args(self, request, obj):
175 """Override to parse view args and give additional args to the form."""
178 def extra_context(self, request, obj):
179 """Override to pass something to template."""
182 def context_description(self, request, obj):
183 """Description to appear in standalone form, but not in AJAX form."""
186 def success(self, form, request):
187 """What to do when the form is valid.
189 By default, just save the form.
192 return form.save(request)