def create_product(): if user_validate(): if request.method == 'POST': name = request.form['name'].strip().upper() gross_price = float(request.form['gross_price'].strip()) net_price = float(request.form['net_price'].strip()) profit_percentage = round((net_price - gross_price) / gross_price, 2) provider = persistence.read_one( 'SELECT * FROM PROVIDERS WHERE NAME="{}"'.format( request.form['provider'].strip())) if provider and persistence.create( 'INSERT INTO PRODUCTS (NAME, GROSS_PRICE, NET_PRICE, PROFIT_PERCENTAGE, PROVIDER) VALUES ("{}", {}, {}, {}, {})' .format(name, gross_price, net_price, profit_percentage, provider[0])): flash('Produto cadastrado com sucesso.') flash('success') log( 'create', 'PRODUCT: ("{}", {}, {}, {}, {}) -- {} ({})'.format( name, gross_price, net_price, profit_percentage, provider, session['user'][0], datetime.now())) else: flash('Não foi possível cadastrar o produto.') flash('danger') return redirect(url_for('products')) else: return redirect(url_for('login'))
def update_product(): if user_validate(): if request.method == 'POST': id = request.form['id'].strip() name = request.form['name'].strip().upper() gross_price = float(request.form['gross_price'].strip()) net_price = float(request.form['net_price'].strip()) profit_percentage = round((net_price - gross_price) / gross_price, 2) provider = persistence.read_one( 'SELECT * FROM PROVIDERS WHERE NAME="{}"'.format( request.form['provider'].strip())) if provider and persistence.update( 'UPDATE PRODUCTS SET NAME="{}", GROSS_PRICE={}, NET_PRICE={}, PROFIT_PERCENTAGE={}, PROVIDER={} WHERE ID={}' .format(name, gross_price, net_price, profit_percentage, provider, id)): flash('Produto alterado com sucesso.') flash('success') log( 'update', 'PRODUCT: ({}, "{}", {}, {}, {}, {}) -- {} ({})'.format( id, name, gross_price, net_price, profit_percentage, provider, session['user'][0], datetime.now())) else: flash('Não foi possível alterar o produto.') flash('danger') return redirect(url_for('products')) else: return redirect(url_for('login'))
def user_validate(alias='', password=''): try: alias, password = session['user'] return True except: row = persistence.read_one( 'SELECT * FROM USERS WHERE ALIAS="{}"'.format(alias)) if row and alias == row[2] and password == row[3]: return True return False
def delete_provider(): if user_validate(): if request.method == 'POST': id = request.form['id'].strip() if not persistence.read_one( 'SELECT * FROM PRODUCTS WHERE PROVIDER={}'.format( id)) and persistence.delete( 'DELETE FROM PROVIDERS WHERE ID={}'.format(id)): flash('Fornecedor removido com sucesso.') flash('success') log( 'delete', 'PROVIDER: ({}) -- {} ({})'.format(id, session['user'][0], datetime.now())) else: flash('Não foi possível remover o fornecedor.') flash('danger') return redirect(url_for('providers')) else: return redirect(url_for('login'))
def read_product(): if user_validate(): products = [] providers = [] if request.args.get('name'): rows = persistence.read( 'SELECT * FROM PRODUCTS WHERE NAME LIKE "%{}%"'.format( request.args.get('name').strip())) else: rows = persistence.read('SELECT * FROM PRODUCTS') for product in rows: provider = persistence.read_one( 'SELECT * FROM PROVIDERS WHERE ID={}'.format(product[5])) products.append( Product(id=product[0], name=product[1], gross_price=locale.currency(product[2], grouping=True, symbol=True), net_price=locale.currency(product[3], grouping=True, symbol=True), profit_percentage='{:.2%}'.format(product[4]), provider=Provider(id=provider[0], name=provider[1], cnpj=provider[2]))) rows = persistence.read('SELECT * FROM PROVIDERS') for provider in rows: providers.append( Provider(id=provider[0], name=provider[1], cnpj=provider[2], email=provider[3])) if not products: flash('Não foi encontrado nenhum produto.') flash('warning') return render_template('products.html', products=products, providers=providers) else: return redirect(url_for('login'))