From c07c19ca32f0e54744baf0314c4c467a4c1f9e5e Mon Sep 17 00:00:00 2001 From: crudo Date: Fri, 24 Mar 2017 19:23:37 +0100 Subject: [PATCH] Database schema change and new UI feature. The user can now rearrange the products. * Product has a new 'order' column. * The user can now specify the sort order at product creation time with the 'product add --sort INT' command. * The user can now rearrange the products with the 'products set --sort INT' comand. * The user can now show a sorted list of products in cli.py with the 'product list --sort' command. --- cli.py | 50 ++++++++++++++++++++++++++++++++++++++++++++----- pos/database.py | 1 + web.py | 1 + 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/cli.py b/cli.py index 643ccb9..61ab867 100755 --- a/cli.py +++ b/cli.py @@ -224,26 +224,37 @@ def product(): def tabulate_products(products): - tab = [["UID", "Name", "Price", "Enabled", "Created at"]] + tab = [["UID", "Name", "Price", "Sort", "Enabled", "Created at"]] for p in products: - tab.append([p.uid, p.name, p.price, p.is_active, p.created_at]) + tab.append([p.uid, p.name, p.price, p.sort, p.is_active, p.created_at]) return tabulate(tab, headers='firstrow') @product.command('add') @click.argument('name') @click.argument('price') -def product_add(name, price): +@click.option('-s', '--sort', type=click.INT) +def product_add(name, price, sort): product = Product(name=name, price=price) + + if sort: + product.sort = sort + with db.get_session() as session: session.add(product) print("Product successfully added.") @product.command('list') -def product_list(): +@click.option('-s', '--sort', is_flag=True) +def product_list(sort): with db.get_session() as session: - products = session.query(Product).all() + products = session.query(Product) + + if sort: + products = products.order_by(Product.sort.asc()) + + products = products.all() if products: print(tabulate_products(products)) @@ -251,6 +262,35 @@ def product_list(): print("No products found.") +@product.command('set') +@click.option('-n', '--name') +@click.option('-p', '--price', type=click.INT) +@click.option('-s', '--sort', type=click.INT) +@click.argument('product_uid') +def product_set(product_uid, name, price, sort): + with db.get_session() as session: + product = session.query(Product).get(product_uid) + + if not product: + print("No product found with id #{}.".format(product_uid)) + return + + if name: + product.name = name + + if price: + product.price = price + + if sort: + product.sort = sort + + if any([name, price, sort]): + with db.get_session() as session: + session.add(product) + print("Event successfully edited.") + print(tabulate_products([product])) + + @cli.group('transaction') def transaction(): pass diff --git a/pos/database.py b/pos/database.py index cd5576a..4491905 100644 --- a/pos/database.py +++ b/pos/database.py @@ -118,6 +118,7 @@ class Product(Base): uid = Column(Integer, primary_key=True) name = Column(String, nullable=False) price = Column(Integer, nullable=False) + sort = Column(Integer, nullable=False, server_default='0') created_at = Column(DateTime, nullable=False, default=datetime.now) is_active = Column(Boolean, nullable=False, server_default='1') diff --git a/web.py b/web.py index cc8429e..96b0878 100644 --- a/web.py +++ b/web.py @@ -87,6 +87,7 @@ def logout(): def get_products(session): return session.query(Product)\ .filter(Product.is_active == 1)\ + .order_by(Product.sort.asc())\ .all()