Server IP : 85.214.239.14 / Your IP : 18.116.88.132 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /srv/modoboa/env/lib64/python3.5/site-packages/rest_framework/utils/ |
Upload File : |
""" Helper functions for creating user-friendly representations of serializer classes and serializer fields. """ import re from django.db import models from django.utils.encoding import force_str from django.utils.functional import Promise def manager_repr(value): model = value.model opts = model._meta names_and_managers = [ (manager.name, manager) for manager in opts.managers ] for manager_name, manager_instance in names_and_managers: if manager_instance == value: return '%s.%s.all()' % (model._meta.object_name, manager_name) return repr(value) def smart_repr(value): if isinstance(value, models.Manager): return manager_repr(value) if isinstance(value, Promise) and value._delegate_text: value = force_str(value) value = repr(value) # 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> return re.sub(' at 0x[0-9A-Fa-f]{4,32}>', '>', value) 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) elif hasattr(field, 'child_relation'): ret += field_repr(field.child_relation, force_many=field.child_relation) else: ret += field_repr(field) if serializer.validators: ret += '\n' + indent_str + 'class Meta:' ret += '\n' + indent_str + ' validators = ' + smart_repr(serializer.validators) 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)