def save_product(p_id, staff): for f in ('p_name', 'price', 'c_id', 'pos_machs', 'available'): if f not in request.json: return json_err('field `{}` is required'.format(f)), 400 p_name = request.json['p_name'] price = request.json['price'] c_id = request.json['c_id'] pos_machs = request.json['pos_machs'] available = request.json['available'] if p_id != 0: product = Product.query.get(p_id) if not product: abort(403) category = Category.query.get(c_id) if not category: abort(403) product.name = p_name product.price = price product.category = category product.available = available db.session.commit() for pp in product.pos_products: if pp.pos.id not in pos_machs: db.session.delete(pp) db.session.commit() for id in pos_machs: pos = POS.query.get(id) if pos not in product.poss: pos_product = PosProduct() pos_product.pos = pos pos_product.product = product db.session.add(pos_product) db.session.commit() else: category = Category.query.get(c_id) if not category: abort(403) product = Product(name=p_name, price=price, available=available) product.category = category db.session.add(product) db.session.commit() for id in pos_machs: pos = POS.query.get(id) pos_product = PosProduct() pos_product.pos = pos pos_product.product = product db.session.add(pos_product) db.session.commit() return {'state': 'ok', 'category': c_id}
def fetch_category(search_index, amazon_node_id): api = caching.ResponseCachingAPI(settings.AMAZON_AWS_KEY, settings.AMAZON_SECRET_KEY, settings.AMAZON_API_LOCALE, settings.AMAZON_ASSOCIATE_TAG, cachedir='cache', cachetime=86400) try: for root in api.item_search( search_index, BrowseNode=str(amazon_node_id), ResponseGroup=settings.AMAZON_RESPONSE_GROUP): for item in root.Items.Item: product = Product() product.category = Category.objects.get( amazon_node_id=amazon_node_id) product.asin = item.ASIN product.title = unicode(item.ItemAttributes.Title) product.detailpageurl = unicode(item.DetailPageURL) product.manufacturer = unicode( getattr(item.ItemAttributes, 'Manufacturer', None)) product.publisher = unicode( getattr(item.ItemAttributes, 'Publisher', None)) product.brand = unicode( getattr(item.ItemAttributes, 'Brand', None)) product.popularity = getattr(item, 'SalesRank', 1000) if hasattr(item, 'MediumImage'): product.medium_image = getattr(item.MediumImage, 'URL', None) if hasattr(item, 'LargeImage'): product.large_image = getattr(item.LargeImage, 'URL', None) if hasattr(item, 'EditorialReviews'): product.description = unicode( getattr(item.EditorialReviews.EditorialReview, 'Content', None)) if hasattr(item.Offers, 'Offer'): product.price = item.Offers.Offer.OfferListing.Price.FormattedPrice.pyval elif hasattr(item.ItemAttributes, 'ListPrice'): product.price = item.ItemAttributes.ListPrice.FormattedPrice.pyval elif hasattr(item.OfferSummary, 'LowestUsedPrice'): product.price = u'used from %s' % item.OfferSummary.LowestUsedPrice.FormattedPrice.pyval else: product.price = None product.save() except AWSError, e: if e.code == 'AWS.ParameterOutOfRange': pass # reached the api limit of 10 pages else: raise ValidationError(message=e.msg)
def SaveChanges(self): for row in self.change_rows.keys(): row = int(row) product = Product() product.category = models.ALL_PRODUCT_TYPE[self.m_grid5.GetCellValue(row, 0)] product.type = self.m_grid5.GetCellValue(row, 1) product.length = self.m_grid5.GetCellValue(row, 2) product.width = self.m_grid5.GetCellValue(row, 3) product.height = self.m_grid5.GetCellValue(row, 4) product.per_weight = self.m_grid5.GetCellValue(row, 5) product.price = self.m_grid5.GetCellValue(row, 6) db.UpdateProduct(product) for new_row in self.new_products.keys(): product = Product() product.category = models.ALL_PRODUCT_TYPE[self.m_grid5.GetCellValue(row, 0)] product.type = self.m_grid5.GetCellValue(new_row, 1) product.length = self.m_grid5.GetCellValue(new_row, 2) product.width = self.m_grid5.GetCellValue(new_row, 3) product.height = self.m_grid5.GetCellValue(new_row, 4) product.per_weight = self.m_grid5.GetCellValue(new_row, 5) product.price = self.m_grid5.GetCellValue(new_row, 6) db.InsertProduct(product)
def test_update_a_product(self): """ Update a Product """ p = Product(0, 'Asus2500', 'Laptop', '234', 'laptop', 'blue', 4) p.save() self.assertEqual(p.id, 1) # Change it an save it p.category = "gaming_laptop" p.save() self.assertEqual(p.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change products = Product.all() self.assertEqual(len(products), 1) self.assertEqual(products[0].category, "gaming_laptop")
def SaveChanges(self): for row in self.change_rows.keys(): row = int(row) product = Product() product.category = models.ALL_PRODUCT_TYPE[ self.m_grid5.GetCellValue(row, 0)] product.type = self.m_grid5.GetCellValue(row, 1) product.length = self.m_grid5.GetCellValue(row, 2) product.width = self.m_grid5.GetCellValue(row, 3) product.height = self.m_grid5.GetCellValue(row, 4) product.per_weight = self.m_grid5.GetCellValue(row, 5) product.price = self.m_grid5.GetCellValue(row, 6) db.UpdateProduct(product) for new_row in self.new_products.keys(): product = Product() product.category = models.ALL_PRODUCT_TYPE[ self.m_grid5.GetCellValue(row, 0)] product.type = self.m_grid5.GetCellValue(new_row, 1) product.length = self.m_grid5.GetCellValue(new_row, 2) product.width = self.m_grid5.GetCellValue(new_row, 3) product.height = self.m_grid5.GetCellValue(new_row, 4) product.per_weight = self.m_grid5.GetCellValue(new_row, 5) product.price = self.m_grid5.GetCellValue(new_row, 6) db.InsertProduct(product)
def menuItems(item_id=None): # Get All Menu Items specific_vendor_id = get_jwt_identity() if request.method == 'GET': if item_id is None: vendor_items = Product.query.filter_by( vendor_id=specific_vendor_id).all() serialized_items = [] for item in vendor_items: serialized_items.append(item.serialize()) return jsonify(serialized_items), 200 else: specific_item = Product.query.filter_by(id=item_id).one_or_none() return jsonify(specific_item.serialize()), 200 elif request.method == 'POST': body = request.get_json() item = Product(name=body['name'], category=body['category'], vendor_id=specific_vendor_id, price=body['price'], description=body['description']) db.session.add(item) db.session.commit() print(item) return jsonify(item.serialize()), 201 elif request.method == 'PUT': body = request.get_json() item = Product.query.get(item_id) if item is None: raise APIException('Menu item not found', status_code=404) if 'name' in body: item.name = body['name'] if 'price' in body: item.price = body['price'] if 'description' in body: item.description = body['description'] if 'category' in body: item.category = body['category'] db.session.commit() return jsonify(item.serialize()), 200 elif request.method == 'DELETE': item = Product.query.get(item_id) if item is None: raise APIException('Item not found', status_code=404) db.session.delete(item) db.session.commit() return jsonify({}), 204
def GetAllProducts(): """获取所有的产品数据""" db_cur = db_conn.cursor() db_cur.execute(SQL_ALL_PRODUCT) all_products = {} for category in models.ALL_PRODUCT_TYPE.values(): all_products[category] = {} for row in db_cur: rec = Product() rec.category = str(row[0]) rec.type = row[1] rec.length = str(row[2]) rec.width = str(row[3]) rec.height = str(row[4]) rec.per_weight = str(row[5]) rec.price = str(row[6]) all_products[rec.category][rec.type] = rec return all_products
def fetch_category(search_index, amazon_node_id): api = caching.ResponseCachingAPI( settings.AMAZON_AWS_KEY, settings.AMAZON_SECRET_KEY, settings.AMAZON_API_LOCALE, settings.AMAZON_ASSOCIATE_TAG, cachedir='cache', cachetime=86400) try: for root in api.item_search(search_index, BrowseNode=str(amazon_node_id), ResponseGroup=settings.AMAZON_RESPONSE_GROUP): for item in root.Items.Item: product = Product() product.category = Category.objects.get(amazon_node_id=amazon_node_id) product.asin = item.ASIN product.title = unicode(item.ItemAttributes.Title) product.detailpageurl = unicode(item.DetailPageURL) product.manufacturer = unicode(getattr(item.ItemAttributes, 'Manufacturer', None)) product.publisher = unicode(getattr(item.ItemAttributes, 'Publisher', None)) product.brand = unicode(getattr(item.ItemAttributes, 'Brand', None)) product.popularity = getattr(item, 'SalesRank', 1000) if hasattr(item, 'MediumImage'): product.medium_image = getattr(item.MediumImage, 'URL', None) if hasattr(item, 'LargeImage'): product.large_image = getattr(item.LargeImage, 'URL', None) if hasattr(item, 'EditorialReviews'): product.description = unicode(getattr(item.EditorialReviews.EditorialReview, 'Content', None)) if hasattr(item.Offers, 'Offer'): product.price = item.Offers.Offer.OfferListing.Price.FormattedPrice.pyval elif hasattr(item.ItemAttributes, 'ListPrice'): product.price = item.ItemAttributes.ListPrice.FormattedPrice.pyval elif hasattr(item.OfferSummary, 'LowestUsedPrice'): product.price = u'used from %s' % item.OfferSummary.LowestUsedPrice.FormattedPrice.pyval else: product.price = None product.save() except AWSError, e: if e.code == 'AWS.ParameterOutOfRange': pass # reached the api limit of 10 pages else: raise ValidationError(message=e.msg)
def post(self): json_data = request.json errors = ProductSchemaAdd().validate(json_data) if errors: return errors category_name = json_data.get('category') category = Category.objects(name=category_name) if len(category) == 0: category = Category(name=category_name) category.save() else: category = category[0] try: product = Product(**json_data) product.category = category product.save() user_json = product.to_json() return json.loads(user_json) except ValidationError as errors: print(errors)
def process_item(self, item, spider): # Check if the Product already exists product = ( self.session.query(Product) .filter_by(store=item["store"], sku=item["sku"]) .first() ) if product is None: product = Product(store=item["store"], sku=item["sku"]) product.barcodes = item["barcodes"] product.brand = item["brand"] product.name = item["name"] product.description = item["description"] product.image_url = item["image_url"] product.category = item["category"] product.package = item["package"] product.url = item["url"] self.session.add(product) self.session.commit() # Check if the BranchProduct already exists branch_product = ( self.session.query(BranchProduct) .filter_by(product=product, branch=item["branch"]) .first() ) if branch_product is None: branch_product = BranchProduct(product=product, branch=item["branch"]) branch_product.stock = item["stock"] branch_product.price = item["price"] self.session.add(branch_product) self.session.commit() return item
def products_to_db(products): """ It saves the products in the database :param products: dictionary with the desired information """ session = load_session() for key, item in products.items(): print('\n>>> Processing:', key, item['NAME']) product = (session.query(Product).filter_by(store="Richart's", sku=item["SKU"]).first()) if product is None: product = Product(store="Richart's", sku=item["SKU"]) product.barcodes = item["BARCODES"] product.brand = item["BRAND"].capitalize() product.name = item["NAME"].capitalize() description = remove_html_tags(item["DESCRIPTION"]) product.description = description.capitalize() product.image_url = item["IMAGE_URL"] product.category = item["FULL_CATEGORY"] product.package = product.description.replace(product.name, '') session.add(product) session.commit() # Check if the BranchProduct already exists branch_product = (session.query(BranchProduct).filter_by( product=product, branch=item["BRANCH"]).first()) if branch_product is None: branch_product = BranchProduct(product=product, branch=item["BRANCH"]) branch_product.stock = item["STOCK"] branch_product.price = item["PRICE"] session.add(branch_product) session.commit() session.close()