2019-02-20 18:03:27 +08:00
|
|
|
from rest_framework import viewsets, mixins, routers
|
|
|
|
|
from rest_framework.viewsets import GenericViewSet
|
|
|
|
|
|
|
|
|
|
from core import drf_api as api
|
|
|
|
|
from core.models import Image, Pin
|
|
|
|
|
from core.permissions import IsOwnerOrReadOnly
|
|
|
|
|
from users.models import User
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserViewSet(viewsets.ModelViewSet):
|
|
|
|
|
queryset = User.objects.all()
|
|
|
|
|
serializer_class = api.UserSerializer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageViewSet(mixins.CreateModelMixin, GenericViewSet):
|
|
|
|
|
queryset = Image.objects.all()
|
|
|
|
|
serializer_class = api.ImageSerializer
|
|
|
|
|
|
2019-02-20 19:01:40 +08:00
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
|
super(ImageViewSet, self).create(request, *args, **kwargs)
|
|
|
|
|
|
2019-02-20 18:03:27 +08:00
|
|
|
|
|
|
|
|
class PinViewSet(viewsets.ModelViewSet):
|
|
|
|
|
queryset = Pin.objects.all()
|
|
|
|
|
serializer_class = api.PinSerializer
|
|
|
|
|
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)
|