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
\r
11 def render(template=None, tab=None, tab_title='', weight=500, tabbed=True):
\r
12 def decorator(func):
\r
13 def decorated(request, *args, **kwargs):
\r
14 context = func(request, *args, **kwargs)
\r
17 context['tab'] = tab
\r
19 return render_to_response(context.pop('template', template), context,
\r
20 context_instance=RequestContext(request))
\r
23 ui.register(ui.PAGE_TOP_TABS,
\r
24 ui.PageTab(tab, tab_title, lambda: reverse(func.__name__), weight=weight))
\r
30 def list(paginate, default_page_size):
\r
31 def decorator(func):
\r
32 def decorated(request, *args, **kwargs):
\r
33 context = func(request, *args, **kwargs)
\r
35 pagesize = request.utils.page_size(default_page_size)
\r
36 page = int(request.GET.get('page', 1))
\r
38 big_list = context[paginate]
\r
39 paginator = Paginator(big_list, pagesize)
\r
42 page_obj = paginator.page(page)
\r
46 context[paginate] = page_obj.object_list.lazy()
\r
48 base_path = context.get('base_path', None) or request.path
\r
49 sort = request.utils.sort_method('')
\r
51 context["pagination_context"] = {
\r
52 'is_paginated' : True,
\r
53 'pages': paginator.num_pages,
\r
55 'has_previous': page_obj.has_previous(),
\r
56 'has_next': page_obj.has_next(),
\r
57 'previous': page_obj.previous_page_number(),
\r
58 'next': page_obj.next_page_number(),
\r
59 'base_url' : "%s%ssort=%s&" % (base_path, ('?' in base_path) and '&' or '?', sort),
\r
60 'pagesize' : pagesize
\r
63 context['sort_context'] = {
\r
64 'base_url': "%s%ssort=" % (base_path, ('?' in base_path) and '&' or '?'),
\r
75 class CommandException(Exception):
\r
78 class RefreshPageCommand(HttpResponse):
\r
80 super(RefreshPageCommand, self).__init__(
\r
81 content=simplejson.dumps({'commands': {'refresh_page': []}, 'success': True}),
\r
82 mimetype="application/json")
\r
85 def decorated(request, *args, **kwargs):
\r
87 response = func(request, *args, **kwargs)
\r
89 if isinstance(response, HttpResponse):
\r
92 response['success'] = True
\r
93 except Exception, e:
\r
95 #traceback.print_exc()
\r
97 if isinstance(e, CommandException):
\r
100 'error_message': e.message
\r
103 logging.error("%s: %s" % (func.__name__, str(e)))
\r
104 logging.error(traceback.format_exc())
\r
107 'error_message': _("We're sorry, but an unknown error ocurred.<br />Please try again in a while.")
\r
110 if request.is_ajax():
\r
111 return HttpResponse(simplejson.dumps(response), mimetype="application/json")
\r
113 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
\r