]> git.openstreetmap.org Git - osqa.git/blob - forum/views/decorators.py
be able to pass additional context to the _edit_question view
[osqa.git] / forum / views / decorators.py
1 # -*- coding: utf-8 -*-\r
2 \r
3 import logging\r
4 \r
5 from datetime import datetime\r
6 \r
7 from django.http import HttpResponse, HttpResponseRedirect\r
8 from django.utils import simplejson\r
9 from django.shortcuts import render_to_response\r
10 from django.core.urlresolvers import reverse\r
11 from django.template import RequestContext\r
12 from django.utils.translation import ugettext as _\r
13 \r
14 from forum.modules import ui, decorate\r
15 from forum.settings import ONLINE_USERS\r
16 \r
17 def login_required(func, request, *args, **kwargs):\r
18     if not request.user.is_authenticated():\r
19         return HttpResponseRedirect(reverse('auth_signin'))\r
20     else:\r
21         return func(request, *args, **kwargs)\r
22 \r
23 def render(template=None, tab=None, tab_title='', weight=500, tabbed=True):\r
24     def decorator(func):        \r
25         def decorated(context, request, *args, **kwargs):\r
26             if request.user.is_authenticated():\r
27                 ONLINE_USERS[request.user] = datetime.now()\r
28 \r
29             if isinstance(context, HttpResponse):\r
30                 return context\r
31 \r
32             if tab is not None:\r
33                 context['tab'] = tab\r
34 \r
35             return render_to_response(context.pop('template', template), context,\r
36                                       context_instance=RequestContext(request))\r
37 \r
38         if tabbed and tab and tab_title:\r
39             ui.register(ui.PAGE_TOP_TABS,\r
40                         ui.PageTab(tab, tab_title, lambda: reverse(func.__name__), weight=weight))\r
41             \r
42         return decorate.result.withfn(decorated, needs_params=True)(func)\r
43 \r
44     return decorator\r
45 \r
46 class CommandException(Exception):\r
47     pass\r
48 \r
49 class RefreshPageCommand(HttpResponse):\r
50     def __init__(self):\r
51         super(RefreshPageCommand, self).__init__(\r
52                 content=simplejson.dumps({'commands': {'refresh_page': []}, 'success': True}),\r
53                 mimetype="application/json")\r
54 \r
55 def command(func, request, *args, **kwargs):\r
56     try:\r
57         response = func(request, *args, **kwargs)\r
58 \r
59         if isinstance(response, HttpResponse):\r
60             return response\r
61 \r
62         response['success'] = True\r
63     except Exception, e:\r
64         import traceback\r
65         #traceback.print_exc()\r
66 \r
67         if isinstance(e, CommandException):\r
68             response = {\r
69             'success': False,\r
70             'error_message': e.message\r
71             }\r
72         else:\r
73             logging.error("%s: %s" % (func.__name__, str(e)))\r
74             logging.error(traceback.format_exc())\r
75             response = {\r
76             'success': False,\r
77             'error_message': _("We're sorry, but an unknown error ocurred.<br />Please try again in a while.")\r
78             }\r
79 \r
80     if request.is_ajax():\r
81         return HttpResponse(simplejson.dumps(response), mimetype="application/json")\r
82     else:\r
83         return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))\r
84 \r