2010-05-07 19:32:39 +02:00
|
|
|
from db import *
|
|
|
|
from sqlalchemy import or_
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
def search_user(string, session):
|
|
|
|
exact_match = session.query(User).filter(or_(User.name==string, User.card==string)).first()
|
|
|
|
if exact_match:
|
|
|
|
return exact_match
|
|
|
|
user_list = session.query(User).filter(or_(User.name.like('%'+string+'%'),User.card.like('%'+string+'%'))).all()
|
|
|
|
return user_list
|
|
|
|
|
|
|
|
def search_product(string, session):
|
|
|
|
exact_match = session.query(Product)\
|
|
|
|
.filter(or_(Product.bar_code==string,
|
|
|
|
Product.name==string)).first()
|
|
|
|
if exact_match:
|
|
|
|
return exact_match
|
|
|
|
product_list = session.query(Product)\
|
|
|
|
.filter(or_(Product.bar_code.like('%'+string+'%'),
|
|
|
|
Product.name.like('%'+string+'%'))).all()
|
|
|
|
return product_list
|
|
|
|
|
|
|
|
def guess_data_type(string):
|
|
|
|
if string.startswith('NTNU'):
|
|
|
|
return 'card'
|
|
|
|
if string.isdigit():
|
|
|
|
return 'bar_code'
|
|
|
|
if string.isalpha() and string.islower():
|
|
|
|
return 'username'
|
|
|
|
return 'product_name'
|
|
|
|
|
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
def retrieve_user(string, session):
|
2010-05-08 12:29:03 +02:00
|
|
|
# first = session.query(User).filter(or_(User.name==string, User.card==string)).first()
|
|
|
|
search = search_user(string,session)
|
|
|
|
if isinstance(search,User):
|
|
|
|
print "Found user "+search.name
|
|
|
|
return search
|
2010-05-07 19:32:39 +02:00
|
|
|
else:
|
2010-05-08 12:29:03 +02:00
|
|
|
if len(search) == 0:
|
2010-05-07 19:32:39 +02:00
|
|
|
print "No users found matching your search"
|
|
|
|
return None
|
2010-05-08 12:29:03 +02:00
|
|
|
if len(search) == 1:
|
2010-05-07 19:32:39 +02:00
|
|
|
print "Found one user: "+list[0].name
|
|
|
|
if confirm():
|
|
|
|
return list[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
else:
|
2010-05-08 12:29:03 +02:00
|
|
|
print "Found "+str(len(search))+" users:"
|
|
|
|
return select_from_list(search)
|
2010-05-07 19:32:39 +02:00
|
|
|
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
def confirm(prompt='Confirm? (y/n) '):
|
2010-05-07 19:32:39 +02:00
|
|
|
while True:
|
2010-05-08 02:23:21 +02:00
|
|
|
input = raw_input(prompt)
|
2010-05-07 19:32:39 +02:00
|
|
|
if input in ["y","yes"]:
|
|
|
|
return True
|
|
|
|
elif input in ["n","no"]:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
print "Nonsense!"
|
|
|
|
|
|
|
|
def select_from_list(list):
|
|
|
|
while True:
|
|
|
|
for i in range(len(list)):
|
|
|
|
print i+1, " ) ", list[i].name
|
|
|
|
choice = raw_input("Select user :\n")
|
|
|
|
if choice in [str(x+1) for x in range(len(list))]:
|
|
|
|
return list[int(choice)-1]
|
|
|
|
else:
|
|
|
|
return None
|
2010-05-08 20:05:28 +02:00
|
|
|
|
|
|
|
def argmax(d, all=False, value=None):
|
|
|
|
maxarg = None
|
|
|
|
maxargs = []
|
|
|
|
if value != None:
|
|
|
|
dd = d
|
|
|
|
d = {}
|
|
|
|
for key in dd.keys():
|
|
|
|
d[key] = value(dd[key])
|
|
|
|
for key in d.keys():
|
|
|
|
if maxarg == None or d[key] > d[maxarg]:
|
|
|
|
maxarg = key
|
|
|
|
if all:
|
|
|
|
return filter(lambda k: d[k] == d[maxarg], d.keys())
|
|
|
|
return maxarg
|