From 32dc00fa344cd91d7aba0d33ef24d7c8036367ec Mon Sep 17 00:00:00 2001 From: winkidney Date: Wed, 20 Feb 2019 17:40:19 +0800 Subject: [PATCH] Feature: Add image creation api and api-docs of DRF api --- core/drf_api.py | 27 +++++++++++++++++++++++---- pinry/urls.py | 2 ++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/core/drf_api.py b/core/drf_api.py index 96f74ff..3acc76b 100644 --- a/core/drf_api.py +++ b/core/drf_api.py @@ -1,4 +1,5 @@ -from rest_framework import serializers, viewsets, routers +from rest_framework import serializers, viewsets, routers, mixins +from rest_framework.viewsets import GenericViewSet from taggit.models import Tag from core.models import Image, Pin @@ -14,7 +15,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer): fields = ( 'username', 'gravatar', - 'url', + settings.DRF_URL_FIELD_NAME, ) @@ -44,16 +45,32 @@ class ImageSerializer(serializers.ModelSerializer): "thumbnail", "square", ) + extra_kwargs = { + "width": {"read_only": True}, + "height": {"read_only": True}, + } + standard = ThumbnailSerializer(read_only=True) thumbnail = ThumbnailSerializer(read_only=True) square = ThumbnailSerializer(read_only=True) + def create(self, validated_data): + image = super(ImageSerializer, self).create(validated_data) + for size in settings.IMAGE_SIZES: + Thumbnail.objects.get_or_create_at_size(image.pk, size) + return image + + +class ImageViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, GenericViewSet): + queryset = Image.objects.all() + serializer_class = ImageSerializer + class TagSerializer(serializers.ModelSerializer): class Meta: model = Tag - fields = ("name", ) + fields = ("name",) class PinSerializer(serializers.HyperlinkedModelSerializer): @@ -71,6 +88,7 @@ class PinSerializer(serializers.HyperlinkedModelSerializer): "tags", ) + tags = serializers.SlugRelatedField( many=True, source="tag_list", @@ -108,10 +126,11 @@ class PinSerializer(serializers.HyperlinkedModelSerializer): class PinViewSet(viewsets.ModelViewSet): queryset = Pin.objects.all() serializer_class = PinSerializer - filter_fields = ('submitter__username', ) + filter_fields = ('submitter__username',) permission_classes = [IsOwnerOrReadOnly("submitter"), ] drf_router = routers.DefaultRouter() drf_router.register(r'users', UserViewSet) drf_router.register(r'pins', PinViewSet) +drf_router.register(r'images', ImageViewSet) diff --git a/pinry/urls.py b/pinry/urls.py index e296fa7..d3954ea 100644 --- a/pinry/urls.py +++ b/pinry/urls.py @@ -3,6 +3,7 @@ from django.conf.urls import include, url from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.contrib import admin from django.views.static import serve +from rest_framework.documentation import include_docs_urls from core.drf_api import drf_router @@ -14,6 +15,7 @@ urlpatterns = [ # drf api url(r'^drf_api/', include(drf_router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace="rest_framework")), + url(r'^drf_api/docs/', include_docs_urls(title='PinryAPI', schema_url='/')), # old api and views url(r'^admin/', include(admin.site.urls)),