def setup(self): self.service = Repair('Аренда', 2, 100) self.coffee_machines = [ CoffeeMachine('Lirica', 20, Brand('Saeco')), CoffeeMachine('Jura', 5, Brand('X9')) ] self.catlog = Repair('Repair', 300, 2, self.coffee_machines)
def api_brand_insert(): new_brand = request.get_json() brand = Brand(id=new_brand['id'], name=new_brand['name']) db.session.add(brand) db.session.commit() brand_json = {"id": brand.id, "name": brand.name} return jsonify(brand_json)
def create_brand(): auth_token = request.args.get('access_token') try: keys = bu.decode_auth_token(auth_token) except jwt.ExpiredSignatureError: return jsonify({'message': 'Signature expired. Please log in again.'}), 401 except jwt.InvalidTokenError: return jsonify({'message': 'Invalid token. Please log in again.'}), 401 admin = keys[0] id = keys[1] if admin == 0: return jsonify({'response': "This is not an admin"}), 403 data = request.get_json() if not data: return {"response": "No input data provided"}, 400 try: result = BrandSchema().load(data) except Exception: return jsonify({'response': "Invalid input"}), 403 brand = Brand(name=result["name"]) db.session.add(brand) db.session.commit() return jsonify({'response': "Success"}), 201
def setup(self): self.brand = Brand('Saeco') item_factory = ItemFactory() self.coffee = item_factory.create_item('coffee', 'Gold', 12, brands=[self.brand]) self.coffee_machine1 = CoffeeMachine('Lirica', 20, self.brand) self.coffee_machine2 = CoffeeMachine('One Touch', 20, self.brand) self.repair = item_factory.create_item( 'repair', name='Program', quantity=30, coffee_machines=[self.coffee_machine1, self.coffee_machine2], price=100) self.coffee_machine3 = CoffeeMachine('Any', 20, self.brand) self.compilation = item_factory.create_item('compilation', name='by me', quantity=30, theme='Program') files = [self.repair, self.coffee, self.coffee_machine3] for f in files: self.compilation.items.append(f)
def get_or_create_brand(session, brand_name, label): brand = session.query(Brand).filter(Brand.name == brand_name).first() print(brand) if not brand: brand = Brand(name=brand_name, label=label) session.add(brand) return brand
def post(self): parsed_args = parser.parse_args() brand = Brand() brand.name = parsed_args['name'] brand.website = parsed_args['website'] session.add(brand) session.commit() return brand, 201
def test_create_item(): brand = Brand('Sirocco') item_factory = ItemFactory() coffee_item = item_factory.create_item('coffee', name='Espresso', quantity=20, brands=[brand]) assert isinstance(coffee_item, OrderItem) assert isinstance(coffee_item, Coffee) assert coffee_item.name == 'Espresso' assert brand in coffee_item.brands with raises(Exception): item_factory.create_item('coffee', name='Espresso', quantity=20, brand=brand) with raises(TypeError): item_factory.create_item('coffee', name='Espresso', quantity=20, brand=brand) coffee_machine_item = item_factory.create_item('coffee_machine', name='Jura', quantity=10, brand=Brand('Saeco')) assert isinstance(coffee_machine_item, OrderItem) assert isinstance(coffee_machine_item, CoffeeMachine) repair_item = item_factory.create_item( 'repair', name='Ремонт чего-то', quantity=30, coffee_machines=[coffee_machine_item], price=100) assert isinstance(repair_item, OrderItem) assert isinstance(repair_item, Repair) with raises(KeyError): item_factory.create_item('what', name='what', quantity=1) compilation_item = item_factory.create_item('compilation', name='by me', quantity=30, theme='Program') assert isinstance(compilation_item, OrderItem) assert isinstance(compilation_item, Compilation)
def test_init(self): assert self.coffee.name == 'Mocca' assert self.coffee.quantity == 5 assert len(self.coffee.brands) == 2 self.coffee.brands.append(Brand('Kate')) assert len(self.coffee.brands) == 3 assert self.sirocco in self.coffee.brands assert self.lasemeuse in self.coffee.brands assert self.coffee.brands is self.brands
def save(self): """Save data in target table.""" if self.table == "Category": Category(self.db).insert_query(self.elements) elif self.table == "Brand": Brand(self.db).insert_query(self.elements) elif self.table == "Store": Store(self.db).insert_query(self.elements) elif self.table == "Product": Product(self.db).insert_query(self.elements)
def addbrand(): if request.method == 'POST': form = BrandForm(request.form) if form.validate(): brand = Brand(name=form.name.data) db.session.add(brand) db.session.commit() flash('Brand "%s" created successfully.' % brand.name, 'success') return redirect(url_for('products.index', slug=brand.slug)) else: form = BrandForm() return render_template('products/addbrand.html', form=form)
def init_db(): brand = Brand(name='Ford', brand_id=1, country='US').save() Brand(name='Ford2', brand_id=2, country='US').save() brand.save() fiesta = Car(brand=brand, name='Fiesta', car_id=1) edge = Car(brand=brand, name='Edge', car_id=2) ecosport = Car(brand=brand, name='Ecosport', car_id=3) ka = Car(brand=brand, name='Ka', car_id=4) flex_counter = 0 gas_counter = 0 for car in [fiesta, edge, ecosport, ka]: flex_counter += 1 gas_counter += 1 car.save() flex_version = Version(price=random.randrange(0, 50000), model=car, name='{name} {fuel} - {year}'.format( name=car.name, fuel='Flex', year=2011, version_id=flex_counter)) gasoline_version = Version(price=random.randrange(0, 50000), model=car, name='{name} {fuel} - {year}'.format( name=car.name, fuel='Gasolina', year=2011, version_id=gas_counter)) flex_version.save() gasoline_version.save()
def check_brand(brand_name): """ Checks if brand is available in database already, else adds and returns id :param brand_name: Name of the brand to search in database. :return: int: Id of the brand that already exists/is inserted in the database. """ brand = Brand.query.filter_by(brand_name=brand_name).first() if brand is None: # Brand doesn't exist in database, so insert it and return brand_id new_brand = Brand(brand_name=brand_name) db.session.add(new_brand) db.session.flush() brand_id = Brand.query.filter_by( brand_name=brand_name).first().brand_id else: # Brand already exists in database, so return brand_id brand_id = brand.brand_id return brand_id
def __init__(self, db_connection): """Please see help(Navigation) for more details.""" self.view = Display() self.db = db_connection self.category_table = Category(db_connection) self.product_table = Product(db_connection) self.brand_table = Brand(db_connection) self.substitution_table = Substitution(db_connection) self.current_pos = 0 self.selections = { "category": None, "sub_category": None, "name": None, "brand": None, "brand_name": None, "nutri_score": None, "substitute": None, "substitute_brand": None } self.active = self.main
def import_data(): session = Session() client = OpenFoodFactsApi() categories = client.get_categories() stores = client.get_stores() # product_schema = ProductSchema() # insert data to Category for store in stores: store1 = Store(name=store) session.add(store1) for cat in categories: category1 = Category(name=cat) print(category1) products = client.get_products(cat) # insert data to Product, Store, Brand for product in products: if product.get("product_name") != "": b = session.query(Brand).filter( Brand.name == product.get("brands")).first() if not b: brand = Brand(name=product.get("brands"), label=product.get("label")) session.add(brand) b = session.query(Brand).filter( Brand.name == product.get("brands")).first() product = Product( name=product.get("product_name"), nutriscore=product.get("nutrition_grades"), nova=product.get("nova_groups_tags"), brand=b, url=product.get("url"), barcode=product.get("code")) product.categories.append(category1) product.stores.append(store1) print(product) session.add(product) session.commit() # dump_data = product_schema.dump(product) # load_data = product_schema.load(dump_data, session=session) session.close()
def setup(self): self.brand = Brand('Saeco') self.coffee_machine = CoffeeMachine('Lirica', 20, self.brand)
def setup(self): self.sirocco = Brand('Sirocco') self.lasemeuse = Brand('La Semeuse') self.brands = [self.sirocco, self.lasemeuse] self.coffee = Coffee(name='Mocca', brands=self.brands, quantity=5)
def test_eq(self): assert self.sirocco != self.lasemeuse assert self.sirocco == self.sirocco # Авторы равны когда равны их имена assert self.sirocco == Brand('Sirocco')
def setup(self): self.sirocco = Brand('Sirocco') self.lasemeuse = Brand('La Semeuse')