]> git.openstreetmap.org Git - osqa.git/blob - forum/views/decorators.py
fix in paginator
[osqa.git] / forum / views / decorators.py
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
6 \r
7 def render(template=None, tab=None):\r
8     def decorator(func):\r
9         def decorated(request, *args, **kwargs):\r
10             context = func(request, *args, **kwargs)\r
11 \r
12             if tab is not None:\r
13                 context['tab'] = tab\r
14 \r
15             return render_to_response(context.pop('template', template), context, context_instance=RequestContext(request))\r
16         return decorated\r
17     return decorator\r
18 \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
23 \r
24             pagesize = request.utils.page_size(default_page_size)\r
25             page = int(request.GET.get('page', 1))\r
26 \r
27             big_list = context[paginate]\r
28             paginator = Paginator(big_list, pagesize)\r
29 \r
30             page_obj = paginator.page(page)\r
31             context[paginate] = page_obj.object_list\r
32 \r
33             base_path = context.get('base_path', None) or request.path\r
34             sort = request.utils.sort_method('')\r
35 \r
36             context["pagination_context"] = {\r
37                 'is_paginated' : True,\r
38                 'pages': paginator.num_pages,\r
39                 'page': page,\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
46             }\r
47 \r
48             context['sort_context'] = {\r
49                 'base_url': "%s%ssort=" % (base_path, ('?' in base_path) and '&' or '?'),\r
50                 'current': sort,\r
51             }\r
52 \r
53             return context\r
54         return decorated\r
55     return decorator\r
56 \r
57 \r
58 def command(func):\r
59     def decorated(request, *args, **kwargs):\r
60         try:\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
66 \r
67             response = {\r
68                 'success': False,\r
69                 'error_message': str(e)\r
70             }\r
71 \r
72         if request.is_ajax():\r
73             return HttpResponse(simplejson.dumps(response), mimetype="application/json")\r
74         else:\r
75             return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))\r
76 \r
77     return decorated