From 97537eaa83f5bb7426c57c4aa81080752352cf7a Mon Sep 17 00:00:00 2001 From: Christoffer Viken Date: Mon, 5 Jun 2017 13:24:05 +0000 Subject: [PATCH] Updated statistics * Unicode and antipurchase for "by popularity" --- text_interface/stats.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/text_interface/stats.py b/text_interface/stats.py index a0a31f6..11f9093 100644 --- a/text_interface/stats.py +++ b/text_interface/stats.py @@ -18,8 +18,8 @@ class ProductPopularityMenu(Menu): text = '' sub = \ self.session.query(PurchaseEntry.product_id, - func.count('*').label('purchase_count')) \ - .group_by(PurchaseEntry.product_id) \ + func.sum(PurchaseEntry.amount).label('purchase_count')) \ + .filter(PurchaseEntry.amount > 0).group_by(PurchaseEntry.product_id) \ .subquery() product_list = \ self.session.query(Product, sub.c.purchase_count) \ @@ -27,11 +27,14 @@ class ProductPopularityMenu(Menu): .order_by(desc(sub.c.purchase_count)) \ .filter(sub.c.purchase_count is not None) \ .all() - line_format = '%10s | %-' + str(Product.name_length) + 's\n' - text += line_format % ('items sold', 'product') - text += '-' * 58 + '\n' + line_format = u'{0:10s} | {1:>45s}\n' + text += line_format.format('items sold', 'product') + text += '-' * (31 + Product.name_length) + '\n' 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)