Exemple #1
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")
Exemple #2
0
def get_sales_query():
    """ get sales model query.
    """

    marketplaceModel = marketplace.get_marketplace()

    return SaleModel.query(ancestor=marketplaceModel.key)
Exemple #3
0
def delete(id):
    """ Remove customer by id.
    """

    # Get marketplace
    marketplaceModel = marketplace.get_marketplace()

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

    # Handle if not exists
    if customerKey is None:
        raise NotFoundEntityException(message='messages.customer.notfound')

    # Are there sales with this customer,
    # if true, is not possible to delete
    from app.sale import models as sales
    if sales.has_sales_by_customer(customerKey) == True:
        raise IntegrityViolationException(
            message='messages.customer.salesintegrityviolation')

    logging.debug("Check constraint validation OK")

    # Remove from datastore
    customerKey.delete()

    logging.debug("Customer id %s removed success!", id)

    # Update index
    remove_index(id)

    logging.debug("Index updated to customer id %s", id)
Exemple #4
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")
Exemple #5
0
    def get_query(self):

        # Get parent marketplace
        marketplaceModel = marketplace.get_marketplace()

        # Get query
        return self.query(ancestor=marketplaceModel.key)
Exemple #6
0
def get_sales_query():
    """ get sales model query.
    """

    marketplaceModel = marketplace.get_marketplace()

    return SaleModel.query(ancestor=marketplaceModel.key)
Exemple #7
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
Exemple #8
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
Exemple #9
0
def list():
    """ List all products.
    """
    marketplaceModel = marketplace.get_marketplace()

    products = ProductModel.query(ancestor=marketplaceModel.key).order(
        ProductModel.name).fetch()

    return products
Exemple #10
0
def list():
    """ List all products.
    """
    marketplaceModel = marketplace.get_marketplace()

    products = ProductModel.query(ancestor=marketplaceModel.key).order(
        ProductModel.name).fetch()

    return products
Exemple #11
0
def get_query_purchase():
    """ Get purchase model query.
    """

    marketplaceModel = marketplace.get_marketplace()

    purchasesQuery = PurchaseModel.query(ancestor=marketplaceModel.key)

    return purchasesQuery
Exemple #12
0
def get_query_purchase():
    """ Get purchase model query.
    """

    marketplaceModel = marketplace.get_marketplace()

    purchasesQuery = PurchaseModel.query(ancestor=marketplaceModel.key)

    return purchasesQuery
Exemple #13
0
def get(id):
    """ Select supplier by id.
    """

    marketplaceModel = marketplace.get_marketplace()

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

    return supplier
Exemple #14
0
def get(id):
    """ Select supplier by id.
    """

    marketplaceModel = marketplace.get_marketplace()

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

    return supplier
Exemple #15
0
def list():
    """ List suppliers.
    """

    marketplaceModel = marketplace.get_marketplace()

    suppliers = SupplierModel.query(ancestor=marketplaceModel.key).order(
        SupplierModel.name).fetch()

    return suppliers
Exemple #16
0
def list():
    """ List suppliers.
    """

    marketplaceModel = marketplace.get_marketplace()

    suppliers = SupplierModel.query(ancestor=marketplaceModel.key).order(
        SupplierModel.name).fetch()

    return suppliers
Exemple #17
0
def get_customer_query():
    """ Get customer model query.
    """

    # Get user marketplace
    marketplaceModel = marketplace.get_marketplace()

    # Get query, notice marketplace as parent
    query = CustomerModel.query(ancestor=marketplaceModel.key)

    # Return query
    return query
Exemple #18
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()
Exemple #19
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
Exemple #20
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()
Exemple #21
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
Exemple #22
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
Exemple #23
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
Exemple #24
0
def get(id):
    """ Get customer by its id.
    """

    # Get marketplace
    marketplaceModel = marketplace.get_marketplace()

    # Get customer by id, notice marketplace as parent
    customer = ndb.Key('CustomerModel', int(
        id), parent=marketplaceModel.key).get()

    # Return customer
    return customer
Exemple #25
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()
Exemple #26
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()
Exemple #27
0
def save(supplier):
    """Inclui ou atualiza um fornecedor.
    """

    # Get parent
    marketplaceModel = marketplace.get_marketplace()

    logging.debug("Get user marketplace")

    if supplier.id is not None:

        # Create supplier with id
        supplierModel = SupplierModel(
            id=int(supplier.id), parent=marketplaceModel.key)

    else:

        # Create supplier with random unique id
        supplierModel = SupplierModel(parent=marketplaceModel.key)

    logging.debug("Supplier model created")

    # Set attributes
    supplierModel.name = supplier.name
    supplierModel.email = supplier.email
    supplierModel.phone = supplier.phone
    supplierModel.location = supplier.location

    logging.debug("Set attributes ok")

    # Perstite it
    supplierModel.put()

    logging.debug("Supplier %d persisted successfully",
                  supplierModel.key.id())

    # Update index
    update_index(supplierModel)

    logging.debug("Index updated to supplier %s",
                  supplierModel.key.id())

    # Return it
    return supplierModel
Exemple #28
0
def save(supplier):
    """Inclui ou atualiza um fornecedor.
    """

    # Get parent
    marketplaceModel = marketplace.get_marketplace()

    logging.debug("Get user marketplace")

    if supplier.id is not None:

        # Create supplier with id
        supplierModel = SupplierModel(id=int(supplier.id),
                                      parent=marketplaceModel.key)

    else:

        # Create supplier with random unique id
        supplierModel = SupplierModel(parent=marketplaceModel.key)

    logging.debug("Supplier model created")

    # Set attributes
    supplierModel.name = supplier.name
    supplierModel.email = supplier.email
    supplierModel.phone = supplier.phone
    supplierModel.location = supplier.location

    logging.debug("Set attributes ok")

    # Perstite it
    supplierModel.put()

    logging.debug("Supplier %d persisted successfully", supplierModel.key.id())

    # Update index
    update_index(supplierModel)

    logging.debug("Index updated to supplier %s", supplierModel.key.id())

    # Return it
    return supplierModel
Exemple #29
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")
Exemple #30
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")
Exemple #31
0
def update_stock(item, calculates):
    """ Add product to stock.
        Calculates the average product cost avaliable in stock.
    """

    # Get query
    query = StockItemModel().get_query()

    # Filter by product
    stock = query.filter(StockItemModel.product == item.product).get()

    # If it does not exists create new one...
    if stock is None:

        # Get market place parent
        marketplaceModel = marketplace.get_marketplace()

        # Create stock item
        stock = StockItemModel(parent=marketplaceModel.key,
                               product=item.product)

    # Calculate the quantity
    stock.quantity = stock.quantity + calculates(item.quantity)

    # Stock can't be less then zero
    if stock.quantity < 0:
        raise IllegalStateException(
            message='messages.stockitem.quantitynotlesszero')

    # Update datastore
    stock.put()

    # Add stock log
    stockLog = StockLogModel(parent=stock.key,
                             product=item.product,
                             quantity=calculates(item.quantity))

    # Update datastore
    stockLog.put()
Exemple #32
0
def save(product):
    """ Add or update product.
    """

    # Get parent
    marketplaceModel = marketplace.get_marketplace()

    logging.debug("Get user marketplace")

    if product.id is not None:

        # Create product with id
        productModel = ProductModel(id=int(product.id),
                                    parent=marketplaceModel.key)
    else:

        # Create product with random unique id
        productModel = ProductModel(parent=marketplaceModel.key)

    logging.debug("Created product model")

    # Set attributes
    productModel.code = product.code
    productModel.name = product.name

    logging.debug("Set attributes ok")

    # Perstite it
    productModel.put()

    logging.debug("Product model put successfully")

    # Update index
    update_index(productModel)

    logging.debug("Product model index update successfully")

    # Return product
    return productModel
Exemple #33
0
def save(customer):
    """ Add or update a customer in datastore.
    """

    # Get marketplace
    marketplaceModel = marketplace.get_marketplace()

    logging.debug("Get user marketplace")

    # Get customer model if exists
    # or instantiate one, instead.
    if customer.id is not None:
        customerModel = CustomerModel(id=int(customer.id),
                                      parent=marketplaceModel.key)
    else:
        customerModel = CustomerModel(parent=marketplaceModel.key)

    logging.debug("Customer model created")

    # Pass values
    customerModel.name = customer.name
    customerModel.email = customer.email
    customerModel.phone = customer.phone
    customerModel.location = customer.location

    # Persist ir
    customerModel.put()

    logging.debug("Customer id %d saved success to %s",
                  customerModel.key.id(), marketplaceModel.name)

    # Update index
    update_index(customerModel)

    logging.debug("Index updated to customer id %s",
                  customerModel.key.id())

    # Return
    return customerModel
Exemple #34
0
def save(product):
    """ Add or update product.
    """

    # Get parent
    marketplaceModel = marketplace.get_marketplace()

    logging.debug("Get user marketplace")

    if product.id is not None:

        # Create product with id
        productModel = ProductModel(
            id=int(product.id), parent=marketplaceModel.key)
    else:

        # Create product with random unique id
        productModel = ProductModel(parent=marketplaceModel.key)

    logging.debug("Created product model")

    # Set attributes
    productModel.code = product.code
    productModel.name = product.name

    logging.debug("Set attributes ok")

    # Perstite it
    productModel.put()

    logging.debug("Product model put successfully")

    # Update index
    update_index(productModel)

    logging.debug("Product model index update successfully")

    # Return product
    return productModel
Exemple #35
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
Exemple #36
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