def get_order(id): order = select_one_or_404('orders', {'id': id}) order_products = select('order_products', {'order_id': id}) status = select( 'order_statuses', {'id': order.status_id} )[0].status_name return Order(order.id, order.client_id, order_products, status)
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_product(id): product = utils.select_one_or_404('products', {'id': id}) images = utils.select('product_images', {'product_id': id}) c = Product(product.id, product.name, product.price, product.description, [image.filename for image in images]) return c
def confirm_order(order_id): confirmed_status = select('order_statuses', {'id': 3}) try: update('orders', {'id': order_id}, {'status_id': confirmed_status[0].id}) except: abort(404)
def cancel_order(order_id): cancelled_status = select('order_statuses', {'id': 2}) try: update('orders', {'id': order_id}, {'status_id': cancelled_status[0].id}) except: abort(404)
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_client(id): client = utils.select_one_or_404('clients', {'id': id}) phones = utils.select('client_phones', {'client_id': id}) billing_address = utils.select( 'client_addresses', {'client_id': id, 'type': 'billing'} ) delivery_address = utils.select( 'client_addresses', {'client_id': id, 'type': 'delivery'} ) b_address = None if billing_address: billing_addr = utils.select('addresses', {'id': billing_address[0].id}) if billing_addr: b_address = Address(billing_addr[0]) d_addr = None if delivery_address: delivery_addr = utils.select( 'addresses', {'id': delivery_address[0].id} ) if delivery_addr: d_addr = Address(delivery_addr[0]) Client = namedtuple( 'Client', [ 'id', 'name', 'surname', 'email', 'phones', 'billing_address', 'delivery_address' ] ) client = Client( id=client.id, name=client.name, surname=client.surname, email=client.email, phones=[{'id': phone.id, 'phone': phone.phone} for phone in phones], billing_address=b_address, delivery_address=d_addr ) c = Cl(client) return c
def get_initial_status(): data = select('order_statuses', {'id': 1}) try: status = Status(id=data[0].id, name=data[0].status_name) except IndexError: abort(500) return status
def get_new_orders_for_user(user_id): initial_status_pk = get_initial_status().id orders = select('orders', { 'status_id': initial_status_pk, 'user_id': user_id }) data = [] for order in orders: data.append(get_order(order.id)) return data
def confirm_order(order_id): confirmed_status = select('order_statuses', {'id': 3}) try: update( 'orders', {'id': order_id}, {'status_id': confirmed_status[0].id} ) except: abort(404)
def cancel_order(order_id): cancelled_status = select('order_statuses', {'id': 2}) try: update( 'orders', {'id': order_id}, {'status_id': cancelled_status[0].id} ) except: abort(404)
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_new_orders_for_user(user_id): initial_status_pk = get_initial_status().id orders = select( 'orders', {'status_id': initial_status_pk, 'user_id': user_id} ) data = [] for order in orders: data.append(get_order(order.id)) return data
def get_product(id): product = utils.select_one_or_404('products', {'id': id}) images = utils.select('product_images', {'product_id': id}) c = Product( product.id, product.name, product.price, product.description, [image.filename for image in images] ) return c
def get_client(id): client = utils.select_one_or_404('clients', {'id': id}) phones = utils.select('client_phones', {'client_id': id}) billing_address = utils.select('client_addresses', { 'client_id': id, 'type': 'billing' }) delivery_address = utils.select('client_addresses', { 'client_id': id, 'type': 'delivery' }) b_address = None if billing_address: billing_addr = utils.select('addresses', {'id': billing_address[0].id}) if billing_addr: b_address = Address(billing_addr[0]) d_addr = None if delivery_address: delivery_addr = utils.select('addresses', {'id': delivery_address[0].id}) if delivery_addr: d_addr = Address(delivery_addr[0]) Client = namedtuple('Client', [ 'id', 'name', 'surname', 'email', 'phones', 'billing_address', 'delivery_address' ]) client = Client(id=client.id, name=client.name, surname=client.surname, email=client.email, phones=[{ 'id': phone.id, 'phone': phone.phone } for phone in phones], billing_address=b_address, delivery_address=d_addr) c = Cl(client) return c
def save_order(data): data_orders = {'client_id': data['client'], 'user_id': session.get('user')} statuses = select('order_statuses', {'status_name': data['status']}) if statuses: data_orders['status_id'] = statuses[0].id order_id = insert('orders', data_orders) for product in data['products']: data_products = { 'order_id': order_id, 'product_id': product['product'], 'price': product['price'], 'quantity': product['quantity'], } insert('order_products', data_products)
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 save_order(data): data_orders = { 'client_id': data['client'], 'user_id': session.get('user') } statuses = select('order_statuses', {'status_name': data['status']}) if statuses: data_orders['status_id'] = statuses[0].id order_id = insert('orders', data_orders) for product in data['products']: data_products = { 'order_id': order_id, 'product_id': product['product'], 'price': product['price'], 'quantity': product['quantity'], } insert('order_products', data_products)
def update_order(order_id, data): order_data = { 'client_id': data['client'], } statuses = select('order_statuses', {'status_name': data['status']}) if statuses: order_data['status_id'] = statuses[0].id update('orders', {'id': order_id}, order_data) delete('order_products', {'order_id': order_id}) for product in data['products']: data_products = { 'order_id': order_id, 'product_id': product['product'], 'price': product['price'], 'quantity': product['quantity'], } insert('order_products', data_products)
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_all_products(where_params={}): products = utils.select('products', where_params) products_list = [] for product in products: products_list.append(get_product(product.id)) return products_list
def get_statuses_choices(): statuses = utils.select('order_statuses') return [(record.status_name, record.status_name) for record in statuses]
def get_all_statuses(): statuses = utils.select('order_statuses') return [Status(record) for record in statuses]
def get_all_clients(where_params={}): clients = utils.select('clients', where_params=where_params) clients_list = [] for client in clients: clients_list.append(get_client(client.id)) return clients_list
def get_initial_status(): data = select('order_statuses', {'status_name': 'New'}) import ipdb ipdb.set_trace() return Status(**data.__dict__)
def get_statuses_choices(): statuses = utils.select("order_statuses") return [(record.status_name, record.status_name) for record in statuses]
def get_all_statuses(): statuses = utils.select("order_statuses") return [Status(record) for record in statuses]
def get_order(id): order = select_one_or_404('orders', {'id': id}) order_products = select('order_products', {'order_id': id}) status = select('order_statuses', {'id': order.status_id})[0].status_name return Order(order.id, order.client_id, order_products, status)