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)
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)
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)
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)
def get(self, request, pk, format=None): shopper = Shopper.objects.filter(user=request.user).first() shopping_list = self.get_shopping_list(pk) if shopper != shopping_list.owner: raise ValueError('Shopper is not the owner of this list') serializer = ShoppingListSerializer(shopping_list) return Response(serializer.data)
def get(self, request, format=None): message = json.loads(request.data['message']) shopper = Shopper.objects.filter(user=request.user).first() if message['endpoint'].lower() == 'all lists': serializer = ShoppingListSerializer(shopper.shopping_lists, many=True) elif message['endpoint'].lower() == 'list': guid = message['guid'] try: shopping_list = ShoppingList.get_by_guid(guid, shopper=shopper) except MultipleObjectsReturned: return Response('Provided guid matched multiple lists', status=status.HTTP_409_CONFLICT) except ShoppingList.DoesNotExist: return Response('Provided guid did not match any lists', status=status.HTTP_404_NOT_FOUND) if shopping_list in shopper.shopping_lists.all(): serializer = ShoppingListSerializer(shopping_list) elif message['endpoint'].lower() == 'list items': guid = message['guid'] try: shopping_list = ShoppingList.get_by_guid(guid, shopper=shopper) except MultipleObjectsReturned: return Response('Provided guid matched multiple lists', status=status.HTTP_409_CONFLICT) except ShoppingList.DoesNotExist: return Response('Provided guid did not match any lists', status=status.HTTP_404_NOT_FOUND) if shopping_list in shopper.shopping_lists.all(): serializer = ItemSerializer( shopping_list.items.filter(_done=False), many=True) elif message['endpoint'].lower() == 'item': guid = message['guid'] try: item = Item.get_by_guid(guid, shopper=shopper) except MultipleObjectsReturned: return Response('Provided guid matched multiple items', status=status.HTTP_409_CONFLICT) except Item.DoesNotExist: return Response('Provided guid did not match any items', status=status.HTTP_404_NOT_FOUND) serializer = ItemSerializer(item) elif message['endpoint'].lower() == 'completed': serializer = ItemSerializer( [x for x in Item.recentlyCompletedByShopper(shopper)], many=True) elif message['endpoint'].lower() == 'list all items': guid = message['guid'] try: shopping_list = ShoppingList.get_by_guid(guid, shopper=shopper) except MultipleObjectsReturned: return Response('Provided guid matched multiple lists', status=status.HTTP_409_CONFLICT) except ShoppingList.DoesNotExist: return Response('Provided guid did not match any lists', status=status.HTTP_404_NOT_FOUND) if shopping_list in shopper.shopping_lists.all(): serializer = ItemSerializer([ x for x in shopping_list.items.all() if not x.done or x.recentlyCompleted() ], many=True) return Response(serializer.data)