]> git.openstreetmap.org Git - osqa.git/blob - forum/utils/decorators.py
Django 1.3 fix.
[osqa.git] / forum / utils / decorators.py
1 from django.http import HttpResponse, HttpResponseForbidden, Http404
2 from django.utils import simplejson
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 = simplejson.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 = simplejson.dumps(retval)
23             return HttpResponse(json,mimetype='application/json')
24     return wrap
25