def create_stock(self, product_id: int, location: str, qty: int) -> StockItemDalModel: """ Creates stock but first checks for existing items to avoid duplicates :param product_id: The id of the product to use :param location: The location within the warehouse :param qty: The number of product at the location :return: The stock DAL model """ product = Product.get_by_id(product_id) if product is None: raise Exception("Product cannot be None when creating stock") try: stock_item = StockItem.select().where( StockItem.product_id == product_id, StockItem.location == location).get() stock_item.quantity = qty stock_item.save() except DoesNotExist: stock_item = StockItem.create(location=location, quantity=qty, product=product) return StockItemDalModel.create_from_model(stock_item)
def get_total_product_stock(self, product_id: int) -> int: """ Gets a count of all stock stored for a product :param product_id: The id of the product :return: The total stock stored """ return Product.get_by_id(product_id).stock.count
def get_stock_for_product(self, product_id: int) -> [StockItemDalModel]: """ Gets all of the stock items for the specified product :param product_id: The id of the product :return: List of products stock """ p = Product.get_by_id(product_id) dal_models = [StockItemDalModel.create_from_model(m) for m in p.stock] return dal_models
def get(self): """HTML GET handler. Renders a form for inputing the information used to create a monster under a specific set of rules. The rules are specified by the MonsterBuilder being used.""" template_values = self.build_template_values() template_values['questions']= CoreMonsterBuilder.questions() product = self.request.get('product') if product: product = Product.get_by_id(int(product)) if product.creator.account != template_values[handlers.base.USER_KEY]: return self.forbidden() template_values['product'] = product template = configuration.site.jinja_environment.get_template('monster/create.html') self.response.write(template.render(template_values))
def get(self, entity_id=None): """HTML GET handler. Display the product with the specified ID""" template_values = self.build_template_values() if entity_id: try: product = Product.get_by_id(int(entity_id)) except ValueError: return self.not_found() if not product: self.not_found() return template_values['product'] = product else: self.redirect(self.uri_for('product.home')) return template = configuration.site.jinja_environment.get_template('product/view.html') self.response.write(template.render(template_values))
def get(self, entity_id=None): """HTML GET handler. Update the product with the specified ID""" template_values = self.build_template_values() if entity_id: try: product = Product.get_by_id(int(entity_id)) except ValueError: return self.not_found() if not product: self.not_found() return product.generate_access_code() product.put() else: self.redirect(self.uri_for('product.home')) return return self.redirect(self.uri_for('profile.edit'))