Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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 20KB

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