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")
def get_sales_query(): """ get sales model query. """ marketplaceModel = marketplace.get_marketplace() return SaleModel.query(ancestor=marketplaceModel.key)
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)
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")
def get_query(self): # Get parent marketplace marketplaceModel = marketplace.get_marketplace() # Get query return self.query(ancestor=marketplaceModel.key)
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
def list(): """ List all products. """ marketplaceModel = marketplace.get_marketplace() products = ProductModel.query(ancestor=marketplaceModel.key).order( ProductModel.name).fetch() return products
def get_query_purchase(): """ Get purchase model query. """ marketplaceModel = marketplace.get_marketplace() purchasesQuery = PurchaseModel.query(ancestor=marketplaceModel.key) return purchasesQuery
def get(id): """ Select supplier by id. """ marketplaceModel = marketplace.get_marketplace() supplier = ndb.Key('SupplierModel', int( id), parent=marketplaceModel.key).get() return supplier
def get(id): """ Select supplier by id. """ marketplaceModel = marketplace.get_marketplace() supplier = ndb.Key('SupplierModel', int(id), parent=marketplaceModel.key).get() return supplier
def list(): """ List suppliers. """ marketplaceModel = marketplace.get_marketplace() suppliers = SupplierModel.query(ancestor=marketplaceModel.key).order( SupplierModel.name).fetch() return suppliers
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
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()
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
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
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
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
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()
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()
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
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
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")
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")
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()
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
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
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
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
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