]> git.openstreetmap.org Git - osqa.git/blob - forum/views/decorators.py
OSQA-386
[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.core.urlresolvers import reverse\r
6 from django.template import RequestContext\r
7 from django.utils.translation import ungettext, ugettext as _\r
8 from forum.modules import ui, decorate\r
9 import logging\r
10 \r
11 def render(template=None, tab=None, tab_title='', weight=500, tabbed=True):\r
12     def decorator(func):\r
13         def decorated(request, *args, **kwargs):\r
14             context = func(request, *args, **kwargs)\r
15 \r
16             if isinstance(context, HttpResponse):\r
17                 return context\r
18 \r
19             if tab is not None:\r
20                 context['tab'] = tab\r
21 \r
22             return render_to_response(context.pop('template', template), context,\r
23                                       context_instance=RequestContext(request))\r
24 \r
25         if tabbed and tab:\r
26             ui.register(ui.PAGE_TOP_TABS,\r
27                         ui.PageTab(tab, tab_title, lambda: reverse(func.__name__), weight=weight))\r
28             \r
29         return decorated\r
30 \r
31     return decorator\r
32 \r
33 def list(paginate, default_page_size):\r
34     def decorator(func):\r
35         def decorated(request, *args, **kwargs):\r
36             context = func(request, *args, **kwargs)\r
37 \r
38             if isinstance(context, HttpResponse):\r
39                 return context\r
40 \r
41             pagesize = request.utils.page_size(default_page_size)\r
42             page = int(request.GET.get('page', 1))\r
43 \r
44             big_list = context[paginate]\r
45             paginator = Paginator(big_list, pagesize)\r
46 \r
47             try:\r
48                 page_obj = paginator.page(page)\r
49             except EmptyPage:\r
50                 raise Http404()\r
51 \r
52             context[paginate] = page_obj.object_list.lazy()\r
53 \r
54             base_path = context.get('base_path', None) or request.path\r
55             sort = request.utils.sort_method('')\r
56 \r
57             context["pagination_context"] = {\r
58             'is_paginated' : True,\r
59             'pages': paginator.num_pages,\r
60             'page': page,\r
61             'has_previous': page_obj.has_previous(),\r
62             'has_next': page_obj.has_next(),\r
63             'previous': page_obj.previous_page_number(),\r
64             'next': page_obj.next_page_number(),\r
65             'base_url' : "%s%ssort=%s&" % (base_path, ('?' in base_path) and '&' or '?', sort),\r
66             'pagesize' : pagesize\r
67             }\r
68 \r
69             context['sort_context'] = {\r
70             'base_url': "%s%ssort=" % (base_path, ('?' in base_path) and '&' or '?'),\r
71             'current': sort,\r
72             }\r
73 \r
74             return context\r
75 \r
76         return decorated\r
77 \r
78     return decorator\r
79 \r
80 \r
81 class CommandException(Exception):\r
82     pass\r
83 \r
84 class RefreshPageCommand(HttpResponse):\r
85     def __init__(self):\r
86         super(RefreshPageCommand, self).__init__(\r
87                 content=simplejson.dumps({'commands': {'refresh_page': []}, 'success': True}),\r
88                 mimetype="application/json")\r
89 \r
90 def command(func, request, *args, **kwargs):\r
91     try:\r
92         response = func(request, *args, **kwargs)\r
93 \r
94         if isinstance(response, HttpResponse):\r
95             return response\r
96 \r
97         response['success'] = True\r
98     except Exception, e:\r
99         import traceback\r
100         #traceback.print_exc()\r
101 \r
102         if isinstance(e, CommandException):\r
103             response = {\r
104             'success': False,\r
105             'error_message': e.message\r
106             }\r
107         else:\r
108             logging.error("%s: %s" % (func.__name__, str(e)))\r
109             logging.error(traceback.format_exc())\r
110             response = {\r
111             'success': False,\r
112             'error_message': _("We're sorry, but an unknown error ocurred.<br />Please try again in a while.")\r
113             }\r
114 \r
115     if request.is_ajax():\r
116         return HttpResponse(simplejson.dumps(response), mimetype="application/json")\r
117     else:\r
118         return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))\r
119 \r