Esempio n. 1
0
 def put(self, request, format=None):
   review = self.get_object(pk=request.data['id'])
   user = User.objects.get(username=self.request.data['owner'])
   serializer = ReviewSerializer(review, data=request.data)
   if serializer.is_valid():
     serializer.save(owner=user)
     return Response('Food updated', status.HTTP_200_OK)
   return Response('Couldn\'t update the review', status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
 def put(self, request, format=None):
     review = self.get_object(pk=request.data['id'])
     user = User.objects.get(username=self.request.data['owner'])
     serializer = ReviewSerializer(review, data=request.data)
     if serializer.is_valid():
         serializer.save(owner=user)
         return Response('Food updated', status.HTTP_200_OK)
     return Response('Couldn\'t update the review',
                     status.HTTP_400_BAD_REQUEST)
Esempio n. 3
0
class AccountSerializer(serializers.ModelSerializer):
    # id = serializers.IntegerField()
    user = UserSerializer()
    food_favorites = FoodSerializer(many=True)
    food_liked = FoodSerializer(many=True)
    food_disliked = FoodSerializer(many=True)
    images_liked = ImageSerializer(many=True)
    reviews_liked = ReviewSerializer(many=True)
    reviews_disliked = ReviewSerializer(many=True)

    class Meta:
        model = Account
        fields = [
            'id', 'user', 'food_favorites', 'food_liked', 'food_disliked',
            'images_liked', 'reviews_liked', 'reviews_disliked'
        ]

    def update(self, instance, validated_data):
        user_data = validated_data.pop('user')
        user = User.objects.get(username=user_data['username'])
        user.first_name = user_data['first_name']
        user.last_name = user_data['last_name']
        user.email = user_data['email']
        user.save()
        account = Account.objects.get(id=instance.pk)

        if self.context[0] == 'param':
            return account
        elif self.context[0] == 'images_liked':
            updated_data = validated_data.pop(self.context[0])
            if self.context[1] == 'upvote':
                for data in updated_data:
                    img = data['image']
                    query = Image.objects.get(image=img)
                    if not account.images_liked.filter(pk=query.id).exists():
                        account.images_liked.add(query)
                        Image.objects.filter(image=img).update(
                            votes=F('votes') + 1)
            elif self.context[1] == 'downvote':
                for data in updated_data:
                    img = data['image']
                    query = Image.objects.get(image=img)
                    if account.images_liked.filter(pk=query.id).exists():
                        account.images_liked.remove(query)
                        Image.objects.filter(image=img).update(
                            votes=F('votes') - 1)
        else:
            updated_data = validated_data.pop(self.context[0])
            for data in updated_data:
                food = data['name']
                query = Food.objects.get(name=food)
                if not account.food_liked.filter(pk=query.id).exists():
                    account.food_liked.add(query)
        return account
Esempio n. 4
0
 def post(self, request, format=None):    
   user = User.objects.get(username=self.request.data['owner'])
   food = Food.objects.get(pk=self.request.data['food'])
   serializer = ReviewSerializer(data=request.data)
   if Review.objects.filter(food=food, owner=user).exists():
     return Response('Food already reviewed', status.HTTP_400_BAD_REQUEST)
   else:
     if serializer.is_valid():
       serializer.save(owner=user)
       food.avgRating = (food.avgRating * food.numRating + Decimal(serializer.data['foodRating']))/(food.numRating + 1)
       food.numRating += 1
       food.save()
       return Response('Successfully created review', status.HTTP_201_CREATED)
Esempio n. 5
0
 def post(self, request, format=None):
     user = User.objects.get(username=self.request.data['owner'])
     food = Food.objects.get(pk=self.request.data['food'])
     serializer = ReviewSerializer(data=request.data)
     if Review.objects.filter(food=food, owner=user).exists():
         return Response('Food already reviewed',
                         status.HTTP_400_BAD_REQUEST)
     else:
         if serializer.is_valid():
             serializer.save(owner=user)
             food.avgRating = (food.avgRating * food.numRating + Decimal(
                 serializer.data['foodRating'])) / (food.numRating + 1)
             food.numRating += 1
             food.save()
             return Response('Successfully created review',
                             status.HTTP_201_CREATED)
Esempio n. 6
0
 def get(self, request, food_pk, format=None):
     review = Review.objects.filter(food_id=food_pk)
     serializer = ReviewSerializer(review, many=True)
     return Response(serializer.data)
Esempio n. 7
0
 def get(self, request, format=None):
     reviews = Review.objects.all()
     serializer = ReviewSerializer(reviews, many=True)
     return Response(serializer.data)