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.
7 from functools import wraps
10 from inspect import getargspec
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
18 tz = pytz.timezone(settings.TIME_ZONE)
21 def localtime_to_utc(localtime):
22 return timezone.utc.normalize(
23 tz.localize(localtime)
28 return dt.strftime('%Y/%m/%d %H:%M:%S UTC')
32 if not os.path.isdir(path):
36 def stringify_keys(dictionary):
37 return dict((keyword.encode('ascii'), value)
38 for keyword, value in dictionary.iteritems())
41 def json_encode(obj, sort_keys=True, ensure_ascii=False):
42 return json.dumps(obj, sort_keys=sort_keys, ensure_ascii=ensure_ascii)
46 return json.loads(obj)
49 def json_decode_fallback(value):
51 return json_decode(value)
56 class AjaxError(Exception):
60 def ajax(login_required=True, method=None, template=None, permission_required=None):
63 def ajax_view(request):
67 request_params = request.POST
69 request_params = request.GET
70 fun_params, xx, fun_kwargs, defaults = getargspec(fun)
72 required_params = fun_params[1:-len(defaults)]
74 required_params = fun_params[1:]
75 missing_params = set(required_params) - set(request_params)
78 'result': 'missing params',
79 'missing': ', '.join(missing_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))
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'}
96 res = fun(request, **kwargs)
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:
103 return HttpResponse(json_encode(res), content_type='application/json; charset=utf-8',
104 status=200 if res['result'] == 'ok' else 400)