1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from functools import wraps
7 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
8 from django.shortcuts import render
9 from django.utils.encoding import force_text
10 from django.utils.functional import Promise
11 from django.utils.http import urlquote_plus
12 from django.utils.translation import ugettext_lazy as _
13 from django.views.decorators.vary import vary_on_headers
14 from honeypot.decorators import verify_honeypot_value
17 class LazyEncoder(json.JSONEncoder):
19 if isinstance(o, Promise):
24 def method_decorator(function_decorator):
25 """Converts a function decorator to a method decorator.
27 It just makes it ignore first argument.
29 def decorator(method):
31 def wrapped_method(self, *args, **kwargs):
32 def function(*fargs, **fkwargs):
33 return method(self, *fargs, **fkwargs)
34 return function_decorator(function)(*args, **kwargs)
39 def require_login(request):
40 """Return 403 if request is AJAX. Redirect to login page if not."""
42 return HttpResponseForbidden('Not logged in')
43 return HttpResponseRedirect('/uzytkownicy/zaloguj') # next?=request.build_full_path())
46 def placeholdized(form):
47 for field in form.fields.values():
48 field.widget.attrs['placeholder'] = field.label + ('*' if field.required else '')
52 class AjaxableFormView:
53 """Subclass this to create an ajaxable view for any form.
55 In the subclass, provide at least form_class.
60 # override to customize form look
61 template = "ajaxable/form.html"
70 full_template = "ajaxable/form_on_page.html"
73 @method_decorator(vary_on_headers('X-Requested-With'))
74 def __call__(self, request, *args, **kwargs):
75 """A view displaying a form, or JSON if request is AJAX."""
76 obj = self.get_object(request, *args, **kwargs)
78 response = self.validate_object(obj, request)
82 form_args, form_kwargs = self.form_args(request, obj)
84 form_kwargs['prefix'] = self.form_prefix
86 if request.method == "POST":
88 response = verify_honeypot_value(request, None)
92 # do I need to be logged in?
93 if self.POST_login and not request.user.is_authenticated:
94 return require_login(request)
96 form_kwargs['data'] = request.POST
97 form = self.form_class(*form_args, **form_kwargs)
99 add_args = self.success(form, request)
102 'message': self.success_message,
103 'redirect': request.GET.get('next')
106 response_data.update(add_args)
107 if not request.is_ajax() and response_data['redirect']:
108 return HttpResponseRedirect(urlquote_plus(
109 response_data['redirect'], safe='/?=&'))
110 elif request.is_ajax():
111 # Form was sent with errors. Send them back.
114 for key, value in form.errors.items():
115 errors["%s-%s" % (self.form_prefix, key)] = value
118 response_data = {'success': False, 'errors': errors}
121 if request.is_ajax():
122 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
124 if self.POST_login and not request.user.is_authenticated and not request.is_ajax():
125 return require_login(request)
127 form = self.form_class(*form_args, **form_kwargs)
131 if request.is_ajax():
132 template = self.template
134 template = self.full_template
135 cd = self.context_description(request, obj)
138 if self.placeholdize:
139 form = placeholdized(form)
143 "honeypot": self.honeypot,
144 "placeholdize": self.placeholdize,
145 "submit": self.submit,
146 "action": self.action,
147 "response_data": response_data,
148 "ajax_template": self.template,
150 "view_kwargs": kwargs,
152 context.update(self.extra_context(request, obj))
153 return render(request, template, context)
155 def validate_object(self, obj, request):
158 def redirect_or_refresh(self, request, path, message=None):
159 """If the form is AJAX, refresh the page. If not, go to `path`."""
160 if request.is_ajax():
161 output = "<script>window.location.reload()</script>"
163 output = "<div class='normal-text'>" + message + "</div>" + output
164 return HttpResponse(output)
165 return HttpResponseRedirect(path)
167 def get_object(self, request, *args, **kwargs):
168 """Override to parse view args and get some associated data."""
171 def form_args(self, request, obj):
172 """Override to parse view args and give additional args to the form."""
175 def extra_context(self, request, obj):
176 """Override to pass something to template."""
179 def context_description(self, request, obj):
180 """Description to appear in standalone form, but not in AJAX form."""
183 def success(self, form, request):
184 """What to do when the form is valid.
186 By default, just save the form.
189 return form.save(request)