1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from functools import wraps
7 from django.http import (HttpResponse, HttpResponseRedirect,
9 from django.shortcuts import render_to_response
10 from django.template import RequestContext
11 from django.utils.encoding import force_unicode
12 from django.utils.functional import Promise
13 from django.utils.http import urlquote_plus
15 from django.utils.translation import ugettext_lazy as _
16 from django.views.decorators.vary import vary_on_headers
17 from honeypot.decorators import verify_honeypot_value
20 class LazyEncoder(json.JSONEncoder):
21 def default(self, obj):
22 if isinstance(obj, Promise):
23 return force_unicode(obj)
26 # shortcut for JSON reponses
27 class JSONResponse(HttpResponse):
28 def __init__(self, data={}, callback=None, **kwargs):
30 kwargs.pop('mimetype', None)
31 data = json.dumps(data)
33 data = callback + "(" + data + ");"
34 super(JSONResponse, self).__init__(data, content_type="application/json", **kwargs)
37 def method_decorator(function_decorator):
38 """Converts a function decorator to a method decorator.
40 It just makes it ignore first argument.
42 def decorator(method):
44 def wrapped_method(self, *args, **kwargs):
45 def function(*fargs, **fkwargs):
46 return method(self, *fargs, **fkwargs)
47 return function_decorator(function)(*args, **kwargs)
52 def require_login(request):
53 """Return 403 if request is AJAX. Redirect to login page if not."""
55 return HttpResponseForbidden('Not logged in')
57 return HttpResponseRedirect('/uzytkownicy/zaloguj')# next?=request.build_full_path())
60 def placeholdized(form):
61 for field in form.fields.values():
62 field.widget.attrs['placeholder'] = field.label
66 class AjaxableFormView(object):
67 """Subclass this to create an ajaxable view for any form.
69 In the subclass, provide at least form_class.
74 # override to customize form look
75 template = "ajaxable/form.html"
83 full_template = "ajaxable/form_on_page.html"
86 @method_decorator(vary_on_headers('X-Requested-With'))
87 def __call__(self, request, *args, **kwargs):
88 """A view displaying a form, or JSON if request is AJAX."""
89 obj = self.get_object(request, *args, **kwargs)
90 form_args, form_kwargs = self.form_args(request, obj)
92 form_kwargs['prefix'] = self.form_prefix
94 if request.method == "POST":
96 response = verify_honeypot_value(request, None)
100 # do I need to be logged in?
101 if self.POST_login and not request.user.is_authenticated():
102 return require_login(request)
104 form_kwargs['data'] = request.POST
105 form = self.form_class(*form_args, **form_kwargs)
107 add_args = self.success(form, request)
110 'message': self.success_message,
111 'redirect': request.GET.get('next')
114 response_data.update(add_args)
115 if not request.is_ajax() and response_data['redirect']:
116 return HttpResponseRedirect(urlquote_plus(
117 response_data['redirect'], safe='/?=&'))
118 elif request.is_ajax():
119 # Form was sent with errors. Send them back.
122 for key, value in form.errors.items():
123 errors["%s-%s" % (self.form_prefix, key)] = value
126 response_data = {'success': False, 'errors': errors}
129 if request.is_ajax():
130 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
132 if (self.POST_login and not request.user.is_authenticated()
133 and not request.is_ajax()):
134 return require_login(request)
136 form = self.form_class(*form_args, **form_kwargs)
140 if request.is_ajax():
141 template = self.template
143 template = self.full_template
144 cd = self.context_description(request, obj)
147 if self.placeholdize:
148 form = placeholdized(form)
152 "honeypot": self.honeypot,
153 "placeholdize": self.placeholdize,
154 "submit": self.submit,
155 "response_data": response_data,
156 "ajax_template": self.template,
158 "view_kwargs": kwargs,
160 context.update(self.extra_context(request, obj))
161 return render_to_response(template, context,
162 context_instance=RequestContext(request))
164 def redirect_or_refresh(self, request, path, message=None):
165 """If the form is AJAX, refresh the page. If not, go to `path`."""
166 if request.is_ajax():
167 output = "<script>window.location.reload()</script>"
169 output = "<div class='normal-text'>" + message + "</div>" + output
170 return HttpResponse(output)
172 return HttpResponseRedirect(path)
174 def get_object(self, request, *args, **kwargs):
175 """Override to parse view args and get some associated data."""
178 def form_args(self, request, obj):
179 """Override to parse view args and give additional args to the form."""
182 def extra_context(self, request, obj):
183 """Override to pass something to template."""
186 def context_description(self, request, obj):
187 """Description to appear in standalone form, but not in AJAX form."""
190 def success(self, form, request):
191 """What to do when the form is valid.
193 By default, just save the form.
196 return form.save(request)