Add more information on deploying; fix format.
[ganeti_webmgr.git] / ganeti_web / views / generic.py
blob5aa27248aab9735c23383c86403e367941bdc774
1 # Copyright (c) 2012 Oregon State University
3 # Generic class-based view mixins and helpers.
5 from django.conf import settings
6 from django.contrib.auth.decorators import login_required
7 from django.utils.decorators import method_decorator
8 from django.utils.translation import ugettext as _
9 from django.views.generic.list import ListView
11 # Standard translation messages. We use these everywhere.
13 NO_PRIVS = _('You do not have sufficient privileges')
16 class LoginRequiredMixin(object):
17 """
18 Helper mixin which applies @login_required to all methods on a view.
20 Meant to massively simplify the four-line prelude common to many of our
21 views.
22 """
24 @method_decorator(login_required)
25 def dispatch(self, *args, **kwargs):
26 return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
29 class PagedListView(ListView):
30 """
31 Helper which automatically applies uniform pagination options to any
32 paginated list.
34 This helper should be mixed in *before* ListView or any of its relatives.
35 """
37 def get_paginate_by(self, queryset):
38 """
39 Return the number of items to paginate by.
41 I like the wording on the other docstring: "An integer specifying how
42 many objects should be displayed per page."
43 """
45 return self.request.GET.get("count", settings.ITEMS_PER_PAGE)
47 def paginate_queryset(self, queryset, page_size):
48 """
49 Returns a 4-tuple containing (paginator, page, object_list,
50 is_paginated).
52 The Django docstring isn't super-helpful. This function is the actual
53 workhorse of pagination. Our hook here is meant to order the queryset,
54 if needed, prior to pagination since Django won't do it otherwise.
55 """
57 if "order_by" in self.request.GET:
58 queryset = queryset.order_by(self.request.GET["order_by"])
59 return super(PagedListView, self).paginate_queryset(queryset,
60 page_size)