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 GET(instrument_id=None): """CherryPy GET method.""" if instrument_id is not None and re.match('[0-9]+', instrument_id): cherrypy.log.error('instrument details request') return InstrumentLookup._get_instrument_details(instrument_id) else: message = 'Invalid instrument details lookup request. ' message += "'{0}' is not a valid instrument_id".format( instrument_id) cherrypy.log.error(message) raise HTTPError(status='400 Invalid Request Options', message=QueryBase.instrument_help_block_message())
def get_instruments_for_user(user_id): """Return a list of formatted instrument objects for the indicated user.""" where_clause = ProjectUser().where_clause( {'user': user_id}) instrument_list = (Instruments .select() .distinct() .join(ProjectInstrument, on=(ProjectInstrument.instrument == Instruments.id)) .join(ProjectUser, on=(ProjectUser.project == ProjectInstrument.project)) .where(where_clause) .order_by(Instruments.display_name)) return [QueryBase.format_instrument_block(obj) for obj in instrument_list]
def generate_instrument_list(): """Generate instrument objects with linkages.""" instrument_list = {} inst_collection = (Instruments.select(Instruments).order_by( Instruments.id).where(Instruments.deleted.is_null())) project_collection = (ProjectInstrument.select().order_by( ProjectInstrument.project)) instruments_with_projects = prefetch(inst_collection, project_collection) for inst in instruments_with_projects: inst_entry = InstQueryBase.format_instrument_block(inst) inst_entry['projects'] = [ proj.project.id for proj in inst.projects ] instrument_list[inst.id] = inst_entry return instrument_list
def _get_instrument_details(instrument_id): """Return a formatted dictionary containing the details of a given Instrument entry.""" terms = re.findall(r'[^+ ,;]+', str(instrument_id)) for term in terms: # Take the first thing that matches standard project id numbering if re.match('[0-9]+', term): instrument_id = term break try: i = Instruments.select(Instruments.id, Instruments.display_name, Instruments.name, Instruments.name_short, Instruments.active).where( Instruments.id == instrument_id).get() except DoesNotExist: message = 'No Instrument with an ID of {0} was found'.format( instrument_id) raise HTTPError('404 Not Found', message) return QueryBase.format_instrument_block(i)