client-barcode: Integrate networking (little error checking for now)

This commit is contained in:
Sebastian Pipping 2011-10-05 12:31:48 +02:00
parent a4c37ce813
commit 5b42351454
1 changed files with 28 additions and 14 deletions

View File

@ -69,9 +69,11 @@ class Status:
print() print()
if self.transfers: if self.transfers:
print('Geplante Änderungen:') 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))) 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(('%2d) %-40s %c %6.2f Euro') % (i + 1, label, '+' if difference > 0 else '-', abs(difference)))
print(('%3s %-40s %8s') % ('', '', '=============')) print(('%3s %-40s %8s') % ('', '', '============='))
print(('%3s %-40s %c %6.2f Euro') % ('', '', ' ' if self.balance > 0 else '-', abs(self.balance))) 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): def login(self, auth_blob):
assert(not self.logged_in()) assert(not self.logged_in())
user_name = net.get_user_name_from_auth_blob(auth_blob)
self.auth_blob = auth_blob self.auth_blob = auth_blob
self.login_name = 'Sebastian Pipping' ############ self.login_name = user_name
self.balance = decimal.Decimal('0.20') ###################### self.balance = net.get_balance(user_name)
self.transfers = list() self.transfers = list()
def commit(self): def commit(self):
assert(self.logged_in()) 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 self.transfers = None
# Show final balance for some time # Show final balance for some time
@ -115,23 +121,25 @@ class Status:
# Logout # Logout
self._reset() self._reset()
def _do_money(self, label, difference): def find(self, item_id):
log_entry = (label, difference, self.balance) return net.get_item(item_id)
self.transfers.append(log_entry)
self.balance = self.balance + difference
def buy(self, title, price): def buy(self, item):
assert(self.logged_in()) 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): def deposit(self, amount):
assert(self.logged_in()) 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): def undo(self):
assert(self.logged_in()) assert(self.logged_in())
if self.transfers: if self.transfers:
(label, difference, balance_backup) = self.transfers[-1] (command, balance_backup) = self.transfers[-1]
self.transfers.pop() self.transfers.pop()
self.balance = balance_backup self.balance = balance_backup
else: else:
@ -154,7 +162,13 @@ def handle(line, status):
params = call[1:] params = call[1:]
getattr(status, method)(*params) getattr(status, method)(*params)
else: else:
item_id = int(line) # TODO
try:
item = status.find(item_id)
except: # TODO
error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line) error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line)
else:
status.buy(item)
else: else:
status.login(line) status.login(line)