Beispiel #1
0
    def submit(self, request):
        try:
            received = ReceivedRequests.getByID(request.user.pk)
        except CassaNotFoundException:
            received = ReceivedRequests(user_id=request.user.pk, requests={})

        return ReceivedRequestsSerializer(received).data
Beispiel #2
0
    def submit(self, request):
        user_id = self.cleaned_data['user_id']

        # check to see if the two users are already friends
        try:
            friends = Friends.getByID(request.user.pk)
            if user_id in friends.friends_list:
                raise AlreadyFriendsException()
        except CassaNotFoundException:
            pass #then they are definitely not friends


        try:
            received = ReceivedRequests.getByID(user_id)
        except CassaNotFoundException:
            received = ReceivedRequests(user_id=user_id, requests={})
            
        now = datetime.datetime.now().isoformat()
        received.requests[request.user.pk] = now
        received.save()
        
        try:
            sent = SentRequests.getByID(request.user.pk)
        except CassaNotFoundException:
            sent = SentRequests(user_id=request.user.pk, requests={})
        
        sent.requests[user_id] = now
        sent.save()
        
        return SentRequestsSerializer(sent).data
Beispiel #3
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
Beispiel #4
0
    def submit(self, request):
        user_id = self.cleaned_data['user_id']
        
        try:
            received_requests = ReceivedRequests.getByID(user_id)
            del received_requests.requests[request.user.pk]
            received_requests.save()
        except CassaNotFoundException:
            pass
            
        try:
            sent_requests = SentRequests.getByID(request.user.pk)
            del sent_requests.requests[user_id]
            sent_requests.save()
        except CassaNotFoundException:
            sent_requests = SentRequests(user_id=request.user.pk)

        return SentRequestsSerializer(sent_requests).data