Example #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)
Example #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)
Example #3
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)
Example #4
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)