示例#1
0
 def post(self, request, format=None):
     shopper = Shopper.objects.filter(user=request.user).first()
     owner_id = request.data.get('owner_id') or shopper.id
     if int(owner_id) != shopper.id:
         raise ValueError('Cannot create shopping list owned by another user')
     serializer = ShoppingListSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例#2
0
 def put(self, request, pk, format=None):
     shopper = Shopper.objects.filter(user=request.user).first()
     if request.data['owner_id'] != shopper.id:
         raise ValueError('Cannot create shopping list owned by another user')
     shopping_list = self.get_shopping_list(pk)
     serializer = ShoppingListSerializer(shopping_list, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例#3
0
 def post(self, request, format=None):
     shopper = Shopper.objects.filter(user=request.user).first()
     owner_id = request.data.get('owner_id') or shopper.id
     if int(owner_id) != shopper.id:
         raise ValueError(
             'Cannot create shopping list owned by another user')
     serializer = ShoppingListSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例#4
0
 def put(self, request, pk, format=None):
     shopper = Shopper.objects.filter(user=request.user).first()
     if request.data['owner_id'] != shopper.id:
         raise ValueError(
             'Cannot modify shopping list owned by another user')
     shopping_list = self.get_shopping_list(pk)
     serializer = ShoppingListSerializer(shopping_list, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)