1 from django.http import HttpResponse, HttpResponseRedirect
\r
2 from django.utils import simplejson
\r
3 from django.core.paginator import Paginator
\r
4 from django.shortcuts import render_to_response
\r
5 from django.template import RequestContext
\r
7 def render(template=None, tab=None):
\r
9 def decorated(request, *args, **kwargs):
\r
10 context = func(request, *args, **kwargs)
\r
13 context['tab'] = tab
\r
15 return render_to_response(context.pop('template', template), context, context_instance=RequestContext(request))
\r
19 def list(paginate, default_page_size):
\r
20 def decorator(func):
\r
21 def decorated(request, *args, **kwargs):
\r
22 context = func(request, *args, **kwargs)
\r
24 pagesize = request.utils.page_size(default_page_size)
\r
25 page = int(request.GET.get('page', 1))
\r
27 big_list = context[paginate]
\r
28 paginator = Paginator(big_list, pagesize)
\r
30 page_obj = paginator.page(page)
\r
31 context[paginate] = page_obj.object_list
\r
33 base_path = context.get('base_path', None) or request.path
\r
34 sort = request.utils.sort_method('')
\r
36 context["pagination_context"] = {
\r
37 'is_paginated' : True,
\r
38 'pages': paginator.num_pages,
\r
40 'has_previous': page_obj.has_previous(),
\r
41 'has_next': page_obj.has_next(),
\r
42 'previous': page_obj.previous_page_number(),
\r
43 'next': page_obj.next_page_number(),
\r
44 'base_url' : "%s%ssort=%s" % (base_path, ('?' in base_path) and '&' or '?', sort),
\r
45 'pagesize' : pagesize
\r
48 context['sort_context'] = {
\r
49 'base_url': "%s%ssort=" % (base_path, ('?' in base_path) and '&' or '?'),
\r
59 def decorated(request, *args, **kwargs):
\r
61 response = func(request, *args, **kwargs)
\r
62 response['success'] = True
\r
63 except Exception, e:
\r
64 import sys, traceback
\r
65 traceback.print_exc(file=sys.stdout)
\r
69 'error_message': str(e)
\r
72 if request.is_ajax():
\r
73 return HttpResponse(simplejson.dumps(response), mimetype="application/json")
\r
75 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
\r