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
|
import click
|
||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
@ -15,6 +16,15 @@ log = get_logger('cli')
|
||||||
db = Database(**conf_db)
|
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()
|
@click.group()
|
||||||
def cli():
|
def cli():
|
||||||
pass
|
pass
|
||||||
|
@ -44,13 +54,13 @@ def user_add(username, password):
|
||||||
|
|
||||||
@user.command('list')
|
@user.command('list')
|
||||||
def user_list():
|
def user_list():
|
||||||
users = None
|
|
||||||
with db.get_session() as session:
|
with db.get_session() as session:
|
||||||
users = session.query(User).all()
|
users = session.query(User).all()
|
||||||
if len(users) == 0:
|
|
||||||
|
if users:
|
||||||
|
print(tabulate_users(users))
|
||||||
|
else:
|
||||||
print("No users found.")
|
print("No users found.")
|
||||||
return
|
|
||||||
print(tabulate_users(users))
|
|
||||||
|
|
||||||
|
|
||||||
@cli.group('event')
|
@cli.group('event')
|
||||||
|
@ -60,12 +70,11 @@ def event():
|
||||||
|
|
||||||
def tabulate_events(events):
|
def tabulate_events(events):
|
||||||
tab = [["UID", "Name", "Starts at", "Ends at", "Income", "Created at"]]
|
tab = [["UID", "Name", "Starts at", "Ends at", "Income", "Created at"]]
|
||||||
|
|
||||||
for e in events:
|
for e in events:
|
||||||
sum = 0
|
tab.append([e.uid, e.name, e.starts_at,
|
||||||
for t in e.transactions:
|
e.ends_at, get_income(e), e.created_at])
|
||||||
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])
|
|
||||||
return tabulate(tab, headers='firstrow')
|
return tabulate(tab, headers='firstrow')
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,6 +86,7 @@ def event_add(name, start, end):
|
||||||
start = datetime.strptime(start, "%Y-%m-%d %H:%M")
|
start = datetime.strptime(start, "%Y-%m-%d %H:%M")
|
||||||
end = datetime.strptime(end, "%Y-%m-%d %H:%M")
|
end = datetime.strptime(end, "%Y-%m-%d %H:%M")
|
||||||
event = Event(name=name, starts_at=start, ends_at=end)
|
event = Event(name=name, starts_at=start, ends_at=end)
|
||||||
|
|
||||||
with db.get_session() as session:
|
with db.get_session() as session:
|
||||||
session.add(event)
|
session.add(event)
|
||||||
print("Event succesfully added.")
|
print("Event succesfully added.")
|
||||||
|
@ -84,13 +94,13 @@ def event_add(name, start, end):
|
||||||
|
|
||||||
@event.command('list')
|
@event.command('list')
|
||||||
def event_list():
|
def event_list():
|
||||||
events = None
|
|
||||||
with db.get_session() as session:
|
with db.get_session() as session:
|
||||||
events = session.query(Event).all()
|
events = session.query(Event).all()
|
||||||
if len(events) == 0:
|
|
||||||
|
if events:
|
||||||
|
print(tabulate_events(events))
|
||||||
|
else:
|
||||||
print("No events found.")
|
print("No events found.")
|
||||||
return
|
|
||||||
print(tabulate_events(events))
|
|
||||||
|
|
||||||
|
|
||||||
@cli.group('product')
|
@cli.group('product')
|
||||||
|
@ -117,13 +127,13 @@ def product_add(name, price):
|
||||||
|
|
||||||
@product.command('list')
|
@product.command('list')
|
||||||
def product_list():
|
def product_list():
|
||||||
products = None
|
|
||||||
with db.get_session() as session:
|
with db.get_session() as session:
|
||||||
products = session.query(Product).all()
|
products = session.query(Product).all()
|
||||||
if len(products) == 0:
|
|
||||||
|
if products:
|
||||||
|
print(tabulate_products(products))
|
||||||
|
else:
|
||||||
print("No products found.")
|
print("No products found.")
|
||||||
return
|
|
||||||
print(tabulate_products(products))
|
|
||||||
|
|
||||||
|
|
||||||
@cli.group('transaction')
|
@cli.group('transaction')
|
||||||
|
@ -134,39 +144,29 @@ def transaction():
|
||||||
def tabulate_orders(orders):
|
def tabulate_orders(orders):
|
||||||
tab = [["Product", "Price", "Quantity", "Total"]]
|
tab = [["Product", "Price", "Quantity", "Total"]]
|
||||||
for o in orders:
|
for o in orders:
|
||||||
if o.quantity != 0:
|
if o.quantity > 0:
|
||||||
tab.append([o.product.name, o.product.price, o.quantity,
|
tab.append([o.product.name, o.product.price, o.quantity,
|
||||||
o.product.price * o.quantity])
|
o.product.price * o.quantity])
|
||||||
return tabulate(tab, headers='firstrow')
|
return tabulate(tab, headers='firstrow')
|
||||||
|
|
||||||
|
|
||||||
def print_transactions(transactions):
|
def print_transactions(transactions):
|
||||||
sum = 0
|
|
||||||
for t in transactions:
|
for t in transactions:
|
||||||
print("Listing transaction #{} ({}):".format(t.uid, t.created_at))
|
print("Listing transaction #{} ({}):".format(t.uid, t.created_at))
|
||||||
print(tabulate_orders(t.orders))
|
print(tabulate_orders(t.orders))
|
||||||
|
print("Total:", get_total(t))
|
||||||
tot = 0
|
|
||||||
for o in t.orders:
|
|
||||||
tot += o.product.price * o.quantity
|
|
||||||
sum += tot
|
|
||||||
print("Total: {}".format(tot))
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
print("Total income: {}".format(sum))
|
|
||||||
print()
|
|
||||||
|
|
||||||
|
|
||||||
@transaction.command('list')
|
@transaction.command('list')
|
||||||
def transaction_list():
|
def transaction_list():
|
||||||
with db.get_session() as session:
|
with db.get_session() as session:
|
||||||
transactions = session.query(Transaction).all()
|
transactions = session.query(Transaction).all()
|
||||||
|
|
||||||
if len(transactions) == 0:
|
if transactions:
|
||||||
|
print_transactions(transactions)
|
||||||
|
else:
|
||||||
print("No transactions found.")
|
print("No transactions found.")
|
||||||
return
|
|
||||||
|
|
||||||
return print_transactions(transactions)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user