client-barcode: Split Status.commit() up into sub-functions

master
Sebastian Pipping 13 years ago
parent 27962ad107
commit 156a0df88d

@ -267,63 +267,68 @@ class Status:
def commit(self): def commit(self):
assert(self.logged_in()) assert(self.logged_in())
# Compress DepositCommands def compress_deposit_commands():
dummy, initial_balance = self.transfers[0] dummy, initial_balance = self.transfers[0]
balance_before = initial_balance balance_before = initial_balance
compressed_deposit = DepositCommand(Decimal('0')) compressed_deposit = DepositCommand(Decimal('0'))
others = list() others = list()
for (command, dummy) in list(self.transfers): for (command, dummy) in list(self.transfers):
if isinstance(command, DepositCommand): if isinstance(command, DepositCommand):
compressed_deposit.add(command) compressed_deposit.add(command)
else: else:
others.append((command, balance_before)) others.append((command, balance_before))
balance_before += command.difference() balance_before += command.difference()
if compressed_deposit.difference() != 0: if compressed_deposit.difference() != 0:
others.append((compressed_deposit, balance_before)) others.append((compressed_deposit, balance_before))
self.transfers = others self.transfers = others
# Compress BuyCommands, use a single bulkbuy def process_buy_commands_combined():
dummy, initial_balance = self.transfers[0] # Compress BuyCommands, use a single bulkbuy
balance_before = initial_balance dummy, initial_balance = self.transfers[0]
balance_before = initial_balance
buy_commands = list() buy_commands = list()
non_buy_commands = list() non_buy_commands = list()
total_buy_diff = 0 total_buy_diff = 0
for command, dummy in self.transfers:
if isinstance(command, BuyCommand):
buy_commands.append(command)
else:
balance_before += command.difference()
non_buy_commands.append((command, balance_before))
try: for command, dummy in self.transfers:
net.bulk_buy(buy_commands, self.login_name) if isinstance(command, BuyCommand):
except urllib2.HTTPError as e: buy_commands.append(command)
myDisplay.display_screen("Server error",'Server Error: %s' % str(e)) else:
error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) balance_before += command.difference()
else: non_buy_commands.append((command, balance_before))
self.transfers = non_buy_commands
# Process remaining command queue
for (command, balance_backup) in list(self.transfers):
try: try:
command.run(self.login_name) net.bulk_buy(buy_commands, self.login_name)
except urllib2.HTTPError as e: except urllib2.HTTPError as e:
myDisplay.display_screen("Server error",'Server Error: %s' % str(e)) myDisplay.display_screen("Server error",'Server Error: %s' % str(e))
error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
break
else: else:
self.transfers.pop(0) self.transfers = non_buy_commands
if not self.transfers: def process_commands():
# Show final balance for some time for (command, balance_backup) in list(self.transfers):
clear() try:
self.dump() command.run(self.login_name)
delay('Logout', 3) except urllib2.HTTPError as e:
myDisplay.display_screen("Server error",'Server Error: %s' % str(e))
error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
break
else:
self.transfers.pop(0)
def finish():
if not self.transfers:
# Show final balance for some time
clear()
self.dump()
delay('Logout', 3)
self.logout() self.logout()
compress_deposit_commands()
process_buy_commands_combined()
process_commands()
finish()
def find(self, barcode): def find(self, barcode):
try: try:

Loading…
Cancel
Save