Last active
August 28, 2020 07:37
-
-
Save mentix02/cc1589666b7a2ff06aadd3f8a5fd42a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models import QuerySet | |
from django.shortcuts import get_object_or_404 | |
from rest_framework.permissions import IsAuthenticated | |
from rest_framework.exceptions import PermissionDenied | |
from rest_framework.generics import ( | |
ListAPIView, | |
CreateAPIView, | |
RetrieveAPIView, | |
RetrieveUpdateAPIView, | |
) | |
from user.models import User | |
from post.models import Post | |
from post.serializers import PostSerializer | |
from user.serializers import UserSerializer | |
from user.permissions import UserIsOwnerOrReadOnly | |
class UserRegistrationAPIView(CreateAPIView): | |
serializer_class = UserSerializer | |
class UserUpdateAPIView(RetrieveUpdateAPIView): | |
lookup_url_kwarg = 'username' | |
queryset = User.objects.all() | |
lookup_field = 'username__iexact' | |
serializer_class = UserSerializer | |
permission_classes = (IsAuthenticated, UserIsOwnerOrReadOnly) | |
class UserFollowingListAPIView(ListAPIView): | |
serializer_class = UserSerializer | |
permission_classes = (IsAuthenticated,) | |
def get_queryset(self) -> QuerySet: | |
return self.request.user.following | |
class UserFollowersListAPIView(ListAPIView): | |
serializer_class = UserSerializer | |
permission_classes = (IsAuthenticated,) | |
def get_queryset(self) -> QuerySet: | |
return self.request.user.followers | |
class UserDetailAPIView(RetrieveAPIView): | |
lookup_url_kwarg = 'username' | |
queryset = User.objects.all() | |
lookup_field = 'username__iexact' | |
serializer_class = UserSerializer | |
class UserPostListAPIView(ListAPIView): | |
serializer_class = PostSerializer | |
permission_classes = (IsAuthenticated,) | |
def get_queryset(self) -> QuerySet: | |
username = self.kwargs['username'] | |
user = get_object_or_404(User, username__iexact=username) | |
if user in self.request.user.following or not user.private: | |
return Post.objects.filter(user__username__iexact=username) | |
else: | |
raise PermissionDenied('Follow user to see their posts.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment