Litt mer sjekking av inputdata.

Har dessuten fjernet litt ubrukt kode.
This commit is contained in:
Øystein Ingmar Skartsæterhagen 2010-05-09 18:24:05 +00:00
parent 32880031cf
commit daf5ae2294
2 changed files with 86 additions and 83 deletions

5
db.py
View File

@ -18,6 +18,8 @@ class User(Base):
def __init__(self, name, card, credit=0): def __init__(self, name, card, credit=0):
self.name = name self.name = name
if card == '':
card = None
self.card = card self.card = card
self.credit = credit self.credit = credit
@ -34,6 +36,9 @@ class Product(Base):
name = Column(String(30)) name = Column(String(30))
price = Column(Integer) price = Column(Integer)
bar_code_re = r"[0-9]+"
name_re = r".+"
def __init__(self, bar_code, name, price): def __init__(self, bar_code, name, price):
self.name = name self.name = name
self.bar_code = bar_code self.bar_code = bar_code

View File

@ -38,14 +38,29 @@ class Menu():
return i return i
return self.items[i] return self.items[i]
def input_str(self, prompt=None, regex=None): def input_str(self, prompt=None, regex=None, length_range=(None,None),
empty_string_is_none=False):
if regex != None: if regex != None:
while True: while True:
result = self.input_str(prompt) result = self.input_str(prompt, length_range=length_range)
if re.match(regex+'$', result): if 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):
while True:
result = self.input_str(prompt)
length = len(result)
if ((length_range[0] and length < length_range[0]) or
(length_range[1] and length > length_range[1])):
if length_range[0] and length_range[1]:
print 'Value must have length in range [%d,%d]' % length_range
elif allowed_range[0]:
print 'Value must have length at least %d' % length_range[0]
else:
print 'Value must have length at most %d' % length_range[1]
else:
return result
if prompt == None: if prompt == None:
prompt = self.prompt prompt = self.prompt
try: try:
@ -55,6 +70,8 @@ class Menu():
raise ExitMenu() raise ExitMenu()
if result in exit_commands: if result in exit_commands:
raise ExitMenu() raise ExitMenu()
if empty_string_is_none and result == '':
return None
return result return result
def input_int(self, prompt=None, allowed_range=(None,None)): def input_int(self, prompt=None, allowed_range=(None,None)):
@ -162,22 +179,22 @@ class Selector(Menu):
print self.name print self.name
class ChargeMenu(Menu): # class ChargeMenu(Menu):
def __init__(self): # def __init__(self):
self.name = "Add credits to a user account" # self.name = "Add credits to a user account"
def execute(self): # def execute(self):
self.session = Session() # self.session = Session()
amount = self.input_int('Amount to be added> ') # amount = self.input_int('Amount to be added> ')
user = self.input_user('To user>') # user = self.input_user('To user>')
t = Transaction(user, -amount, 'Add '+str(amount)+' to user '+user.name) # t = Transaction(user, -amount, 'Add '+str(amount)+' to user '+user.name)
t.perform_transaction() # t.perform_transaction()
self.session.add(t) # self.session.add(t)
self.session.commit() # self.session.commit()
print 'Added %d kr to user %s\'s account' % (amount, user.name) # print 'Added %d kr to user %s\'s account' % (amount, user.name)
print 'User %s\'s credit is now %d kr' % (user,user.credit) # print 'User %s\'s credit is now %d kr' % (user,user.credit)
self.session.close() # self.session.close()
self.pause() # self.pause()
class TransferMenu(Menu): class TransferMenu(Menu):
def __init__(self): def __init__(self):
@ -186,7 +203,7 @@ 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> ') amount = self.input_int('Transfer amount> ', (-100000,100000))
user1 = self.input_user('From user> ') user1 = self.input_user('From user> ')
user2 = self.input_user('To user> ') user2 = self.input_user('To user> ')
t1 = Transaction(user1, amount, t1 = Transaction(user1, amount,
@ -197,10 +214,13 @@ class TransferMenu(Menu):
t2.perform_transaction() t2.perform_transaction()
self.session.add(t1) self.session.add(t1)
self.session.add(t2) self.session.add(t2)
try:
self.session.commit() self.session.commit()
print 'Transfered %d kr from %s to %s' % (amount, user1, user2) print 'Transfered %d kr from %s to %s' % (amount, user1, user2)
print 'User %s\'s credit is now %d kr' % (user1, user1.credit) print 'User %s\'s credit is now %d kr' % (user1, user1.credit)
print 'User %s\'s credit is now %d kr' % (user2, user2.credit) print 'User %s\'s credit is now %d kr' % (user2, user2.credit)
except sqlalchemy.exc.SQLAlchemyError, e:
print 'Could not perform transfer: %s' % e
self.session.close() self.session.close()
self.pause() self.pause()
@ -212,8 +232,8 @@ class AddUserMenu(Menu):
def _execute(self): def _execute(self):
self.print_header() self.print_header()
self.session = Session() self.session = Session()
username = self.input_str('User name> ', regex=User.name_re) username = self.input_str('User name> ', User.name_re, (1,10))
cardnum = self.input_str('Card number> ', regex=User.card_re) cardnum = self.input_str('Card number> ', User.card_re, (0,10))
user = User(username, cardnum) user = User(username, cardnum)
self.session.add(user) self.session.add(user)
try: try:
@ -233,10 +253,15 @@ class EditUserMenu(Menu):
self.print_header() self.print_header()
self.session = Session() self.session = Session()
user = self.input_user('User> ') user = self.input_user('User> ')
print 'Editing user %s' % user.name
user.card = self.input_str('Card number (currently "%s")> ' % user.card, user.card = self.input_str('Card number (currently "%s")> ' % user.card,
regex=User.card_re) User.card_re, (0,10),
empty_string_is_none=True)
try:
self.session.commit() self.session.commit()
print 'User %s stored' % user.name print 'User %s stored' % user.name
except sqlalchemy.exc.SQLAlchemyError, e:
print 'Could not store user %s: %s' % (user.name,e)
self.session.close() self.session.close()
self.pause() self.pause()
@ -249,14 +274,14 @@ class AddProductMenu(Menu):
self.session = Session() self.session = Session()
self.print_header() self.print_header()
bar_code = self.input_int('Bar code> ') bar_code = self.input_int('Bar code> ')
name = self.input_str('Name> ', regex=r".+") name = self.input_str('Name> ', Product.name_re, (1,30))
price = self.input_int('Price> ', (1,None)) price = self.input_int('Price> ', (1,100000))
product = Product(bar_code, name, price) product = Product(bar_code, name, price)
self.session.add(product) self.session.add(product)
try: try:
self.session.commit() self.session.commit()
print 'Product %s stored' % name print 'Product %s stored' % name
except sqlalchemy.exc.IntegrityError, e: except sqlalchemy.exc.SQLAlchemyError, e:
print 'Could not store product %s: %s' % (name,e) print 'Could not store product %s: %s' % (name,e)
self.session.close() self.session.close()
self.pause() self.pause()
@ -277,12 +302,15 @@ class EditProductMenu(Menu):
('store', 'Store')]) ('store', 'Store')])
what = selector.execute() what = selector.execute()
if what == 'name': if what == 'name':
product.name = self.input_str('Name> ') product.name = self.input_str('Name> ', Product.name_re, (1,30))
elif what == 'price': elif what == 'price':
product.price = self.input_int('Price> ') product.price = self.input_int('Price> ', (1,100000))
elif what == 'store': elif what == 'store':
try:
self.session.commit() self.session.commit()
print 'Product %s stored' % product.name print 'Product %s stored' % product.name
except sqlalchemy.exc.SQLAlchemyError, e:
print 'Could not store product %s: %s' % (product.name, e)
self.session.close() self.session.close()
self.pause() self.pause()
return return
@ -355,10 +383,12 @@ class BuyMenu(Menu):
PurchaseEntry(self.purchase, thing, 1) PurchaseEntry(self.purchase, thing, 1)
self.purchase.perform_purchase() self.purchase.perform_purchase()
self.session.add(self.purchase) self.session.add(self.purchase)
try:
self.session.commit() self.session.commit()
except sqlalchemy.exc.SQLAlchemyError, e:
print 'Could not store purchase: %s' % e
else:
print 'Purchase stored.' print 'Purchase stored.'
self.print_purchase() self.print_purchase()
for t in self.purchase.transactions: for t in self.purchase.transactions:
@ -408,15 +438,18 @@ class AdjustCreditMenu(Menu): # reimplements ChargeMenu; these should be combine
self.session = Session() self.session = Session()
user = self.input_user('User> ') user = self.input_user('User> ')
print 'User %s\'s credit is %d kr' % (user.name, user.credit) print 'User %s\'s credit is %d kr' % (user.name, user.credit)
amount = self.input_int('Add amount> ') amount = self.input_int('Add amount> ', (-100000,100000))
description = self.input_str('Log message> ') description = self.input_str('Log message> ', length_range=(0,50))
if description == '': if description == '':
description = 'manually adjusted credit' description = 'manually adjusted credit'
transaction = Transaction(user, -amount, description) transaction = Transaction(user, -amount, description)
transaction.perform_transaction() transaction.perform_transaction()
self.session.add(transaction) self.session.add(transaction)
try:
self.session.commit() self.session.commit()
print 'User %s\'s credit is now %d kr' % (user.name, user.credit) print 'User %s\'s credit is now %d kr' % (user.name, user.credit)
except sqlalchemy.exc.SQLAlchemyError, e:
print 'Could not store transaction: %s' % e
self.session.close() self.session.close()
self.pause() self.pause()
@ -429,48 +462,13 @@ class ProductListMenu(Menu):
self.print_header() self.print_header()
session = Session() session = Session()
product_list = session.query(Product).all() product_list = session.query(Product).all()
line_format = '%-20s %6s %-15s' line_format = '%-30s | %6s | %-15s'
print line_format % ('name', 'price', 'bar code') print line_format % ('name', 'price', 'bar code')
print '-------------------------------------------' print '---------------------------------------------------------'
for p in product_list: for p in product_list:
print line_format % (p.name, p.price, p.bar_code) print line_format % (p.name, p.price, p.bar_code)
self.pause() self.pause()
class AddProductMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Add product')
def _execute(self):
name = self.input_str('Product name> ')
bar_code = self.input_int('Bar code> ')
price = self.input_int('Price> ')
product = Product(bar_code,name,price)
session = Session()
session.add(product)
session.commit()
session.close()
print 'Added product %s, price %d, bar code %d' % (name,price,bar_code)
self.pause()
#class MainMenu():
# def __init__(self):
# self.menu_list = [Menu("Buy"),ChargeMenu(), Menu("Add User"), Menu("Add Product")]
# def execute(self):
# while 1:
# print "Main Menu: \nWhat do you want to do? \n"
# for i in range(len(self.menu_list)):
# print i+1," ) ",self.menu_list[i].name
# result = raw_input('\nEnter a number corresponding to your action, or "exit" to exit \n')
# if result in ["1","2","3","4"]:
# self.menu_list[int(result)-1].execute()
# elif result in ["quit", "exit", "abort"]:
# print "OK, quitting"
# break
# else:
# print "This does not make sense"
def dwim_search(string, session): def dwim_search(string, session):
typ = guess_data_type(string) typ = guess_data_type(string)
@ -529,7 +527,7 @@ def retrieve_product(search_str, session):
#main = MainMenu() #main = MainMenu()
main = Menu('Dibbler main menu', main = Menu('Dibbler main menu',
items=[BuyMenu(), ProductListMenu(), ShowUserMenu(), items=[BuyMenu(), ProductListMenu(), ShowUserMenu(),
AdjustCreditMenu(), ChargeMenu(), TransferMenu(), AdjustCreditMenu(), TransferMenu(),
Menu('Add/edit', Menu('Add/edit',
items=[AddUserMenu(), EditUserMenu(), items=[AddUserMenu(), EditUserMenu(),
AddProductMenu(), EditProductMenu()]) AddProductMenu(), EditProductMenu()])