Пример #1
0
    def field_to_swagger_object(self, field, swagger_object_type, **kwargs):
        if not isinstance(field, serializers.IntegerField) or not is_json_api(
                self.view):
            return inspectors.NotHandled

        stringify_id = False
        integer_format = None

        parent_serializer = get_parent_serializer(field)
        serializer_meta = getattr(parent_serializer, 'Meta', None)
        model = getattr(serializer_meta, 'model', None)
        if model is not None:
            field_name = getattr(field, 'source', None) or field.field_name
            model_field = get_model_field(model, field_name)
            # Check for primary key only if sure it is pure Field (not FieldCacheMixin or anything)
            if (model_field is not None
                    and isinstance(model_field,
                                   (models.IntegerField, models.AutoField))
                    and model_field.primary_key):
                stringify_id = True
                integer_format = self.get_format(model_field)

        elif field.field_name == 'id' and isinstance(field,
                                                     serializers.IntegerField):
            stringify_id = True

        if not stringify_id:
            return inspectors.NotHandled

        SwaggerType, ChildSwaggerType = self._get_partial_types(
            field, swagger_object_type, **kwargs)
        return SwaggerType(type=openapi.TYPE_STRING, format=integer_format)
Пример #2
0
    def field_to_swagger_object(self, field, **kwargs):

        if not isinstance(
                field, serializers.IntegerField) or not is_json_api_response(
                    self.view.renderer_classes):
            return inspectors.NotHandled

        parent_serializer = get_parent_serializer(field)
        serializer_meta = getattr(parent_serializer, 'Meta', None)
        model = getattr(serializer_meta, 'model', None)

        if model is None:
            return inspectors.NotHandled

        source = getattr(field, 'source', '') or field.field_name

        # Use source but mean field_name
        model_field = get_model_field(model, source)

        if (isinstance(model_field, models.IntegerField) and model_field.primary_key) or \
                isinstance(model_field, models.AutoField):
            SwaggerType, ChildSwaggerType = self._get_partial_types(
                field, **kwargs)
            return SwaggerType(type=openapi.TYPE_STRING,
                               format=openapi.FORMAT_INT32)

        return inspectors.NotHandled
Пример #3
0
    def field_to_swagger_object(self, field, **kwargs):

        if not isinstance(field.parent, serializers.ManyRelatedField) or \
                not is_json_api_response(self.view.renderer_classes):
            return inspectors.NotHandled

        parent_serializer = get_parent_serializer(field)
        serializer_meta = getattr(parent_serializer, 'Meta', None)
        model = getattr(serializer_meta, 'model', None)

        if model is None:
            return inspectors.NotHandled

        source = getattr(field.parent, 'source', '') or field.parent.field_name

        field_model = get_related_model(model, source)
        if field_model is None:
            return inspectors.NotHandled

        model_field = get_model_field(field_model, 'pk')

        if isinstance(model_field, (models.IntegerField, models.AutoField)):
            SwaggerType, ChildSwaggerType = self._get_partial_types(
                field, **kwargs)
            return SwaggerType(type=openapi.TYPE_STRING,
                               format=openapi.FORMAT_INT32)

        return inspectors.NotHandled
Пример #4
0
    def get_resource_name_from_id_field(self, field_name, id_field):
        parent_serializer = get_parent_serializer(id_field)
        if isinstance(id_field, dja_serializers.ResourceRelatedField):
            return id_field.get_resource_type_from_included_serializer()

        elif isinstance(id_field, serializers.Serializer):
            return json_api_utils.get_resource_type_from_serializer(id_field)

        # Other kinds of fields
        if hasattr(id_field, 'model'):
            model = id_field.model
        elif hasattr(id_field, 'get_queryset') and id_field.get_queryset():
            model = id_field.get_queryset().model
        # If the RelatedField hasn't got a queryset, take model from the serializer and find proper model field
        else:
            serializer_meta = getattr(parent_serializer, 'Meta', None)
            this_model = getattr(serializer_meta, 'model', None)

            source = getattr(id_field, 'source', '') or id_field.field_name
            if not source and isinstance(id_field.parent,
                                         serializers.ManyRelatedField):
                source = getattr(id_field.parent, 'source',
                                 '') or id_field.parent.field_name

            model = get_related_model(this_model, source)

        # Resource name from model
        if model:
            return get_resource_type_from_model(model)

        raise ValueError(
            f"Unable to extract resource name for {parent_serializer}.{field_name} serializer field"
        )
Пример #5
0
def get_field_related_model(field) -> Optional[models.Model]:
    # Try extracting directly from field
    related_model = _get_field_model(field)
    # If failed try to extract by traversing model and model fields
    if related_model is None:
        parent_serializer = get_parent_serializer(field)
        serializer_meta = getattr(parent_serializer, 'Meta', None)
        model = getattr(serializer_meta, 'model', None)
        if model is not None:
            related_model = get_related_model(model,
                                              source=get_field_source(field))
    return related_model
Пример #6
0
    def get_resource_name_from_related_id_field(self, field_name, id_field):
        # Unpack ManyRelatedField from many wrapper
        id_field = getattr(id_field, 'child_relation', None) or id_field

        # Not very frequent case but different from others
        if isinstance(id_field, serializers.Serializer):
            return json_api_utils.get_resource_type_from_serializer(id_field)
        # Most cases
        else:
            # Try to get from included serializers
            parent_serializer = get_parent_serializer(id_field)
            if isinstance(id_field, dja_serializers.ResourceRelatedField):
                resource_name = id_field.get_resource_type_from_included_serializer(
                )
                if resource_name:
                    return resource_name

            related_model = get_field_related_model(id_field)
            if related_model:
                return get_resource_type_from_model(related_model)

        raise ValueError(
            f"Unable to extract resource name for {parent_serializer}.{field_name} serializer field"
        )