def search_for_instrument(search_term): """Return a dictionary containing information about a given instrument.""" terms = re.findall(r'[^+ ,;]+', search_term) if not terms: return [] keys = ['display_name', 'name_short', 'name', 'id'] where_clause = Expression(1, OP.EQ, 1) for item in terms: term = str(item) where_clause_part = Expression(1, OP.EQ, 0) for k in keys: field = getattr(Instruments, k) if k == 'id': if re.match('[0-9]+', term): where_clause_part |= ( field == int(term) ) where_clause_part |= ( fn.TO_CHAR(field, '99999999999').contains(term) ) else: where_clause_part |= ( field.contains(term)) where_clause &= (where_clause_part) objs = Instruments.select().where(where_clause).order_by(Instruments.name_short) if not objs: message = 'No instrument entries were retrieved using the terms: \'' message += '\' and \''.join(terms) + '\'' raise cherrypy.HTTPError( '404 No Valid Instruments Located', message) return [QueryBase.format_instrument_block(obj) for obj in objs]
def search_for_user(search_term, option): """Return a dictionary containing information about a given user.""" terms = re.findall(r'[^+ ,;]+', search_term) keys = ['first_name', 'last_name', 'network_id', 'email_address', 'id'] where_clause = Expression(1, OP.EQ, 1) for user_term in terms: user_term = str(user_term) where_clause_part = Expression(1, OP.EQ, 0) for k in keys: if k == 'id': if re.match('[0-9]+', user_term): where_clause_part |= Expression( Users.id, OP.EQ, user_term) where_clause_part |= ( fn.TO_CHAR(Users.id, '99999999999').contains(user_term) ) else: where_clause_part |= ( getattr(Users, k).contains(user_term) ) where_clause &= (where_clause_part) objs = Users.select().where(where_clause).order_by(Users.last_name, Users.first_name) # print objs.sql() if not objs: message = "No user entries were retrieved using the terms: '" message += '\' and \''.join(terms) + '\'' raise cherrypy.HTTPError('404 No Valid Users Located', message) return [QueryBase.format_user_block(obj, option) for obj in objs]