]> git.openstreetmap.org Git - osqa.git/blob - forum/views/decorators.py
Closing OSQA 35. Give admins and moderators an easy way to award karma points.
[osqa.git] / forum / views / decorators.py
1 from django.http import HttpResponse, HttpResponseRedirect, Http404\r
2 from django.utils import simplejson\r
3 from django.core.paginator import Paginator, EmptyPage\r
4 from django.shortcuts import render_to_response\r
5 from django.template import RequestContext\r
6 from django.utils.translation import ungettext, ugettext as _\r
7 import logging\r
8 \r
9 def render(template=None, tab=None):\r
10     def decorator(func):\r
11         def decorated(request, *args, **kwargs):\r
12             context = func(request, *args, **kwargs)\r
13 \r
14             if tab is not None:\r
15                 context['tab'] = tab\r
16 \r
17             return render_to_response(context.pop('template', template), context, context_instance=RequestContext(request))\r
18         return decorated\r
19     return decorator\r
20 \r
21 def list(paginate, default_page_size):\r
22     def decorator(func):\r
23         def decorated(request, *args, **kwargs):\r
24             context = func(request, *args, **kwargs)\r
25 \r
26             pagesize = request.utils.page_size(default_page_size)\r
27             page = int(request.GET.get('page', 1))\r
28 \r
29             big_list = context[paginate]\r
30             paginator = Paginator(big_list, pagesize)\r
31 \r
32             try:\r
33                 page_obj = paginator.page(page)\r
34             except EmptyPage:\r
35                 raise Http404()\r
36                 \r
37             context[paginate] = page_obj.object_list.lazy()\r
38 \r
39             base_path = context.get('base_path', None) or request.path\r
40             sort = request.utils.sort_method('')\r
41 \r
42             context["pagination_context"] = {\r
43                 'is_paginated' : True,\r
44                 'pages': paginator.num_pages,\r
45                 'page': page,\r
46                 'has_previous': page_obj.has_previous(),\r
47                 'has_next': page_obj.has_next(),\r
48                 'previous': page_obj.previous_page_number(),\r
49                 'next': page_obj.next_page_number(),\r
50                 'base_url' : "%s%ssort=%s&" % (base_path, ('?' in base_path) and '&' or '?', sort),\r
51                 'pagesize' : pagesize\r
52             }\r
53 \r
54             context['sort_context'] = {\r
55                 'base_url': "%s%ssort=" % (base_path, ('?' in base_path) and '&' or '?'),\r
56                 'current': sort,\r
57             }\r
58 \r
59             return context\r
60         return decorated\r
61     return decorator\r
62 \r
63 \r
64 class CommandException(Exception):\r
65     pass\r
66 \r
67 \r
68 def command(func):\r
69     def decorated(request, *args, **kwargs):\r
70         try:\r
71             response = func(request, *args, **kwargs)\r
72 \r
73             if isinstance(response, HttpResponse):\r
74                 return response\r
75 \r
76             response['success'] = True\r
77         except Exception, e:\r
78             import sys, traceback\r
79             traceback.print_exc(file=sys.stdout)\r
80 \r
81             if isinstance(e, CommandException):\r
82                 response = {\r
83                     'success': False,\r
84                     'error_message': str(e)\r
85                 }\r
86             else:\r
87                 logging.error("%s: %s" % (func.__name__, str(e)))\r
88                 response = {\r
89                     'success': False,\r
90                     'error_message': _("We're sorry, but an unknown error ocurred.<br />Please try again in a while.")\r
91                 }\r
92 \r
93         if request.is_ajax():\r
94             return HttpResponse(simplejson.dumps(response), mimetype="application/json")\r
95         else:\r
96             return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))\r
97 \r
98     return decorated