Product/user summary now ordered by number of products bought

This commit is contained in:
Christoffer Viken 2017-02-10 13:56:34 +00:00
parent dd12b05ea2
commit ec977fe155
1 changed files with 6 additions and 17 deletions

View File

@ -825,30 +825,19 @@ class ShowUserMenu(Menu):
less(string) less(string)
def print_purchased_products(self, user): def print_purchased_products(self, user):
products = {} products = []
for ref in user.products: for ref in user.products:
product = ref.product product = ref.product
if product in products: count = ref.count
products[product] += ref.count products.append((product, count))
else:
products[product] = ref.count
#for transaction in user.transactions:
# if transaction.purchase:
# for entry in transaction.purchase.entries:
# n = entry.product.name
# if n in products:
# products[n]+=1
# else:
# products[n]=1
num_products = len(products) num_products = len(products)
if num_products == 0: if num_products == 0:
print 'No products purchased yet' print('No products purchased yet')
else: else:
text = '' text = ''
text += 'Products purchased:\n' text += 'Products purchased:\n'
for product in products: for product, count in products:
text += ('%-'+str(Product.name_length)+'s %3i\n') \ text += '{0:<47} {1:>3}\n'.format(product.name, count)
% (product, products[product])
less(text) less(text)