コード例 #1
0
ファイル: relations.py プロジェクト: webmaks/aiorest-ws
    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)
コード例 #2
0
ファイル: abstract.py プロジェクト: Relrin/aiorest-ws
    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)
コード例 #3
0
ファイル: relations.py プロジェクト: Relrin/aiorest-ws
    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)
コード例 #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)
コード例 #5
0
ファイル: relations.py プロジェクト: Relrin/aiorest-ws
    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
コード例 #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
コード例 #7
0
ファイル: serializers.py プロジェクト: webmaks/aiorest-ws
 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)
コード例 #8
0
ファイル: abstract.py プロジェクト: webmaks/aiorest-ws
    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)
コード例 #9
0
ファイル: test_fields.py プロジェクト: Relrin/aiorest-ws
def test_get_attribute_failed(instance, attrs, exc_cls):
    with pytest.raises(exc_cls):
        get_attribute(instance, attrs)
コード例 #10
0
ファイル: test_fields.py プロジェクト: Relrin/aiorest-ws
def test_get_attribute(instance, attrs, expected):
    assert get_attribute(instance, attrs) == expected
コード例 #11
0
ファイル: relations.py プロジェクト: webmaks/aiorest-ws
    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)
コード例 #12
0
def test_get_attribute_failed(instance, attrs, exc_cls):
    with pytest.raises(exc_cls):
        get_attribute(instance, attrs)
コード例 #13
0
def test_get_attribute(instance, attrs, expected):
    assert get_attribute(instance, attrs) == expected