BVB Source Codes

django-rest-framework Show representation.py Source code

Return Download django-rest-framework: download representation.py Source code - Download django-rest-framework Source code - Type:.py
  1. """
  2. Helper functions for creating user-friendly representations
  3. of serializer classes and serializer fields.
  4. """
  5. from __future__ import unicode_literals
  6.  
  7. import re
  8.  
  9. from django.db import models
  10. from django.utils.encoding import force_text
  11. from django.utils.functional import Promise
  12.  
  13. from rest_framework.compat import get_names_and_managers, unicode_repr
  14.  
  15.  
  16. def manager_repr(value):
  17.     model = value.model
  18.     opts = model._meta
  19.     for manager_name, manager_instance in get_names_and_managers(opts):
  20.         if manager_instance == value:
  21.             return '%s.%s.all()' % (model._meta.object_name, manager_name)
  22.     return repr(value)
  23.  
  24.  
  25. def smart_repr(value):
  26.     if isinstance(value, models.Manager):
  27.         return manager_repr(value)
  28.  
  29.     if isinstance(value, Promise) and value._delegate_text:
  30.         value = force_text(value)
  31.  
  32.     value = unicode_repr(value)
  33.  
  34.     # Representations like u'help text'
  35.     # should simply be presented as 'help text'
  36.     if value.startswith("u'") and value.endswith("'"):
  37.         return value[1:]
  38.  
  39.     # Representations like
  40.     # <django.core.validators.RegexValidator object at 0x1047af050>
  41.     # Should be presented as
  42.     # <django.core.validators.RegexValidator object>
  43.     value = re.sub(' at 0x[0-9A-Fa-f]{4,32}>', '>', value)
  44.  
  45.     return value
  46.  
  47.  
  48. def field_repr(field, force_many=False):
  49.     kwargs = field._kwargs
  50.     if force_many:
  51.         kwargs = kwargs.copy()
  52.         kwargs['many'] = True
  53.         kwargs.pop('child', None)
  54.  
  55.     arg_string = ', '.join([smart_repr(val) for val in field._args])
  56.     kwarg_string = ', '.join([
  57.         '%s=%s' % (key, smart_repr(val))
  58.         for key, val in sorted(kwargs.items())
  59.     ])
  60.     if arg_string and kwarg_string:
  61.         arg_string += ', '
  62.  
  63.     if force_many:
  64.         class_name = force_many.__class__.__name__
  65.     else:
  66.         class_name = field.__class__.__name__
  67.  
  68.     return "%s(%s%s)" % (class_name, arg_string, kwarg_string)
  69.  
  70.  
  71. def serializer_repr(serializer, indent, force_many=None):
  72.     ret = field_repr(serializer, force_many) + ':'
  73.     indent_str = '    ' * indent
  74.  
  75.     if force_many:
  76.         fields = force_many.fields
  77.     else:
  78.         fields = serializer.fields
  79.  
  80.     for field_name, field in fields.items():
  81.         ret += '\n' + indent_str + field_name + ' = '
  82.         if hasattr(field, 'fields'):
  83.             ret += serializer_repr(field, indent + 1)
  84.         elif hasattr(field, 'child'):
  85.             ret += list_repr(field, indent + 1)
  86.         elif hasattr(field, 'child_relation'):
  87.             ret += field_repr(field.child_relation, force_many=field.child_relation)
  88.         else:
  89.             ret += field_repr(field)
  90.  
  91.     if serializer.validators:
  92.         ret += '\n' + indent_str + 'class Meta:'
  93.         ret += '\n' + indent_str + '    validators = ' + smart_repr(serializer.validators)
  94.  
  95.     return ret
  96.  
  97.  
  98. def list_repr(serializer, indent):
  99.     child = serializer.child
  100.     if hasattr(child, 'fields'):
  101.         return serializer_repr(serializer, indent, force_many=child)
  102.     return field_repr(serializer)
  103.  
downloadrepresentation.py Source code - Download django-rest-framework Source code
Related Source Codes/Software:
lectures - Oxford Deep NLP 2017 course 2017-06-10
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Public-APIs - 2017-06-09
angular-starter - 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11

 Back to top