2012-04-29 17:51:12 +08:00
|
|
|
import datetime
|
|
|
|
import decimal
|
2014-04-05 23:58:05 +08:00
|
|
|
from collections import defaultdict
|
2012-04-29 17:51:12 +08:00
|
|
|
|
2013-07-01 21:19:55 +08:00
|
|
|
from django.contrib.auth import get_permission_codename
|
2015-01-02 23:14:23 +08:00
|
|
|
from django.core.exceptions import FieldDoesNotExist
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.db import models
|
2012-09-09 07:51:36 +08:00
|
|
|
from django.db.models.constants import LOOKUP_SEP
|
2010-11-10 00:46:42 +08:00
|
|
|
from django.db.models.deletion import Collector
|
2015-04-14 17:09:58 +08:00
|
|
|
from django.db.models.sql.constants import QUERY_TERMS
|
2015-09-09 21:09:59 +08:00
|
|
|
from django.forms.utils import pretty_name
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import NoReverseMatch, reverse
|
2016-12-29 23:27:49 +08:00
|
|
|
from django.utils import formats, timezone
|
2015-03-04 23:26:04 +08:00
|
|
|
from django.utils.html import format_html
|
2014-04-05 23:58:05 +08:00
|
|
|
from django.utils.text import capfirst
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import ngettext, override as translation_override
|
2009-12-23 02:29:00 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2016-04-24 01:35:18 +08:00
|
|
|
class FieldIsAForeignKeyColumnName(Exception):
|
|
|
|
"""A field is a foreign key attname, i.e. <FK>_id."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-11-22 20:26:17 +08:00
|
|
|
def lookup_needs_distinct(opts, lookup_path):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return True if 'distinct()' should be used to query the given lookup path.
|
2011-11-22 20:26:17 +08:00
|
|
|
"""
|
2016-10-06 18:02:48 +08:00
|
|
|
lookup_fields = lookup_path.split(LOOKUP_SEP)
|
2015-04-14 17:09:58 +08:00
|
|
|
# Remove the last item of the lookup path if it is a query term
|
|
|
|
if lookup_fields[-1] in QUERY_TERMS:
|
|
|
|
lookup_fields = lookup_fields[:-1]
|
|
|
|
# Now go through the fields (following all relations) and look for an m2m
|
|
|
|
for field_name in lookup_fields:
|
2017-03-16 01:45:18 +08:00
|
|
|
if field_name == 'pk':
|
|
|
|
field_name = opts.pk.name
|
2015-04-14 17:09:58 +08:00
|
|
|
field = opts.get_field(field_name)
|
|
|
|
if hasattr(field, 'get_path_info'):
|
|
|
|
# This field is a relation, update opts to follow the relation
|
|
|
|
path_info = field.get_path_info()
|
|
|
|
opts = path_info[-1].to_opts
|
|
|
|
if any(path.m2m for path in path_info):
|
|
|
|
# This field is a m2m relation so we know we need to call distinct
|
|
|
|
return True
|
2011-11-22 20:26:17 +08:00
|
|
|
return False
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2011-11-22 20:26:17 +08:00
|
|
|
def prepare_lookup_value(key, value):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return a lookup value prepared to be used in queryset filtering.
|
2011-11-22 20:26:17 +08:00
|
|
|
"""
|
|
|
|
# if key ends with __in, split parameter into separate values
|
|
|
|
if key.endswith('__in'):
|
|
|
|
value = value.split(',')
|
2013-05-28 00:31:49 +08:00
|
|
|
# if key ends with __isnull, special case '' and the string literals 'false' and '0'
|
2011-11-22 20:26:17 +08:00
|
|
|
if key.endswith('__isnull'):
|
2013-05-28 00:31:49 +08:00
|
|
|
if value.lower() in ('', 'false', '0'):
|
2011-11-22 20:26:17 +08:00
|
|
|
value = False
|
|
|
|
else:
|
|
|
|
value = True
|
|
|
|
return value
|
2010-11-10 00:46:42 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def quote(s):
|
|
|
|
"""
|
|
|
|
Ensure that primary key values do not confuse the admin URLs by escaping
|
2012-09-25 09:02:59 +08:00
|
|
|
any '/', '_' and ':' and similarly problematic characters.
|
|
|
|
Similar to urllib.quote, except that the quoting is slightly different so
|
|
|
|
that it doesn't get automatically unquoted by the Web browser.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2016-12-29 23:27:49 +08:00
|
|
|
if not isinstance(s, str):
|
2008-07-19 07:54:34 +08:00
|
|
|
return s
|
|
|
|
res = list(s)
|
|
|
|
for i in range(len(res)):
|
|
|
|
c = res[i]
|
2015-06-19 03:46:15 +08:00
|
|
|
if c in """:/_#?;@&=+$,"[]<>%\n\\""":
|
2008-07-19 07:54:34 +08:00
|
|
|
res[i] = '_%02X' % ord(c)
|
|
|
|
return ''.join(res)
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def unquote(s):
|
|
|
|
"""
|
|
|
|
Undo the effects of quote(). Based heavily on urllib.unquote().
|
|
|
|
"""
|
|
|
|
mychr = chr
|
|
|
|
myatoi = int
|
|
|
|
list = s.split('_')
|
|
|
|
res = [list[0]]
|
|
|
|
myappend = res.append
|
|
|
|
del list[0]
|
|
|
|
for item in list:
|
|
|
|
if item[1:2]:
|
|
|
|
try:
|
|
|
|
myappend(mychr(myatoi(item[:2], 16)) + item[2:])
|
|
|
|
except ValueError:
|
|
|
|
myappend('_' + item)
|
|
|
|
else:
|
|
|
|
myappend('_' + item)
|
|
|
|
return "".join(res)
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
|
2014-02-15 18:28:09 +08:00
|
|
|
def flatten(fields):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""
|
|
|
|
Return a list which is a single level of flattening of the original list.
|
|
|
|
"""
|
2014-02-15 18:28:09 +08:00
|
|
|
flat = []
|
|
|
|
for field in fields:
|
|
|
|
if isinstance(field, (list, tuple)):
|
|
|
|
flat.extend(field)
|
|
|
|
else:
|
|
|
|
flat.append(field)
|
|
|
|
return flat
|
|
|
|
|
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
def flatten_fieldsets(fieldsets):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Return a list of field names from an admin fieldsets structure."""
|
2008-08-18 23:49:58 +08:00
|
|
|
field_names = []
|
|
|
|
for name, opts in fieldsets:
|
2014-02-15 18:28:09 +08:00
|
|
|
field_names.extend(
|
|
|
|
flatten(opts['fields'])
|
|
|
|
)
|
2008-08-18 23:49:58 +08:00
|
|
|
return field_names
|
|
|
|
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def get_deleted_objects(objs, opts, user, admin_site, using):
|
2009-03-24 04:22:56 +08:00
|
|
|
"""
|
2010-11-10 00:46:42 +08:00
|
|
|
Find all objects related to ``objs`` that should also be deleted. ``objs``
|
2014-03-02 22:25:53 +08:00
|
|
|
must be a homogeneous iterable of objects (e.g. a QuerySet).
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
Return a nested list of strings suitable for display in the
|
2010-02-26 21:17:43 +08:00
|
|
|
template with the ``unordered_list`` filter.
|
2009-03-24 04:22:56 +08:00
|
|
|
"""
|
2010-11-10 00:46:42 +08:00
|
|
|
collector = NestedObjects(using=using)
|
|
|
|
collector.collect(objs)
|
2010-02-26 21:17:43 +08:00
|
|
|
perms_needed = set()
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def format_callback(obj):
|
|
|
|
has_admin = obj.__class__ in admin_site._registry
|
|
|
|
opts = obj._meta
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2017-03-04 22:47:49 +08:00
|
|
|
no_edit_link = '%s: %s' % (capfirst(opts.verbose_name), obj)
|
2013-08-18 06:05:13 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
if has_admin:
|
2013-08-18 06:05:13 +08:00
|
|
|
try:
|
|
|
|
admin_url = reverse('%s:%s_%s_change'
|
|
|
|
% (admin_site.name,
|
|
|
|
opts.app_label,
|
|
|
|
opts.model_name),
|
|
|
|
None, (quote(obj._get_pk_val()),))
|
|
|
|
except NoReverseMatch:
|
|
|
|
# Change url doesn't exist -- don't display link to edit
|
|
|
|
return no_edit_link
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
p = '%s.%s' % (opts.app_label,
|
2013-07-01 21:19:55 +08:00
|
|
|
get_permission_codename('delete', opts))
|
2010-11-10 00:46:42 +08:00
|
|
|
if not user.has_perm(p):
|
|
|
|
perms_needed.add(opts.verbose_name)
|
|
|
|
# Display a link to the admin page.
|
2014-11-27 08:41:27 +08:00
|
|
|
return format_html('{}: <a href="{}">{}</a>',
|
2012-07-03 07:31:14 +08:00
|
|
|
capfirst(opts.verbose_name),
|
|
|
|
admin_url,
|
|
|
|
obj)
|
2010-11-10 00:46:42 +08:00
|
|
|
else:
|
|
|
|
# Don't display link to edit, because it either has no
|
|
|
|
# admin or is edited inline.
|
2013-08-18 06:05:13 +08:00
|
|
|
return no_edit_link
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
to_delete = collector.nested(format_callback)
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2011-01-20 08:33:32 +08:00
|
|
|
protected = [format_callback(obj) for obj in collector.protected]
|
2015-12-10 15:06:01 +08:00
|
|
|
model_count = {model._meta.verbose_name_plural: len(objs) for model, objs in collector.model_objs.items()}
|
2011-01-20 08:33:32 +08:00
|
|
|
|
2015-12-10 15:06:01 +08:00
|
|
|
return to_delete, model_count, perms_needed, protected
|
2010-02-26 21:17:43 +08:00
|
|
|
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
class NestedObjects(Collector):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.edges = {} # {from_instance: [to_instances]}
|
2011-01-20 08:33:32 +08:00
|
|
|
self.protected = set()
|
2015-12-10 15:06:01 +08:00
|
|
|
self.model_objs = defaultdict(set)
|
2010-03-15 21:15:01 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def add_edge(self, source, target):
|
|
|
|
self.edges.setdefault(source, []).append(target)
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2014-01-22 01:25:33 +08:00
|
|
|
def collect(self, objs, source=None, source_attr=None, **kwargs):
|
2010-11-10 00:46:42 +08:00
|
|
|
for obj in objs:
|
2014-03-04 19:23:32 +08:00
|
|
|
if source_attr and not source_attr.endswith('+'):
|
2014-01-22 01:25:33 +08:00
|
|
|
related_name = source_attr % {
|
|
|
|
'class': source._meta.model_name,
|
|
|
|
'app_label': source._meta.app_label,
|
|
|
|
}
|
|
|
|
self.add_edge(getattr(obj, related_name), obj)
|
2010-11-10 00:46:42 +08:00
|
|
|
else:
|
|
|
|
self.add_edge(None, obj)
|
2015-12-10 15:06:01 +08:00
|
|
|
self.model_objs[obj._meta.model].add(obj)
|
2011-01-20 08:33:32 +08:00
|
|
|
try:
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().collect(objs, source_attr=source_attr, **kwargs)
|
2012-04-29 00:09:37 +08:00
|
|
|
except models.ProtectedError as e:
|
2011-01-20 08:33:32 +08:00
|
|
|
self.protected.update(e.protected_objects)
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def related_objects(self, related, objs):
|
2017-01-21 21:13:44 +08:00
|
|
|
qs = super().related_objects(related, objs)
|
2010-11-10 00:46:42 +08:00
|
|
|
return qs.select_related(related.field.name)
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def _nested(self, obj, seen, format_callback):
|
|
|
|
if obj in seen:
|
|
|
|
return []
|
|
|
|
seen.add(obj)
|
|
|
|
children = []
|
|
|
|
for child in self.edges.get(obj, ()):
|
|
|
|
children.extend(self._nested(child, seen, format_callback))
|
2010-02-26 21:17:43 +08:00
|
|
|
if format_callback:
|
2010-11-10 00:46:42 +08:00
|
|
|
ret = [format_callback(obj)]
|
2008-07-19 07:54:34 +08:00
|
|
|
else:
|
2010-02-26 21:17:43 +08:00
|
|
|
ret = [obj]
|
|
|
|
if children:
|
|
|
|
ret.append(children)
|
|
|
|
return ret
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def nested(self, format_callback=None):
|
2010-02-26 21:17:43 +08:00
|
|
|
"""
|
|
|
|
Return the graph as a nested list.
|
|
|
|
"""
|
2010-11-10 00:46:42 +08:00
|
|
|
seen = set()
|
2010-02-26 21:17:43 +08:00
|
|
|
roots = []
|
2010-11-10 00:46:42 +08:00
|
|
|
for root in self.edges.get(None, ()):
|
|
|
|
roots.extend(self._nested(root, seen, format_callback))
|
2010-02-26 21:17:43 +08:00
|
|
|
return roots
|
|
|
|
|
2012-09-20 23:51:30 +08:00
|
|
|
def can_fast_delete(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
We always want to load the objects into memory so that we can display
|
|
|
|
them to the user in confirm page.
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
2009-03-24 04:22:56 +08:00
|
|
|
|
|
|
|
def model_format_dict(obj):
|
|
|
|
"""
|
|
|
|
Return a `dict` with keys 'verbose_name' and 'verbose_name_plural',
|
|
|
|
typically for use with string formatting.
|
|
|
|
|
|
|
|
`obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.
|
|
|
|
"""
|
|
|
|
if isinstance(obj, (models.Model, models.base.ModelBase)):
|
|
|
|
opts = obj._meta
|
|
|
|
elif isinstance(obj, models.query.QuerySet):
|
|
|
|
opts = obj.model._meta
|
|
|
|
else:
|
|
|
|
opts = obj
|
|
|
|
return {
|
2017-03-04 22:47:49 +08:00
|
|
|
'verbose_name': opts.verbose_name,
|
|
|
|
'verbose_name_plural': opts.verbose_name_plural,
|
2009-03-24 04:22:56 +08:00
|
|
|
}
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
|
2009-03-24 04:22:56 +08:00
|
|
|
def model_ngettext(obj, n=None):
|
|
|
|
"""
|
2009-03-25 09:31:57 +08:00
|
|
|
Return the appropriate `verbose_name` or `verbose_name_plural` value for
|
|
|
|
`obj` depending on the count `n`.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
|
|
|
`obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.
|
|
|
|
If `obj` is a `QuerySet` instance, `n` is optional and the length of the
|
|
|
|
`QuerySet` is used.
|
|
|
|
"""
|
|
|
|
if isinstance(obj, models.query.QuerySet):
|
|
|
|
if n is None:
|
|
|
|
n = obj.count()
|
|
|
|
obj = obj.model
|
|
|
|
d = model_format_dict(obj)
|
2009-03-25 09:31:57 +08:00
|
|
|
singular, plural = d["verbose_name"], d["verbose_name_plural"]
|
2017-01-27 03:58:33 +08:00
|
|
|
return ngettext(singular, plural, n or 0)
|
2009-12-23 02:29:00 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
|
2009-12-23 02:29:00 +08:00
|
|
|
def lookup_field(name, obj, model_admin=None):
|
|
|
|
opts = obj._meta
|
|
|
|
try:
|
2015-01-07 08:16:35 +08:00
|
|
|
f = _get_non_gfk_field(opts, name)
|
2016-04-24 01:35:18 +08:00
|
|
|
except (FieldDoesNotExist, FieldIsAForeignKeyColumnName):
|
2009-12-23 02:29:00 +08:00
|
|
|
# For non-field values, the value is either a method, property or
|
|
|
|
# returned via a callable.
|
|
|
|
if callable(name):
|
|
|
|
attr = name
|
|
|
|
value = attr(obj)
|
2017-01-21 05:04:05 +08:00
|
|
|
elif model_admin is not None and hasattr(model_admin, name) and name != '__str__':
|
2009-12-23 02:29:00 +08:00
|
|
|
attr = getattr(model_admin, name)
|
|
|
|
value = attr(obj)
|
|
|
|
else:
|
|
|
|
attr = getattr(obj, name)
|
|
|
|
if callable(attr):
|
|
|
|
value = attr()
|
|
|
|
else:
|
|
|
|
value = attr
|
|
|
|
f = None
|
|
|
|
else:
|
|
|
|
attr = None
|
2010-02-01 22:12:56 +08:00
|
|
|
value = getattr(obj, name)
|
2009-12-23 02:29:00 +08:00
|
|
|
return f, attr, value
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
def _get_non_gfk_field(opts, name):
|
|
|
|
"""
|
|
|
|
For historical reasons, the admin app relies on GenericForeignKeys as being
|
|
|
|
"not found" by get_field(). This could likely be cleaned up.
|
2015-08-24 23:52:58 +08:00
|
|
|
|
|
|
|
Reverse relations should also be excluded as these aren't attributes of the
|
|
|
|
model (rather something like `foo_set`).
|
2015-01-07 08:16:35 +08:00
|
|
|
"""
|
|
|
|
field = opts.get_field(name)
|
2015-08-24 23:52:58 +08:00
|
|
|
if (field.is_relation and
|
|
|
|
# Generic foreign keys OR reverse relations
|
|
|
|
((field.many_to_one and not field.related_model) or field.one_to_many)):
|
2015-01-07 08:16:35 +08:00
|
|
|
raise FieldDoesNotExist()
|
2016-04-24 01:35:18 +08:00
|
|
|
|
|
|
|
# Avoid coercing <FK>_id fields to FK
|
2016-09-11 23:19:56 +08:00
|
|
|
if field.is_relation and not field.many_to_many and hasattr(field, 'attname') and field.attname == name:
|
2016-04-24 01:35:18 +08:00
|
|
|
raise FieldIsAForeignKeyColumnName()
|
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
return field
|
|
|
|
|
|
|
|
|
2010-01-10 11:36:59 +08:00
|
|
|
def label_for_field(name, model, model_admin=None, return_attr=False):
|
2011-06-02 07:17:40 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return a sensible label for a field name. The name can be a callable,
|
|
|
|
property (but not created with @property decorator), or the name of an
|
|
|
|
object's attribute, as well as a model field. If return_attr is True, also
|
|
|
|
return the resolved attribute (which could be a callable). This will be
|
|
|
|
None if (and only if) the name refers to a field.
|
2011-06-02 07:17:40 +08:00
|
|
|
"""
|
2010-01-10 11:36:59 +08:00
|
|
|
attr = None
|
2009-12-23 02:29:00 +08:00
|
|
|
try:
|
2015-01-07 08:16:35 +08:00
|
|
|
field = _get_non_gfk_field(model._meta, name)
|
2013-11-21 20:18:30 +08:00
|
|
|
try:
|
2010-10-17 23:17:52 +08:00
|
|
|
label = field.verbose_name
|
2013-11-21 20:18:30 +08:00
|
|
|
except AttributeError:
|
2013-11-09 20:25:15 +08:00
|
|
|
# field is likely a ForeignObjectRel
|
2015-02-23 15:31:58 +08:00
|
|
|
label = field.related_model._meta.verbose_name
|
2015-01-02 23:14:23 +08:00
|
|
|
except FieldDoesNotExist:
|
2017-01-12 06:17:25 +08:00
|
|
|
if name == "__str__":
|
2017-04-22 01:52:26 +08:00
|
|
|
label = str(model._meta.verbose_name)
|
2016-12-29 23:27:49 +08:00
|
|
|
attr = str
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
2010-01-10 11:36:59 +08:00
|
|
|
if callable(name):
|
|
|
|
attr = name
|
|
|
|
elif model_admin is not None and hasattr(model_admin, name):
|
|
|
|
attr = getattr(model_admin, name)
|
|
|
|
elif hasattr(model, name):
|
|
|
|
attr = getattr(model, name)
|
|
|
|
else:
|
|
|
|
message = "Unable to lookup '%s' on %s" % (name, model._meta.object_name)
|
|
|
|
if model_admin:
|
2011-02-26 09:44:41 +08:00
|
|
|
message += " or %s" % (model_admin.__class__.__name__,)
|
2010-01-10 11:36:59 +08:00
|
|
|
raise AttributeError(message)
|
2009-12-23 02:29:00 +08:00
|
|
|
|
2010-01-10 11:36:59 +08:00
|
|
|
if hasattr(attr, "short_description"):
|
|
|
|
label = attr.short_description
|
2013-05-21 19:03:45 +08:00
|
|
|
elif (isinstance(attr, property) and
|
|
|
|
hasattr(attr, "fget") and
|
|
|
|
hasattr(attr.fget, "short_description")):
|
|
|
|
label = attr.fget.short_description
|
2010-01-10 11:36:59 +08:00
|
|
|
elif callable(attr):
|
|
|
|
if attr.__name__ == "<lambda>":
|
|
|
|
label = "--"
|
|
|
|
else:
|
2010-04-11 16:35:04 +08:00
|
|
|
label = pretty_name(attr.__name__)
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
2010-04-11 16:35:04 +08:00
|
|
|
label = pretty_name(name)
|
2016-04-24 01:35:18 +08:00
|
|
|
except FieldIsAForeignKeyColumnName:
|
|
|
|
label = pretty_name(name)
|
|
|
|
attr = name
|
|
|
|
|
2010-01-10 11:36:59 +08:00
|
|
|
if return_attr:
|
|
|
|
return (label, attr)
|
|
|
|
else:
|
|
|
|
return label
|
2009-12-23 02:29:00 +08:00
|
|
|
|
2013-05-21 19:03:45 +08:00
|
|
|
|
2011-02-19 20:55:09 +08:00
|
|
|
def help_text_for_field(name, model):
|
2013-08-29 21:39:31 +08:00
|
|
|
help_text = ""
|
2011-02-19 20:55:09 +08:00
|
|
|
try:
|
2015-01-07 08:16:35 +08:00
|
|
|
field = _get_non_gfk_field(model._meta, name)
|
2016-04-24 01:35:18 +08:00
|
|
|
except (FieldDoesNotExist, FieldIsAForeignKeyColumnName):
|
2013-08-29 21:39:31 +08:00
|
|
|
pass
|
|
|
|
else:
|
2013-11-21 20:18:30 +08:00
|
|
|
if hasattr(field, 'help_text'):
|
2013-08-29 21:39:31 +08:00
|
|
|
help_text = field.help_text
|
2017-03-04 22:47:49 +08:00
|
|
|
return help_text
|
2011-02-19 20:55:09 +08:00
|
|
|
|
2009-12-23 02:29:00 +08:00
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
def display_for_field(value, field, empty_value_display):
|
2009-12-23 02:29:00 +08:00
|
|
|
from django.contrib.admin.templatetags.admin_list import _boolean_icon
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2016-01-10 04:15:21 +08:00
|
|
|
if getattr(field, 'flatchoices', None):
|
2015-03-13 18:08:03 +08:00
|
|
|
return dict(field.flatchoices).get(value, empty_value_display)
|
2010-03-10 16:37:17 +08:00
|
|
|
# NullBooleanField needs special-case null-handling, so it comes
|
|
|
|
# before the general null test.
|
|
|
|
elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
|
|
|
|
return _boolean_icon(value)
|
2010-01-10 05:28:54 +08:00
|
|
|
elif value is None:
|
2015-03-13 18:08:03 +08:00
|
|
|
return empty_value_display
|
2011-11-18 21:01:06 +08:00
|
|
|
elif isinstance(field, models.DateTimeField):
|
2012-04-29 21:37:23 +08:00
|
|
|
return formats.localize(timezone.template_localtime(value))
|
2012-04-29 17:51:12 +08:00
|
|
|
elif isinstance(field, (models.DateField, models.TimeField)):
|
2009-12-23 02:29:00 +08:00
|
|
|
return formats.localize(value)
|
|
|
|
elif isinstance(field, models.DecimalField):
|
|
|
|
return formats.number_format(value, field.decimal_places)
|
2015-04-15 05:09:27 +08:00
|
|
|
elif isinstance(field, (models.IntegerField, models.FloatField)):
|
2009-12-23 02:29:00 +08:00
|
|
|
return formats.number_format(value)
|
2015-02-12 00:40:03 +08:00
|
|
|
elif isinstance(field, models.FileField) and value:
|
2015-03-04 23:26:04 +08:00
|
|
|
return format_html('<a href="{}">{}</a>', value.url, value)
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
2016-05-08 03:49:41 +08:00
|
|
|
return display_for_value(value, empty_value_display)
|
2010-11-22 03:29:15 +08:00
|
|
|
|
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
def display_for_value(value, empty_value_display, boolean=False):
|
2012-04-29 17:51:12 +08:00
|
|
|
from django.contrib.admin.templatetags.admin_list import _boolean_icon
|
|
|
|
|
|
|
|
if boolean:
|
|
|
|
return _boolean_icon(value)
|
|
|
|
elif value is None:
|
2015-03-13 18:08:03 +08:00
|
|
|
return empty_value_display
|
2012-04-29 17:51:12 +08:00
|
|
|
elif isinstance(value, datetime.datetime):
|
2012-04-29 21:37:23 +08:00
|
|
|
return formats.localize(timezone.template_localtime(value))
|
2012-04-29 17:51:12 +08:00
|
|
|
elif isinstance(value, (datetime.date, datetime.time)):
|
|
|
|
return formats.localize(value)
|
2016-12-29 23:27:49 +08:00
|
|
|
elif isinstance(value, (int, decimal.Decimal, float)):
|
2012-04-29 17:51:12 +08:00
|
|
|
return formats.number_format(value)
|
2016-05-08 03:49:41 +08:00
|
|
|
elif isinstance(value, (list, tuple)):
|
2017-04-22 01:52:26 +08:00
|
|
|
return ', '.join(str(v) for v in value)
|
2012-04-29 17:51:12 +08:00
|
|
|
else:
|
2017-04-22 01:52:26 +08:00
|
|
|
return str(value)
|
2012-04-29 17:51:12 +08:00
|
|
|
|
|
|
|
|
2010-11-22 03:29:15 +08:00
|
|
|
class NotRelationField(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def get_model_from_relation(field):
|
2013-11-21 20:11:57 +08:00
|
|
|
if hasattr(field, 'get_path_info'):
|
|
|
|
return field.get_path_info()[-1].to_opts.model
|
2010-11-22 03:29:15 +08:00
|
|
|
else:
|
|
|
|
raise NotRelationField
|
|
|
|
|
|
|
|
|
|
|
|
def reverse_field_path(model, path):
|
|
|
|
""" Create a reversed field path.
|
|
|
|
|
|
|
|
E.g. Given (Order, "user__groups"),
|
|
|
|
return (Group, "user__order").
|
|
|
|
|
|
|
|
Final field must be a related model, not a data field.
|
|
|
|
"""
|
|
|
|
reversed_path = []
|
|
|
|
parent = model
|
|
|
|
pieces = path.split(LOOKUP_SEP)
|
|
|
|
for piece in pieces:
|
2015-01-07 08:16:35 +08:00
|
|
|
field = parent._meta.get_field(piece)
|
2010-11-22 03:29:15 +08:00
|
|
|
# skip trailing data field if extant:
|
2013-11-04 02:08:55 +08:00
|
|
|
if len(reversed_path) == len(pieces) - 1: # final iteration
|
2010-11-22 03:29:15 +08:00
|
|
|
try:
|
|
|
|
get_model_from_relation(field)
|
|
|
|
except NotRelationField:
|
|
|
|
break
|
2015-01-07 08:16:35 +08:00
|
|
|
|
|
|
|
# Field should point to another model
|
|
|
|
if field.is_relation and not (field.auto_created and not field.concrete):
|
2010-11-22 03:29:15 +08:00
|
|
|
related_name = field.related_query_name()
|
2015-02-26 22:19:17 +08:00
|
|
|
parent = field.remote_field.model
|
2010-11-22 03:29:15 +08:00
|
|
|
else:
|
|
|
|
related_name = field.field.name
|
2015-01-07 08:16:35 +08:00
|
|
|
parent = field.related_model
|
2010-11-22 03:29:15 +08:00
|
|
|
reversed_path.insert(0, related_name)
|
|
|
|
return (parent, LOOKUP_SEP.join(reversed_path))
|
|
|
|
|
|
|
|
|
|
|
|
def get_fields_from_path(model, path):
|
|
|
|
""" Return list of Fields given path relative to model.
|
|
|
|
|
|
|
|
e.g. (ModelX, "user__groups__name") -> [
|
|
|
|
<django.db.models.fields.related.ForeignKey object at 0x...>,
|
|
|
|
<django.db.models.fields.related.ManyToManyField object at 0x...>,
|
|
|
|
<django.db.models.fields.CharField object at 0x...>,
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
pieces = path.split(LOOKUP_SEP)
|
|
|
|
fields = []
|
|
|
|
for piece in pieces:
|
|
|
|
if fields:
|
|
|
|
parent = get_model_from_relation(fields[-1])
|
|
|
|
else:
|
|
|
|
parent = model
|
2015-01-07 08:16:35 +08:00
|
|
|
fields.append(parent._meta.get_field(piece))
|
2010-11-22 03:29:15 +08:00
|
|
|
return fields
|
|
|
|
|
|
|
|
|
2016-10-05 20:02:33 +08:00
|
|
|
def construct_change_message(form, formsets, add):
|
|
|
|
"""
|
|
|
|
Construct a JSON structure describing changes from a changed object.
|
|
|
|
Translations are deactivated so that strings are stored untranslated.
|
|
|
|
Translation happens later on LogEntry access.
|
|
|
|
"""
|
|
|
|
change_message = []
|
|
|
|
if add:
|
|
|
|
change_message.append({'added': {}})
|
|
|
|
elif form.changed_data:
|
|
|
|
change_message.append({'changed': {'fields': form.changed_data}})
|
|
|
|
|
|
|
|
if formsets:
|
|
|
|
with translation_override(None):
|
|
|
|
for formset in formsets:
|
|
|
|
for added_object in formset.new_objects:
|
|
|
|
change_message.append({
|
|
|
|
'added': {
|
2017-04-22 01:52:26 +08:00
|
|
|
'name': str(added_object._meta.verbose_name),
|
|
|
|
'object': str(added_object),
|
2016-10-05 20:02:33 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
for changed_object, changed_fields in formset.changed_objects:
|
|
|
|
change_message.append({
|
|
|
|
'changed': {
|
2017-04-22 01:52:26 +08:00
|
|
|
'name': str(changed_object._meta.verbose_name),
|
|
|
|
'object': str(changed_object),
|
2016-10-05 20:02:33 +08:00
|
|
|
'fields': changed_fields,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for deleted_object in formset.deleted_objects:
|
|
|
|
change_message.append({
|
|
|
|
'deleted': {
|
2017-04-22 01:52:26 +08:00
|
|
|
'name': str(deleted_object._meta.verbose_name),
|
|
|
|
'object': str(deleted_object),
|
2016-10-05 20:02:33 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return change_message
|