Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1.     def _serialize_instance(self, val_, as_minimal=False):
  2.         """
  3.        Given a model instance, returns its corresponding serialization as
  4.        a Python object.
  5.  
  6.        Serialization definitions are described at the very beginning of
  7.        this module, under `BASE_SERIALIZERS`.
  8.  
  9.        Special field names are "*" (a wildcard for "include all fields"),
  10.        "unicode()" (the unicode representation of the current object,
  11.        which then goes mapped into a key named "__unicode__") and
  12.        "unicode(path)" (the unicode representation of an object under
  13.        the given path). If both "path" and "unicode(path)" are defined,
  14.        value for "path" (its ID) will be mapped under "path_id", and the
  15.        stringified version under "path" instead.
  16.  
  17.        :param val_: The model instance to serialize.
  18.        :type val_: object
  19.        :param as_minimal: Whether to run it in minimal serialization mode (only a subset
  20.                           of fields) or not. Default False.
  21.        :type as_minimal: bool
  22.  
  23.        :returns: A Python-serialized representation of the given object.
  24.        :rtype: dict
  25.        """
  26.         serializer = self._get_scheme(val_)
  27.         fields, exclude = serializer.get_fields(as_minimal), serializer.exclude
  28.         fields = set(fields or ['*'])
  29.         exclude = set(exclude or [])
  30.  
  31.         base_fields = None if '*' in fields else fields.difference(exclude)
  32.         serialized = serializers.serialize(
  33.             'python', [val_], fields=base_fields)[0]
  34.         response = Munch(serialized['fields'])
  35.         keys = set(response.keys())
  36.  
  37.         if '*' in fields:
  38.             # all fields selected
  39.             fields.remove('*')
  40.             fields = fields.union(keys)
  41.         fields = fields.difference(exclude)
  42.  
  43.         for key in keys:
  44.             if key not in fields:
  45.                 del response[key]
  46.             else:
  47.                 fields.remove(key)
  48.  
  49.         for field in fields:
  50.             # unicode(field) -> translates as 'field' in the response,
  51.             # with the field's own ID translated as 'field_id' instead.
  52.             if field.startswith('unicode('):
  53.                 field = field[8:-1]
  54.                 if not field:
  55.                     response['__unicode__'] = str(val_ or '')
  56.                 else:
  57.                     if field in response:
  58.                         id_field = '%s_id' % field
  59.                         response[id_field] = response[field]
  60.                     response[field] = str(self._run_path(val_, field) or '')
  61.             elif field.startswith('len('):
  62.                 # len(field) -> translates as 'field.length' in the response
  63.                 field = field[4:-1]
  64.                 entry = self._run_path(val_, field)
  65.                 if entry is None:
  66.                     entry = []
  67.                 if isinstance(entry, QuerySet):
  68.                     response['%s.length' % field] = entry.count()
  69.                 else:
  70.                     response['%s.length' % field] = len(entry)
  71.             else:
  72.                 if serializer.has_custom(field):
  73.                     response[field] = serializer.get_custom(field, val_)
  74.                 else:
  75.                     response[field] = self._run_path(val_, field)
  76.                 if isinstance(response[field], (set, frozenset, tuple)):
  77.                     # normalizing types (using lists always)
  78.                     # due to JSON serialization issues
  79.                     response[field] = list(response[field])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement