Redirect towarzystwo with query string.
[wolnelektury.git] / src / ajaxable / utils.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from functools import wraps
5 import json
6
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
15
16
17 class LazyEncoder(json.JSONEncoder):
18     def default(self, o):
19         if isinstance(o, Promise):
20             return force_text(o)
21         return o
22
23
24 def method_decorator(function_decorator):
25     """Converts a function decorator to a method decorator.
26
27     It just makes it ignore first argument.
28     """
29     def decorator(method):
30         @wraps(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)
35         return wrapped_method
36     return decorator
37
38
39 def require_login(request):
40     """Return 403 if request is AJAX. Redirect to login page if not."""
41     if request.is_ajax():
42         return HttpResponseForbidden('Not logged in')
43     return HttpResponseRedirect('/uzytkownicy/zaloguj')  # next?=request.build_full_path())
44
45
46 def placeholdized(form):
47     for field in form.fields.values():
48         field.widget.attrs['placeholder'] = field.label + ('*' if field.required else '')
49     return form
50
51
52 class AjaxableFormView:
53     """Subclass this to create an ajaxable view for any form.
54
55     In the subclass, provide at least form_class.
56
57     """
58     form_class = None
59     placeholdize = False
60     # override to customize form look
61     template = "ajaxable/form.html"
62     submit = _('Send')
63     action = ''
64
65     title = ''
66     success_message = ''
67     POST_login = False
68     formname = "form"
69     form_prefix = None
70     full_template = "ajaxable/form_on_page.html"
71     honeypot = False
72
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)
77
78         response = self.validate_object(obj, request)
79         if response:
80             return response
81
82         form_args, form_kwargs = self.form_args(request, obj)
83         if self.form_prefix:
84             form_kwargs['prefix'] = self.form_prefix
85
86         if request.method == "POST":
87             if self.honeypot:
88                 response = verify_honeypot_value(request, None)
89                 if response:
90                     return response
91
92             # do I need to be logged in?
93             if self.POST_login and not request.user.is_authenticated:
94                 return require_login(request)
95
96             form_kwargs['data'] = request.POST
97             form = self.form_class(*form_args, **form_kwargs)
98             if form.is_valid():
99                 add_args = self.success(form, request)
100                 response_data = {
101                     'success': True,
102                     'message': self.success_message,
103                     'redirect': request.GET.get('next')
104                     }
105                 if add_args:
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.
112                 if self.form_prefix:
113                     errors = {}
114                     for key, value in form.errors.items():
115                         errors["%s-%s" % (self.form_prefix, key)] = value
116                 else:
117                     errors = form.errors
118                 response_data = {'success': False, 'errors': errors}
119             else:
120                 response_data = None
121             if request.is_ajax():
122                 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
123         else:
124             if self.POST_login and not request.user.is_authenticated and not request.is_ajax():
125                 return require_login(request)
126
127             form = self.form_class(*form_args, **form_kwargs)
128             response_data = None
129
130         title = self.title
131         if request.is_ajax():
132             template = self.template
133         else:
134             template = self.full_template
135             cd = self.context_description(request, obj)
136             if cd:
137                 title += ": " + cd
138         if self.placeholdize:
139             form = placeholdized(form)
140         context = {
141             self.formname: form,
142             "title": title,
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,
149             "view_args": args,
150             "view_kwargs": kwargs,
151         }
152         context.update(self.extra_context(request, obj))
153         return render(request, template, context)
154
155     def validate_object(self, obj, request):
156         return None
157
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>"
162             if message:
163                 output = "<div class='normal-text'>" + message + "</div>" + output
164             return HttpResponse(output)
165         return HttpResponseRedirect(path)
166
167     def get_object(self, request, *args, **kwargs):
168         """Override to parse view args and get some associated data."""
169         return None
170
171     def form_args(self, request, obj):
172         """Override to parse view args and give additional args to the form."""
173         return (), {}
174
175     def extra_context(self, request, obj):
176         """Override to pass something to template."""
177         return {}
178
179     def context_description(self, request, obj):
180         """Description to appear in standalone form, but not in AJAX form."""
181         return ""
182
183     def success(self, form, request):
184         """What to do when the form is valid.
185
186         By default, just save the form.
187
188         """
189         return form.save(request)