def follow(request): if request.method == 'POST': try: data = JSONParser().parse(request) user = Users.objects.get(username=data['user']) following = Users.objects.get(username=data['following']) follow = Follow(user=user, following=following) follow.save() return JsonResponse({ 'success': True, 'message': 'followed', "follow_id": follow.id }) except IntegrityError as e: if 'unique constraint' in e.message: return JsonResponse({ 'success': False, 'message': 'already following' }) return JsonResponse({'success': False, 'message': 'some error occured'})
def follow(request): user = User.objects.get(username=request.POST['user']) user1 = User.objects.get(username=request.POST['profile']) profile = user1.profile foll = Follow(following=profile, follower=user) foll.save() return JsonResponse({'message': 'ok'})
def follow(self, user_pk): """ Follow given user by pk :param user_pk: str: user id :return: Follow """ from users.models import Follow user_exists = User.objects.filter(pk=user_pk).exists() if not user_exists: raise ValidationError(u'User doesn\'t exist') follow = Follow.objects.filter(follower_id=self.pk, following_id=user_pk).first() if follow and follow.is_active: raise ValidationError(u'Already following this user') if follow: follow.is_active = True follow.save() else: follow = Follow( follower_id=self.pk, following_id=user_pk, ) follow.save() return follow
def post(self, request, *args, **kwargs): if request.user.id is not None: banned = Ban.objects.filter( who_blocking=request.user, blocked_users=self.visible_user()).count() == 0 if banned == True: follows_between = Follow.objects.filter( user=request.user, follow_user=self.visible_user()) ban = Ban.objects.filter(who_blocking=request.user, blocked_users=self.visible_user()) if 'follow' in request.POST: new_relation = Follow(user=request.user, follow_user=self.visible_user()) if follows_between.count() == 0: new_relation.save() elif 'unfollow' in request.POST: if follows_between.count() > 0: follows_between.delete() elif 'ban' in request.POST: new_ban = Ban(who_blocking=request.user, blocked_users=self.visible_user()) # new_ban = Ban(who_blocking=request.user) # ManyToManyField if ban.count() == 0: new_ban.save() # new_ban.blocked_users.add(self.visible_user()) # ManyToManyField else: ban = Ban.objects.filter(who_blocking=request.user, blocked_users=self.visible_user()) if 'unban' in request.POST: if ban.count() > 0: ban.delete() return self.get(self, request, *args, **kwargs)
def follow_another(request, user_id): user = Profile.objects.filter(pk=user_id).first() follow = Follow(user_id=request.user.id, following_user_id=user.id) try: follow.save() except: messages.warning(request, f'You have already followed this user') return redirect('user-profile', user_id=user.id) return redirect('user-profile', user_id=user.id)
def post(self, request, *args, **kwargs): if request.user.id is not None: follows_between = Follow.objects.filter(user=request.user, follow_user=self.visible_user()) if 'follow' in request.POST: new_relation = Follow(user=request.user, follow_user=self.visible_user()) if follows_between.count() == 0: new_relation.save() elif 'unfollow' in request.POST: if follows_between.count() > 0: follows_between.delete() return self.get(self, request, *args, **kwargs)
def put(self, request, pk): user = request.user user_to_follow = CustomUser.objects.get(pk=pk) if user.id == user_to_follow.id: print("Cannot follow yourself!!!") return Response(status=status.HTTP_400_BAD_REQUEST) follows = Follow.objects.filter(who_id=user.id, whom_id=user_to_follow.id) if follows: # user vec follow-a drugog usera, napravi unfollow follows.delete() return Response() follow = Follow() follow.who = user follow.whom = user_to_follow follow.save() return Response()