def put(self, request, pk, format = None): profile = UserProfiles.objects.get(user_id = request.user.id) project = Projects.objects.get(pk = pk) requestData = request.data.copy() # Make a mutable copy of the request requestData['user_profile'] = profile.id # Set the user field to requesting user requestData['project'] = project.id try: swipe = Swipes.objects.get(user_profile = profile, project = project) serializer = SwipesSerializer(swipe, data = requestData) if serializer.is_valid(): serializer.save() swipe = Swipes.objects.get(user_profile = profile, project = project) if swipe.user_likes == Swipes.YES and swipe.project_likes == Swipes.YES: project_owner_device = project.owner.device # Get Device ID of project owner user_device = profile.device # Get GCM device with requesting user's device ID project_owner_device.send_message( "MatchNotification" + " " + "Project: " + project.project_name + " has found a match!") user_device.send_message("MatchNotification" + " " + "You have found a match!") return Response(serializer.data) return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST) except Swipes.DoesNotExist: serializer = SwipesSerializer(data = requestData) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
def project_swipe( request, **kwargs ): print "hi" #Try to obtain the project and user specified in the URL. project = getProject( kwargs['project'] ) print "got the project" if not isOwner(request, project): return Response(status = status.HTTP_403_FORBIDDEN) #TODO: IMPLEMENT PERMISSION CHECK TO SEE IF REQUESTING USER OWNS THE PROJECT user = getUser( kwargs['user'] ) print "got thet user" #Attempt to update the swipe in the Swipes table if it exists try: swipe = Swipes.objects.get(user_profile=user, project=project ) serializer = SwipesSerializer( swipe, data=request.data, partial=True ) #update the swipe if serializer.is_valid() : serializer.save() #Send push notification to both users involved if mutual interest is expressed if swipe.project_likes == Swipes.YES and swipe.user_likes == Swipes.YES : project_owner_device = project.owner.device user_device = user.device project_owner_device.send_message( "MatchNotification" + " " + "Project: " + project.project_name + " has found a match!" ) user_device.send_message("MatchNotification" + " " + "You have found a match!" ) return Response(serializer.data) except Swipes.DoesNotExist: #Create a new entry in swipes table if it doesn't exist #Populate request.data with the project and user for creation of new Swipe requestData = request.data.copy() print "IT DOESN'T EXIST OKAY?" requestData['user_profile'] = user.id print "got the user profile..... AGAIN" requestData['project'] = project.id print "got user and project already..." serializer = SwipesSerializer(data=requestData) if serializer.is_valid(): serializer.save() return Response( serializer.data ) return Response(serializer.errors, status.HTTP_400_BAD_REQUEST ) #If neither an update or create occurred (data was invalid)