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)
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)
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)