Exemplo n.º 1
0
    def get_attribute(self, instance):
        if self.use_pk_only_optimization() and self.source_attrs:
            # Optimized case, return a mock object only containing the
            # pk attribute
            try:
                instance = get_attribute(instance, self.source_attrs[:-1])
                value = getattr(instance, self.source_attrs[-1])
                return PKOnlyObject(pk=value)
            except AttributeError:
                pass

        # Standard case, return the object instance
        return get_attribute(instance, self.source_attrs)
Exemplo n.º 2
0
    def get_attribute(self, instance):
        """
        Given the *outgoing* object instance, return the primitive value
        that should be used for this field.

        :param instance: object, from which value will have taken.
        """
        try:
            return get_attribute(instance, self.source_attrs)
        except (KeyError, AttributeError) as exc:
            if not self.required and self.default is empty:
                raise SkipField()
            msg = (
                'Got {exc_type} when attempting to get a value for field '
                '`{field}` on serializer `{serializer}`.\nThe serializer '
                'field might be named incorrectly and not match '
                'any attribute or key on the `{instance}` instance.\n'
                'Original exception text was: {exc}.'.format(
                    exc_type=type(exc).__name__,
                    field=self.field_name,
                    serializer=self.parent.__class__.__name__,
                    instance=instance.__class__.__name__,
                    exc=exc
                )
            )
            raise type(exc)(msg)
Exemplo n.º 3
0
    def get_attribute(self, instance):
        if self.use_pk_only_optimization() and self.source_attrs:
            # Optimized case, return a mock object only containing the
            # pk attribute
            try:
                instance = get_attribute(instance, self.source_attrs[:-1])
                value = instance.serializable_value(self.source_attrs[-1])
                if is_simple_callable(value):
                    # Handle edge case where the relationship `source` argument
                    # points to a `get_relationship()` method on the model
                    value = value().pk
                return relations.PKOnlyObject(pk=value)
            except AttributeError:
                pass

        # Standard case, return the object instance.
        return get_attribute(instance, self.source_attrs)
Exemplo n.º 4
0
    def get_attribute(self, instance):
        if self.use_pk_only_optimization() and self.source_attrs:
            # Optimized case, return a mock object only containing the
            # pk attribute
            try:
                instance = get_attribute(instance, self.source_attrs[:-1])
                value = instance.serializable_value(self.source_attrs[-1])
                if is_simple_callable(value):
                    # Handle edge case where the relationship `source` argument
                    # points to a `get_relationship()` method on the model
                    value = value().pk
                return relations.PKOnlyObject(pk=value)
            except AttributeError:
                pass

        # Standard case, return the object instance.
        return get_attribute(instance, self.source_attrs)
Exemplo n.º 5
0
    def get_attribute(self, instance):
        # Can't have any relationships if not created
        if hasattr(instance, 'pk') and instance.pk is None:
            return []

        relationship = get_attribute(instance, self.source_attrs)
        if hasattr(relationship, 'all'):
            return relationship.all()
        return relationship
Exemplo n.º 6
0
    def get_attribute(self, instance):
        # Can't have any relationships if not created
        if hasattr(instance, 'pk') and instance.pk is None:
            return []

        relationship = get_attribute(instance, self.source_attrs)
        if hasattr(relationship, 'all'):
            return relationship.all()
        return relationship
Exemplo n.º 7
0
 def get_attribute(self, instance):
     try:
         return get_attribute(instance, self.source_attrs)
     except (KeyError, AttributeError) as exc:
         if not self.required and self.default is empty:
             raise SkipField()
         msg = (
             'Got {exc_type} when attempting to get a value for field '
             '`{field}` on serializer `{serializer}`.\nThe serializer '
             'field might be named incorrectly and not match '
             'any attribute or key on the `{instance}` instance.\n'
             'Original exception text was: {exc}.'.format(
                 exc_type=type(exc).__name__,
                 field=self.field_name,
                 serializer=self.parent.__class__.__name__,
                 instance=instance.__class__.__name__,
                 exc=exc
             )
         )
         raise type(exc)(msg)
Exemplo n.º 8
0
    def get_attribute(self, instance):
        """
        Given the *outgoing* object instance, return the primitive value
        that should be used for this field.

        :param instance: object, from which value will have taken.
        """
        try:
            return get_attribute(instance, self.source_attrs)
        except (KeyError, AttributeError) as exc:
            if not self.required and self.default is empty:
                raise SkipField()
            msg = ('Got {exc_type} when attempting to get a value for field '
                   '`{field}` on serializer `{serializer}`.\nThe serializer '
                   'field might be named incorrectly and not match '
                   'any attribute or key on the `{instance}` instance.\n'
                   'Original exception text was: {exc}.'.format(
                       exc_type=type(exc).__name__,
                       field=self.field_name,
                       serializer=self.parent.__class__.__name__,
                       instance=instance.__class__.__name__,
                       exc=exc))
            raise type(exc)(msg)
Exemplo n.º 9
0
def test_get_attribute_failed(instance, attrs, exc_cls):
    with pytest.raises(exc_cls):
        get_attribute(instance, attrs)
Exemplo n.º 10
0
def test_get_attribute(instance, attrs, expected):
    assert get_attribute(instance, attrs) == expected
Exemplo n.º 11
0
    def get_attribute(self, instance):
        object_state = instance._sa_instance_state
        if object_state.transient or object_state.pending:
            return []

        return get_attribute(instance, self.source_attrs)
Exemplo n.º 12
0
def test_get_attribute_failed(instance, attrs, exc_cls):
    with pytest.raises(exc_cls):
        get_attribute(instance, attrs)
Exemplo n.º 13
0
def test_get_attribute(instance, attrs, expected):
    assert get_attribute(instance, attrs) == expected