api: fix unlike
[wolnelektury.git] / src / ajaxable / utils.py
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.
4 #
5 from functools import wraps
6
7 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
8 from django.shortcuts import render_to_response
9 from django.template import RequestContext
10 from django.utils.encoding import force_unicode
11 from django.utils.functional import Promise
12 from django.utils.http import urlquote_plus
13 import json
14 from django.utils.translation import ugettext_lazy as _
15 from django.views.decorators.vary import vary_on_headers
16 from honeypot.decorators import verify_honeypot_value
17
18
19 class LazyEncoder(json.JSONEncoder):
20     def default(self, obj):
21         if isinstance(obj, Promise):
22             return force_unicode(obj)
23         return obj
24
25
26 def method_decorator(function_decorator):
27     """Converts a function decorator to a method decorator.
28
29     It just makes it ignore first argument.
30     """
31     def decorator(method):
32         @wraps(method)
33         def wrapped_method(self, *args, **kwargs):
34             def function(*fargs, **fkwargs):
35                 return method(self, *fargs, **fkwargs)
36             return function_decorator(function)(*args, **kwargs)
37         return wrapped_method
38     return decorator
39
40
41 def require_login(request):
42     """Return 403 if request is AJAX. Redirect to login page if not."""
43     if request.is_ajax():
44         return HttpResponseForbidden('Not logged in')
45     else:
46         return HttpResponseRedirect('/uzytkownicy/zaloguj')  # next?=request.build_full_path())
47
48
49 def placeholdized(form):
50     for field in form.fields.values():
51         field.widget.attrs['placeholder'] = field.label + ('*' if field.required else '')
52     return form
53
54
55 class AjaxableFormView(object):
56     """Subclass this to create an ajaxable view for any form.
57
58     In the subclass, provide at least form_class.
59
60     """
61     form_class = None
62     placeholdize = False
63     # override to customize form look
64     template = "ajaxable/form.html"
65     submit = _('Send')
66     action = ''
67
68     title = ''
69     success_message = ''
70     POST_login = False
71     formname = "form"
72     form_prefix = None
73     full_template = "ajaxable/form_on_page.html"
74     honeypot = False
75
76     @method_decorator(vary_on_headers('X-Requested-With'))
77     def __call__(self, request, *args, **kwargs):
78         """A view displaying a form, or JSON if request is AJAX."""
79         obj = self.get_object(request, *args, **kwargs)
80
81         response = self.validate_object(obj, request)
82         if response:
83             return response
84
85         form_args, form_kwargs = self.form_args(request, obj)
86         if self.form_prefix:
87             form_kwargs['prefix'] = self.form_prefix
88
89         if request.method == "POST":
90             if self.honeypot:
91                 response = verify_honeypot_value(request, None)
92                 if response:
93                     return response
94
95             # do I need to be logged in?
96             if self.POST_login and not request.user.is_authenticated():
97                 return require_login(request)
98
99             form_kwargs['data'] = request.POST
100             form = self.form_class(*form_args, **form_kwargs)
101             if form.is_valid():
102                 add_args = self.success(form, request)
103                 response_data = {
104                     'success': True,
105                     'message': self.success_message,
106                     'redirect': request.GET.get('next')
107                     }
108                 if add_args:
109                     response_data.update(add_args)
110                 if not request.is_ajax() and response_data['redirect']:
111                     return HttpResponseRedirect(urlquote_plus(
112                             response_data['redirect'], safe='/?=&'))
113             elif request.is_ajax():
114                 # Form was sent with errors. Send them back.
115                 if self.form_prefix:
116                     errors = {}
117                     for key, value in form.errors.items():
118                         errors["%s-%s" % (self.form_prefix, key)] = value
119                 else:
120                     errors = form.errors
121                 response_data = {'success': False, 'errors': errors}
122             else:
123                 response_data = None
124             if request.is_ajax():
125                 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
126         else:
127             if self.POST_login and not request.user.is_authenticated() and not request.is_ajax():
128                 return require_login(request)
129
130             form = self.form_class(*form_args, **form_kwargs)
131             response_data = None
132
133         title = self.title
134         if request.is_ajax():
135             template = self.template
136         else:
137             template = self.full_template
138             cd = self.context_description(request, obj)
139             if cd:
140                 title += ": " + cd
141         if self.placeholdize:
142             form = placeholdized(form)
143         context = {
144                 self.formname: form,
145                 "title": title,
146                 "honeypot": self.honeypot,
147                 "placeholdize": self.placeholdize,
148                 "submit": self.submit,
149                 "action": self.action,
150                 "response_data": response_data,
151                 "ajax_template": self.template,
152                 "view_args": args,
153                 "view_kwargs": kwargs,
154             }
155         context.update(self.extra_context(request, obj))
156         return render_to_response(template, context, context_instance=RequestContext(request))
157
158     def validate_object(self, obj, request):
159         return None
160
161     def redirect_or_refresh(self, request, path, message=None):
162         """If the form is AJAX, refresh the page. If not, go to `path`."""
163         if request.is_ajax():
164             output = "<script>window.location.reload()</script>"
165             if message:
166                 output = "<div class='normal-text'>" + message + "</div>" + output
167             return HttpResponse(output)
168         else:
169             return HttpResponseRedirect(path)
170
171     def get_object(self, request, *args, **kwargs):
172         """Override to parse view args and get some associated data."""
173         return None
174
175     def form_args(self, request, obj):
176         """Override to parse view args and give additional args to the form."""
177         return (), {}
178
179     def extra_context(self, request, obj):
180         """Override to pass something to template."""
181         return {}
182
183     def context_description(self, request, obj):
184         """Description to appear in standalone form, but not in AJAX form."""
185         return ""
186
187     def success(self, form, request):
188         """What to do when the form is valid.
189
190         By default, just save the form.
191
192         """
193         return form.save(request)