Ymse små forbedringer; fikset noen feil i Menu.input_str.

This commit is contained in:
Øystein Ingmar Skartsæterhagen 2010-05-11 20:12:32 +00:00
parent 2074bd237f
commit b5785d9a85
1 changed files with 27 additions and 7 deletions

View File

@ -35,7 +35,7 @@ class Menu():
print self.exit_disallowed_msg print self.exit_disallowed_msg
return return
if self.exit_confirm_msg != None: if self.exit_confirm_msg != None:
if not self.confirm(self.exit_confirm_msg): if not self.confirm(self.exit_confirm_msg, default=True):
return return
raise ExitMenu() raise ExitMenu()
@ -48,6 +48,9 @@ class Menu():
if self.context != None and display: if self.context != None and display:
print self.context print self.context
def add_to_context(self, string):
self.context += string
def printc(self, string): def printc(self, string):
print string print string
if self.context == None: if self.context == None:
@ -82,14 +85,18 @@ class Menu():
empty_string_is_none=False): empty_string_is_none=False):
if regex != None: if regex != None:
while True: while True:
result = self.input_str(prompt, length_range=length_range) result = self.input_str(prompt, length_range=length_range,
if re.match(regex+'$', result): empty_string_is_none=empty_string_is_none)
if result == None or re.match(regex+'$', result):
return result return result
else: else:
print 'Value must match regular expression "%s"' % regex print 'Value must match regular expression "%s"' % regex
if length_range != (None,None): if length_range != (None,None):
while True: while True:
result = self.input_str(prompt) result = self.input_str(prompt, empty_string_is_none=empty_string_is_none)
if result == None:
length = 0
else:
length = len(result) length = len(result)
if ((length_range[0] and length < length_range[0]) or if ((length_range[0] and length < length_range[0]) or
(length_range[1] and length > length_range[1])): (length_range[1] and length > length_range[1])):
@ -317,9 +324,13 @@ class TransferMenu(Menu):
def _execute(self): def _execute(self):
self.print_header() self.print_header()
self.session = Session() self.session = Session()
amount = self.input_int('Transfer amount> ', (-100000,100000)) amount = self.input_int('Transfer amount> ', (1,100000))
self.set_context('Transfering %d kr' % amount, display=False)
user1 = self.input_user('From user> ') user1 = self.input_user('From user> ')
self.add_to_context(' from ' + user1.name)
user2 = self.input_user('To user> ') user2 = self.input_user('To user> ')
self.add_to_context(' to ' + user2.name)
t1 = Transaction(user1, amount, t1 = Transaction(user1, amount,
'transfer to '+user2.name) 'transfer to '+user2.name)
t2 = Transaction(user2, -amount, t2 = Transaction(user2, -amount,
@ -362,13 +373,22 @@ class AddUserMenu(Menu):
class EditUserMenu(Menu): class EditUserMenu(Menu):
def __init__(self): def __init__(self):
Menu.__init__(self, 'Edit user') Menu.__init__(self, 'Edit user')
self.help_text = '''
The only editable part of a user is its card number.
First select an existing user, then enter a new card number for that
user (write an empty line to remove the card number).
'''
def _execute(self): def _execute(self):
self.print_header() self.print_header()
self.session = Session() self.session = Session()
user = self.input_user('User> ') user = self.input_user('User> ')
self.printc('Editing user %s' % user.name) self.printc('Editing user %s' % user.name)
user.card = self.input_str('Card number (currently "%s")> ' % user.card, card_str = '"%s"' % user.card
if user.card == None:
card_str = 'empty'
user.card = self.input_str('Card number (currently %s)> ' % card_str,
User.card_re, (0,10), User.card_re, (0,10),
empty_string_is_none=True) empty_string_is_none=True)
try: try: