Fikset litt på hurtigkjøpsopplegget.

Man kan nå gjøre hurtigkjøp kun fra hovedmenyen, ikke fra en vilkårlig
meny med undermenyer.
This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-03-07 16:45:15 +00:00
parent ee943404af
commit 0e02a6476c
2 changed files with 91 additions and 70 deletions

3
db.py
View File

@ -30,6 +30,9 @@ class User(Base):
def __str__(self): def __str__(self):
return self.name return self.name
def is_anonymous(self):
return self.card == '11122233'
class Product(Base): class Product(Base):
__tablename__ = 'products' __tablename__ = 'products'

View File

@ -39,6 +39,7 @@ class Menu():
self.context = None self.context = None
self.header_format = '[%s]' self.header_format = '[%s]'
self.uses_db = uses_db self.uses_db = uses_db
self.session = None
def exit_menu(self): def exit_menu(self):
if self.exit_disallowed_msg != None: if self.exit_disallowed_msg != None:
@ -152,16 +153,15 @@ class Menu():
return None return None
return result return result
def thing_in_menu_choice(self, result): def special_input_choice(self, str):
self.session = Session() '''
thing = self.search_for_thing(result) Handle choices which are not simply menu items.
if thing:
BuyMenu(initThing = thing).execute(self.session) Override this in subclasses to implement magic menu
print "" choices. Return True if str was some valid magic menu
self.show_context() choice, False otherwise.
self.session.close() '''
else: return False
print "Please enter an integer"
def input_choice(self, number_of_choices, prompt=None): def input_choice(self, number_of_choices, prompt=None):
if prompt == None: if prompt == None:
@ -169,7 +169,7 @@ class Menu():
while True: while True:
result = self.input_str(prompt) result = self.input_str(prompt)
if result == '': if result == '':
print 'Please enter something' print 'Please enter something'
# 'c' i hovedmenyen for å endre farger # 'c' i hovedmenyen for å endre farger
elif result == 'c': elif result == 'c':
os.system('echo -e "\033['+str(random.randint(40,49))+';'+str(random.randint(30,37))+';5m"') os.system('echo -e "\033['+str(random.randint(40,49))+';'+str(random.randint(30,37))+';5m"')
@ -183,13 +183,12 @@ class Menu():
self.show_context() self.show_context()
else: else:
try: if result.isdigit():
choice = int(result) choice = int(result)
if (choice > 0 and choice <= number_of_choices): if (choice > 0 and choice <= number_of_choices):
return choice return choice
except ValueError: if not self.special_input_choice(result):
pass print 'Please enter a valid choice.'
self.thing_in_menu_choice(result)
def input_int(self, prompt=None, allowed_range=(None,None)): def input_int(self, prompt=None, allowed_range=(None,None)):
@ -376,13 +375,12 @@ it by putting money in the box and using the "Adjust credit" menu.
print 'Help for %s:' % (self.header_format%self.name) print 'Help for %s:' % (self.header_format%self.name)
print self.help_text print self.help_text
def execute(self, session=None): def execute(self, **kwargs):
self.set_context(None) self.set_context(None)
self.session = session
try: try:
if self.uses_db and not session: if self.uses_db and not self.session:
self.session = Session() self.session = Session()
return self._execute() return self._execute(**kwargs)
except ExitMenu: except ExitMenu:
self.at_exit() self.at_exit()
return None return None
@ -799,9 +797,10 @@ class UserListMenu(Menu):
class BuyMenu(Menu): class BuyMenu(Menu):
def __init__(self, initThing=None): def __init__(self, session=None):
Menu.__init__(self, 'Buy', uses_db=True) Menu.__init__(self, 'Buy', uses_db=True)
self.initThing = initThing if session:
self.session = session
self.help_text = ''' self.help_text = '''
Each purchase may contain one or more products and one or more buyers. Each purchase may contain one or more products and one or more buyers.
@ -812,10 +811,26 @@ addition, and you can type 'what' at any time to redisplay it.
When finished, write an empty line to confirm the purchase. When finished, write an empty line to confirm the purchase.
''' '''
def _execute(self): def add_thing_to_purchase(self, thing):
if isinstance(thing, User):
if thing.is_anonymous():
print '--------------------------------------------'
print 'You are now purchasing as the user anonym.'
print 'You have to put money in the anonym-jar.'
print '--------------------------------------------'
Transaction(thing, purchase=self.purchase)
elif isinstance(thing, Product):
PurchaseEntry(self.purchase, thing, 1)
def _execute(self, initialContents=[]):
self.print_header() self.print_header()
self.purchase = Purchase() self.purchase = Purchase()
self.exit_confirm_msg=None self.exit_confirm_msg=None
for thing in initialContents:
self.add_thing_to_purchase(thing)
while True: while True:
self.print_purchase() self.print_purchase()
self.printc({(False,False): 'Enter user or product identification', self.printc({(False,False): 'Enter user or product identification',
@ -824,11 +839,12 @@ When finished, write an empty line to confirm the purchase.
(True,True): 'Enter more products or users, or an empty line to confirm' (True,True): 'Enter more products or users, or an empty line to confirm'
}[(len(self.purchase.transactions) > 0, }[(len(self.purchase.transactions) > 0,
len(self.purchase.entries) > 0)]) len(self.purchase.entries) > 0)])
if self.initThing:
thing = self.initThing # Read in a 'thing' (product or user):
else: thing = self.input_thing(add_nonexisting=('user',),
thing = self.input_thing(add_nonexisting=('user',), empty_input_permitted=True)
empty_input_permitted=True)
# Possibly exit from the menu:
if thing == None: if thing == None:
if not self.complete_input(): if not self.complete_input():
if self.confirm('Not enough information entered. Abort purchase?', if self.confirm('Not enough information entered. Abort purchase?',
@ -841,20 +857,9 @@ When finished, write an empty line to confirm the purchase.
# purchase, we want to protect the # purchase, we want to protect the
# user from accidentally killing it # user from accidentally killing it
self.exit_confirm_msg='Abort purchase?' self.exit_confirm_msg='Abort purchase?'
if isinstance(thing, User):
if thing.card=='11122233': # Add the thing to our purchase object:
print '--------------------------------------------' self.add_thing_to_purchase(thing)
print 'You are now purchasing as the user anonym.'
print 'All you purchases must be done as this user,'
print 'and you have to put money in the anonym-jar.'
print 'Ignore the credit of this user.'
print '--------------------------------------------'
Transaction(thing, purchase=self.purchase)
else:
Transaction(thing, purchase=self.purchase)
elif isinstance(thing, Product):
PurchaseEntry(self.purchase, thing, 1)
self.initThing = None
self.purchase.perform_purchase() self.purchase.perform_purchase()
self.session.add(self.purchase) self.session.add(self.purchase)
@ -866,10 +871,11 @@ When finished, write an empty line to confirm the purchase.
print 'Purchase stored.' print 'Purchase stored.'
self.print_purchase() self.print_purchase()
for t in self.purchase.transactions: for t in self.purchase.transactions:
print 'User %s\'s credit is now %d kr' % (t.user.name, t.user.credit) if not t.user.is_anonymous():
if (t.user.credit < low_credit_warning_limit and t.user.card != '11122233'): print 'User %s\'s credit is now %d kr' % (t.user.name, t.user.credit)
print ('USER %s HAS LOWER CREDIT THAN %d, AND SHOULD CONSIDER PUTTING SOME MONEY IN THE BOX.' if t.user.credit < low_credit_warning_limit:
% (t.user.name, low_credit_warning_limit)) print ('USER %s HAS LOWER CREDIT THAN %d, AND SHOULD CONSIDER PUTTING SOME MONEY IN THE BOX.'
% (t.user.name, low_credit_warning_limit))
self.pause() self.pause()
#skriver til log #skriver til log
#print Product.price #print Product.price
@ -1086,28 +1092,40 @@ if not conf.stop_allowed:
main = Menu('Dibbler main menu', class MainMenu(Menu):
items=[BuyMenu(), def special_input_choice(self, str):
ProductListMenu(), buy_menu = BuyMenu(Session())
ShowUserMenu(), thing = buy_menu.search_for_thing(str)
UserListMenu(), if thing:
AdjustCreditMenu(), buy_menu.execute(initialContents=[thing])
TransferMenu(), print
Menu('Add/edit', self.show_context()
items=[AddUserMenu(), return True
EditUserMenu(), return False
AddProductMenu(),
EditProductMenu(),
AdjustStockMenu(),]), main = MainMenu('Dibbler main menu',
ProductSearchMenu(), items=[BuyMenu(),
Menu('Statistics', ProductListMenu(),
items=[ProductPopularityMenu(), ShowUserMenu(),
ProductRevenueMenu(), UserListMenu(),
BalanceMenu()]), AdjustCreditMenu(),
FAQMenu() TransferMenu(),
], Menu('Add/edit',
exit_msg='happy happy joy joy', items=[AddUserMenu(),
exit_confirm_msg='Really quit Dibbler?') EditUserMenu(),
AddProductMenu(),
EditProductMenu(),
AdjustStockMenu(),]),
ProductSearchMenu(),
Menu('Statistics',
items=[ProductPopularityMenu(),
ProductRevenueMenu(),
BalanceMenu()]),
FAQMenu()
],
exit_msg='happy happy joy joy',
exit_confirm_msg='Really quit Dibbler?')
if not conf.quit_allowed: if not conf.quit_allowed:
main.exit_disallowed_msg = 'You can check out any time you like, but you can never leave.' main.exit_disallowed_msg = 'You can check out any time you like, but you can never leave.'
while True: while True: