예제 #1
0
 def submit(self, request):
     user_id = self.cleaned_data['user_id']
     
     # get my received requests
     try:
         received_requests = ReceivedRequests.getByID(request.user.pk)
     except CassaNotFoundException:
         received_requests = ReceivedRequests(user_id=request.user.pk, requests={})
         
     try:
         sent_requests = SentRequests.getByID(user_id)
     except CassaNotFoundException:
         sent_requests = SentRequests(user_id=request.user.pk, requests={})
     
     # check to make sure that at least one of them saw the request at some point
     # this will avoid phantom requests
     stop = not sent_requests and not received_requests
     stop2 = (sent_requests.requests and request.user.pk in sent_requests.requests)
     stop2 = stop2 or (received_requests.requests and user_id in sent_requests.requests)
     stop = stop or not stop2
         
     if stop:
         raise RequestNotFoundException()
         
     
     # the user accepted the friend request
     # so we create a friendship then delete the request
     try:
         other_friends = Friends.getByID(user_id)
     except CassaNotFoundException:
         other_friends = Friends(user_id=user_id, friends_list={})
     try:
         my_friends = Friends.getByID(request.user.pk)
     except CassaNotFoundException:
         my_friends = Friends(user_id=request.user.pk, friends_list={})
         
     now = datetime.datetime.now().isoformat()
     other_friends.friends_list[request.user.pk] = now
     my_friends.friends_list[user_id] = now
     
     other_friends.save()
     my_friends.save()
     
     del received_requests.requests[user_id]
     del sent_requests.requests[request.user.pk]
     
     received_requests.save()
     sent_requests.save()
     
     return FriendsSerializer(my_friends).data
예제 #2
0
 def submit(self, request):
     
     try:
         ans = Friends.getByID(request.user.pk)
     except CassaNotFoundException:
         ans = Friends(user_id=request.user.pk, friends_list={})
     return FriendsSerializer(ans).data
예제 #3
0
    def submit(self, request):
        try:
            ans = Friends.getByID(request.user.pk)
            if not ans:
                raise FriendNotFoundException()
        except CassaNotFoundException:
            raise FriendNotFoundException()

        del ans.friends_list[self.cleaned_data['user_id']]
        ans.save()

        return UserClient(request.auth).get(self.cleaned_data['user_id'])