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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. from collections import OrderedDict
  2. from datetime import datetime, timedelta
  3. from django.conf import settings
  4. from django.contrib.admin import FieldListFilter
  5. from django.contrib.admin.exceptions import (
  6. DisallowedModelAdminLookup, DisallowedModelAdminToField,
  7. )
  8. from django.contrib.admin.options import (
  9. IS_POPUP_VAR, TO_FIELD_VAR, IncorrectLookupParameters,
  10. )
  11. from django.contrib.admin.utils import (
  12. get_fields_from_path, lookup_needs_distinct, prepare_lookup_value, quote,
  13. )
  14. from django.core.exceptions import (
  15. FieldDoesNotExist, ImproperlyConfigured, SuspiciousOperation,
  16. )
  17. from django.core.paginator import InvalidPage
  18. from django.db import models
  19. from django.db.models.expressions import Combinable, F, OrderBy
  20. from django.urls import reverse
  21. from django.utils.http import urlencode
  22. from django.utils.timezone import make_aware
  23. from django.utils.translation import gettext
  24. # Changelist settings
  25. ALL_VAR = 'all'
  26. ORDER_VAR = 'o'
  27. ORDER_TYPE_VAR = 'ot'
  28. PAGE_VAR = 'p'
  29. SEARCH_VAR = 'q'
  30. ERROR_FLAG = 'e'
  31. IGNORED_PARAMS = (
  32. ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR)
  33. class ChangeList:
  34. def __init__(self, request, model, list_display, list_display_links,
  35. list_filter, date_hierarchy, search_fields, list_select_related,
  36. list_per_page, list_max_show_all, list_editable, model_admin, sortable_by):
  37. self.model = model
  38. self.opts = model._meta
  39. self.lookup_opts = self.opts
  40. self.root_queryset = model_admin.get_queryset(request)
  41. self.list_display = list_display
  42. self.list_display_links = list_display_links
  43. self.list_filter = list_filter
  44. self.has_filters = None
  45. self.date_hierarchy = date_hierarchy
  46. self.search_fields = search_fields
  47. self.list_select_related = list_select_related
  48. self.list_per_page = list_per_page
  49. self.list_max_show_all = list_max_show_all
  50. self.model_admin = model_admin
  51. self.preserved_filters = model_admin.get_preserved_filters(request)
  52. self.sortable_by = sortable_by
  53. # Get search parameters from the query string.
  54. try:
  55. self.page_num = int(request.GET.get(PAGE_VAR, 0))
  56. except ValueError:
  57. self.page_num = 0
  58. self.show_all = ALL_VAR in request.GET
  59. self.is_popup = IS_POPUP_VAR in request.GET
  60. to_field = request.GET.get(TO_FIELD_VAR)
  61. if to_field and not model_admin.to_field_allowed(request, to_field):
  62. raise DisallowedModelAdminToField("The field %s cannot be referenced." % to_field)
  63. self.to_field = to_field
  64. self.params = dict(request.GET.items())
  65. if PAGE_VAR in self.params:
  66. del self.params[PAGE_VAR]
  67. if ERROR_FLAG in self.params:
  68. del self.params[ERROR_FLAG]
  69. if self.is_popup:
  70. self.list_editable = ()
  71. else:
  72. self.list_editable = list_editable
  73. self.query = request.GET.get(SEARCH_VAR, '')
  74. self.queryset = self.get_queryset(request)
  75. self.get_results(request)
  76. if self.is_popup:
  77. title = gettext('Select %s')
  78. elif self.model_admin.has_change_permission(request):
  79. title = gettext('Select %s to change')
  80. else:
  81. title = gettext('Select %s to view')
  82. self.title = title % self.opts.verbose_name
  83. self.pk_attname = self.lookup_opts.pk.attname
  84. def get_filters_params(self, params=None):
  85. """
  86. Return all params except IGNORED_PARAMS.
  87. """
  88. params = params or self.params
  89. lookup_params = params.copy() # a dictionary of the query string
  90. # Remove all the parameters that are globally and systematically
  91. # ignored.
  92. for ignored in IGNORED_PARAMS:
  93. if ignored in lookup_params:
  94. del lookup_params[ignored]
  95. return lookup_params
  96. def get_filters(self, request):
  97. lookup_params = self.get_filters_params()
  98. use_distinct = False
  99. for key, value in lookup_params.items():
  100. if not self.model_admin.lookup_allowed(key, value):
  101. raise DisallowedModelAdminLookup("Filtering by %s not allowed" % key)
  102. filter_specs = []
  103. for list_filter in self.list_filter:
  104. if callable(list_filter):
  105. # This is simply a custom list filter class.
  106. spec = list_filter(request, lookup_params, self.model, self.model_admin)
  107. else:
  108. field_path = None
  109. if isinstance(list_filter, (tuple, list)):
  110. # This is a custom FieldListFilter class for a given field.
  111. field, field_list_filter_class = list_filter
  112. else:
  113. # This is simply a field name, so use the default
  114. # FieldListFilter class that has been registered for the
  115. # type of the given field.
  116. field, field_list_filter_class = list_filter, FieldListFilter.create
  117. if not isinstance(field, models.Field):
  118. field_path = field
  119. field = get_fields_from_path(self.model, field_path)[-1]
  120. lookup_params_count = len(lookup_params)
  121. spec = field_list_filter_class(
  122. field, request, lookup_params,
  123. self.model, self.model_admin, field_path=field_path,
  124. )
  125. # field_list_filter_class removes any lookup_params it
  126. # processes. If that happened, check if distinct() is needed to
  127. # remove duplicate results.
  128. if lookup_params_count > len(lookup_params):
  129. use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, field_path)
  130. if spec and spec.has_output():
  131. filter_specs.append(spec)
  132. if self.date_hierarchy:
  133. # Create bounded lookup parameters so that the query is more
  134. # efficient.
  135. year = lookup_params.pop('%s__year' % self.date_hierarchy, None)
  136. if year is not None:
  137. month = lookup_params.pop('%s__month' % self.date_hierarchy, None)
  138. day = lookup_params.pop('%s__day' % self.date_hierarchy, None)
  139. try:
  140. from_date = datetime(
  141. int(year),
  142. int(month if month is not None else 1),
  143. int(day if day is not None else 1),
  144. )
  145. except ValueError as e:
  146. raise IncorrectLookupParameters(e) from e
  147. if settings.USE_TZ:
  148. from_date = make_aware(from_date)
  149. if day:
  150. to_date = from_date + timedelta(days=1)
  151. elif month:
  152. # In this branch, from_date will always be the first of a
  153. # month, so advancing 32 days gives the next month.
  154. to_date = (from_date + timedelta(days=32)).replace(day=1)
  155. else:
  156. to_date = from_date.replace(year=from_date.year + 1)
  157. lookup_params.update({
  158. '%s__gte' % self.date_hierarchy: from_date,
  159. '%s__lt' % self.date_hierarchy: to_date,
  160. })
  161. # At this point, all the parameters used by the various ListFilters
  162. # have been removed from lookup_params, which now only contains other
  163. # parameters passed via the query string. We now loop through the
  164. # remaining parameters both to ensure that all the parameters are valid
  165. # fields and to determine if at least one of them needs distinct(). If
  166. # the lookup parameters aren't real fields, then bail out.
  167. try:
  168. for key, value in lookup_params.items():
  169. lookup_params[key] = prepare_lookup_value(key, value)
  170. use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, key)
  171. return filter_specs, bool(filter_specs), lookup_params, use_distinct
  172. except FieldDoesNotExist as e:
  173. raise IncorrectLookupParameters(e) from e
  174. def get_query_string(self, new_params=None, remove=None):
  175. if new_params is None:
  176. new_params = {}
  177. if remove is None:
  178. remove = []
  179. p = self.params.copy()
  180. for r in remove:
  181. for k in list(p):
  182. if k.startswith(r):
  183. del p[k]
  184. for k, v in new_params.items():
  185. if v is None:
  186. if k in p:
  187. del p[k]
  188. else:
  189. p[k] = v
  190. return '?%s' % urlencode(sorted(p.items()))
  191. def get_results(self, request):
  192. paginator = self.model_admin.get_paginator(request, self.queryset, self.list_per_page)
  193. # Get the number of objects, with admin filters applied.
  194. result_count = paginator.count
  195. # Get the total number of objects, with no admin filters applied.
  196. if self.model_admin.show_full_result_count:
  197. full_result_count = self.root_queryset.count()
  198. else:
  199. full_result_count = None
  200. can_show_all = result_count <= self.list_max_show_all
  201. multi_page = result_count > self.list_per_page
  202. # Get the list of objects to display on this page.
  203. if (self.show_all and can_show_all) or not multi_page:
  204. result_list = self.queryset._clone()
  205. else:
  206. try:
  207. result_list = paginator.page(self.page_num + 1).object_list
  208. except InvalidPage:
  209. raise IncorrectLookupParameters
  210. self.result_count = result_count
  211. self.show_full_result_count = self.model_admin.show_full_result_count
  212. # Admin actions are shown if there is at least one entry
  213. # or if entries are not counted because show_full_result_count is disabled
  214. self.show_admin_actions = not self.show_full_result_count or bool(full_result_count)
  215. self.full_result_count = full_result_count
  216. self.result_list = result_list
  217. self.can_show_all = can_show_all
  218. self.multi_page = multi_page
  219. self.paginator = paginator
  220. def _get_default_ordering(self):
  221. ordering = []
  222. if self.model_admin.ordering:
  223. ordering = self.model_admin.ordering
  224. elif self.lookup_opts.ordering:
  225. ordering = self.lookup_opts.ordering
  226. return ordering
  227. def get_ordering_field(self, field_name):
  228. """
  229. Return the proper model field name corresponding to the given
  230. field_name to use for ordering. field_name may either be the name of a
  231. proper model field or the name of a method (on the admin or model) or a
  232. callable with the 'admin_order_field' attribute. Return None if no
  233. proper model field name can be matched.
  234. """
  235. try:
  236. field = self.lookup_opts.get_field(field_name)
  237. return field.name
  238. except FieldDoesNotExist:
  239. # See whether field_name is a name of a non-field
  240. # that allows sorting.
  241. if callable(field_name):
  242. attr = field_name
  243. elif hasattr(self.model_admin, field_name):
  244. attr = getattr(self.model_admin, field_name)
  245. else:
  246. attr = getattr(self.model, field_name)
  247. return getattr(attr, 'admin_order_field', None)
  248. def get_ordering(self, request, queryset):
  249. """
  250. Return the list of ordering fields for the change list.
  251. First check the get_ordering() method in model admin, then check
  252. the object's default ordering. Then, any manually-specified ordering
  253. from the query string overrides anything. Finally, a deterministic
  254. order is guaranteed by calling _get_deterministic_ordering() with the
  255. constructed ordering.
  256. """
  257. params = self.params
  258. ordering = list(self.model_admin.get_ordering(request) or self._get_default_ordering())
  259. if ORDER_VAR in params:
  260. # Clear ordering and used params
  261. ordering = []
  262. order_params = params[ORDER_VAR].split('.')
  263. for p in order_params:
  264. try:
  265. none, pfx, idx = p.rpartition('-')
  266. field_name = self.list_display[int(idx)]
  267. order_field = self.get_ordering_field(field_name)
  268. if not order_field:
  269. continue # No 'admin_order_field', skip it
  270. if hasattr(order_field, 'as_sql'):
  271. # order_field is an expression.
  272. ordering.append(order_field.desc() if pfx == '-' else order_field.asc())
  273. # reverse order if order_field has already "-" as prefix
  274. elif order_field.startswith('-') and pfx == '-':
  275. ordering.append(order_field[1:])
  276. else:
  277. ordering.append(pfx + order_field)
  278. except (IndexError, ValueError):
  279. continue # Invalid ordering specified, skip it.
  280. # Add the given query's ordering fields, if any.
  281. ordering.extend(queryset.query.order_by)
  282. return self._get_deterministic_ordering(ordering)
  283. def _get_deterministic_ordering(self, ordering):
  284. """
  285. Ensure a deterministic order across all database backends. Search for a
  286. single field or unique together set of fields providing a total
  287. ordering. If these are missing, augment the ordering with a descendant
  288. primary key.
  289. """
  290. ordering = list(ordering)
  291. ordering_fields = set()
  292. total_ordering_fields = {'pk'} | {
  293. field.attname for field in self.lookup_opts.fields
  294. if field.unique and not field.null
  295. }
  296. for part in ordering:
  297. # Search for single field providing a total ordering.
  298. field_name = None
  299. if isinstance(part, str):
  300. field_name = part.lstrip('-')
  301. elif isinstance(part, F):
  302. field_name = part.name
  303. elif isinstance(part, OrderBy) and isinstance(part.expression, F):
  304. field_name = part.expression.name
  305. if field_name:
  306. # Normalize attname references by using get_field().
  307. try:
  308. field = self.lookup_opts.get_field(field_name)
  309. except FieldDoesNotExist:
  310. # Could be "?" for random ordering or a related field
  311. # lookup. Skip this part of introspection for now.
  312. continue
  313. # Ordering by a related field name orders by the referenced
  314. # model's ordering. Skip this part of introspection for now.
  315. if field.remote_field and field_name == field.name:
  316. continue
  317. if field.attname in total_ordering_fields:
  318. break
  319. ordering_fields.add(field.attname)
  320. else:
  321. # No single total ordering field, try unique_together.
  322. for field_names in self.lookup_opts.unique_together:
  323. # Normalize attname references by using get_field().
  324. fields = [self.lookup_opts.get_field(field_name) for field_name in field_names]
  325. # Composite unique constraints containing a nullable column
  326. # cannot ensure total ordering.
  327. if any(field.null for field in fields):
  328. continue
  329. if ordering_fields.issuperset(field.attname for field in fields):
  330. break
  331. else:
  332. # If no set of unique fields is present in the ordering, rely
  333. # on the primary key to provide total ordering.
  334. ordering.append('-pk')
  335. return ordering
  336. def get_ordering_field_columns(self):
  337. """
  338. Return an OrderedDict of ordering field column numbers and asc/desc.
  339. """
  340. # We must cope with more than one column having the same underlying sort
  341. # field, so we base things on column numbers.
  342. ordering = self._get_default_ordering()
  343. ordering_fields = OrderedDict()
  344. if ORDER_VAR not in self.params:
  345. # for ordering specified on ModelAdmin or model Meta, we don't know
  346. # the right column numbers absolutely, because there might be more
  347. # than one column associated with that ordering, so we guess.
  348. for field in ordering:
  349. if isinstance(field, (Combinable, OrderBy)):
  350. if not isinstance(field, OrderBy):
  351. field = field.asc()
  352. if isinstance(field.expression, F):
  353. order_type = 'desc' if field.descending else 'asc'
  354. field = field.expression.name
  355. else:
  356. continue
  357. elif field.startswith('-'):
  358. field = field[1:]
  359. order_type = 'desc'
  360. else:
  361. order_type = 'asc'
  362. for index, attr in enumerate(self.list_display):
  363. if self.get_ordering_field(attr) == field:
  364. ordering_fields[index] = order_type
  365. break
  366. else:
  367. for p in self.params[ORDER_VAR].split('.'):
  368. none, pfx, idx = p.rpartition('-')
  369. try:
  370. idx = int(idx)
  371. except ValueError:
  372. continue # skip it
  373. ordering_fields[idx] = 'desc' if pfx == '-' else 'asc'
  374. return ordering_fields
  375. def get_queryset(self, request):
  376. # First, we collect all the declared list filters.
  377. (self.filter_specs, self.has_filters, remaining_lookup_params,
  378. filters_use_distinct) = self.get_filters(request)
  379. # Then, we let every list filter modify the queryset to its liking.
  380. qs = self.root_queryset
  381. for filter_spec in self.filter_specs:
  382. new_qs = filter_spec.queryset(request, qs)
  383. if new_qs is not None:
  384. qs = new_qs
  385. try:
  386. # Finally, we apply the remaining lookup parameters from the query
  387. # string (i.e. those that haven't already been processed by the
  388. # filters).
  389. qs = qs.filter(**remaining_lookup_params)
  390. except (SuspiciousOperation, ImproperlyConfigured):
  391. # Allow certain types of errors to be re-raised as-is so that the
  392. # caller can treat them in a special way.
  393. raise
  394. except Exception as e:
  395. # Every other error is caught with a naked except, because we don't
  396. # have any other way of validating lookup parameters. They might be
  397. # invalid if the keyword arguments are incorrect, or if the values
  398. # are not in the correct type, so we might get FieldError,
  399. # ValueError, ValidationError, or ?.
  400. raise IncorrectLookupParameters(e)
  401. if not qs.query.select_related:
  402. qs = self.apply_select_related(qs)
  403. # Set ordering.
  404. ordering = self.get_ordering(request, qs)
  405. qs = qs.order_by(*ordering)
  406. # Apply search results
  407. qs, search_use_distinct = self.model_admin.get_search_results(request, qs, self.query)
  408. # Remove duplicates from results, if necessary
  409. if filters_use_distinct | search_use_distinct:
  410. return qs.distinct()
  411. else:
  412. return qs
  413. def apply_select_related(self, qs):
  414. if self.list_select_related is True:
  415. return qs.select_related()
  416. if self.list_select_related is False:
  417. if self.has_related_field_in_list_display():
  418. return qs.select_related()
  419. if self.list_select_related:
  420. return qs.select_related(*self.list_select_related)
  421. return qs
  422. def has_related_field_in_list_display(self):
  423. for field_name in self.list_display:
  424. try:
  425. field = self.lookup_opts.get_field(field_name)
  426. except FieldDoesNotExist:
  427. pass
  428. else:
  429. if isinstance(field.remote_field, models.ManyToOneRel):
  430. # <FK>_id field names don't require a join.
  431. if field_name != field.get_attname():
  432. return True
  433. return False
  434. def url_for_result(self, result):
  435. pk = getattr(result, self.pk_attname)
  436. return reverse('admin:%s_%s_change' % (self.opts.app_label,
  437. self.opts.model_name),
  438. args=(quote(pk),),
  439. current_app=self.model_admin.admin_site.name)