2ce231b1a4afe69af2b3f332a65c0f6d4c3e60a7
[wolnelektury.git] / src / wolnelektury / 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 import json
6 import os
7 from functools import wraps
8
9 import pytz
10 from inspect import getargspec
11
12 from django.http import HttpResponse
13 from django.template import RequestContext
14 from django.template.loader import render_to_string
15 from django.utils import timezone
16 from django.conf import settings
17
18 tz = pytz.timezone(settings.TIME_ZONE)
19
20
21 def localtime_to_utc(localtime):
22     return timezone.utc.normalize(
23         tz.localize(localtime)
24     )
25
26
27 def utc_for_js(dt):
28     return dt.strftime('%Y/%m/%d %H:%M:%S UTC')
29
30
31 def makedirs(path):
32     if not os.path.isdir(path):
33         os.makedirs(path)
34
35
36 def stringify_keys(dictionary):
37     return dict((keyword.encode('ascii'), value)
38                 for keyword, value in dictionary.iteritems())
39
40
41 def json_encode(obj, sort_keys=True, ensure_ascii=False):
42     return json.dumps(obj, sort_keys=sort_keys, ensure_ascii=ensure_ascii)
43
44
45 def json_decode(obj):
46     return json.loads(obj)
47
48
49 def json_decode_fallback(value):
50     try:
51         return json_decode(value)
52     except ValueError:
53         return value
54
55
56 class AjaxError(Exception):
57     pass
58
59
60 def ajax(login_required=False, method=None, template=None, permission_required=None):
61     def decorator(fun):
62         @wraps(fun)
63         def ajax_view(request):
64             kwargs = {}
65             request_params = None
66             if method == 'post':
67                 request_params = request.POST
68             elif method == 'get':
69                 request_params = request.GET
70             fun_params, xx, fun_kwargs, defaults = getargspec(fun)
71             if defaults:
72                 required_params = fun_params[1:-len(defaults)]
73             else:
74                 required_params = fun_params[1:]
75             missing_params = set(required_params) - set(request_params)
76             if missing_params:
77                 res = {
78                     'result': 'missing params',
79                     'missing': ', '.join(missing_params),
80                 }
81             else:
82                 if request_params:
83                     request_params = dict(
84                         (key, json_decode_fallback(value))
85                         for key, value in request_params.iteritems()
86                         if fun_kwargs or key in fun_params)
87                     kwargs.update(stringify_keys(request_params))
88                 res = None
89                 if login_required and not request.user.is_authenticated():
90                     res = {'result': 'logout'}
91                 if (permission_required and
92                         not request.user.has_perm(permission_required)):
93                     res = {'result': 'access denied'}
94             if not res:
95                 try:
96                     res = fun(request, **kwargs)
97                     if res and template:
98                         res = {'html': render_to_string(template, res, RequestContext(request))}
99                 except AjaxError as e:
100                     res = {'result': e.args[0]}
101             if 'result' not in res:
102                 res['result'] = 'ok'
103             return HttpResponse(json_encode(res), content_type='application/json; charset=utf-8',
104                                 status=200 if res['result'] == 'ok' else 400)
105
106         return ajax_view
107
108     return decorator