From 5b423514545cbb6b815c94a9c3b154b4ad503b11 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Wed, 5 Oct 2011 12:31:48 +0200 Subject: [PATCH] client-barcode: Integrate networking (little error checking for now) --- client-barcode/freitagskasse.py | 42 ++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/client-barcode/freitagskasse.py b/client-barcode/freitagskasse.py index 08a998d..48330b8 100644 --- a/client-barcode/freitagskasse.py +++ b/client-barcode/freitagskasse.py @@ -69,9 +69,11 @@ class Status: print() if self.transfers: print('Geplante Ă„nderungen:') - initial_balance = self.transfers[0][2] + initial_command, initial_balance = self.transfers[0] print(('%3s %-40s %c %6.2f Euro') % ('', '', ' ' if initial_balance > 0 else '-', abs(initial_balance))) - for i, (label, difference, balance_backup) in enumerate(self.transfers): + for i, (command, balance_backup) in enumerate(self.transfers): + difference = command.difference() + label = command.label() print(('%2d) %-40s %c %6.2f Euro') % (i + 1, label, '+' if difference > 0 else '-', abs(difference))) print(('%3s %-40s %8s') % ('', '', '=============')) print(('%3s %-40s %c %6.2f Euro') % ('', '', ' ' if self.balance > 0 else '-', abs(self.balance))) @@ -97,14 +99,18 @@ class Status: def login(self, auth_blob): assert(not self.logged_in()) + user_name = net.get_user_name_from_auth_blob(auth_blob) self.auth_blob = auth_blob - self.login_name = 'Sebastian Pipping' ############ - self.balance = decimal.Decimal('0.20') ###################### + self.login_name = user_name + self.balance = net.get_balance(user_name) self.transfers = list() def commit(self): assert(self.logged_in()) - # TODO save balance + + # Process command queue + for (command, balance_backup) in self.transfers: + command.run(self.login_name) self.transfers = None # Show final balance for some time @@ -115,23 +121,25 @@ class Status: # Logout self._reset() - def _do_money(self, label, difference): - log_entry = (label, difference, self.balance) - self.transfers.append(log_entry) - self.balance = self.balance + difference + def find(self, item_id): + return net.get_item(item_id) - def buy(self, title, price): + def buy(self, item): assert(self.logged_in()) - self._do_money(title, -price) + log_entry = (BuyCommand(item), self.balance) + self.transfers.append(log_entry) + self.balance = self.balance - item.price def deposit(self, amount): assert(self.logged_in()) - self._do_money('%.2f Euro einzahlen' % amount, amount) + log_entry = (DepositCommand(amount), self.balance) + self.transfers.append(log_entry) + self.balance = self.balance + amount def undo(self): assert(self.logged_in()) if self.transfers: - (label, difference, balance_backup) = self.transfers[-1] + (command, balance_backup) = self.transfers[-1] self.transfers.pop() self.balance = balance_backup else: @@ -154,7 +162,13 @@ def handle(line, status): params = call[1:] getattr(status, method)(*params) else: - error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line) + item_id = int(line) # TODO + try: + item = status.find(item_id) + except: # TODO + error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line) + else: + status.buy(item) else: status.login(line)