예제 #1
0
 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)
예제 #2
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)
예제 #3
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)
예제 #4
0
    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)