1 # -*- coding: utf-8 -*-
5 from datetime import datetime
7 from django.http import HttpResponse, HttpResponseRedirect
8 from django.shortcuts import render_to_response
9 from django.core.urlresolvers import reverse
10 from django.template import RequestContext
11 from django.utils.translation import ugettext as _
13 from forum.modules import ui, decorate
14 from forum.settings import ONLINE_USERS
16 def login_required(func, request, *args, **kwargs):
17 if not request.user.is_authenticated():
18 return HttpResponseRedirect(reverse('auth_signin'))
20 return func(request, *args, **kwargs)
22 def render(template=None, tab=None, tab_title='', weight=500, tabbed=True):
24 def decorated(context, request, *args, **kwargs):
25 if request.user.is_authenticated():
26 ONLINE_USERS[request.user] = datetime.now()
28 if isinstance(context, HttpResponse):
34 return render_to_response(context.pop('template', template), context,
35 context_instance=RequestContext(request))
37 if tabbed and tab and tab_title:
38 ui.register(ui.PAGE_TOP_TABS,
39 ui.PageTab(tab, tab_title, lambda: reverse(func.__name__), weight=weight))
41 return decorate.result.withfn(decorated, needs_params=True)(func)
45 class CommandException(Exception):
48 class RefreshPageCommand(HttpResponse):
50 super(RefreshPageCommand, self).__init__(
51 content=json.dumps({'commands': {'refresh_page': []}, 'success': True}),
52 mimetype="application/json")
54 def command(func, request, *args, **kwargs):
56 response = func(request, *args, **kwargs)
58 if isinstance(response, HttpResponse):
61 response['success'] = True
64 #traceback.print_exc()
66 if isinstance(e, CommandException):
69 'error_message': e.message
72 logging.error("%s: %s" % (func.__name__, str(e)))
73 logging.error(traceback.format_exc())
76 'error_message': _("We're sorry, but an unknown error ocurred.<br />Please try again in a while.")
80 return HttpResponse(json.dumps(response), mimetype="application/json")
82 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))