Ejemplo n.º 1
0
    def get(request, t):
        engine = Engine()
        try:
            user = Utils.get_user_from_auth(request)
            engine.set_user(user.id)
            engine.set_type(t)
            games = engine.get_best_matching()
            data = []
            for g in games.keys():
                game = Game.objects.filter(id=g)[0]
                dat = {
                    "id": game.id,
                    "title": game.title,
                    "description": game.description,
                    "platform": game.platform,
                    "score": games[g],
                    "url": game.url
                }
                data.append(dat)
        except ValueError as e:
            return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
        except TypeError as e:

            return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
        return Response(data, status=status.HTTP_200_OK)
Ejemplo n.º 2
0
 def remove_review(request, pk):
     current_user = Utils.get_user_from_auth(request=request)
     if request.method == 'DELETE':
         try:
             reviews = Reviews.objects.get(user=current_user, game=pk)
             reviews.delete()
         except Reviews.DoesNotExist:
             return Response(status=status.HTTP_404_NOT_FOUND)
         return Response(status=status.HTTP_200_OK)
Ejemplo n.º 3
0
 def get_my_friends(request):
     current_user = Utils.get_user_from_auth(request)
     friends = Friends.objects.filter(
         Q(user_one=current_user.id) | Q(user_two=current_user.id))
     users = []
     for friend in friends:
         if friend.user_two_id == current_user.id:
             id = friend.user_one_id
         else:
             id = friend.user_two_id
         users.append(User.objects.get(id=id))
     serializers = MyFriendsSerilizer(users, many=True)
     return Response(serializers.data, status=status.HTTP_200_OK)
Ejemplo n.º 4
0
 def invite_user(request):
     current_user = Utils.get_user_from_auth(request)
     data = request.data
     data.update({"user": current_user.id})
     serializer = InvitesSerializer(data=data)
     if serializer.is_valid():
         init_data = int(serializer.initial_data["user_two"])
         flag = False
         try:
             User.objects.get(id=init_data)
         except User.DoesNotExist:
             return Response("Cannot invite non existing user",
                             status=status.HTTP_400_BAD_REQUEST)
         if current_user.id == init_data:
             return Response("Cannot send invite to yourself",
                             status=status.HTTP_400_BAD_REQUEST)
         try:
             friends = Friends.objects.get(user_one_id=current_user.id,
                                           user_two_id=init_data)
             return Response("Cannot invite user. Already friend",
                             status=status.HTTP_400_BAD_REQUEST)
         except Friends.DoesNotExist:
             pass
         try:
             friends = Friends.objects.get(user_one_id=init_data,
                                           user_two_id=current_user.id)
             return Response("Cannot invite user. Already friend",
                             status=status.HTTP_400_BAD_REQUEST)
         except Friends.DoesNotExist:
             pass
         invitation = FriendsPending(user_one_id=current_user.id,
                                     user_two_id=init_data)
         try:
             FriendsPending.objects.get(user_one_id=current_user,
                                        user_two_id=init_data)
             return Response("Cannot send invite twice",
                             status=status.HTTP_400_BAD_REQUEST)
         except FriendsPending.DoesNotExist:
             pass
         try:
             FriendsPending.objects.get(user_one_id=init_data,
                                        user_two_id=current_user)
             return Response("Cannot send invite twice",
                             status=status.HTTP_400_BAD_REQUEST)
         except FriendsPending.DoesNotExist:
             pass
         invitation.save()
         return Response(serializer.initial_data,
                         status=status.HTTP_202_ACCEPTED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
 def accept_invite(request, pk):
     current_user = Utils.get_user_from_auth(request)
     try:
         invite = FriendsPendingController.get_object(
             pk=pk, current_id=current_user.id)
     except FriendsPending.DoesNotExist:
         return Response("Invitation does not exist",
                         status=status.HTTP_404_NOT_FOUND)
     except IndexError:
         return Response("Invitation does not exist",
                         status=status.HTTP_404_NOT_FOUND)
     friends = Friends(user_one_id=invite.user_one_id,
                       user_two_id=invite.user_two_id)
     try:
         friends.save()
         invite.delete()
     except ValueError:
         return Response(status=status.HTTP_400_BAD_REQUEST)
     return Response(status=status.HTTP_200_OK)
Ejemplo n.º 6
0
 def post_game_review(request):
     current_user = Utils.get_user_from_auth(request=request)
     if request.method == 'POST':
         rev = Reviews.objects.filter(game=request.data['game'],
                                      user=current_user)
         if len(rev) > 0:
             return Response(
                 {'message': 'Cannot review a game multiple times'},
                 status=status.HTTP_409_CONFLICT)
         data = request.data
         data.update({"user": current_user.id})
         serializer = ReviewsSerializer(data=data)
         if serializer.is_valid():
             review = Reviews(user=current_user,
                              game_id=serializer.data["game"],
                              review=serializer.data["review"])
             review.save()
             return Response("Review saved successfully",
                             status=status.HTTP_202_ACCEPTED)
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 7
0
 def get(request):
     current_user = Utils.get_user_from_auth(request)
     if request.method == 'GET':
         libs = GameLib.objects.filter(user=current_user.id)
         serializer = GameLibSerializer(libs, many=True)
         return Response(serializer.data)
     if request.method == 'POST':
         data = request.data
         data.update({"user": current_user.id})
         serializer = GameLibSerializer(data=data)
         if serializer.is_valid():
             user_game = GameLib.objects.filter(user=current_user.id,
                                                game=request.data['game'])
             if len(user_game) > 0:
                 return Response("Cannot add twice",
                                 status=status.HTTP_400_BAD_REQUEST)
             serializer.save()
             return Response(serializer.data,
                             status=status.HTTP_202_ACCEPTED)
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 8
0
 def get_put_delete_user(request):
     try:
         user = Utils.get_user_from_auth(request)
     except [ValueError, User.DoesNotExist] as e:
         return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
     if request.method == 'GET':
         serializer = UserSerializer(user)
         return Response(serializer.data)
     if request.method == 'DELETE':
         user.delete()
         return Response(status=status.HTTP_204_NO_CONTENT)
     if request.method == 'PUT':
         data = request.data
         data.update({'is_superuser': False})
         serializer = UserSerializer(user, data=data)
         if serializer.is_valid():
             u = User.objects.get(pk=user.id)
             u.username = serializer.initial_data["username"]
             u.password = serializer.initial_data["password"]
             u.save()
             return Response(request.data, status=status.HTTP_200_OK)
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 9
0
 def get_put_delete_lib(request, pk):
     lib = GameLibController.get_object(pk,
                                        Utils.get_user_from_auth(request))
     if request.method == 'DELETE':
         lib.delete()
         return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 10
0
 def remove_favorite(request, pk):
     fav = FavoritesController.get_object(pk,
                                          Utils.get_user_from_auth(request))
     if request.method == 'DELETE':
         fav.delete()
         return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 11
0
 def get_received(request):
     current_user = Utils.get_user_from_auth(request)
     sent = FriendsPending.objects.filter(user_two=current_user.id)
     serializer = FriendsPendingSerializer(sent, many=True)
     return Response(serializer.data)