예제 #1
0
    def put(self, request, pk, format=None):
        modified_category = modified_done = modified_comments = False
        item = self.get_item(pk, request.user)
        request_data = queryDictToDict(request.data)
        request_data['category_id'] = request_data.get('category_id') or None
        serializer = ItemSerializer(item, data=request_data)
        if serializer.is_valid():
            if ('done' in serializer.validated_data and
                    serializer.validated_data.get('done') != item.done):
                na = NotifyAction()
                na.shopper = Shopper.objects.filter(user=request.user).first()
                na.shopping_list = item.shopping_list
                na.item = item
                if serializer.validated_data.get('done'):
                    template = '{item_name} has been completed by {username}'
                else:
                    template = '{item_name} has been uncompleted by {username}'
                na.action = template.format(
                                item_name=item.name,
                                username=na.shopper.username)
                na.save()

                modified_done = True

            if ('comments' in serializer.validated_data and
                    serializer.validated_data.get('comments') != item.comments):
                modified_comments = True

            if ('category_id' in serializer.validated_data and
                    serializer.validated_data.get('category_id') != item.category_id):
                modified_category = True
            serializer.save()

            data = {'list_id': item.shopping_list.id,
                    'category_id': item.category and item.category.id or '',
                    'category_name': item.category and item.category.name or 'None',
                    'checked': item.done,
                    'comments': item.comments,
                    'name': item.name,
                    'modified_done': modified_done,
                    'modified_comments': modified_comments,
                    'modified_category': modified_category}

            shopping_list_members = ShoppingListMember.objects.filter(shopping_list=item.shopping_list).all()
            for shopping_list_member in shopping_list_members:
                socket_tokens = WebSocketToken.objects.filter(shopper=shopping_list_member.shopper).filter(active=True).all()
                for socket_token in socket_tokens:
                    data['socket_token'] = socket_token.guid
                    node_resp = requests.put('%s/item/%s' % (NODE_SERVER,
                                                             item.id), data=data)
                    node_resp.raise_for_status()

            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
    def post(self, request, format=None):
        serializer = ItemSerializer(data=request.data)

        if serializer.is_valid():
            shopping_list = ShoppingList.objects.get(
                pk=serializer.validated_data['shopping_list_id'])
            shopper = Shopper.objects.filter(user=request.user).first()
            if shopping_list not in shopper.shopping_lists.all():
                raise PermissionDenied
            serializer.save()

            item = serializer.instance
            na = NotifyAction()
            na.shopper = shopper
            na.shopping_list = item.shopping_list
            na.item = item
            template = '{item_name} has been added to {shopping_list} by {username}'
            na.action = template.format(item_name=item.name,
                                        username=na.shopper.username,
                                        shopping_list=item.shopping_list.name)
            na.save()

            data = {
                'item_id': item.id,
                'category_id': item.category and item.category.id or '',
                'category_name': item.category and item.category.name
                or 'None',
                'list_id': item.shopping_list.id,
                'name': item.name,
                'comments': item.comments
            }

            shopping_list_members = ShoppingListMember.objects.filter(
                shopping_list=item.shopping_list).all()
            for shopping_list_member in shopping_list_members:
                socket_tokens = WebSocketToken.objects.filter(
                    shopper=shopping_list_member.shopper).filter(
                        active=True).all()
                for socket_token in socket_tokens:
                    data['socket_token'] = socket_token.guid
                    node_resp = requests.post('%s/item' % INTERNAL_NODE_SERVER,
                                              data=data)
                    node_resp.raise_for_status()

            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, format=None):
        message = json.loads(request.data['message'])
        shopper = Shopper.objects.filter(user=request.user).first()

        guid = message['guid']

        try:
            item = Item.get_by_guid(guid, shopper=shopper)
            if message['endpoint'].lower() == 'complete':
                item.done = True
            elif message['endpoint'].lower() == 'uncomplete':
                item.done = False
            elif message['endpoint'].lower() == 'modify':
                if message['delete'] and message['comments']:
                    return Response('Cannot both delete and set comments',
                                    status=status.HTTP_400_BAD_REQUEST)
                comment = message['comments']
                if message['append'] and item.comments:
                    comment = '\n'.join([item.comments, comment])
                elif message['delete']:
                    comment = ''
                item.comments = comment
            elif message['endpoint'].lower() == 'move':
                try:
                    to_list = ShoppingList.get_by_guid(message['to_list_guid'],
                                                       shopper=shopper)
                except MultipleObjectsReturned:
                    return Response(
                        'Provided to_list_guid matched multiple lists',
                        status=status.HTTP_409_CONFLICT)
                except ShoppingList.DoesNotExist:
                    return Response(
                        'Provided to_list_guid did not match any lists',
                        status=status.HTTP_404_NOT_FOUND)
                item.move(to_list, shopper)
            elif message['endpoint'].lower() == 'categorize':
                try:
                    category = ShoppingListCategory.get_shopping_list_category_by_name(
                        message['category_name'], item.shopping_list)
                except ShoppingListCategory.DoesNotExist:
                    return Response('Could not find provided category %s' %
                                    (message['category_name'], ),
                                    status=status.HTTP_404_NOT_FOUND)

                item.category = category
            item.save()
        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)
        return Response(serializer.data)
예제 #4
0
    def post(self, request, format=None):
        serializer = ItemSerializer(data=request.data)

        if serializer.is_valid():
            shopping_list = ShoppingList.objects.get(pk=serializer.validated_data['shopping_list_id'])
            shopper = Shopper.objects.filter(user=request.user).first()
            if shopping_list not in shopper.shopping_lists.all():
                raise PermissionDenied
            serializer.save()

            item = serializer.instance
            na = NotifyAction()
            na.shopper = shopper
            na.shopping_list = item.shopping_list
            na.item = item
            template = '{item_name} has been added to {shopping_list} by {username}'
            na.action = template.format(
                            item_name=item.name,
                            username=na.shopper.username,
                            shopping_list=item.shopping_list.name)
            na.save()

            data = {'item_id': item.id,
                    'category_id': item.category and item.category.id or '',
                    'category_name': item.category and item.category.name or 'None',
                    'list_id': item.shopping_list.id,
                    'name': item.name,
                    'comments': item.comments}

            shopping_list_members = ShoppingListMember.objects.filter(shopping_list=item.shopping_list).all()
            for shopping_list_member in shopping_list_members:
                socket_tokens = WebSocketToken.objects.filter(shopper=shopping_list_member.shopper).filter(active=True).all()
                for socket_token in socket_tokens:
                    data['socket_token'] = socket_token.guid
                    node_resp = requests.post('%s/item' % NODE_SERVER, data=data)
                    node_resp.raise_for_status()

            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #5
0
    def post(self, request):
        message = json.loads(request.data['message'])
        shopper = Shopper.objects.filter(user=request.user).first()

        if message['endpoint'].lower() == 'add item':
            guid = message['guid']
            name = message['name']
            comments = message.get('comments', '')

            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)

            item = Item.new(name, shopping_list, comments=comments)
            item.save()

            serializer = ItemSerializer(item)
            return Response(serializer.data)
예제 #6
0
    def put(self, request, pk, format=None):
        modified_category = modified_done = modified_comments = False
        item = self.get_item(pk, request.user)
        request_data = queryDictToDict(request.data)
        request_data['category_id'] = request_data.get('category_id') or None
        serializer = ItemSerializer(item, data=request_data)
        if serializer.is_valid():
            if ('done' in serializer.validated_data
                    and serializer.validated_data.get('done') != item.done):
                na = NotifyAction()
                na.shopper = Shopper.objects.filter(user=request.user).first()
                na.shopping_list = item.shopping_list
                na.item = item
                if serializer.validated_data.get('done'):
                    template = '{item_name} has been completed by {username}'
                else:
                    template = '{item_name} has been uncompleted by {username}'
                na.action = template.format(item_name=item.name,
                                            username=na.shopper.username)
                na.save()

                modified_done = True

            if ('comments' in serializer.validated_data
                    and serializer.validated_data.get('comments') !=
                    item.comments):
                modified_comments = True

            if ('category_id' in serializer.validated_data
                    and serializer.validated_data.get('category_id') !=
                    item.category_id):
                modified_category = True
            serializer.save()

            data = {
                'list_id': item.shopping_list.id,
                'category_id': item.category and item.category.id or '',
                'category_name': item.category and item.category.name
                or 'None',
                'checked': item.done,
                'comments': item.comments,
                'name': item.name,
                'modified_done': modified_done,
                'modified_comments': modified_comments,
                'modified_category': modified_category
            }

            shopping_list_members = ShoppingListMember.objects.filter(
                shopping_list=item.shopping_list).all()
            for shopping_list_member in shopping_list_members:
                socket_tokens = WebSocketToken.objects.filter(
                    shopper=shopping_list_member.shopper).filter(
                        active=True).all()
                for socket_token in socket_tokens:
                    data['socket_token'] = socket_token.guid
                    node_resp = requests.put('%s/item/%s' %
                                             (INTERNAL_NODE_SERVER, item.id),
                                             data=data)
                    node_resp.raise_for_status()

            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #7
0
 def get(self, request, pk, format=None):
     item = self.get_item(pk, request.user)
     serializer = ItemSerializer(item)
     return Response(serializer.data)
예제 #8
0
 def get(self, request, pk, format=None):
     items = self.get_items(pk)
     serializer = ItemSerializer(items, many=True)
     return Response(serializer.data)
예제 #9
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)