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.
This commit is contained in:
crudo 2017-03-24 19:23:37 +01:00
parent 800be08471
commit c771aaadbc
3 changed files with 47 additions and 5 deletions

50
cli.py
View File

@ -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

View File

@ -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')

1
web.py
View File

@ -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()