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.

utils.py 18KB

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