Feature: Allow current user to fetch its information

This commit is contained in:
winkidney
2019-11-29 12:56:44 +08:00
committed by Isaac Bythewood
parent a2bdf58943
commit c3b0cffea3

View File

@@ -9,9 +9,18 @@ from core.permissions import IsOwnerOrReadOnly
from users.models import User from users.models import User
class UserViewSet(mixins.RetrieveModelMixin, GenericViewSet): class UserViewSet(
queryset = User.objects.all() mixins.RetrieveModelMixin,
mixins.ListModelMixin,
GenericViewSet,
):
serializer_class = api.UserSerializer serializer_class = api.UserSerializer
pagination_class = None
def get_queryset(self):
if self.request.user.is_anonymous:
return User.objects.none()
return User.objects.filter(id=self.request.user.id)
class ImageViewSet(mixins.CreateModelMixin, GenericViewSet): class ImageViewSet(mixins.CreateModelMixin, GenericViewSet):
@@ -43,7 +52,7 @@ class BoardViewSet(viewsets.ModelViewSet):
drf_router = routers.DefaultRouter() drf_router = routers.DefaultRouter()
drf_router.register(r'users', UserViewSet) drf_router.register(r'users', UserViewSet, base_name="user")
drf_router.register(r'pins', PinViewSet) drf_router.register(r'pins', PinViewSet)
drf_router.register(r'images', ImageViewSet) drf_router.register(r'images', ImageViewSet)
drf_router.register(r'boards', BoardViewSet) drf_router.register(r'boards', BoardViewSet)