From fc58fc83a1451a4674c2d276c9a81a24639aaad6 Mon Sep 17 00:00:00 2001 From: oysteini Date: Sat, 28 Aug 2010 14:13:59 +0000 Subject: [PATCH] =?UTF-8?q?Mulighet=20for=20=C3=A5=20vise=20alle=20transak?= =?UTF-8?q?sjoner=20og=20for=20=C3=A5=20kalle=20den=20nye=20fine=20produkt?= =?UTF-8?q?listetingen=20i=20ShowUserMenu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helpers.py | 11 +++++++++++ text_based.py | 32 +++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/helpers.py b/helpers.py index ddd2fab..e1d7263 100644 --- a/helpers.py +++ b/helpers.py @@ -1,6 +1,8 @@ from db import * from sqlalchemy import or_ import pwd +import subprocess +import os def search_user(string, session): exact_match = session.query(User).filter(or_(User.name==string, User.card==string)).first() @@ -111,3 +113,12 @@ def safe_str(obj): return obj.encode('utf8') else: return safe_str(unicode(obj)) + +def less(string): + ''' + Run less with string as input; wait until it finishes. + ''' + env = dict(os.environ) + env['LESSSECURE'] = '1' + proc = subprocess.Popen('less', env=env, stdin=subprocess.PIPE) + proc.communicate(safe_str(string)) diff --git a/text_based.py b/text_based.py index 7f9f7a9..4a0bd56 100755 --- a/text_based.py +++ b/text_based.py @@ -644,9 +644,35 @@ class ShowUserMenu(Menu): print 'User name: %s' % user.name print 'Card number: %s' % user.card print 'Credit: %s kr' % user.credit - self.print_transactions(user) -# self.print_purchased_products(user) - self.pause() + selector = Selector('What do you want to know about %s?' % user.name, + items=[('transactions', 'Everything (list of all transactions)'), + ('products', 'Which products %s has bought, and how many' % user.name)]) + what = selector.execute() + if what == 'transactions': + self.print_all_transactions(user) + elif what == 'products': + self.print_purchased_products(user) + self.pause() + else: + print 'What what?' + + def print_all_transactions(self, user): + num_trans = len(user.transactions) + string = '%s\'s transactions (%d):\n' % (user.name, num_trans) + for t in user.transactions: + string += ' * %s: %s %d kr, ' % \ + (t.time.strftime('%Y-%m-%d %H:%M'), + {True:'in', False:'out'}[t.amount<0], + abs(t.amount)) + if t.purchase: + string += 'purchase (' + string += ', '.join(map(lambda e: e.product.name, + t.purchase.entries)) + string += ')' + else: + string += t.description + string += '\n' + less(string) def print_transactions(self, user, limit=10): num_trans = len(user.transactions)