Debian Codegraph @

django/contrib/admin/views/main.py

raw
1 import warnings
2 from datetime import datetime, timedelta
3
4 from django import forms
5 from django.conf import settings
6 from django.contrib import messages
7 from django.contrib.admin import FieldListFilter
8 from django.contrib.admin.exceptions import (
9 DisallowedModelAdminLookup,
10 DisallowedModelAdminToField,
11 )
12 from django.contrib.admin.options import (
13 IS_FACETS_VAR,
14 IS_POPUP_VAR,
15 TO_FIELD_VAR,
16 IncorrectLookupParameters,
17 ShowFacets,
18 )
19 from django.contrib.admin.utils import (
20 build_q_object_from_lookup_parameters,
21 get_fields_from_path,
22 lookup_spawns_duplicates,
23 prepare_lookup_value,
24 quote,
25 )
26 from django.core.exceptions import (
27 FieldDoesNotExist,
28 ImproperlyConfigured,
29 SuspiciousOperation,
30 )
31 from django.core.paginator import InvalidPage
32 from django.db.models import F, Field, ManyToOneRel, OrderBy
33 from django.db.models.constants import LOOKUP_SEP
34 from django.db.models.expressions import Combinable
35 from django.urls import reverse
36 from django.utils.deprecation import RemovedInDjango60Warning
37 from django.utils.http import urlencode
38 from django.utils.inspect import func_supports_parameter
39 from django.utils.timezone import make_aware
40 from django.utils.translation import gettext
41
42 # Changelist settings
43 ALL_VAR = "all"
44 ORDER_VAR = "o"
45 PAGE_VAR = "p"
46 SEARCH_VAR = "q"
47 ERROR_FLAG = "e"
48
49 IGNORED_PARAMS = (
50 ALL_VAR,
51 ORDER_VAR,
52 SEARCH_VAR,
53 IS_FACETS_VAR,
54 IS_POPUP_VAR,
55 TO_FIELD_VAR,
56 )
57
58
59 class ChangeListSearchForm(forms.Form):
60 def __init__(self, *args, **kwargs):
61 super().__init__(*args, **kwargs)
62 # Populate "fields" dynamically because SEARCH_VAR is a variable:
63 self.fields = {
64 SEARCH_VAR: forms.CharField(required=False, strip=False),
65 }
66
67
68 class ChangeList:
69 search_form_class = ChangeListSearchForm
70
71 def __init__(
72 self,
73 request,
74 model,
75 list_display,
76 list_display_links,
77 list_filter,
78 date_hierarchy,
79 search_fields,
80 list_select_related,
81 list_per_page,
82 list_max_show_all,
83 list_editable,
84 model_admin,
85 sortable_by,
86 search_help_text,
87 ):
88 self.model = model
89 self.opts = model._meta
90 self.lookup_opts = self.opts
91 self.root_queryset = model_admin.get_queryset(request)
92 self.list_display = list_display
93 self.list_display_links = list_display_links
94 self.list_filter = list_filter
95 self.has_filters = None
96 self.has_active_filters = None
97 self.clear_all_filters_qs = None
98 self.date_hierarchy = date_hierarchy
99 self.search_fields = search_fields
100 self.list_select_related = list_select_related
101 self.list_per_page = list_per_page
102 self.list_max_show_all = list_max_show_all
103 self.model_admin = model_admin
104 self.preserved_filters = model_admin.get_preserved_filters(request)
105 self.sortable_by = sortable_by
106 self.search_help_text = search_help_text
107
108 # Get search parameters from the query string.
109 _search_form = self.search_form_class(request.GET)
110 if not _search_form.is_valid():
111 for error in _search_form.errors.values():
112 messages.error(request, ", ".join(error))
113 self.query = _search_form.cleaned_data.get(SEARCH_VAR) or ""
114 try:
115 self.page_num = int(request.GET.get(PAGE_VAR, 1))
116 except ValueError:
117 self.page_num = 1
118 self.show_all = ALL_VAR in request.GET
119 self.is_popup = IS_POPUP_VAR in request.GET
120 self.add_facets = model_admin.show_facets is ShowFacets.ALWAYS or (
121 model_admin.show_facets is ShowFacets.ALLOW and IS_FACETS_VAR in request.GET
122 )
123 self.is_facets_optional = model_admin.show_facets is ShowFacets.ALLOW
124 to_field = request.GET.get(TO_FIELD_VAR)
125 if to_field and not model_admin.to_field_allowed(request, to_field):
126 raise DisallowedModelAdminToField(
127 "The field %s cannot be referenced." % to_field
128 )
129 self.to_field = to_field
130 self.params = dict(request.GET.items())
131 self.filter_params = dict(request.GET.lists())
132 if PAGE_VAR in self.params:
133 del self.params[PAGE_VAR]
134 del self.filter_params[PAGE_VAR]
135 if ERROR_FLAG in self.params:
136 del self.params[ERROR_FLAG]
137 del self.filter_params[ERROR_FLAG]
138 self.remove_facet_link = self.get_query_string(remove=[IS_FACETS_VAR])
139 self.add_facet_link = self.get_query_string({IS_FACETS_VAR: True})
140
141 if self.is_popup:
142 self.list_editable = ()
143 else:
144 self.list_editable = list_editable
145 self.queryset = self.get_queryset(request)
146 self.get_results(request)
147 if self.is_popup:
148 title = gettext("Select %s")
149 elif self.model_admin.has_change_permission(request):
150 title = gettext("Select %s to change")
151 else:
152 title = gettext("Select %s to view")
153 self.title = title % self.opts.verbose_name
154 self.pk_attname = self.lookup_opts.pk.attname
155
156 def __repr__(self):
157 return "<%s: model=%s model_admin=%s>" % (
158 self.__class__.__qualname__,
159 self.model.__qualname__,
160 self.model_admin.__class__.__qualname__,
161 )
162
163 def get_filters_params(self, params=None):
164 """
165 Return all params except IGNORED_PARAMS.
166 """
167 params = params or self.filter_params
168 lookup_params = params.copy() # a dictionary of the query string
169 # Remove all the parameters that are globally and systematically
170 # ignored.
171 for ignored in IGNORED_PARAMS:
172 if ignored in lookup_params:
173 del lookup_params[ignored]
174 return lookup_params
175
176 def get_filters(self, request):
177 lookup_params = self.get_filters_params()
178 may_have_duplicates = False
179 has_active_filters = False
180
181 supports_request = func_supports_parameter(
182 self.model_admin.lookup_allowed, "request"
183 )
184 if not supports_request:
185 warnings.warn(
186 f"`request` must be added to the signature of "
187 f"{self.model_admin.__class__.__qualname__}.lookup_allowed().",
188 RemovedInDjango60Warning,
189 )
190 for key, value_list in lookup_params.items():
191 for value in value_list:
192 params = (key, value, request) if supports_request else (key, value)
193 if not self.model_admin.lookup_allowed(*params):
194 raise DisallowedModelAdminLookup(f"Filtering by {key} not allowed")
195
196 filter_specs = []
197 for list_filter in self.list_filter:
198 lookup_params_count = len(lookup_params)
199 if callable(list_filter):
200 # This is simply a custom list filter class.
201 spec = list_filter(request, lookup_params, self.model, self.model_admin)
202 else:
203 field_path = None
204 if isinstance(list_filter, (tuple, list)):
205 # This is a custom FieldListFilter class for a given field.
206 field, field_list_filter_class = list_filter
207 else:
208 # This is simply a field name, so use the default
209 # FieldListFilter class that has been registered for the
210 # type of the given field.
211 field, field_list_filter_class = list_filter, FieldListFilter.create
212 if not isinstance(field, Field):
213 field_path = field
214 field = get_fields_from_path(self.model, field_path)[-1]
215
216 spec = field_list_filter_class(
217 field,
218 request,
219 lookup_params,
220 self.model,
221 self.model_admin,
222 field_path=field_path,
223 )
224 # field_list_filter_class removes any lookup_params it
225 # processes. If that happened, check if duplicates should be
226 # removed.
227 if lookup_params_count > len(lookup_params):
228 may_have_duplicates |= lookup_spawns_duplicates(
229 self.lookup_opts,
230 field_path,
231 )
232 if spec and spec.has_output():
233 filter_specs.append(spec)
234 if lookup_params_count > len(lookup_params):
235 has_active_filters = True
236
237 if self.date_hierarchy:
238 # Create bounded lookup parameters so that the query is more
239 # efficient.
240 year = lookup_params.pop("%s__year" % self.date_hierarchy, None)
241 if year is not None:
242 month = lookup_params.pop("%s__month" % self.date_hierarchy, None)
243 day = lookup_params.pop("%s__day" % self.date_hierarchy, None)
244 try:
245 from_date = datetime(
246 int(year[-1]),
247 int(month[-1] if month is not None else 1),
248 int(day[-1] if day is not None else 1),
249 )
250 except ValueError as e:
251 raise IncorrectLookupParameters(e) from e
252 if day:
253 to_date = from_date + timedelta(days=1)
254 elif month:
255 # In this branch, from_date will always be the first of a
256 # month, so advancing 32 days gives the next month.
257 to_date = (from_date + timedelta(days=32)).replace(day=1)
258 else:
259 to_date = from_date.replace(year=from_date.year + 1)
260 if settings.USE_TZ:
261 from_date = make_aware(from_date)
262 to_date = make_aware(to_date)
263 lookup_params.update(
264 {
265 "%s__gte" % self.date_hierarchy: [from_date],
266 "%s__lt" % self.date_hierarchy: [to_date],
267 }
268 )
269
270 # At this point, all the parameters used by the various ListFilters
271 # have been removed from lookup_params, which now only contains other
272 # parameters passed via the query string. We now loop through the
273 # remaining parameters both to ensure that all the parameters are valid
274 # fields and to determine if at least one of them spawns duplicates. If
275 # the lookup parameters aren't real fields, then bail out.
276 try:
277 for key, value in lookup_params.items():
278 lookup_params[key] = prepare_lookup_value(key, value)
279 may_have_duplicates |= lookup_spawns_duplicates(self.lookup_opts, key)
280 return (
281 filter_specs,
282 bool(filter_specs),
283 lookup_params,
284 may_have_duplicates,
285 has_active_filters,
286 )
287 except FieldDoesNotExist as e:
288 raise IncorrectLookupParameters(e) from e
289
290 def get_query_string(self, new_params=None, remove=None):
291 if new_params is None:
292 new_params = {}
293 if remove is None:
294 remove = []
295 p = self.filter_params.copy()
296 for r in remove:
297 for k in list(p):
298 if k.startswith(r):
299 del p[k]
300 for k, v in new_params.items():
301 if v is None:
302 if k in p:
303 del p[k]
304 else:
305 p[k] = v
306 return "?%s" % urlencode(sorted(p.items()), doseq=True)
307
308 def get_results(self, request):
309 paginator = self.model_admin.get_paginator(
310 request, self.queryset, self.list_per_page
311 )
312 # Get the number of objects, with admin filters applied.
313 result_count = paginator.count
314
315 # Get the total number of objects, with no admin filters applied.
316 # Note this isn't necessarily the same as result_count in the case of
317 # no filtering. Filters defined in list_filters may still apply some
318 # default filtering which may be removed with query parameters.
319 if self.model_admin.show_full_result_count:
320 full_result_count = self.root_queryset.count()
321 else:
322 full_result_count = None
323 can_show_all = result_count <= self.list_max_show_all
324 multi_page = result_count > self.list_per_page
325
326 # Get the list of objects to display on this page.
327 if (self.show_all and can_show_all) or not multi_page:
328 result_list = self.queryset._clone()
329 else:
330 try:
331 result_list = paginator.page(self.page_num).object_list
332 except InvalidPage:
333 raise IncorrectLookupParameters
334
335 self.result_count = result_count
336 self.show_full_result_count = self.model_admin.show_full_result_count
337 # Admin actions are shown if there is at least one entry
338 # or if entries are not counted because show_full_result_count is disabled
339 self.show_admin_actions = not self.show_full_result_count or bool(
340 full_result_count
341 )
342 self.full_result_count = full_result_count
343 self.result_list = result_list
344 self.can_show_all = can_show_all
345 self.multi_page = multi_page
346 self.paginator = paginator
347
348 def _get_default_ordering(self):
349 ordering = []
350 if self.model_admin.ordering:
351 ordering = self.model_admin.ordering
352 elif self.lookup_opts.ordering:
353 ordering = self.lookup_opts.ordering
354 return ordering
355
356 def get_ordering_field(self, field_name):
357 """
358 Return the proper model field name corresponding to the given
359 field_name to use for ordering. field_name may either be the name of a
360 proper model field, possibly across relations, or the name of a method
361 (on the admin or model) or a callable with the 'admin_order_field'
362 attribute. Return None if no proper model field name can be matched.
363 """
364 try:
365 field = self.lookup_opts.get_field(field_name)
366 return field.name
367 except FieldDoesNotExist:
368 # See whether field_name is a name of a non-field
369 # that allows sorting.
370 if callable(field_name):
371 attr = field_name
372 elif hasattr(self.model_admin, field_name):
373 attr = getattr(self.model_admin, field_name)
374 else:
375 try:
376 attr = getattr(self.model, field_name)
377 except AttributeError:
378 if LOOKUP_SEP in field_name:
379 return field_name
380 raise
381 if isinstance(attr, property) and hasattr(attr, "fget"):
382 attr = attr.fget
383 return getattr(attr, "admin_order_field", None)
384
385 def get_ordering(self, request, queryset):
386 """
387 Return the list of ordering fields for the change list.
388 First check the get_ordering() method in model admin, then check
389 the object's default ordering. Then, any manually-specified ordering
390 from the query string overrides anything. Finally, a deterministic
391 order is guaranteed by calling _get_deterministic_ordering() with the
392 constructed ordering.
393 """
394 params = self.params
395 ordering = list(
396 self.model_admin.get_ordering(request) or self._get_default_ordering()
397 )
398 if params.get(ORDER_VAR):
399 # Clear ordering and used params
400 ordering = []
401 order_params = params[ORDER_VAR].split(".")
402 for p in order_params:
403 try:
404 none, pfx, idx = p.rpartition("-")
405 field_name = self.list_display[int(idx)]
406 order_field = self.get_ordering_field(field_name)
407 if not order_field:
408 continue # No 'admin_order_field', skip it
409 if isinstance(order_field, OrderBy):
410 if pfx == "-":
411 order_field = order_field.copy()
412 order_field.reverse_ordering()
413 ordering.append(order_field)
414 elif hasattr(order_field, "resolve_expression"):
415 # order_field is an expression.
416 ordering.append(
417 order_field.desc() if pfx == "-" else order_field.asc()
418 )
419 # reverse order if order_field has already "-" as prefix
420 elif pfx == "-" and order_field.startswith(pfx):
421 ordering.append(order_field.removeprefix(pfx))
422 else:
423 ordering.append(pfx + order_field)
424 except (IndexError, ValueError):
425 continue # Invalid ordering specified, skip it.
426
427 # Add the given query's ordering fields, if any.
428 ordering.extend(queryset.query.order_by)
429
430 return self._get_deterministic_ordering(ordering)
431
432 def _get_deterministic_ordering(self, ordering):
433 """
434 Ensure a deterministic order across all database backends. Search for a
435 single field or unique together set of fields providing a total
436 ordering. If these are missing, augment the ordering with a descendant
437 primary key.
438 """
439 ordering = list(ordering)
440 ordering_fields = set()
441 total_ordering_fields = {"pk"} | {
442 field.attname
443 for field in self.lookup_opts.fields
444 if field.unique and not field.null
445 }
446 for part in ordering:
447 # Search for single field providing a total ordering.
448 field_name = None
449 if isinstance(part, str):
450 field_name = part.lstrip("-")
451 elif isinstance(part, F):
452 field_name = part.name
453 elif isinstance(part, OrderBy) and isinstance(part.expression, F):
454 field_name = part.expression.name
455 if field_name:
456 # Normalize attname references by using get_field().
457 try:
458 field = self.lookup_opts.get_field(field_name)
459 except FieldDoesNotExist:
460 # Could be "?" for random ordering or a related field
461 # lookup. Skip this part of introspection for now.
462 continue
463 # Ordering by a related field name orders by the referenced
464 # model's ordering. Skip this part of introspection for now.
465 if field.remote_field and field_name == field.name:
466 continue
467 if field.attname in total_ordering_fields:
468 break
469 ordering_fields.add(field.attname)
470 else:
471 # No single total ordering field, try unique_together and total
472 # unique constraints.
473 constraint_field_names = (
474 *self.lookup_opts.unique_together,
475 *(
476 constraint.fields
477 for constraint in self.lookup_opts.total_unique_constraints
478 ),
479 )
480 for field_names in constraint_field_names:
481 # Normalize attname references by using get_field().
482 fields = [
483 self.lookup_opts.get_field(field_name) for field_name in field_names
484 ]
485 # Composite unique constraints containing a nullable column
486 # cannot ensure total ordering.
487 if any(field.null for field in fields):
488 continue
489 if ordering_fields.issuperset(field.attname for field in fields):
490 break
491 else:
492 # If no set of unique fields is present in the ordering, rely
493 # on the primary key to provide total ordering.
494 ordering.append("-pk")
495 return ordering
496
497 def get_ordering_field_columns(self):
498 """
499 Return a dictionary of ordering field column numbers and asc/desc.
500 """
501 # We must cope with more than one column having the same underlying sort
502 # field, so we base things on column numbers.
503 ordering = self._get_default_ordering()
504 ordering_fields = {}
505 if ORDER_VAR not in self.params:
506 # for ordering specified on ModelAdmin or model Meta, we don't know
507 # the right column numbers absolutely, because there might be more
508 # than one column associated with that ordering, so we guess.
509 for field in ordering:
510 if isinstance(field, (Combinable, OrderBy)):
511 if not isinstance(field, OrderBy):
512 field = field.asc()
513 if isinstance(field.expression, F):
514 order_type = "desc" if field.descending else "asc"
515 field = field.expression.name
516 else:
517 continue
518 elif field.startswith("-"):
519 field = field.removeprefix("-")
520 order_type = "desc"
521 else:
522 order_type = "asc"
523 for index, attr in enumerate(self.list_display):
524 if self.get_ordering_field(attr) == field:
525 ordering_fields[index] = order_type
526 break
527 else:
528 for p in self.params[ORDER_VAR].split("."):
529 none, pfx, idx = p.rpartition("-")
530 try:
531 idx = int(idx)
532 except ValueError:
533 continue # skip it
534 ordering_fields[idx] = "desc" if pfx == "-" else "asc"
535 return ordering_fields
536
537 def get_queryset(self, request, exclude_parameters=None):
538 # First, we collect all the declared list filters.
539 (
540 self.filter_specs,
541 self.has_filters,
542 remaining_lookup_params,
543 filters_may_have_duplicates,
544 self.has_active_filters,
545 ) = self.get_filters(request)
546 # Then, we let every list filter modify the queryset to its liking.
547 qs = self.root_queryset
548 for filter_spec in self.filter_specs:
549 if (
550 exclude_parameters is None
551 or filter_spec.expected_parameters() != exclude_parameters
552 ):
553 new_qs = filter_spec.queryset(request, qs)
554 if new_qs is not None:
555 qs = new_qs
556
557 try:
558 # Finally, we apply the remaining lookup parameters from the query
559 # string (i.e. those that haven't already been processed by the
560 # filters).
561 q_object = build_q_object_from_lookup_parameters(remaining_lookup_params)
562 qs = qs.filter(q_object)
563 except (SuspiciousOperation, ImproperlyConfigured):
564 # Allow certain types of errors to be re-raised as-is so that the
565 # caller can treat them in a special way.
566 raise
567 except Exception as e:
568 # Every other error is caught with a naked except, because we don't
569 # have any other way of validating lookup parameters. They might be
570 # invalid if the keyword arguments are incorrect, or if the values
571 # are not in the correct type, so we might get FieldError,
572 # ValueError, ValidationError, or ?.
573 raise IncorrectLookupParameters(e)
574
575 if not qs.query.select_related:
576 qs = self.apply_select_related(qs)
577
578 # Set ordering.
579 ordering = self.get_ordering(request, qs)
580 qs = qs.order_by(*ordering)
581
582 # Apply search results
583 qs, search_may_have_duplicates = self.model_admin.get_search_results(
584 request,
585 qs,
586 self.query,
587 )
588
589 # Set query string for clearing all filters.
590 self.clear_all_filters_qs = self.get_query_string(
591 new_params=remaining_lookup_params,
592 remove=self.get_filters_params(),
593 )
594 # Remove duplicates from results, if necessary
595 if filters_may_have_duplicates | search_may_have_duplicates:
596 return qs.distinct()
597 else:
598 return qs
599
600 def apply_select_related(self, qs):
601 if self.list_select_related is True:
602 return qs.select_related()
603
604 if self.list_select_related is False:
605 if self.has_related_field_in_list_display():
606 return qs.select_related()
607
608 if self.list_select_related:
609 return qs.select_related(*self.list_select_related)
610 return qs
611
612 def has_related_field_in_list_display(self):
613 for field_name in self.list_display:
614 try:
615 field = self.lookup_opts.get_field(field_name)
616 except FieldDoesNotExist:
617 pass
618 else:
619 if isinstance(field.remote_field, ManyToOneRel):
620 # <FK>_id field names don't require a join.
621 if field_name != field.attname:
622 return True
623 return False
624
625 def url_for_result(self, result):
626 pk = getattr(result, self.pk_attname)
627 return reverse(
628 "admin:%s_%s_change" % (self.opts.app_label, self.opts.model_name),
629 args=(quote(pk),),
630 current_app=self.model_admin.admin_site.name,
631 )