]> git.openstreetmap.org Git - osqa.git/blob - forum/utils/decorators.py
Merge branch 'threadsafe_requestholder' into update_django
[osqa.git] / forum / utils / decorators.py
1 import json
2 from django.http import HttpResponse, HttpResponseForbidden, Http404
3
4 def ajax_login_required(view_func):
5     def wrap(request,*args,**kwargs):
6         if request.user.is_authenticated():
7             return view_func(request,*args,**kwargs)
8         else:
9             json = json.dumps({'login_required':True})
10             return HttpResponseForbidden(json, mimetype='application/json')
11     return wrap
12
13 def ajax_method(view_func):
14     def wrap(request,*args,**kwargs):
15         if not request.is_ajax():
16             raise Http404
17         retval = view_func(request,*args,**kwargs)
18         if isinstance(retval, HttpResponse):
19             retval.mimetype = 'application/json'
20             return retval
21         else:
22             json = json.dumps(retval)
23             return HttpResponse(json, mimetype='application/json')
24     return wrap
25