def post(self, request, *args, **kwargs): friend_list = get_friends(request.user) kwargs["friends"] = friend_list # Determine if user exists. friend_name = request.POST["friend_name"] friend = None try: friend = User.objects.get(username=friend_name) except User.DoesNotExist: kwargs["error"] = "Couldn't find user %s" % friend_name if friend: # Determine if user is already a friend. isAlreadyFriend = False for friend in friend_list: if friend_name == friend.username: isAlreadyFriend = True if not isAlreadyFriend: f = Friendship(user1=request.user, user2=friend) f.save() friend_list.append(friend) else: msg = "You're already friends with %s." % friend_name kwargs["error"] = msg return super(FriendsView, self).get(self, request, *args, **kwargs)
def addFriendView(request): creator = request.user friend_name = request.GET.get('friend', '') action = request.GET.get('accepted', '') friend = dbUser.objects.get(username=friend_name) if (action == 'false'): Friendship.objects.filter(creator=friend, friend=creator).delete() return redirect('/friends/') if not Friendship.objects.filter(creator=creator, friend=friend): if Friendship.objects.filter(creator=friend, friend=creator).exists(): friendship1 = Friendship.objects.get(creator=friend, friend=creator) friendship1.status = 'accepted' friendship2 = Friendship(creator=creator, friend=friend, status='accepted') friendship1.save() notification1 = Notification( user=friend, user2=creator, notification_type="friend-accept", date=datetime.date.today(), time=datetime.datetime.now().strftime('%H:%M:%S'), notification="You are now friends with %s" % creator.username) notification2 = Notification( user=creator, user2=friend, notification_type="friend-accept", date=datetime.date.today(), time=datetime.datetime.now().strftime('%H:%M:%S'), notification="You are now friends with %s" % friend.username) notification1.save() notification2.save() else: friendship2 = Friendship(creator=creator, friend=friend) notification = Notification( user=friend, user2=creator, notification_type="friend-request", date=datetime.date.today(), time=datetime.datetime.now().strftime('%H:%M:%S'), notification="You have a friend request from %s" % creator.username) notification.save() friendship2.save() sent = True context_dict = {'sent': sent} return redirect('/friends/')
def setUp(self): self.user1 = User.objects.get(pk=1) self.user2 = User.objects.get(pk=2) friendship = Friendship(from_user=self.user1, to_user=self.user2) friendship.save()