<s> from rest_framework import viewsets <EOL> from rest_framework import permissions <EOL> from rest_framework . decorators import detail_route <EOL> from rest_framework import status <EOL> from rest_framework . response import Response <EOL> from profiles . models import Profile <EOL> from . serializers import UserProfileSerializer <EOL> from profiles . signals import follow_done , unfollow_done <EOL> from api . v1 . arguments . serializers import ContentionSerializer <EOL> from premises . models import Contention <EOL> from profiles . forms import ProfileUpdateForm <EOL> class UserProfileViewset ( viewsets . ModelViewSet ) : <EOL> queryset = Profile . objects . all ( ) <EOL> permission_classes = ( permissions . IsAuthenticatedOrReadOnly , ) <EOL> serializer_class = UserProfileSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> lookup_url_kwarg = '<STR_LIT:username>' <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def authenticated_user ( self , request ) : <EOL> user = request . user <EOL> serializer = self . serializer_class ( user ) <EOL> return Response ( serializer . data ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def update_profile ( self , request ) : <EOL> form = ProfileUpdateForm ( request . DATA , instance = request . user ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return Response ( self . serializer_class ( request . user ) . data ) <EOL> return Response ( form . errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> class UserFollowViewset ( viewsets . ModelViewSet ) : <EOL> queryset = Profile . objects . all ( ) <EOL> permission_classes = ( permissions . IsAuthenticatedOrReadOnly , ) <EOL> serializer_class = UserProfileSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> lookup_url_kwarg = '<STR_LIT:username>' <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def follow ( self , request , username = None ) : <EOL> user = self . get_object ( ) <EOL> if user . id == request . user . id : <EOL> return Response ( { '<STR_LIT:message>' : "<STR_LIT>" } , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> if user . followers . filter ( pk = request . user . pk ) . exists ( ) : <EOL> return Response ( <EOL> { '<STR_LIT:message>' : "<STR_LIT>" } , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> request . user . following . add ( user ) <EOL> follow_done . send ( sender = self , follower = request . user , following = user ) <EOL> return Response ( status = status . HTTP_201_CREATED ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def unfollow ( self , request , username = None ) : <EOL> user = self . get_object ( ) <EOL> if not request . user . following . filter ( id = user . id ) . exists ( ) : <EOL> return Response ( <EOL> { '<STR_LIT:message>' : "<STR_LIT>" } , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> request . user . following . remove ( user ) <EOL> unfollow_done . send ( sender = self , follower = request . user , following = user ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def followings ( self , request , username = None ) : <EOL> user = self . get_object ( ) <EOL> page = self . paginate_queryset ( user . following . all ( ) ) <EOL> serializer = self . get_pagination_serializer ( page ) <EOL> return Response ( serializer . data ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def followers ( self , request , username = None ) : <EOL> user = self . get_object ( ) <EOL> page = self . paginate_queryset ( user . followers . all ( ) ) <EOL> serializer = self . get_pagination_serializer ( page ) <EOL> return Response ( serializer . data ) <EOL> class UserArgumentsView ( viewsets . ModelViewSet ) : <EOL> queryset = Profile . objects . all ( ) <EOL> serializer_class = ContentionSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> lookup_url_kwarg = '<STR_LIT:username>' <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def user_arguments ( self , request , username = None ) : <EOL> user = self . get_object ( ) <EOL> arguments = user . contention_set . filter ( ) <EOL> if not ( self . request . user . is_authenticated ( ) and <EOL> user == self . request . user ) : <EOL> arguments = arguments . filter ( is_published = True ) <EOL> page = self . paginate_queryset ( arguments ) <EOL> serializer = self . get_pagination_serializer ( page ) <EOL> return Response ( serializer . data ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def user_contributed ( self , request , username = None ) : <EOL> user = self . get_object ( ) <EOL> page = self . paginate_queryset ( Contention . objects . filter ( <EOL> premises__user = user , is_published = True ) . exclude ( user = user ) ) <EOL> serializer = self . get_pagination_serializer ( page ) <EOL> return Response ( serializer . data ) <EOL> profile_detail = UserProfileViewset . as_view ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> profile_me = UserProfileViewset . as_view ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> permission_classes = ( permissions . IsAuthenticated , ) <EOL> ) <EOL> profile_followers = UserFollowViewset . as_view ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> profile_followings = UserFollowViewset . as_view ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> profile_follow = UserFollowViewset . as_view ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> permission_classes = ( permissions . IsAuthenticated , ) <EOL> ) <EOL> user_arguments = UserArgumentsView . as_view ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> ) <EOL> user_contributed_arguments = UserArgumentsView . as_view ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) </s>
