1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
4 from functools import wraps
6 from urllib.parse import quote_plus
8 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
9 from django.shortcuts import render
10 from django.utils.encoding import force_str
11 from django.utils.functional import Promise
12 from django.views.decorators.vary import vary_on_headers
13 from honeypot.decorators import verify_honeypot_value
14 from wolnelektury.utils import is_ajax
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 class AjaxableFormView:
47 """Subclass this to create an ajaxable view for any form.
49 In the subclass, provide at least form_class.
53 # override to customize form look
54 template = "ajaxable/form.html"
63 full_template = "ajaxable/form_on_page.html"
66 @method_decorator(vary_on_headers('X-Requested-With'))
67 def __call__(self, request, *args, **kwargs):
68 """A view displaying a form, or JSON if request is AJAX."""
69 obj = self.get_object(request, *args, **kwargs)
71 response = self.validate_object(obj, request)
75 form_args, form_kwargs = self.form_args(request, obj)
77 form_kwargs['prefix'] = self.form_prefix
79 if request.method == "POST":
81 response = verify_honeypot_value(request, None)
85 # do I need to be logged in?
86 if self.POST_login and not request.user.is_authenticated:
87 return require_login(request)
89 form_kwargs['data'] = request.POST
90 form = self.form_class(*form_args, **form_kwargs)
92 add_args = self.success(form, request)
95 'message': self.success_message,
96 'redirect': request.GET.get('next')
99 response_data.update(add_args)
100 if not is_ajax(request) and response_data['redirect']:
101 return HttpResponseRedirect(quote_plus(
102 response_data['redirect'], safe='/?=&'))
103 elif is_ajax(request):
104 # Form was sent with errors. Send them back.
107 for key, value in form.errors.items():
108 errors["%s-%s" % (self.form_prefix, key)] = value
111 response_data = {'success': False, 'errors': errors}
115 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
117 if self.POST_login and not request.user.is_authenticated and not is_ajax(request):
118 return require_login(request)
120 form = self.form_class(*form_args, **form_kwargs)
125 template = self.template
127 template = self.full_template
128 cd = self.context_description(request, obj)
134 "honeypot": self.honeypot,
135 "submit": self.submit,
136 "action": self.action,
137 "response_data": response_data,
138 "ajax_template": self.template,
140 "view_kwargs": kwargs,
142 context.update(self.extra_context(request, obj))
143 return render(request, template, context)
145 def validate_object(self, obj, request):
148 def redirect_or_refresh(self, request, path, message=None):
149 """If the form is AJAX, refresh the page. If not, go to `path`."""
151 output = "<script>window.location.reload()</script>"
153 output = "<div class='normal-text'>" + message + "</div>" + output
154 return HttpResponse(output)
155 return HttpResponseRedirect(path)
157 def get_object(self, request, *args, **kwargs):
158 """Override to parse view args and get some associated data."""
161 def form_args(self, request, obj):
162 """Override to parse view args and give additional args to the form."""
165 def extra_context(self, request, obj):
166 """Override to pass something to template."""
169 def context_description(self, request, obj):
170 """Description to appear in standalone form, but not in AJAX form."""
173 def success(self, form, request):
174 """What to do when the form is valid.
176 By default, just save the form.
179 return form.save(request)