2014-09-09 20:46:28 +04:00
|
|
|
"""
|
|
|
|
Helper functions for creating user-friendly representations
|
|
|
|
of serializer classes and serializer fields.
|
|
|
|
"""
|
2015-06-18 16:38:29 +03:00
|
|
|
import re
|
|
|
|
|
2014-09-11 23:37:27 +04:00
|
|
|
from django.db import models
|
2019-07-30 03:02:43 +03:00
|
|
|
from django.utils.encoding import force_str
|
2015-06-25 23:55:51 +03:00
|
|
|
from django.utils.functional import Promise
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2014-09-09 20:46:28 +04:00
|
|
|
|
2014-09-11 23:37:27 +04:00
|
|
|
def manager_repr(value):
|
|
|
|
model = value.model
|
|
|
|
opts = model._meta
|
2017-10-05 21:41:38 +03:00
|
|
|
names_and_managers = [
|
|
|
|
(manager.name, manager)
|
|
|
|
for manager
|
|
|
|
in opts.managers
|
|
|
|
]
|
|
|
|
for manager_name, manager_instance in names_and_managers:
|
2016-06-01 17:31:00 +03:00
|
|
|
if manager_instance == value:
|
|
|
|
return '%s.%s.all()' % (model._meta.object_name, manager_name)
|
2014-09-11 23:37:27 +04:00
|
|
|
return repr(value)
|
|
|
|
|
|
|
|
|
2014-09-09 20:46:28 +04:00
|
|
|
def smart_repr(value):
|
2014-09-11 23:37:27 +04:00
|
|
|
if isinstance(value, models.Manager):
|
|
|
|
return manager_repr(value)
|
|
|
|
|
2024-03-08 16:14:27 +03:00
|
|
|
if isinstance(value, Promise):
|
|
|
|
value = force_str(value, strings_only=True)
|
2014-10-01 16:24:47 +04:00
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
value = repr(value)
|
2014-09-09 20:46:28 +04:00
|
|
|
|
|
|
|
# Representations like u'help text'
|
|
|
|
# should simply be presented as 'help text'
|
|
|
|
if value.startswith("u'") and value.endswith("'"):
|
|
|
|
return value[1:]
|
|
|
|
|
|
|
|
# Representations like
|
|
|
|
# <django.core.validators.RegexValidator object at 0x1047af050>
|
|
|
|
# Should be presented as
|
|
|
|
# <django.core.validators.RegexValidator object>
|
2019-05-01 08:51:54 +03:00
|
|
|
return re.sub(' at 0x[0-9A-Fa-f]{4,32}>', '>', value)
|
2014-09-09 20:46:28 +04:00
|
|
|
|
|
|
|
|
|
|
|
def field_repr(field, force_many=False):
|
|
|
|
kwargs = field._kwargs
|
|
|
|
if force_many:
|
|
|
|
kwargs = kwargs.copy()
|
|
|
|
kwargs['many'] = True
|
|
|
|
kwargs.pop('child', None)
|
|
|
|
|
|
|
|
arg_string = ', '.join([smart_repr(val) for val in field._args])
|
|
|
|
kwarg_string = ', '.join([
|
|
|
|
'%s=%s' % (key, smart_repr(val))
|
|
|
|
for key, val in sorted(kwargs.items())
|
|
|
|
])
|
|
|
|
if arg_string and kwarg_string:
|
|
|
|
arg_string += ', '
|
|
|
|
|
|
|
|
if force_many:
|
|
|
|
class_name = force_many.__class__.__name__
|
|
|
|
else:
|
|
|
|
class_name = field.__class__.__name__
|
|
|
|
|
|
|
|
return "%s(%s%s)" % (class_name, arg_string, kwarg_string)
|
|
|
|
|
|
|
|
|
|
|
|
def serializer_repr(serializer, indent, force_many=None):
|
|
|
|
ret = field_repr(serializer, force_many) + ':'
|
|
|
|
indent_str = ' ' * indent
|
|
|
|
|
|
|
|
if force_many:
|
|
|
|
fields = force_many.fields
|
|
|
|
else:
|
|
|
|
fields = serializer.fields
|
|
|
|
|
|
|
|
for field_name, field in fields.items():
|
|
|
|
ret += '\n' + indent_str + field_name + ' = '
|
|
|
|
if hasattr(field, 'fields'):
|
|
|
|
ret += serializer_repr(field, indent + 1)
|
|
|
|
elif hasattr(field, 'child'):
|
|
|
|
ret += list_repr(field, indent + 1)
|
2014-09-18 17:23:00 +04:00
|
|
|
elif hasattr(field, 'child_relation'):
|
|
|
|
ret += field_repr(field.child_relation, force_many=field.child_relation)
|
2014-09-09 20:46:28 +04:00
|
|
|
else:
|
|
|
|
ret += field_repr(field)
|
2014-10-31 19:38:39 +03:00
|
|
|
|
|
|
|
if serializer.validators:
|
|
|
|
ret += '\n' + indent_str + 'class Meta:'
|
|
|
|
ret += '\n' + indent_str + ' validators = ' + smart_repr(serializer.validators)
|
|
|
|
|
2014-09-09 20:46:28 +04:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def list_repr(serializer, indent):
|
|
|
|
child = serializer.child
|
|
|
|
if hasattr(child, 'fields'):
|
|
|
|
return serializer_repr(serializer, indent, force_many=child)
|
|
|
|
return field_repr(serializer)
|