Преглед изворни кода

Feature: Add image creation api and api-docs of DRF api

pull/139/head
winkidney пре 5 година
родитељ
комит
32dc00fa34
2 измењених фајлова са 25 додато и 4 уклоњено
  1. +23
    -4
      core/drf_api.py
  2. +2
    -0
      pinry/urls.py

+ 23
- 4
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)

+ 2
- 0
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)),


Loading…
Откажи
Сачувај