def delete(client_id): try: run_custom_query("""DELETE FROM CLIENTS WHERE ID = {}""".format(client_id), fetch=False) return redirect(url_for("clients.list")) except IntegrityError: flash("Client has got orders. Delete orders at first, then delete client.", "error") return redirect(url_for("clients.list"))
def delete_image(id): run_custom_query("""UPDATE product_images SET deleted = {} WHERE id = \'{}\'""".format(True, id), fetch=False) return jsonify({ 'success': True })
def delete(client_id): try: run_custom_query( """DELETE FROM CLIENTS WHERE ID = {}""".format(client_id), fetch=False) return redirect(url_for('clients.list')) except IntegrityError: flash( 'Client has got orders. Delete orders at first, then delete client.', 'error') return redirect(url_for('clients.list'))
def delete(id): product = select_row_from_table_by_id("products", id) if product is not None: run_custom_query("""SELECT delete_product({})""".format(id), fetch=False) flash( "Succesfully deleted product {}".format(product[0].name), "success" ) else: flash("Product with ID={} does not exist".format(id), "danger") return redirect(url_for('product.list'))
def delete(id): if session.get('user') == id: flash("Don't delete yourself, it's bad idea :)", 'error') return redirect(url_for('users.list')) try: run_custom_query("""DELETE FROM users WHERE ID = {}""".format(id), fetch=False) return redirect(url_for('users.list')) except IntegrityError: flash("User has got orders. Delete user's orders at first", 'error') return redirect(url_for('users.list'))
def delete(id): if session.get('user') == id: flash("Don't delete yourself, it's bad idea :)", 'error') return redirect(url_for('users.list')) try: run_custom_query( """DELETE FROM users WHERE ID = {}""".format(id), fetch=False ) return redirect(url_for('users.list')) except IntegrityError: flash("User has got orders. Delete user's orders at first", 'error') return redirect(url_for('users.list'))
def edit_client(form, client_id): client_data = { 'name': form.data['name'], 'surname': form.data['surname'], 'email': form.data['email'], } utils.run_custom_query( """DELETE FROM CLIENT_PHONES WHERE CLIENT_ID = {}""".format( client_id ), fetch=False ) utils.update('clients', {'id': client_id}, client_data) for phone in form.data['phones']: if phone['phone']: utils.insert( 'client_phones', {'client_id': client_id, 'phone': phone['phone']} ) if form.data['billing_address']: billing = utils.select('client_addresses', {'client_id': client_id, 'type': 'billing'}) data = form.data['billing_address'] utils.update( 'addresses', { 'id': billing[0].id, }, { 'country': data['country'], 'city': data['city'], 'street': data['street'] } ) if form.data['delivery_address']: delivery = utils.select('client_addresses', {'client_id': client_id, 'type': 'delivery'}) data = form.data['delivery_address'] utils.update( 'addresses', { 'id': delivery[0].id, }, { 'country': data['country'], 'city': data['city'], 'street': data['street'] } )
def get_orders_with_query(query): orders_list = [] orders = run_custom_query(""" SELECT o.*, c.name, c.surname, c.email, s.status_name, sum_order(o.id) as sum FROM orders o JOIN clients c ON c.id = o.client_id JOIN order_statuses s ON s.id = o.status_id WHERE ( LOWER(c.name) LIKE '%{0}%' OR LOWER(c.surname) LIKE '%{0}%' OR LOWER(c.email) LIKE '%{0}%' OR LOWER(s.status_name) LIKE '%{0}%' ) OR EXISTS ( SELECT 1 FROM order_products op INNER JOIN products p ON op.product_id = p.id WHERE LOWER(p.name) LIKE '%{0}%' OR LOWER(p.description) LIKE '%{0}%' ) """.format(query.lower())) for order in orders: order_products = select('order_products', {'order_id': order.id}) orders_list.append( Order(id=order.id, client=order.client_id, status=order.status_name, suma=order.sum)) return sorted(orders_list, key=lambda x: x.id)
def get_full_client_dict(client): client_dict = dict(client.__dict__) # Kinda hack :-) Don't learn that :P phones = utils.run_custom_query( '''SELECT phone FROM client_phones WHERE client_id = {}'''.format( client.id)) client_dict['phones'] = [phone.phone for phone in phones if phone.phone] return client_dict
def get_phones_for_client(client): phones = utils.run_custom_query( '''SELECT phone FROM client_phones WHERE client_id = {}'''.format( client.id ) ) return [phone.phone for phone in phones if phone.phone]
def get_full_client_dict(client): client_dict = dict(client.__dict__) # Kinda hack :-) Don't learn that :P phones = utils.run_custom_query( '''SELECT phone FROM client_phones WHERE client_id = {}'''.format( client.id ) ) client_dict['phones'] = [phone.phone for phone in phones if phone.phone] return client_dict
def get_product_main_image(id): images = utils.run_custom_query(""" SELECT * FROM product_images WHERE product_id = {} ORDER BY default_image DESC """.format(id)) if not images: return None return images[0]
def list(): query = request.args.get('query', '') users = run_custom_query(""" SELECT * FROM users WHERE username LIKE '%{}%' ORDER BY id ASC; """.format(query)) return render_template('users/list.html', users=users, query=query)
def edit_client(form, client_id): client_data = { 'name': form.data['name'], 'surname': form.data['surname'], 'email': form.data['email'], } utils.run_custom_query( """DELETE FROM CLIENT_PHONES WHERE CLIENT_ID = {}""".format(client_id), fetch=False) utils.update('clients', {'id': client_id}, client_data) for phone in form.data['phones']: if phone['phone']: utils.insert('client_phones', { 'client_id': client_id, 'phone': phone['phone'] }) if form.data['billing_address']: billing = utils.select('client_addresses', { 'client_id': client_id, 'type': 'billing' }) data = form.data['billing_address'] utils.update('addresses', { 'id': billing[0].id, }, { 'country': data['country'], 'city': data['city'], 'street': data['street'] }) if form.data['delivery_address']: delivery = utils.select('client_addresses', { 'client_id': client_id, 'type': 'delivery' }) data = form.data['delivery_address'] utils.update('addresses', { 'id': delivery[0].id, }, { 'country': data['country'], 'city': data['city'], 'street': data['street'] })
def list(): query = request.args.get('query', '') users = run_custom_query(""" SELECT * FROM users WHERE username LIKE '%{}%' ORDER BY id ASC; """.format(query) ) return render_template('users/list.html', users=users, query=query)
def list(): query = request.args.get('query', '') if not query: products = run_custom_query( """SELECT * FROM products WHERE deleted = False""") else: products = run_custom_query( """ SELECT * FROM products WHERE (LOWER(name) LIKE '%{0}%' OR LOWER(description) LIKE '%{0}%') AND deleted = False """.format(query.lower()) ) return render_template( 'products/list.html', products=products, query=query )
def get_all_orders(): orders_list = [] orders = run_custom_query(""" SELECT o.*, c.name, c.surname, c.email, s.status_name, sum_order(o.id) as sum FROM orders o JOIN clients c ON c.id = o.client_id JOIN order_statuses s ON s.id = o.status_id """) for order in orders: order_products = select('order_products', {'order_id': order.id}) orders_list.append( Order(id=order.id, client=order.client_id, status=order.status_name, suma=order.sum)) return sorted(orders_list, key=lambda x: x.id)
def get_all_orders(): orders_list = [] orders = run_custom_query( """ SELECT o.*, c.name, c.surname, c.email, s.status_name, sum_order(o.id) as sum FROM orders o JOIN clients c ON c.id = o.client_id JOIN order_statuses s ON s.id = o.status_id """ ) for order in orders: order_products = select('order_products', {'order_id': order.id}) orders_list.append( Order( id=order.id, client=order.client_id, status=order.status_name, suma=order.sum ) ) return sorted(orders_list, key=lambda x: x.id)
def get_orders_with_query(query): orders_list = [] orders = run_custom_query( """ SELECT o.*, c.name, c.surname, c.email, s.status_name, sum_order(o.id) as sum FROM orders o JOIN clients c ON c.id = o.client_id JOIN order_statuses s ON s.id = o.status_id WHERE ( LOWER(c.name) LIKE '%{0}%' OR LOWER(c.surname) LIKE '%{0}%' OR LOWER(c.email) LIKE '%{0}%' OR LOWER(s.status_name) LIKE '%{0}%' ) OR EXISTS ( SELECT 1 FROM order_products op INNER JOIN products p ON op.product_id = p.id WHERE LOWER(p.name) LIKE '%{0}%' OR LOWER(p.description) LIKE '%{0}%' ) """.format(query.lower()) ) for order in orders: order_products = select('order_products', {'order_id': order.id}) orders_list.append( Order( id=order.id, client=order.client_id, status=order.status_name, suma=order.sum ) ) return sorted(orders_list, key=lambda x: x.id)
def get_product_images(id): return utils.run_custom_query( """SELECT * FROM {} WHERE product_id = {} AND deleted={};""".format("product_images", id, False))
def delete_status(status_id): utils.run_custom_query( """DELETE FROM order_statuses WHERE ID = {}""".format(status_id), fetch=False )
def get_phones_for_client(client): phones = utils.run_custom_query( '''SELECT phone FROM client_phones WHERE client_id = {}'''.format( client.id)) return [phone.phone for phone in phones if phone.phone]
def get_product_images(id): return utils.run_custom_query("""SELECT * FROM {} WHERE product_id = {} AND deleted={};""".format("product_images", id, False))
def delete_status(status_id): utils.run_custom_query("""DELETE FROM order_statuses WHERE ID = {}""".format(status_id), fetch=False)