示例#1
0
def save(sale):
    """ Add or remove a sale.
    """

    # Get parent
    marketplaceModel = marketplace.get_marketplace()

    logging.debug("Get user marketplace")

    if sale.id is not None:

        # Create sale with id
        saleModel = SaleModel(id=int(sale.id), parent=marketplaceModel.key)

    else:

        # Create sale with random unique id
        saleModel = SaleModel(parent=marketplaceModel.key)

    logging.debug("Sale model created")

    # Get product
    productModel = ndb.Key('ProductModel',
                           int(sale.product.id),
                           parent=marketplaceModel.key)

    if productModel is None:
        raise NotFoundEntityException("messages.product.notfound")

    logging.debug("Get product child entity ok")

    # Get customer
    customerModel = ndb.Key('CustomerModel',
                            int(sale.customer.id),
                            parent=marketplaceModel.key)

    if customerModel is None:
        raise NotFoundEntityException("messages.customer.notfound")

    logging.debug("Get customer child entity ok")

    # Set attributes
    saleModel.product = productModel
    saleModel.customer = customerModel
    saleModel.quantity = sale.quantity
    saleModel.sale_date = sale.sale_date
    saleModel.amount = sale.amount
    saleModel.fare = sale.fare
    saleModel.net_total = sale.net_total
    saleModel.track_code = sale.track_code

    # Persiste it
    saleModel.put()

    logging.debug("Sale %d registered successfully!", saleModel.key.id())

    # Return it
    return saleModel
示例#2
0
def delete(id):
    """ Remove supplier by id.
    """

    logging.debug("Removing supplier %d", id)

    marketplaceModel = marketplace.get_marketplace()

    supplierKey = ndb.Key('SupplierModel',
                          int(id),
                          parent=marketplaceModel.key)

    if supplierKey is None:
        raise NotFoundEntityException(message='messages.supplier.notfound')

    logging.debug("Supplier %d found it!", id)

    # Are there purchases um this supplier,
    # if true, is not possible to delete
    from app.purchase import models as purchase
    if purchase.has_purchases_by_supplier(supplierKey) == True:
        raise IntegrityViolationException(
            message='messages.supplier.purchasesintegrityviolation')

    logging.debug("Check constraint validation OK")

    # Delete supplier
    supplierKey.delete()

    logging.debug("Supplier removed successfully")

    remove_index(id)

    logging.debug("Supplier index updated successfully")
示例#3
0
def get_current_user():

    current_user = endpoints.get_current_user()

    if current_user is None:
        logging.error('Current user is None from email')
        raise NotFoundEntityException('user.notfound')

    logging.debug('User %s retrieved succesful!', current_user.email())

    return current_user
示例#4
0
def get(id):
    """ Get sale by id.
    """

    marketplaceModel = marketplace.get_marketplace()

    sale = ndb.Key('SaleModel', int(id), parent=marketplaceModel.key).get()

    if sale is None:
        raise NotFoundEntityException("messages.sale.notfound")

    return customer
示例#5
0
def delete(id):
    """ Remove a sale.
    """

    marketplaceModel = marketplace.get_marketplace()

    sale = ndb.Key('SaleModel', int(id), parent=marketplaceModel.key).get()

    if sale is None:
        raise NotFoundEntityException("messages.sales.notfound")

    sale.key.delete()
示例#6
0
def get(id):
    """ Get product by id.
    """

    marketplaceModel = marketplace.get_marketplace()

    product = ndb.Key('ProductModel', int(id),
                      parent=marketplaceModel.key).get()

    if product is None:
        raise NotFoundEntityException(message='messages.product.notfound')

    return product
示例#7
0
def delete(id):
    """ Remove purchase.
    """

    marketplaceModel = marketplace.get_marketplace()

    purchase = ndb.Key('PurchaseModel', int(id),
                       parent=marketplaceModel.key).get()

    if purchase is None:
        raise NotFoundEntityException("messages.purchase.notfound")

    # Update stock
    stock.remove_item_from_stock(purchase)

    # Delete from datastore
    purchase.key.delete()
示例#8
0
def delete(id):
    """ Remove a product.
    """

    logging.debug("Removing product %d", id)

    marketplaceModel = marketplace.get_marketplace()

    productKey = ndb.Key('ProductModel', int(id), parent=marketplaceModel.key)

    if productKey is None:
        raise NotFoundEntityException(message='messages.product.notfound')

    logging.debug("Product %d found it!", id)

    # Are there purchases this product,
    # if true, is not possible to delete
    from app.purchase import models as purchase
    if purchase.has_purchases_by_product(productKey) == True:
        raise IntegrityViolationException(
            message='messages.product.purchasesintegrityviolation')

    # Are there purchases this product,
    # if true, is not possible to delete
    from app.sale import models as sale
    if sale.has_sales_by_product(productKey) == True:
        raise IntegrityViolationException(
            message='messages.product.salesintegrityviolation')

    logging.debug("Check constraint validation OK")

    # Delete product
    productKey.delete()

    logging.debug("Product removed successfully")

    # Remove from index
    delete_index(id)

    logging.debug("Produc index updated successfully")
示例#9
0
def save(purchase):
    """ Add or update for purchase.
    """

    # Get parent
    marketplaceModel = marketplace.get_marketplace()

    logging.debug("Get user marketplace")

    if purchase.id is not None:

        # Create purchase with id
        purchaseModel = PurchaseModel(id=int(purchase.id),
                                      parent=marketplaceModel.key)

        try:

            # Reverse in stock
            stock.remove_item_from_stock(purchaseModel)

        except IllegalStateException as error:
            logging.warning("Stock couldn't be reversed by error %s", error)

    else:

        # Create supplier with random unique id
        purchaseModel = PurchaseModel(parent=marketplaceModel.key)

    logging.debug("Purchase model created")

    # Get product
    productKey = ndb.Key('ProductModel',
                         int(purchase.product.id),
                         parent=marketplaceModel.key)

    if productKey is None:
        raise NotFoundEntityException("messages.product.notfound")

    purchaseModel.product = productKey

    logging.debug("Get product child entity ok")

    # Get supplier
    supplierKey = ndb.Key('SupplierModel',
                          int(purchase.supplier.id),
                          parent=marketplaceModel.key)
    if supplierKey is None:
        raise NotFoundEntityException("messages.supplier.notfound")

    purchaseModel.supplier = supplierKey

    logging.debug("Get supplier child entity ok")

    # Set attributes
    purchaseModel.quantity = purchase.quantity
    purchaseModel.purchase_date = purchase.purchase_date
    purchaseModel.received_date = purchase.received_date
    purchaseModel.cost = purchase.cost
    purchaseModel.total_cost = purchase.total_cost
    purchaseModel.exchange_dollar = purchase.exchange_dollar
    purchaseModel.cost_dollar = purchase.cost_dollar
    purchaseModel.total_cost_dollar = purchase.total_cost_dollar
    purchaseModel.shipping_cost = purchase.shipping_cost
    purchaseModel.track_code = purchase.track_code
    purchaseModel.invoice = purchase.invoice
    purchaseModel.payment_date = purchase.payment_date
    purchaseModel.purchase_link = purchase.purchase_link

    # Persiste it
    purchaseModel.put()

    logging.debug("Purchase %d persisted successfully", purchaseModel.key.id())

    # Update stock
    stock.add_item_to_stock(purchaseModel)

    logging.debug("Stock updated successfully")

    # Return it
    return purchaseModel