コード例 #1
0
ファイル: views_review.py プロジェクト: chantysothy/Chew
 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)
コード例 #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)
コード例 #3
0
ファイル: views_review.py プロジェクト: chantysothy/Chew
 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)
コード例 #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)