client-barcode: Integrate networking (little error checking for now)
This commit is contained in:
parent
a4c37ce813
commit
5b42351454
|
@ -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:
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue