Refactor cli.py
This commit is contained in:
parent
3eabeddf74
commit
2325c5f5c3
64
cli.py
Normal file → Executable file
64
cli.py
Normal file → Executable file
|
@ -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:
|
||||
print("No users found.")
|
||||
return
|
||||
|
||||
if users:
|
||||
print(tabulate_users(users))
|
||||
else:
|
||||
print("No users found.")
|
||||
|
||||
|
||||
@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:
|
||||
print("No events found.")
|
||||
return
|
||||
|
||||
if events:
|
||||
print(tabulate_events(events))
|
||||
else:
|
||||
print("No events found.")
|
||||
|
||||
|
||||
@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:
|
||||
print("No products found.")
|
||||
return
|
||||
|
||||
if products:
|
||||
print(tabulate_products(products))
|
||||
else:
|
||||
print("No products found.")
|
||||
|
||||
|
||||
@cli.group('transaction')
|
||||
|
@ -134,26 +144,17 @@ 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()
|
||||
|
||||
print("Total income: {}".format(sum))
|
||||
print("Total:", get_total(t))
|
||||
print()
|
||||
|
||||
|
||||
|
@ -162,11 +163,10 @@ 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__':
|
||||
|
|
Loading…
Reference in New Issue
Block a user