Exemple #1
0
def get_product_by_id(items, id):
    '''
    Return a supplier by id
    '''

    try:
        return items.get(product_id=id)

    except NotUniqueError:
        sentry.captureException()

        return resp_already_exists('Carts', 'carrinho')

    except DoesNotExist:
        return None

    except ValidationError as e:
        sentry.captureException()

        return resp_exception('Carts', msg=_MSG300, description=e)

    except MultipleObjectsReturned as e:
        sentry.captureException()

        return resp_exception('Carts', msg=_MSG300, description=e)

    except Exception as e:
        sentry.captureException()

        return resp_exception('Carts', description=e)
Exemple #2
0
def create_cart(suppliers=None, products=None):
    '''
    Create cart with suppliers and products data if exists
    '''
    cart = Cart()

    if suppliers is None and products is None:
        return cart.save()

    supplier = create_supplier(suppliers)

    if supplier and products:
        try:
            product = create_product(products)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        subtotal, total = calculate_product_price(product)
        product.total = total
        product.subtotal = subtotal

        supplier.items.append(product)

    try:
        cart.suppliers.append(supplier)

    except Exception as e:
        sentry.captureException()

        return resp_exception('Carts', description=e)

    subtotal, total = calculate_total_and_subtotal(supplier.items)
    supplier.subtotal = subtotal
    supplier.total = total

    subtotal, total = calculate_total_and_subtotal(cart.suppliers)
    cart.subtotal = subtotal
    cart.total = total

    try:
        cart.updated = datetime.now()
        cart.save()

    except NotUniqueError:
        sentry.captureException()

        return resp_already_exists('Carts', 'carrinho')

    except ValidationError as e:
        sentry.captureException()

        return resp_exception('Carts', msg=_MSG300, description=e)

    except Exception as e:
        sentry.captureException()

        return resp_exception('Carts', description=e)

    return cart
    def get(self, cart_id):

        try:
            cart = MCart.objects.get(id=cart_id)

        except NotUniqueError:
            sentry.captureException()

            return resp_already_exists('Carts', 'carrinho')

        except ValidationError as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except MultipleObjectsReturned as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        subtotal, total = calculate_total_and_subtotal(cart.suppliers)
        cart.subtotal = subtotal
        cart.total = total

        try:
            # TODO: Update document with atomic update
            cart.updated = datetime.now()
            cart.save()

        except NotUniqueError:
            sentry.captureException()

            return resp_already_exists('Carts', 'carrinho')

        except ValidationError as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        # TODO: Serializer all objects
        data = cart_detail(cart)

        for supplier in cart.suppliers:
            data['suppliers'].append(supplier_detail(supplier))

        return {
            'status': 200,
            'resource': 'Cart',
            'message': _MSG104,
            'data': data
        }, 200, {
            'Set-Cookie': 'api-shopping-cart={}'.format(cart.id)
        }
    def put(self, cart_id):

        try:
            cart = MCart.objects.get(id=cart_id)

        except NotUniqueError:
            sentry.captureException()

            return resp_already_exists('Carts', 'carrinho')

        except ValidationError as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except MultipleObjectsReturned as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        # TODO: Verify if user exists in app only admin has this access
        # Force to data is valid because we do not trust on any information
        # comes from client

        # TODO: With cart get product and do an set atomic update
        # http://docs.mongoengine.org/guide/querying.html#atomic-updates
        # Or we can user save method

        # TODO: Serializer all objects
        # get_cart()

        try:
            # TODO: Update document with atomic update
            cart.updated = datetime.now()
            cart.save()

        except NotUniqueError:
            sentry.captureException()

            return resp_already_exists('Carts', 'carrinho')

        except ValidationError as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        data = cart_detail(cart)

        for supplier in cart.suppliers:
            data['suppliers'].append(supplier_detail(supplier))

        return {
            'status': 200,
            'resource': 'Cart',
            'message': _MSG104,
            'data': data
        }, 200, {
            'Set-Cookie': 'api-shopping-cart={}'.format(cart.id)
        }
    def patch(self, cart_id):

        try:
            cart = MCart.objects.get(id=cart_id)

        except NotUniqueError:
            sentry.captureException()

            return resp_already_exists('Carts', 'carrinho')

        except ValidationError as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except MultipleObjectsReturned as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        supplier_data, product_data = None, None
        data = None

        try:
            data = request.get_json()
        except Exception:
            return resp_form_invalid('Carts', {'product': 'not empty'})

        supplier_data = data.get('supplier', None)
        product_data = data.get('product', None)

        if supplier_data:
            supplier = get_supplier_by_id(cart.suppliers,
                                          supplier_data.get('supplier_id'))

            if not isinstance(supplier, Supplier):
                return supplier
        else:
            data = cart_detail(cart)

            for supplier in cart.suppliers:
                data['suppliers'].append(supplier_detail(supplier))

            return {
                'status': 404,
                'resource': 'Cart',
                'message': _MSG104,
                'description': 'Este fornecedor não foi encontrado',
                'data': data
            }, 404, {
                'Set-Cookie': 'api-shopping-cart={}'.format(cart.id)
            }

        # Object Item Document

        if product_data:
            product = get_product_by_id(supplier.items,
                                        product_data.get('product_id'))

            if not isinstance(product, Item):
                return product

        else:
            data = cart_detail(cart)

            for supplier in cart.suppliers:
                data['suppliers'].append(supplier_detail(supplier))

            return {
                'status': 404,
                'resource': 'Cart',
                'message': _MSG104,
                'description': 'Este produto não foi encontrado',
                'data': data
            }, 404, {
                'Set-Cookie': 'api-shopping-cart={}'.format(cart.id)
            }

        # TODO: With cart get product and do an unset atomic update
        # http://docs.mongoengine.org/guide/querying.html#atomic-updates

        # TODO: Recalculate all the product prices

        qty = product_data.get('qty', None)

        if qty:
            product.qty -= qty

            if product.qty <= 0:
                data = cart_detail(cart)

                for supplier in cart.suppliers:
                    data['suppliers'].append(supplier_detail(supplier))

                return {
                    'status': 400,
                    'resource': 'Cart',
                    'message': _MSG104,
                    'description': 'A quantidade não pode ser menor que zero.',
                    'data': data
                }, 400, {
                    'Set-Cookie': 'api-shopping-cart={}'.format(cart.id)
                }

            # TODO: Recalculate all the product prices
            subtotal, total = calculate_product_price(product)

            product.total = total
            product.subtotal = subtotal

        # TODO: recalculate the prices on each item
        # The supplier object has total and subtotal. This is the sum of
        # all items

        if supplier:
            subtotal, total = calculate_total_and_subtotal(supplier.items)
            supplier.subtotal = subtotal
            supplier.total = total

        # TODO: recalculate the prices of cart
        # The cart has many suppliers. So is need to save the total and
        # subtotal on each supplier

        subtotal, total = calculate_total_and_subtotal(cart.suppliers)
        cart.subtotal = subtotal
        cart.total = total

        try:
            # TODO: Update document with atomic update
            cart.updated = datetime.now()
            cart.save()

        except NotUniqueError:
            sentry.captureException()

            return resp_already_exists('Carts', 'carrinho')

        except ValidationError as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        # TODO: Serializer all objects
        data = cart_detail(cart)

        for supplier in cart.suppliers:
            data['suppliers'].append(supplier_detail(supplier))

        return {
            'status': 200,
            'resource': 'Cart',
            'message': _MSG104,
            'data': data
        }, 200, {
            'Set-Cookie': 'api-shopping-cart={}'.format(cart.id)
        }
    def put(self, cart_id):
        try:
            cart = MCart.objects.get(id=cart_id)

        except NotUniqueError:
            sentry.captureException()

            return resp_already_exists('Carts', 'carrinho')

        except ValidationError as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except MultipleObjectsReturned as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        # Serializer payload request

        supplier_payload, supplier_data = None, None
        product_payload, product_data = None, None
        supplier, product = None, None
        data = None

        try:
            data = request.get_json()
        except Exception:
            return resp_form_invalid('Carts', {'product': 'not empty'})

        if data:
            supplier_data = data.get('supplier', None)
            product_data = data.get('product', None)

        if supplier_data:
            supplier_form = SupplierForm(data=supplier_data)

            if not supplier_form.validate():
                return resp_form_invalid('Carts', supplier_form.errors)

            supplier_payload = supplier_form.data

        if product_data:
            item_form = ItemForm(data=product_data)

            if not item_form.validate():
                return resp_form_invalid('Carts', item_form.errors)

            product_payload = item_form.data

        # Object Supplier EmbbedDocument if not exists

        supplier = get_supplier_by_id(cart.suppliers,
                                      supplier_payload.get('supplier_id'))

        if supplier is None:
            # Create a supplier instance
            supplier = create_supplier(supplier_payload)
            # Add on suppliers list
            try:
                # TODO: Update document with atomic update
                cart.suppliers.append(supplier)

            except Exception as e:
                sentry.captureException()

                return resp_exception('Carts', description=e)

        elif not isinstance(supplier, Supplier):
            return supplier

        # Object Item Document

        product = get_product_by_id(supplier.items,
                                    product_payload.get('product_id'))

        if product is None:
            # Create product instance
            product = create_product(product_payload)
            # Add on product list
            try:
                # TODO: Verify if the product has in stock

                # TODO: Update document with atomic update
                supplier.items.append(product)

            except Exception as e:
                sentry.captureException()

                return resp_exception('Carts', description=e)

        elif not isinstance(product, Item):
            return product
        elif isinstance(product, Item):
            # TODO: Verify if the product has in stock

            # TODO: Update qty of the product or return an error
            product.qty += product_payload.get('qty')

        # TODO: Recalculate all the product prices

        subtotal, total = calculate_product_price(product)

        product.total = total
        product.subtotal = subtotal

        # TODO: recalculate the prices on each item
        # The supplier object has total and subtotal. This is the sum of
        # all items

        subtotal, total = calculate_total_and_subtotal(supplier.items)
        supplier.subtotal = subtotal
        supplier.total = total

        # TODO: recalculate the prices of cart
        # The cart has many suppliers. So is need to save the total and
        # subtotal on each supplier

        subtotal, total = calculate_total_and_subtotal(cart.suppliers)
        cart.subtotal = subtotal
        cart.total = total

        try:
            # TODO: Update document with atomic update
            cart.updated = datetime.now()
            cart.save()

        except NotUniqueError:
            sentry.captureException()

            return resp_already_exists('Carts', 'carrinho')

        except ValidationError as e:
            sentry.captureException()

            return resp_exception('Carts', msg=_MSG300, description=e)

        except Exception as e:
            sentry.captureException()

            return resp_exception('Carts', description=e)

        data = cart_detail(cart)

        for supplier in cart.suppliers:
            data['suppliers'].append(supplier_detail(supplier))

        return {
            'status': 200,
            'resource': 'Cart',
            'message': _MSG104,
            'data': data
        }, 200, {
            'Set-Cookie': 'api-shopping-cart={}'.format(cart.id)
        }