Updated statistics

* Unicode and antipurchase for "by popularity"
This commit is contained in:
Christoffer Viken 2017-06-05 13:24:05 +00:00
parent de24644a51
commit 97537eaa83
1 changed files with 9 additions and 6 deletions

View File

@ -18,8 +18,8 @@ class ProductPopularityMenu(Menu):
text = '' text = ''
sub = \ sub = \
self.session.query(PurchaseEntry.product_id, self.session.query(PurchaseEntry.product_id,
func.count('*').label('purchase_count')) \ func.sum(PurchaseEntry.amount).label('purchase_count')) \
.group_by(PurchaseEntry.product_id) \ .filter(PurchaseEntry.amount > 0).group_by(PurchaseEntry.product_id) \
.subquery() .subquery()
product_list = \ product_list = \
self.session.query(Product, sub.c.purchase_count) \ self.session.query(Product, sub.c.purchase_count) \
@ -27,11 +27,14 @@ class ProductPopularityMenu(Menu):
.order_by(desc(sub.c.purchase_count)) \ .order_by(desc(sub.c.purchase_count)) \
.filter(sub.c.purchase_count is not None) \ .filter(sub.c.purchase_count is not None) \
.all() .all()
line_format = '%10s | %-' + str(Product.name_length) + 's\n' line_format = u'{0:10s} | {1:>45s}\n'
text += line_format % ('items sold', 'product') text += line_format.format('items sold', 'product')
text += '-' * 58 + '\n' text += '-' * (31 + Product.name_length) + '\n'
for product, number in product_list: for product, number in product_list:
text += line_format % (number, product.name) print(product.name)
if number is None:
continue
text += line_format.format(str(number), product.name)
less(text) less(text)