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