2010-05-07 19:32:39 +02:00
|
|
|
from db import *
|
2017-02-05 00:37:45 +01:00
|
|
|
from sqlalchemy import or_, and_
|
2010-05-13 20:11:31 +02:00
|
|
|
import pwd
|
2010-08-28 16:13:59 +02:00
|
|
|
import subprocess
|
|
|
|
import os
|
2010-09-24 23:24:04 +02:00
|
|
|
import signal
|
2010-05-07 19:32:39 +02:00
|
|
|
|
2018-08-24 09:31:15 +02:00
|
|
|
def search_user(string, session, ignorethisflag=None):
|
|
|
|
string = string.lower()
|
|
|
|
exact_match = session.query(User).filter(or_(User.name == string, User.card == string, User.rfid == string)).first()
|
2017-02-05 00:37:45 +01:00
|
|
|
if exact_match:
|
|
|
|
return exact_match
|
2018-08-24 09:31:15 +02:00
|
|
|
user_list = session.query(User).filter(or_(User.name.ilike(f'%{string}%'),
|
|
|
|
User.card.ilike(f'%{string}%'),
|
|
|
|
User.rfid.ilike(f'%{string}%'))).all()
|
2017-02-05 00:37:45 +01:00
|
|
|
return user_list
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2018-08-24 09:31:15 +02:00
|
|
|
def search_product(string, session, find_hidden_products=True):
|
2017-02-05 00:37:45 +01:00
|
|
|
if find_hidden_products:
|
2018-08-24 09:31:15 +02:00
|
|
|
exact_match = session.query(Product).filter(or_(Product.bar_code == string, Product.name == string)).first()
|
2017-02-05 00:37:45 +01:00
|
|
|
else:
|
2018-08-24 09:31:15 +02:00
|
|
|
exact_match = session.query(Product).filter(or_(Product.bar_code == string,
|
|
|
|
and_(Product.name == string, Product.hidden == False))).first()
|
2017-02-05 00:37:45 +01:00
|
|
|
if exact_match:
|
|
|
|
return exact_match
|
|
|
|
if find_hidden_products:
|
2018-08-24 09:31:15 +02:00
|
|
|
product_list = session.query(Product).filter(or_(Product.bar_code.ilike(f'%{string}%'),
|
|
|
|
Product.name.ilike(f'%{string}%'))).all()
|
2017-02-05 00:37:45 +01:00
|
|
|
else:
|
2018-08-24 09:31:15 +02:00
|
|
|
product_list = session.query(Product).filter(or_(Product.bar_code.ilike(f'%{string}%'),
|
|
|
|
and_(Product.name.ilike(f'%{string}%'),
|
2017-02-05 00:37:45 +01:00
|
|
|
Product.hidden == False))).all()
|
|
|
|
return product_list
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-13 20:11:31 +02:00
|
|
|
|
|
|
|
def system_user_exists(username):
|
2017-02-05 00:37:45 +01:00
|
|
|
try:
|
|
|
|
pwd.getpwnam(username)
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2010-05-13 20:11:31 +02:00
|
|
|
|
|
|
|
def guess_data_type(string):
|
2017-02-05 00:37:45 +01:00
|
|
|
if string.startswith('ntnu') and string[4:].isdigit():
|
|
|
|
return 'card'
|
|
|
|
if string.isdigit() and len(string) == 10:
|
|
|
|
return 'rfid'
|
|
|
|
if string.isdigit() and len(string) in [8,13]:
|
|
|
|
return 'bar_code'
|
2010-05-13 20:11:31 +02:00
|
|
|
# if string.isdigit() and len(string) > 5:
|
2010-05-11 21:23:52 +02:00
|
|
|
# return 'card'
|
2017-02-05 00:37:45 +01:00
|
|
|
if string.isalpha() and string.islower() and system_user_exists(string):
|
|
|
|
return 'username'
|
|
|
|
return None
|
2010-05-08 02:23:21 +02:00
|
|
|
|
|
|
|
|
2010-05-11 21:23:52 +02:00
|
|
|
# def retrieve_user(string, session):
|
|
|
|
# # 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
|
|
|
|
# else:
|
|
|
|
# if len(search) == 0:
|
|
|
|
# print "No users found matching your search"
|
|
|
|
# return None
|
|
|
|
# if len(search) == 1:
|
|
|
|
# print "Found one user: "+list[0].name
|
|
|
|
# if confirm():
|
|
|
|
# return list[0]
|
|
|
|
# else:
|
|
|
|
# return None
|
|
|
|
# else:
|
|
|
|
# print "Found "+str(len(search))+" users:"
|
|
|
|
# return select_from_list(search)
|
2017-02-05 00:37:45 +01:00
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
|
2010-05-11 21:23:52 +02:00
|
|
|
# def confirm(prompt='Confirm? (y/n) '):
|
|
|
|
# while True:
|
|
|
|
# input = raw_input(prompt)
|
|
|
|
# if input in ["y","yes"]:
|
|
|
|
# return True
|
|
|
|
# elif input in ["n","no"]:
|
|
|
|
# return False
|
|
|
|
# else:
|
|
|
|
# print "Nonsense!"
|
2010-05-07 19:32:39 +02:00
|
|
|
|
2010-05-11 21:23:52 +02:00
|
|
|
# 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):
|
2017-02-05 00:37:45 +01:00
|
|
|
maxarg = None
|
|
|
|
maxargs = []
|
|
|
|
if value != None:
|
|
|
|
dd = d
|
|
|
|
d = {}
|
2018-08-19 16:38:45 +02:00
|
|
|
for key in list(dd.keys()):
|
2017-02-05 00:37:45 +01:00
|
|
|
d[key] = value(dd[key])
|
2018-08-19 16:38:45 +02:00
|
|
|
for key in list(d.keys()):
|
2017-02-05 00:37:45 +01:00
|
|
|
if maxarg == None or d[key] > d[maxarg]:
|
|
|
|
maxarg = key
|
|
|
|
if all:
|
2018-08-19 16:38:45 +02:00
|
|
|
return [k for k in list(d.keys()) if d[k] == d[maxarg]]
|
2017-02-05 00:37:45 +01:00
|
|
|
return maxarg
|
2010-05-11 21:23:52 +02:00
|
|
|
|
2010-08-28 16:13:59 +02:00
|
|
|
|
|
|
|
def less(string):
|
2017-02-05 00:37:45 +01:00
|
|
|
'''
|
|
|
|
Run less with string as input; wait until it finishes.
|
|
|
|
'''
|
|
|
|
# If we don't ignore SIGINT while running the `less` process,
|
|
|
|
# it will become a zombie when someone presses C-c.
|
|
|
|
int_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
|
|
env = dict(os.environ)
|
|
|
|
env['LESSSECURE'] = '1'
|
2018-08-24 13:43:35 +02:00
|
|
|
proc = subprocess.Popen('less', env=env, encoding='utf-8', stdin=subprocess.PIPE)
|
|
|
|
proc.communicate(string)
|
2017-02-05 00:37:45 +01:00
|
|
|
signal.signal(signal.SIGINT, int_handler)
|