Ejemplo n.º 1
0
 def put(self, request, pk, format=None):
     data = queryDictToDict(request.data)
     shopper = self.get_shopper(pk=pk)
     serializer = ShopperSerializer(shopper, data=data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    def post(self, request, format=None):
        data = queryDictToDict(request.data)
        data['shopping_list_id'] = ALEXA_LIST
        data['name'] = data['name'].title()
        serializer = ItemSerializer(data=data)
        if serializer.is_valid():
            serializer.save()

            item = serializer.instance
            na = NotifyAction()
            na.shopper = item.shopping_list.owner
            na.shopping_list = item.shopping_list
            na.item = item
            template = '{item_name} has been added 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,
                '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',
                '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)
Ejemplo n.º 4
0
    def post(self, request, format=None):
        data = queryDictToDict(request.data)
        data['shopping_list_id'] = ALEXA_LIST
        data['name'] = data['name'].title()
        serializer = ItemSerializer(data=data)
        if serializer.is_valid():
            serializer.save()

            item = serializer.instance
            na = NotifyAction()
            na.shopper = item.shopping_list.owner
            na.shopping_list = item.shopping_list
            na.item = item
            template = '{item_name} has been added 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,
                    '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',
                    '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)
Ejemplo n.º 5
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)