utils.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. import datetime
  2. import decimal
  3. import json
  4. from collections import defaultdict
  5. from django.core.exceptions import FieldDoesNotExist
  6. from django.db import models, router
  7. from django.db.models.constants import LOOKUP_SEP
  8. from django.db.models.deletion import Collector
  9. from django.forms.utils import pretty_name
  10. from django.urls import NoReverseMatch, reverse
  11. from django.utils import formats, timezone
  12. from django.utils.hashable import make_hashable
  13. from django.utils.html import format_html
  14. from django.utils.regex_helper import _lazy_re_compile
  15. from django.utils.text import capfirst
  16. from django.utils.translation import ngettext
  17. from django.utils.translation import override as translation_override
  18. QUOTE_MAP = {i: "_%02X" % i for i in b'":/_#?;@&=+$,"[]<>%\n\\'}
  19. UNQUOTE_MAP = {v: chr(k) for k, v in QUOTE_MAP.items()}
  20. UNQUOTE_RE = _lazy_re_compile("_(?:%s)" % "|".join([x[1:] for x in UNQUOTE_MAP]))
  21. class FieldIsAForeignKeyColumnName(Exception):
  22. """A field is a foreign key attname, i.e. <FK>_id."""
  23. pass
  24. def lookup_spawns_duplicates(opts, lookup_path):
  25. """
  26. Return True if the given lookup path spawns duplicates.
  27. """
  28. lookup_fields = lookup_path.split(LOOKUP_SEP)
  29. # Go through the fields (following all relations) and look for an m2m.
  30. for field_name in lookup_fields:
  31. if field_name == "pk":
  32. field_name = opts.pk.name
  33. try:
  34. field = opts.get_field(field_name)
  35. except FieldDoesNotExist:
  36. # Ignore query lookups.
  37. continue
  38. else:
  39. if hasattr(field, "path_infos"):
  40. # This field is a relation; update opts to follow the relation.
  41. path_info = field.path_infos
  42. opts = path_info[-1].to_opts
  43. if any(path.m2m for path in path_info):
  44. # This field is a m2m relation so duplicates must be
  45. # handled.
  46. return True
  47. return False
  48. def prepare_lookup_value(key, value, separator=","):
  49. """
  50. Return a lookup value prepared to be used in queryset filtering.
  51. """
  52. # if key ends with __in, split parameter into separate values
  53. if key.endswith("__in"):
  54. value = value.split(separator)
  55. # if key ends with __isnull, special case '' and the string literals 'false' and '0'
  56. elif key.endswith("__isnull"):
  57. value = value.lower() not in ("", "false", "0")
  58. return value
  59. def quote(s):
  60. """
  61. Ensure that primary key values do not confuse the admin URLs by escaping
  62. any '/', '_' and ':' and similarly problematic characters.
  63. Similar to urllib.parse.quote(), except that the quoting is slightly
  64. different so that it doesn't get automatically unquoted by the web browser.
  65. """
  66. return s.translate(QUOTE_MAP) if isinstance(s, str) else s
  67. def unquote(s):
  68. """Undo the effects of quote()."""
  69. return UNQUOTE_RE.sub(lambda m: UNQUOTE_MAP[m[0]], s)
  70. def flatten(fields):
  71. """
  72. Return a list which is a single level of flattening of the original list.
  73. """
  74. flat = []
  75. for field in fields:
  76. if isinstance(field, (list, tuple)):
  77. flat.extend(field)
  78. else:
  79. flat.append(field)
  80. return flat
  81. def flatten_fieldsets(fieldsets):
  82. """Return a list of field names from an admin fieldsets structure."""
  83. field_names = []
  84. for name, opts in fieldsets:
  85. field_names.extend(flatten(opts["fields"]))
  86. return field_names
  87. def get_deleted_objects(objs, request, admin_site):
  88. """
  89. Find all objects related to ``objs`` that should also be deleted. ``objs``
  90. must be a homogeneous iterable of objects (e.g. a QuerySet).
  91. Return a nested list of strings suitable for display in the
  92. template with the ``unordered_list`` filter.
  93. """
  94. try:
  95. obj = objs[0]
  96. except IndexError:
  97. return [], {}, set(), []
  98. else:
  99. using = router.db_for_write(obj._meta.model)
  100. collector = NestedObjects(using=using, origin=objs)
  101. collector.collect(objs)
  102. perms_needed = set()
  103. def format_callback(obj):
  104. model = obj.__class__
  105. has_admin = model in admin_site._registry
  106. opts = obj._meta
  107. no_edit_link = "%s: %s" % (capfirst(opts.verbose_name), obj)
  108. if has_admin:
  109. if not admin_site._registry[model].has_delete_permission(request, obj):
  110. perms_needed.add(opts.verbose_name)
  111. try:
  112. admin_url = reverse(
  113. "%s:%s_%s_change"
  114. % (admin_site.name, opts.app_label, opts.model_name),
  115. None,
  116. (quote(obj.pk),),
  117. )
  118. except NoReverseMatch:
  119. # Change url doesn't exist -- don't display link to edit
  120. return no_edit_link
  121. # Display a link to the admin page.
  122. return format_html(
  123. '{}: <a href="{}">{}</a>', capfirst(opts.verbose_name), admin_url, obj
  124. )
  125. else:
  126. # Don't display link to edit, because it either has no
  127. # admin or is edited inline.
  128. return no_edit_link
  129. to_delete = collector.nested(format_callback)
  130. protected = [format_callback(obj) for obj in collector.protected]
  131. model_count = {
  132. model._meta.verbose_name_plural: len(objs)
  133. for model, objs in collector.model_objs.items()
  134. }
  135. return to_delete, model_count, perms_needed, protected
  136. class NestedObjects(Collector):
  137. def __init__(self, *args, **kwargs):
  138. super().__init__(*args, **kwargs)
  139. self.edges = {} # {from_instance: [to_instances]}
  140. self.protected = set()
  141. self.model_objs = defaultdict(set)
  142. def add_edge(self, source, target):
  143. self.edges.setdefault(source, []).append(target)
  144. def collect(self, objs, source=None, source_attr=None, **kwargs):
  145. for obj in objs:
  146. if source_attr and not source_attr.endswith("+"):
  147. related_name = source_attr % {
  148. "class": source._meta.model_name,
  149. "app_label": source._meta.app_label,
  150. }
  151. self.add_edge(getattr(obj, related_name), obj)
  152. else:
  153. self.add_edge(None, obj)
  154. self.model_objs[obj._meta.model].add(obj)
  155. try:
  156. return super().collect(objs, source_attr=source_attr, **kwargs)
  157. except models.ProtectedError as e:
  158. self.protected.update(e.protected_objects)
  159. except models.RestrictedError as e:
  160. self.protected.update(e.restricted_objects)
  161. def related_objects(self, related_model, related_fields, objs):
  162. qs = super().related_objects(related_model, related_fields, objs)
  163. return qs.select_related(
  164. *[related_field.name for related_field in related_fields]
  165. )
  166. def _nested(self, obj, seen, format_callback):
  167. if obj in seen:
  168. return []
  169. seen.add(obj)
  170. children = []
  171. for child in self.edges.get(obj, ()):
  172. children.extend(self._nested(child, seen, format_callback))
  173. if format_callback:
  174. ret = [format_callback(obj)]
  175. else:
  176. ret = [obj]
  177. if children:
  178. ret.append(children)
  179. return ret
  180. def nested(self, format_callback=None):
  181. """
  182. Return the graph as a nested list.
  183. """
  184. seen = set()
  185. roots = []
  186. for root in self.edges.get(None, ()):
  187. roots.extend(self._nested(root, seen, format_callback))
  188. return roots
  189. def can_fast_delete(self, *args, **kwargs):
  190. """
  191. We always want to load the objects into memory so that we can display
  192. them to the user in confirm page.
  193. """
  194. return False
  195. def model_format_dict(obj):
  196. """
  197. Return a `dict` with keys 'verbose_name' and 'verbose_name_plural',
  198. typically for use with string formatting.
  199. `obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.
  200. """
  201. if isinstance(obj, (models.Model, models.base.ModelBase)):
  202. opts = obj._meta
  203. elif isinstance(obj, models.query.QuerySet):
  204. opts = obj.model._meta
  205. else:
  206. opts = obj
  207. return {
  208. "verbose_name": opts.verbose_name,
  209. "verbose_name_plural": opts.verbose_name_plural,
  210. }
  211. def model_ngettext(obj, n=None):
  212. """
  213. Return the appropriate `verbose_name` or `verbose_name_plural` value for
  214. `obj` depending on the count `n`.
  215. `obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.
  216. If `obj` is a `QuerySet` instance, `n` is optional and the length of the
  217. `QuerySet` is used.
  218. """
  219. if isinstance(obj, models.query.QuerySet):
  220. if n is None:
  221. n = obj.count()
  222. obj = obj.model
  223. d = model_format_dict(obj)
  224. singular, plural = d["verbose_name"], d["verbose_name_plural"]
  225. return ngettext(singular, plural, n or 0)
  226. def lookup_field(name, obj, model_admin=None):
  227. opts = obj._meta
  228. try:
  229. f = _get_non_gfk_field(opts, name)
  230. except (FieldDoesNotExist, FieldIsAForeignKeyColumnName):
  231. # For non-field values, the value is either a method, property or
  232. # returned via a callable.
  233. if callable(name):
  234. attr = name
  235. value = attr(obj)
  236. elif hasattr(model_admin, name) and name != "__str__":
  237. attr = getattr(model_admin, name)
  238. value = attr(obj)
  239. else:
  240. attr = getattr(obj, name)
  241. if callable(attr):
  242. value = attr()
  243. else:
  244. value = attr
  245. f = None
  246. else:
  247. attr = None
  248. value = getattr(obj, name)
  249. return f, attr, value
  250. def _get_non_gfk_field(opts, name):
  251. """
  252. For historical reasons, the admin app relies on GenericForeignKeys as being
  253. "not found" by get_field(). This could likely be cleaned up.
  254. Reverse relations should also be excluded as these aren't attributes of the
  255. model (rather something like `foo_set`).
  256. """
  257. field = opts.get_field(name)
  258. if (
  259. field.is_relation
  260. and
  261. # Generic foreign keys OR reverse relations
  262. ((field.many_to_one and not field.related_model) or field.one_to_many)
  263. ):
  264. raise FieldDoesNotExist()
  265. # Avoid coercing <FK>_id fields to FK
  266. if (
  267. field.is_relation
  268. and not field.many_to_many
  269. and hasattr(field, "attname")
  270. and field.attname == name
  271. ):
  272. raise FieldIsAForeignKeyColumnName()
  273. return field
  274. def label_for_field(name, model, model_admin=None, return_attr=False, form=None):
  275. """
  276. Return a sensible label for a field name. The name can be a callable,
  277. property (but not created with @property decorator), or the name of an
  278. object's attribute, as well as a model field. If return_attr is True, also
  279. return the resolved attribute (which could be a callable). This will be
  280. None if (and only if) the name refers to a field.
  281. """
  282. attr = None
  283. try:
  284. field = _get_non_gfk_field(model._meta, name)
  285. try:
  286. label = field.verbose_name
  287. except AttributeError:
  288. # field is likely a ForeignObjectRel
  289. label = field.related_model._meta.verbose_name
  290. except FieldDoesNotExist:
  291. if name == "__str__":
  292. label = str(model._meta.verbose_name)
  293. attr = str
  294. else:
  295. if callable(name):
  296. attr = name
  297. elif hasattr(model_admin, name):
  298. attr = getattr(model_admin, name)
  299. elif hasattr(model, name):
  300. attr = getattr(model, name)
  301. elif form and name in form.fields:
  302. attr = form.fields[name]
  303. else:
  304. message = "Unable to lookup '%s' on %s" % (
  305. name,
  306. model._meta.object_name,
  307. )
  308. if model_admin:
  309. message += " or %s" % model_admin.__class__.__name__
  310. if form:
  311. message += " or %s" % form.__class__.__name__
  312. raise AttributeError(message)
  313. if hasattr(attr, "short_description"):
  314. label = attr.short_description
  315. elif (
  316. isinstance(attr, property)
  317. and hasattr(attr, "fget")
  318. and hasattr(attr.fget, "short_description")
  319. ):
  320. label = attr.fget.short_description
  321. elif callable(attr):
  322. if attr.__name__ == "<lambda>":
  323. label = "--"
  324. else:
  325. label = pretty_name(attr.__name__)
  326. else:
  327. label = pretty_name(name)
  328. except FieldIsAForeignKeyColumnName:
  329. label = pretty_name(name)
  330. attr = name
  331. if return_attr:
  332. return (label, attr)
  333. else:
  334. return label
  335. def help_text_for_field(name, model):
  336. help_text = ""
  337. try:
  338. field = _get_non_gfk_field(model._meta, name)
  339. except (FieldDoesNotExist, FieldIsAForeignKeyColumnName):
  340. pass
  341. else:
  342. if hasattr(field, "help_text"):
  343. help_text = field.help_text
  344. return help_text
  345. def display_for_field(value, field, empty_value_display):
  346. from django.contrib.admin.templatetags.admin_list import _boolean_icon
  347. if getattr(field, "flatchoices", None):
  348. try:
  349. return dict(field.flatchoices).get(value, empty_value_display)
  350. except TypeError:
  351. # Allow list-like choices.
  352. flatchoices = make_hashable(field.flatchoices)
  353. value = make_hashable(value)
  354. return dict(flatchoices).get(value, empty_value_display)
  355. # BooleanField needs special-case null-handling, so it comes before the
  356. # general null test.
  357. elif isinstance(field, models.BooleanField):
  358. return _boolean_icon(value)
  359. elif value is None:
  360. return empty_value_display
  361. elif isinstance(field, models.DateTimeField):
  362. return formats.localize(timezone.template_localtime(value))
  363. elif isinstance(field, (models.DateField, models.TimeField)):
  364. return formats.localize(value)
  365. elif isinstance(field, models.DecimalField):
  366. return formats.number_format(value, field.decimal_places)
  367. elif isinstance(field, (models.IntegerField, models.FloatField)):
  368. return formats.number_format(value)
  369. elif isinstance(field, models.FileField) and value:
  370. return format_html('<a href="{}">{}</a>', value.url, value)
  371. elif isinstance(field, models.JSONField) and value:
  372. try:
  373. return json.dumps(value, ensure_ascii=False, cls=field.encoder)
  374. except TypeError:
  375. return display_for_value(value, empty_value_display)
  376. else:
  377. return display_for_value(value, empty_value_display)
  378. def display_for_value(value, empty_value_display, boolean=False):
  379. from django.contrib.admin.templatetags.admin_list import _boolean_icon
  380. if boolean:
  381. return _boolean_icon(value)
  382. elif value is None:
  383. return empty_value_display
  384. elif isinstance(value, bool):
  385. return str(value)
  386. elif isinstance(value, datetime.datetime):
  387. return formats.localize(timezone.template_localtime(value))
  388. elif isinstance(value, (datetime.date, datetime.time)):
  389. return formats.localize(value)
  390. elif isinstance(value, (int, decimal.Decimal, float)):
  391. return formats.number_format(value)
  392. elif isinstance(value, (list, tuple)):
  393. return ", ".join(str(v) for v in value)
  394. else:
  395. return str(value)
  396. class NotRelationField(Exception):
  397. pass
  398. def get_model_from_relation(field):
  399. if hasattr(field, "path_infos"):
  400. return field.path_infos[-1].to_opts.model
  401. else:
  402. raise NotRelationField
  403. def reverse_field_path(model, path):
  404. """Create a reversed field path.
  405. E.g. Given (Order, "user__groups"),
  406. return (Group, "user__order").
  407. Final field must be a related model, not a data field.
  408. """
  409. reversed_path = []
  410. parent = model
  411. pieces = path.split(LOOKUP_SEP)
  412. for piece in pieces:
  413. field = parent._meta.get_field(piece)
  414. # skip trailing data field if extant:
  415. if len(reversed_path) == len(pieces) - 1: # final iteration
  416. try:
  417. get_model_from_relation(field)
  418. except NotRelationField:
  419. break
  420. # Field should point to another model
  421. if field.is_relation and not (field.auto_created and not field.concrete):
  422. related_name = field.related_query_name()
  423. parent = field.remote_field.model
  424. else:
  425. related_name = field.field.name
  426. parent = field.related_model
  427. reversed_path.insert(0, related_name)
  428. return (parent, LOOKUP_SEP.join(reversed_path))
  429. def get_fields_from_path(model, path):
  430. """Return list of Fields given path relative to model.
  431. e.g. (ModelX, "user__groups__name") -> [
  432. <django.db.models.fields.related.ForeignKey object at 0x...>,
  433. <django.db.models.fields.related.ManyToManyField object at 0x...>,
  434. <django.db.models.fields.CharField object at 0x...>,
  435. ]
  436. """
  437. pieces = path.split(LOOKUP_SEP)
  438. fields = []
  439. for piece in pieces:
  440. if fields:
  441. parent = get_model_from_relation(fields[-1])
  442. else:
  443. parent = model
  444. fields.append(parent._meta.get_field(piece))
  445. return fields
  446. def construct_change_message(form, formsets, add):
  447. """
  448. Construct a JSON structure describing changes from a changed object.
  449. Translations are deactivated so that strings are stored untranslated.
  450. Translation happens later on LogEntry access.
  451. """
  452. # Evaluating `form.changed_data` prior to disabling translations is required
  453. # to avoid fields affected by localization from being included incorrectly,
  454. # e.g. where date formats differ such as MM/DD/YYYY vs DD/MM/YYYY.
  455. changed_data = form.changed_data
  456. with translation_override(None):
  457. # Deactivate translations while fetching verbose_name for form
  458. # field labels and using `field_name`, if verbose_name is not provided.
  459. # Translations will happen later on LogEntry access.
  460. changed_field_labels = _get_changed_field_labels_from_form(form, changed_data)
  461. change_message = []
  462. if add:
  463. change_message.append({"added": {}})
  464. elif form.changed_data:
  465. change_message.append({"changed": {"fields": changed_field_labels}})
  466. if formsets:
  467. with translation_override(None):
  468. for formset in formsets:
  469. for added_object in formset.new_objects:
  470. change_message.append(
  471. {
  472. "added": {
  473. "name": str(added_object._meta.verbose_name),
  474. "object": str(added_object),
  475. }
  476. }
  477. )
  478. for changed_object, changed_fields in formset.changed_objects:
  479. change_message.append(
  480. {
  481. "changed": {
  482. "name": str(changed_object._meta.verbose_name),
  483. "object": str(changed_object),
  484. "fields": _get_changed_field_labels_from_form(
  485. formset.forms[0], changed_fields
  486. ),
  487. }
  488. }
  489. )
  490. for deleted_object in formset.deleted_objects:
  491. change_message.append(
  492. {
  493. "deleted": {
  494. "name": str(deleted_object._meta.verbose_name),
  495. "object": str(deleted_object),
  496. }
  497. }
  498. )
  499. return change_message
  500. def _get_changed_field_labels_from_form(form, changed_data):
  501. changed_field_labels = []
  502. for field_name in changed_data:
  503. try:
  504. verbose_field_name = form.fields[field_name].label or field_name
  505. except KeyError:
  506. verbose_field_name = field_name
  507. changed_field_labels.append(str(verbose_field_name))
  508. return changed_field_labels