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
11 def login_required(func, request, *args, **kwargs):
\r
12 if not request.user.is_authenticated():
\r
13 return HttpResponseRedirect(reverse('auth_signin'))
\r
15 return func(request, *args, **kwargs)
\r
17 def render(template=None, tab=None, tab_title='', weight=500, tabbed=True):
\r
18 def decorator(func):
\r
19 def decorated(context, request, *args, **kwargs):
\r
20 if isinstance(context, HttpResponse):
\r
24 context['tab'] = tab
\r
26 return render_to_response(context.pop('template', template), context,
\r
27 context_instance=RequestContext(request))
\r
29 if tabbed and tab and tab_title:
\r
30 ui.register(ui.PAGE_TOP_TABS,
\r
31 ui.PageTab(tab, tab_title, lambda: reverse(func.__name__), weight=weight))
\r
33 return decorate.result.withfn(decorated, needs_params=True)(func)
\r
37 class CommandException(Exception):
\r
40 class RefreshPageCommand(HttpResponse):
\r
42 super(RefreshPageCommand, self).__init__(
\r
43 content=simplejson.dumps({'commands': {'refresh_page': []}, 'success': True}),
\r
44 mimetype="application/json")
\r
46 def command(func, request, *args, **kwargs):
\r
48 response = func(request, *args, **kwargs)
\r
50 if isinstance(response, HttpResponse):
\r
53 response['success'] = True
\r
54 except Exception, e:
\r
56 #traceback.print_exc()
\r
58 if isinstance(e, CommandException):
\r
61 'error_message': e.message
\r
64 logging.error("%s: %s" % (func.__name__, str(e)))
\r
65 logging.error(traceback.format_exc())
\r
68 'error_message': _("We're sorry, but an unknown error ocurred.<br />Please try again in a while.")
\r
71 if request.is_ajax():
\r
72 return HttpResponse(simplejson.dumps(response), mimetype="application/json")
\r
74 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
\r