def put(self, request, pk=None): target = self.get_object(pk) # add the follower to the user's (followee) friend list follower_url = reverse('user-detail', args=[target.follower.id], request=request) followee_url = reverse('user-detail', args=[target.followee.id], request=request) mutual_data = { "followee": follower_url, "follower": followee_url, "mutual": True, "not_read": False } mutual_serializer = FriendSerializer( data=mutual_data) # create a mutual following if mutual_serializer.is_valid( raise_exception=True ): # have to call is_valid() before excution mutual_serializer.save() serializer = FriendSerializer(target, data={ "mutual": True, "not_read": False }, partial=True, context={'request': request}) if serializer.is_valid(raise_exception=True ): # have to call is_valid() before excution serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request): me = self.get_object() me_serialized = FriendSerializer(me, data=request.data) if me_serialized.is_valid(): me_serialized.save() return Response(me_serialized.data) return Response(me_serialized.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request): me = self.get_object() me_serialized = FriendSerializer(me, data=request.data) if me_serialized.is_valid(): me_serialized.save() return Response(me_serialized.data) return Response( me_serialized.errors, status=status.HTTP_400_BAD_REQUEST )
def put(self, request, pk=None): target = self.get_object(pk) serializer = FriendSerializer(target, data={"not_read": False}, partial=True, context={'request': request}) if serializer.is_valid(raise_exception=True ): # have to call is_valid() before excution serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, pk=None): target = self.get_object(pk) if target.mutual: # if target's mutual is True, set followee's mutual to False followee = Friend.objects.get( Q(followee=target.follower) & Q(follower=target.followee)) followee_serializer = FriendSerializer(followee, data={"mutual": False}, partial=True) if followee_serializer.is_valid( raise_exception=True ): # have to call is_valid() before excution self.perform_update(followee_serializer) target.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request): Friend.objects.all().delete() x = Friend.random_position() y = Friend.random_position() me = Friend(x=x, y=y, me=True) me.save() me.generate_friends() me_serialized = FriendSerializer(me) return Response(me_serialized.data)
def destroy(self, request, followee_id): # followee_id = self.kwargs['followee_id'] try: target = Friend.objects.get( Q(followee_id=followee_id) & Q(follower_id=request.user.id)) except Friend.DoesNotExist: raise Http404 if target.mutual: # if target's mutual is True, set followee's mutual to False followee = Friend.objects.get( Q(followee_url=target.follower_url) & Q(follower_url=target.followee_url)) followee_serializer = FriendSerializer(followee, data={"mutual": False}, partial=True) if followee_serializer.is_valid( raise_exception=True ): # have to call is_valid() before excution self.perform_update(followee_serializer) target.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def put(self, request, follower_id): # follower_id = self.kwargs['follower_id'] target = self.get_object(follower_id, request) # add the follower to the user's (followee) friend list mutual_data = { "followee_id": target.follower_id, "followee_host": target.follower_host, "followee_name": target.follower_name, "followee_url": target.follower_url, "follower_id": target.followee_id, "follower_host": target.followee_host, "follower_name": target.followee_name, "follower_url": target.followee_url, "mutual": True, "not_read": False } mutual_serializer = FriendSerializer( data=mutual_data) # create a mutual following if mutual_serializer.is_valid( raise_exception=True ): # have to call is_valid() before excution mutual_serializer.save() serializer = FriendSerializer(target, data={ "mutual": True, "not_read": False }, partial=True, context={'request': request}) if serializer.is_valid(raise_exception=True ): # have to call is_valid() before excution serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): follower = request.data["author"] followee = request.data["friend"] followee_id = re.findall( r"(https?://[-A-Za-z0-9+&@#%?=~_|!:,.;]+/)author/([-A-Za-z0-9]+)/?", followee["id"], re.I)[0][1] follower_id = re.findall( r"(https?://[-A-Za-z0-9+&@#%?=~_|!:,.;]+/)author/([-A-Za-z0-9]+)/?", follower["id"], re.I)[0][1] # allow the user send request again if this user is rejected or unfollowed rejected_request = Friend.objects.filter( Q(followee_id=followee_id) & Q(follower_id=follower_id) & Q(mutual=False)) num_rejected_request = rejected_request.count() if num_rejected_request > 0: serializer = FriendSerializer(rejected_request[0], data={"not_read": True}, partial=True, context={'request': request}) if serializer.is_valid(raise_exception=True): serializer.save() return Response(status=status.HTTP_200_OK) request_data = { "followee_id": followee_id, "followee_host": followee["host"], "followee_name": followee["displayName"], "followee_url": followee["url"], "follower_id": follower_id, "follower_host": follower["host"], "follower_name": follower["displayName"], "follower_url": follower["url"], } serializer = self.get_serializer_class()(data=request_data, context={ 'request': request }) if serializer.is_valid(raise_exception=True): serializer.save() return Response(status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def getFriendList(request): serializer = FriendSerializer(Friend.objects.all(), many=True) return Response(serializer.data)
def get(self, request): me = self.get_or_create_object() me_serialized = FriendSerializer(me) return Response(me_serialized.data)