Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from django.core.exceptions import ImproperlyConfigured
  2. from django.core.paginator import InvalidPage, Paginator
  3. from django.db.models.query import QuerySet
  4. from django.http import Http404
  5. from django.utils.translation import gettext as _
  6. from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
  7. class MultipleObjectMixin(ContextMixin):
  8. """A mixin for views manipulating multiple objects."""
  9. allow_empty = True
  10. queryset = None
  11. model = None
  12. paginate_by = None
  13. paginate_orphans = 0
  14. context_object_name = None
  15. paginator_class = Paginator
  16. page_kwarg = 'page'
  17. ordering = None
  18. def get_queryset(self):
  19. """
  20. Return the list of items for this view.
  21. The return value must be an iterable and may be an instance of
  22. `QuerySet` in which case `QuerySet` specific behavior will be enabled.
  23. """
  24. if self.queryset is not None:
  25. queryset = self.queryset
  26. if isinstance(queryset, QuerySet):
  27. queryset = queryset.all()
  28. elif self.model is not None:
  29. queryset = self.model._default_manager.all()
  30. else:
  31. raise ImproperlyConfigured(
  32. "%(cls)s is missing a QuerySet. Define "
  33. "%(cls)s.model, %(cls)s.queryset, or override "
  34. "%(cls)s.get_queryset()." % {
  35. 'cls': self.__class__.__name__
  36. }
  37. )
  38. ordering = self.get_ordering()
  39. if ordering:
  40. if isinstance(ordering, str):
  41. ordering = (ordering,)
  42. queryset = queryset.order_by(*ordering)
  43. return queryset
  44. def get_ordering(self):
  45. """Return the field or fields to use for ordering the queryset."""
  46. return self.ordering
  47. def paginate_queryset(self, queryset, page_size):
  48. """Paginate the queryset, if needed."""
  49. paginator = self.get_paginator(
  50. queryset, page_size, orphans=self.get_paginate_orphans(),
  51. allow_empty_first_page=self.get_allow_empty())
  52. page_kwarg = self.page_kwarg
  53. page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
  54. try:
  55. page_number = int(page)
  56. except ValueError:
  57. if page == 'last':
  58. page_number = paginator.num_pages
  59. else:
  60. raise Http404(_("Page is not 'last', nor can it be converted to an int."))
  61. try:
  62. page = paginator.page(page_number)
  63. return (paginator, page, page.object_list, page.has_other_pages())
  64. except InvalidPage as e:
  65. raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
  66. 'page_number': page_number,
  67. 'message': str(e)
  68. })
  69. def get_paginate_by(self, queryset):
  70. """
  71. Get the number of items to paginate by, or ``None`` for no pagination.
  72. """
  73. return self.paginate_by
  74. def get_paginator(self, queryset, per_page, orphans=0,
  75. allow_empty_first_page=True, **kwargs):
  76. """Return an instance of the paginator for this view."""
  77. return self.paginator_class(
  78. queryset, per_page, orphans=orphans,
  79. allow_empty_first_page=allow_empty_first_page, **kwargs)
  80. def get_paginate_orphans(self):
  81. """
  82. Return the maximum number of orphans extend the last page by when
  83. paginating.
  84. """
  85. return self.paginate_orphans
  86. def get_allow_empty(self):
  87. """
  88. Return ``True`` if the view should display empty lists and ``False``
  89. if a 404 should be raised instead.
  90. """
  91. return self.allow_empty
  92. def get_context_object_name(self, object_list):
  93. """Get the name of the item to be used in the context."""
  94. if self.context_object_name:
  95. return self.context_object_name
  96. elif hasattr(object_list, 'model'):
  97. return '%s_list' % object_list.model._meta.model_name
  98. else:
  99. return None
  100. def get_context_data(self, *, object_list=None, **kwargs):
  101. """Get the context for this view."""
  102. queryset = object_list if object_list is not None else self.object_list
  103. page_size = self.get_paginate_by(queryset)
  104. context_object_name = self.get_context_object_name(queryset)
  105. if page_size:
  106. paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
  107. context = {
  108. 'paginator': paginator,
  109. 'page_obj': page,
  110. 'is_paginated': is_paginated,
  111. 'object_list': queryset
  112. }
  113. else:
  114. context = {
  115. 'paginator': None,
  116. 'page_obj': None,
  117. 'is_paginated': False,
  118. 'object_list': queryset
  119. }
  120. if context_object_name is not None:
  121. context[context_object_name] = queryset
  122. context.update(kwargs)
  123. return super().get_context_data(**context)
  124. class BaseListView(MultipleObjectMixin, View):
  125. """A base view for displaying a list of objects."""
  126. def get(self, request, *args, **kwargs):
  127. self.object_list = self.get_queryset()
  128. allow_empty = self.get_allow_empty()
  129. if not allow_empty:
  130. # When pagination is enabled and object_list is a queryset,
  131. # it's better to do a cheap query than to load the unpaginated
  132. # queryset in memory.
  133. if self.get_paginate_by(self.object_list) is not None and hasattr(self.object_list, 'exists'):
  134. is_empty = not self.object_list.exists()
  135. else:
  136. is_empty = not self.object_list
  137. if is_empty:
  138. raise Http404(_("Empty list and '%(class_name)s.allow_empty' is False.") % {
  139. 'class_name': self.__class__.__name__,
  140. })
  141. context = self.get_context_data()
  142. return self.render_to_response(context)
  143. class MultipleObjectTemplateResponseMixin(TemplateResponseMixin):
  144. """Mixin for responding with a template and list of objects."""
  145. template_name_suffix = '_list'
  146. def get_template_names(self):
  147. """
  148. Return a list of template names to be used for the request. Must return
  149. a list. May not be called if render_to_response is overridden.
  150. """
  151. try:
  152. names = super().get_template_names()
  153. except ImproperlyConfigured:
  154. # If template_name isn't specified, it's not a problem --
  155. # we just start with an empty list.
  156. names = []
  157. # If the list is a queryset, we'll invent a template name based on the
  158. # app and model name. This name gets put at the end of the template
  159. # name list so that user-supplied names override the automatically-
  160. # generated ones.
  161. if hasattr(self.object_list, 'model'):
  162. opts = self.object_list.model._meta
  163. names.append("%s/%s%s.html" % (opts.app_label, opts.model_name, self.template_name_suffix))
  164. elif not names:
  165. raise ImproperlyConfigured(
  166. "%(cls)s requires either a 'template_name' attribute "
  167. "or a get_queryset() method that returns a QuerySet." % {
  168. 'cls': self.__class__.__name__,
  169. }
  170. )
  171. return names
  172. class ListView(MultipleObjectTemplateResponseMixin, BaseListView):
  173. """
  174. Render some list of objects, set by `self.model` or `self.queryset`.
  175. `self.queryset` can actually be any iterable of items, not just a queryset.
  176. """