diff --git a/db.py b/db.py index fef6cc4..4fadc49 100644 --- a/db.py +++ b/db.py @@ -66,6 +66,17 @@ class Product(Base): def __str__(self): return self.name + +class UserProducts(Base): + __tablename__ = 'user_products' + user_name = Column(String(10), ForeignKey('users.name'), primary_key=True) + product_id = Column(Integer, ForeignKey("products.product_id"), primary_key=True) + count = Column(Integer) + + user = relationship(User, backref=backref('products', order_by=count.desc()), lazy='joined') + product = relationship(Product, backref="users", lazy='joined') + + class PurchaseEntry(Base): __tablename__ = 'purchase_entries' id = Column(Integer, primary_key=True) diff --git a/text_based.py b/text_based.py index 396c344..f0752d1 100755 --- a/text_based.py +++ b/text_based.py @@ -826,14 +826,20 @@ class ShowUserMenu(Menu): def print_purchased_products(self, user): products = {} - 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 + for ref in user.products: + product = ref.product + if product in products: + products[product] += ref.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) if num_products == 0: print 'No products purchased yet'