1 # -*- coding: utf-8 -*-
\r
5 from datetime import datetime
\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
14 from forum.modules import ui, decorate
\r
15 from forum.settings import ONLINE_USERS
\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
21 return func(request, *args, **kwargs)
\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
29 if isinstance(context, HttpResponse):
\r
33 context['tab'] = tab
\r
35 return render_to_response(context.pop('template', template), context,
\r
36 context_instance=RequestContext(request))
\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
42 return decorate.result.withfn(decorated, needs_params=True)(func)
\r
46 class CommandException(Exception):
\r
49 class RefreshPageCommand(HttpResponse):
\r
51 super(RefreshPageCommand, self).__init__(
\r
52 content=simplejson.dumps({'commands': {'refresh_page': []}, 'success': True}),
\r
53 mimetype="application/json")
\r
55 def command(func, request, *args, **kwargs):
\r
57 response = func(request, *args, **kwargs)
\r
59 if isinstance(response, HttpResponse):
\r
62 response['success'] = True
\r
63 except Exception, e:
\r
65 #traceback.print_exc()
\r
67 if isinstance(e, CommandException):
\r
70 'error_message': e.message
\r
73 logging.error("%s: %s" % (func.__name__, str(e)))
\r
74 logging.error(traceback.format_exc())
\r
77 'error_message': _("We're sorry, but an unknown error ocurred.<br />Please try again in a while.")
\r
80 if request.is_ajax():
\r
81 return HttpResponse(simplejson.dumps(response), mimetype="application/json")
\r
83 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
\r