From 2325c5f5c3649242b3a29ac2a0098e0bcb4330bf Mon Sep 17 00:00:00 2001 From: User Identifier Date: Thu, 23 Mar 2017 19:11:41 +0100 Subject: [PATCH] Refactor cli.py --- cli.py | 64 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) mode change 100644 => 100755 cli.py diff --git a/cli.py b/cli.py old mode 100644 new mode 100755 index c08dd7a..aa82a73 --- a/cli.py +++ b/cli.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import click from tabulate import tabulate from datetime import datetime @@ -15,6 +16,15 @@ log = get_logger('cli') db = Database(**conf_db) +def get_total(transaction): + return sum(o.product.price * o.quantity + for o in transaction.orders) + + +def get_income(event): + return sum(get_total(t) for t in event.transactions) + + @click.group() def cli(): pass @@ -44,13 +54,13 @@ def user_add(username, password): @user.command('list') def user_list(): - users = None with db.get_session() as session: users = session.query(User).all() - if len(users) == 0: + + if users: + print(tabulate_users(users)) + else: print("No users found.") - return - print(tabulate_users(users)) @cli.group('event') @@ -60,12 +70,11 @@ def event(): def tabulate_events(events): tab = [["UID", "Name", "Starts at", "Ends at", "Income", "Created at"]] + for e in events: - sum = 0 - for t in e.transactions: - for o in t.orders: - sum += o.product.price * o.quantity - tab.append([e.uid, e.name, e.starts_at, e.ends_at, sum, e.created_at]) + tab.append([e.uid, e.name, e.starts_at, + e.ends_at, get_income(e), e.created_at]) + return tabulate(tab, headers='firstrow') @@ -77,6 +86,7 @@ def event_add(name, start, end): start = datetime.strptime(start, "%Y-%m-%d %H:%M") end = datetime.strptime(end, "%Y-%m-%d %H:%M") event = Event(name=name, starts_at=start, ends_at=end) + with db.get_session() as session: session.add(event) print("Event succesfully added.") @@ -84,13 +94,13 @@ def event_add(name, start, end): @event.command('list') def event_list(): - events = None with db.get_session() as session: events = session.query(Event).all() - if len(events) == 0: + + if events: + print(tabulate_events(events)) + else: print("No events found.") - return - print(tabulate_events(events)) @cli.group('product') @@ -117,13 +127,13 @@ def product_add(name, price): @product.command('list') def product_list(): - products = None with db.get_session() as session: products = session.query(Product).all() - if len(products) == 0: + + if products: + print(tabulate_products(products)) + else: print("No products found.") - return - print(tabulate_products(products)) @cli.group('transaction') @@ -134,39 +144,29 @@ def transaction(): def tabulate_orders(orders): tab = [["Product", "Price", "Quantity", "Total"]] for o in orders: - if o.quantity != 0: + if o.quantity > 0: tab.append([o.product.name, o.product.price, o.quantity, o.product.price * o.quantity]) return tabulate(tab, headers='firstrow') def print_transactions(transactions): - sum = 0 for t in transactions: print("Listing transaction #{} ({}):".format(t.uid, t.created_at)) print(tabulate_orders(t.orders)) - - tot = 0 - for o in t.orders: - tot += o.product.price * o.quantity - sum += tot - print("Total: {}".format(tot)) + print("Total:", get_total(t)) print() - print("Total income: {}".format(sum)) - print() - @transaction.command('list') def transaction_list(): with db.get_session() as session: transactions = session.query(Transaction).all() - if len(transactions) == 0: + if transactions: + print_transactions(transactions) + else: print("No transactions found.") - return - - return print_transactions(transactions) if __name__ == '__main__':