def create(): product_form = ProductForm(request.form) categories_products = CategoryProduct() view_data = ProductController.__get_view_data() product_form = view_data["product_form"] categories_list = view_data["categories"] if not product_form.validate(): flash("Tuotteen julkaisu epäonnistui", "error") return render("users/product_form.html", product_form = product_form, categories = categories_list) product = Product( product_form.name.data, product_form.price.data, product_form.quantity.data, current_user.id ) if not product.save("product create:"): flash("Tuotteen julkaisu epäonnistui", "error") return render("users/product_form.html", product_form = product_form, categories = categories_list) if not categories_products.add_product_categories(product.id, product_form.categories.data): flash("Tuote julkaistu. Kategorioiden lisäyksessä tapahtui virhe", "error") return render("users/product_form.html", product_form = product_form, categories = categories_list) flash("Tuote julkaistu", "success") return redirect(url_for("product_list"))
def callback(cb, method, properties, body): print('Received in main') data = json.loads(body) db = SessionLocal() if properties.content_type == 'product_created': product = Product(**data) db.add(product) db.commit() db.refresh(product) print('Product created') elif properties.content_type == 'product_updated': product = db.query(Product).get(data['id']) product.title = data['title'] product.image = data['image'] product.likes = data['likes'] db.commit() db.refresh(product) print('Product updated') elif properties.content_type == 'product_deleted': product = db.query(Product).get(data['id']) db.delete(product) db.commit() print('Product deleted')
def drop_collections(): """Drops collections from Mongo that are used for this program """ logger.warning('Dropping "Cusomters"') Customers.drop_collection() logger.warning('Dropping "Rental"') Rental.drop_collection() logger.warning('Dropping "Product"') Product.drop_collection() logger.warning("Purge complete!")
async def add_user(*, pro: ProductM, request: Request): print(request.pony_session) with request.pony_session: Product(name=pro.name, price=pro.price, description=pro.description) commit() return Response('{"code":0000,"msg":"OK"}')
def add_product(body): if request.is_json: name = body.get('name') description = body.get('description') quantity = body.get('quantity') price = body.get('price') new_product = Product(name=name, description=description, quantity=quantity, price=price) existing_product = Product.query\ .filter(Product.name == name)\ .filter(Product.description == description)\ .one_or_none() if existing_product is None: try: db.session.add(new_product) db.session.commit() return make_response('New product successfully created', 201) except IntegrityError: return make_response('A product with that name already exists', 409) else: return make_response(f'Product, {name}, already exists', 409)
def add_product(): name = request.json["name"] onSale = request.json["onSale"] img_src = request.json["img_src"] prod_type = request.json["prod_type"] short_desc = request.json["short_desc"] price = request.json["price"] popupId = request.json["popupId"] popupImg = request.json["popupImg"] isAvailable = request.json["isAvailable"] popupCat = request.json["popupCat"] popupTag = request.json["popupTag"] popupDesc = request.json["popupDesc"] new_product = Product(name=name, onSale=onSale, img_src=img_src, prod_type=prod_type, short_desc=short_desc, price=price, popupId=popupId, popupImg=popupImg, isAvailable=isAvailable, popupCat=popupCat, popupTag=popupTag, popupDesc=popupDesc) db.session.add(new_product) db.session.commit() return product_schema.jsonify(new_product)
def format(pk: str): product = Product.get(pk) return { 'id': product.pk, 'name': product.name, 'price': product.price, 'quantity': product.quantity }
def product_new(): form = ProductForm() if form.validate_on_submit(): product = Product(productName=form.productName.data.lower()) db.session.add(product) db.session.commit() return redirect(url_for("product")) return render_template("create_product.html", title="Create Product", header="Create Products", form=form)
def fill(): db.create_all() product_1 = Product(title='Wolfsbane Potion', price=10.40, inventory_count=3) product_2 = Product(title='Polyjuice Potion', price=20, inventory_count=500) product_3 = Product(title='Felix Felicis', price=2.30, inventory_count=100) product_4 = Product(title='Confusing Concoction', price=8.99, inventory_count=102) product_5 = Product(title='Hiccoughing Potion', price=3.11, inventory_count=100) product_6 = Product(title='Pepperup Potion', price=0.99, inventory_count=900) product_7 = Product(title='Draught of Peace', price=1.12, inventory_count=80) product_8 = Product(title='Ageing Potion', price=5.30, inventory_count=100) product_9 = Product(title='Unicorn Blood', price=57.88, inventory_count=5) product_10 = Product(title='Veritaserum', price=31.20, inventory_count=10) db.session.add(product_1) db.session.add(product_2) db.session.add(product_3) db.session.add(product_4) db.session.add(product_5) db.session.add(product_6) db.session.add(product_7) db.session.add(product_8) db.session.add(product_9) db.session.add(product_10) db.session.commit() return '', 204
def get_unique_products(mock_get_uuid, mock_deduplicate_products): actual = get_unique_products(sample_raw_transaction) assert mock_get_uuid.call_count == len(expected_deduplicated_products) assert mock_deduplicate_products.called_once() expected = [ Product( id=sample_uuid, name="Mocha", flavour="", size="", price=2.3, iced=False ), Product( id=sample_uuid, name="Tea", flavour="Fruit", size="", price=1.3, iced=False ), Product( id=sample_uuid, name="Latte", flavour="Vanilla", size="", price=2.75, iced=True, ), Product( id=sample_uuid, name="Frappes", flavour="Chocolate Cookie", size="", price=2.75, iced=False, ), Product( id=sample_uuid, name="Filter Coffee", flavour="", size="Large", price=1.8, iced=False, ), ] assert actual == expected
def quote_dependencies(test_db): new_customer = Customer(first_name='John', last_name='Doe', email='*****@*****.**', phone_number='(000) 000-0000') new_product = Product(name='Tables', description='10 seater', quantity=5, price=500) test_db.session.add(new_customer) test_db.session.add(new_product) test_db.session.commit()
def get(): search_form = SearchForm(request.args) categories = Category.query.all() search_form.categories.choices = list( map(lambda cat: (cat.id, cat.name), categories)) search_form.categories.choices.append((-1, "Kategoriton")) return render( "search/main.html", search_form=search_form, products=Product.find_by_criteria( search_form.name.data, list(map(lambda id: int(id), search_form.categories.data)), search_form.price.data, search_form.minimum.data, search_form.date_start.data, search_form.date_end.data, search_form.seller.data))
def add_product(): name = request.form.get('name') price = request.form.get('price') quantity = request.form.get('quantity') discount = request.form.get('discount') brand_id = request.form.get('brand_id') category_id = request.form.get('category_id') product = Product.Product(name=name, price=price, quantity=quantity, discount=discount, brand_id=brand_id, category_id=category_id) db.session.add(product) db.session.commit() return "Product added, product id = {}".format(product.id)
def test_add_new_product_to_database(test_db): new_product = Product(name='Tables', description='10 seater', quantity=5, price=500) test_db.session.add(new_product) test_db.session.commit() product = Product.query\ .filter(Product.name == 'Tables')\ .filter(Product.description == '10 seater')\ .filter(Product.quantity == 5)\ .filter(Product.price == 500)\ .one_or_none() assert product is not None
def __init__(self, interval, wc_update, convert_orders): super().__init__() self._interval = interval self.wc_update = wc_update self.convert_orders = convert_orders # models self.category = Category() self.product = Product() self.invoice = Invoice() self.update_models = [self.category, self.product] # events # - stop self.stop_event = Event() # - resume self.resume_event = Event() # signals self.signals = RecipientEngineSignals()
def add_product(self, name, description, price, category): """ Admin only activity Add new product to the database :return: """ try: category = self.db.query(Category).filter( Category.name.ilike(category)).one() new_product = Product(name=name, description=description, price=price, category_id=category.id) session.add(new_product) session.commit() print("\n'%s' added successfully\n" % name) return True except NoResultFound: return False
def get_unique_products(transactions: list) -> list: """ Extract a list of unique products from the transactions list. Each product is assigned a UUID string Returns ------- list A list containing the unique products as dictionaries """ return [ Product(**dict(product, **{"id": str(get_uuid())})) for product in _deduplicate_products( list( chain.from_iterable( [transaction["basket"] for transaction in transactions] ) ) ) ]
def post(): params = { 'ProductName': fields.String(), 'Thumbnail': fields.String(), 'ModelID': fields.Integer() } json_data = parser.parse(params) ProductName = json_data.get('ProductName', "abc").strip() Thumbnail = json_data.get('Thumbnail', "abc").strip() ModelID = json_data.get('ModelID', 1) new_values = Product(ProductName=ProductName, Thumbnail=Thumbnail, ModelID=ModelID) try: db.session.add(new_values) db.session.commit() return create_success() except: return create_fail() return create_fail()
def add_or_update_product(): try: payload = request.get_json() if request.method == 'POST': product = Product() product = fill_product(product, payload) db.session.add(product) if request.method == 'PUT': if 'product_id' not in payload: raise HttpError('Invalid usage') product = Product.query.get(payload['product_id']) product = fill_product(product, payload) db.session.commit() return {'id': product.product_id} except Exception: raise HttpError('Invalid usage. Some required data is not present in the request')
def create_product(product: Product): return product.save()
def delete_product(pk: str): return Product.delete(pk)
def products(): return [format(pk) for pk in Product.all_pks()]
class ProductView(ObjectView): """Product View""" model = Product() message_slice = slice(0, 19)
def product_details(pk: str): try: return Product.get(pk) except Exception as e: return str(f"The product ID not found{e}")