Database schema change. 'Order' becomes 'Transaction'. 'OrderEntry' becomes 'Order'.

This commit is contained in:
crudo 2017-03-22 17:12:15 +01:00
parent 54e814e801
commit 27983883d7
3 changed files with 50 additions and 40 deletions

62
cli.py
View File

@ -4,7 +4,7 @@ from datetime import datetime
from pos.config import Config from pos.config import Config
from pos.logging import init_logging, get_logger from pos.logging import init_logging, get_logger
from pos.database import Database, User, Event, Product, Order from pos.database import Database, User, Event, Product, Transaction
config = Config() config = Config()
conf_db = config.core['DATABASE'] conf_db = config.core['DATABASE']
@ -62,9 +62,9 @@ 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 sum = 0
for order in e.orders: for t in e.transactions:
for entry in order.entries: for o in t.orders:
sum += entry.product.price * entry.quantity 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, sum, e.created_at])
return tabulate(tab, headers='firstrow') return tabulate(tab, headers='firstrow')
@ -128,37 +128,47 @@ def product_list():
print(tabulate_products(products)) print(tabulate_products(products))
@cli.group('order') @cli.group('transaction')
def order(): def transaction():
pass pass
def tabulate_orders(orders): def tabulate_orders(orders):
text = [] tab = [["Product", "Price", "Quantity", "Total"]]
for o in orders: for o in orders:
text.append("Listing order #{} ({}):".format(o.uid, o.created_at)) if o.quantity != 0:
tab = [["Product", "Quantity", "Total"]] tab.append([o.product.name, o.product.price, o.quantity,
total = 0 o.product.price * o.quantity])
for e in o.entries: return tabulate(tab, headers='firstrow')
if e.quantity != 0:
tab.append([e.product.name, e.quantity,
e.product.price * e.quantity])
total += e.product.price * e.quantity
text.append(tabulate(tab, headers='firstrow'))
text.append("Total: {}".format(total))
text.append('\n')
return '\n'.join(text)
@order.command('list') def print_transactions(transactions):
def order_list(): sum = 0
orders = None 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()
@transaction.command('list')
def transaction_list():
with db.get_session() as session: with db.get_session() as session:
orders = session.query(Order).all() transactions = session.query(Transaction).all()
if len(orders) == 0:
print("No orders found.") if len(transactions) == 0:
print("No transactions found.")
return return
print(tabulate_orders(orders))
return print_transactions(transactions)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -112,7 +112,7 @@ class Event(Base):
starts_at = Column(DateTime, nullable=False, default=datetime.now) starts_at = Column(DateTime, nullable=False, default=datetime.now)
ends_at = Column(DateTime) ends_at = Column(DateTime)
orders = relationship('Order', lazy='joined') transactions = relationship('Transaction', lazy='joined')
class Product(Base): class Product(Base):
@ -127,24 +127,24 @@ class Product(Base):
order_entry_association = Table( order_entry_association = Table(
'order_entry_associations', Base.metadata, 'order_entry_associations', Base.metadata,
Column('order_uid', Integer, ForeignKey('orders.uid')), Column('transaction_uid', Integer, ForeignKey('transactions.uid')),
Column('order_entry_uid', Integer, ForeignKey('order_entries.uid')) Column('order_uid', Integer, ForeignKey('orders.uid'))
) )
class Order(Base): class Transaction(Base):
__tablename__ = 'orders' __tablename__ = 'transactions'
uid = Column(Integer, primary_key=True) uid = Column(Integer, primary_key=True)
created_at = Column(DateTime, nullable=False, default=datetime.now) created_at = Column(DateTime, nullable=False, default=datetime.now)
event_uid = Column(Integer, ForeignKey('events.uid'), nullable=False) event_uid = Column(Integer, ForeignKey('events.uid'), nullable=False)
event = relationship('Event') event = relationship('Event')
entries = relationship('OrderEntry', lazy='joined', orders = relationship('Order', lazy='joined',
secondary=order_entry_association) secondary=order_entry_association)
class OrderEntry(Base): class Order(Base):
__tablename__ = 'order_entries' __tablename__ = 'orders'
uid = Column(Integer, primary_key=True) uid = Column(Integer, primary_key=True)
product_uid = Column(Integer, ForeignKey('products.uid'), nullable=False) product_uid = Column(Integer, ForeignKey('products.uid'), nullable=False)
quantity = Column(Integer, nullable=False) quantity = Column(Integer, nullable=False)

10
web.py
View File

@ -5,7 +5,7 @@ from flask_login import LoginManager, login_user, logout_user, login_required
from pos.config import Config from pos.config import Config
from pos.logging import init_logging, get_logger from pos.logging import init_logging, get_logger
from pos.database import Database, User, Product, Event, Order, OrderEntry from pos.database import Database, User, Product, Event, Transaction, Order
config = Config() config = Config()
@ -128,14 +128,14 @@ def sell():
with db.get_session() as session: with db.get_session() as session:
items = request.form.items() items = request.form.items()
entries = [ orders = [
OrderEntry(product_uid=uid, quantity=qty) Order(product_uid=uid, quantity=qty)
for uid, qty in items for uid, qty in items
if int(qty) > 0 if int(qty) > 0
] ]
order = Order(event_uid=event.uid, entries=entries) transaction = Transaction(event_uid=event.uid, orders=orders)
session.add(order) session.add(transaction)
flash("Success!", 'success') flash("Success!", 'success')
return sell_page() return sell_page()