def _get_project_details(project_id): """Return a formatted dictionary containing the details of a given Project entry.""" terms = re.findall(r'[^+ ,;]+', str(project_id)) for term in terms: # Take the first thing that matches standard project id numbering if re.match('[0-9]+[a-z]?', term): project_id = term break try: project_entry = (Projects.get(Projects.id == project_id)) except DoesNotExist: message = 'No Project with an ID of {0} was found'.format( project_id) raise HTTPError('404 Not Found', message) proj_inst = TransSIP() pi_where_clause = proj_inst.where_clause({'project_id': project_id}) instrument_entries = (Instruments.select( Instruments.id, Instruments.display_name, Instruments.name, Instruments.name_short, Instruments.active).order_by( Instruments.id).join(TransSIP).where(pi_where_clause)) instruments = { i.id: { 'id': i.id, 'display_name': i.display_name, 'name': i.name, 'name_short': i.name_short, 'active': i.active } for i in instrument_entries } return QueryBase.format_project_block(project_entry, instruments)
def _get_base_transaction_metadata(transaction_entry, option=None): transaction_id = transaction_entry.get('_id') files = QueryBase._get_file_list(transaction_id) # NOTE: add join with relationship table release_state_obj = TransactionUser.select().where( TransactionUser.transaction == transaction_id) if release_state_obj.exists(): rso = release_state_obj.get().to_hash() release_state_info = { 'release_state': 'released', 'release_state_display_name': 'Released', 'release_date': rso.get('created') } else: release_state_info = { 'release_state': 'not_released', 'release_state_display_name': 'Not Released', 'release_date': None } base_metadata = { 'transaction_id': transaction_id, 'submitter_id': transaction_entry.get('submitter'), 'project_id': transaction_entry.get('project'), 'instrument_id': transaction_entry.get('instrument'), 'file_ids': list(files.keys()), 'submitted': transaction_entry.get('created'), 'release_date': release_state_info.get('release_date'), 'release_state': release_state_info.get('release_state'), 'release_state_display': release_state_info.get('release_state_display_name') } if option == 'details': submitter = Users.get( Users.id == transaction_entry.get('submitter') ).to_hash() project = Projects.get( Projects.id == transaction_entry.get('project') ).to_hash() instrument = Instruments.get( Instruments.id == transaction_entry.get('instrument') ).to_hash() details_metadata = { 'submitter_name': u'{0} {1}'.format( submitter.get('first_name'), submitter.get('last_name') ), 'project_name': project.get('title'), 'instrument_name': instrument.get('display_name'), 'files': QueryBase._get_file_key_values(files) } base_metadata.update(details_metadata) return base_metadata
def get_projects_for_user(user_id): """Return a list of formatted project objects for the indicated user.""" # get list of project_ids for this user where_clause = ProjectUser().where_clause({'user_id': user_id}) # pylint: disable=no-member projects = (Projects.select( Projects.id, Projects.title, Projects.actual_start_date, Projects.actual_end_date, Projects.closed_date, Projects.accepted_date, Projects.submitted_date, Projects.project_type).join(ProjectUser).where( where_clause).order_by(Projects.title)) # pylint: enable=no-member return [QueryBase.format_project_block(p) for p in projects if p]
def get_select_query(cls, time_delta, obj_cls, time_field): """Return the select query based on kwargs provided.""" return (Projects.select().join( ProjectUser, JOIN.LEFT_OUTER, on=(ProjectUser.project == Projects.id) ).join(Users, JOIN.LEFT_OUTER, on=(ProjectUser.user == Users.id)).join( Relationships, JOIN.LEFT_OUTER, on=(Relationships.uuid == ProjectUser.relationship)).join( InstitutionUser, JOIN.LEFT_OUTER, on=(Users.id == InstitutionUser.user)).join( Institutions, JOIN.LEFT_OUTER, on=(InstitutionUser.institution == Institutions.id)).join( ProjectInstrument, JOIN.LEFT_OUTER, on=(ProjectInstrument.project == Projects.id)).join( Instruments, JOIN.LEFT_OUTER, on=(ProjectInstrument.instrument == Instruments.id )). join(InstrumentGroup, JOIN.LEFT_OUTER, on=(InstrumentGroup.instrument == Instruments.id)).join( Groups, JOIN.LEFT_OUTER, on=(InstrumentGroup.group == Groups.id)).join( TransSIP, JOIN.LEFT_OUTER, on=(TransSIP.project == Projects.id)).join( TransSAP, JOIN.LEFT_OUTER, on=(TransSAP.project == Projects.id)). where((getattr(Projects, time_field) > time_delta) | (getattr(ProjectUser, time_field) > time_delta) | (getattr(Relationships, time_field) > time_delta) | (getattr(InstitutionUser, time_field) > time_delta) | (getattr(Institutions, time_field) > time_delta) | (getattr(TransSIP, time_field) > time_delta) | (getattr(TransSAP, time_field) > time_delta) | (getattr(Instruments, time_field) > time_delta) | (getattr(InstrumentGroup, time_field) > time_delta) | (getattr(ProjectInstrument, time_field) > time_delta) | (getattr(Groups, time_field) > time_delta)).order_by( Projects.id).distinct())
def search_for_project(search_term): """Return a dictionary containing information about a given project.""" terms = re.findall(r'[^+ ,;]+', search_term) keys = ['title', 'id'] where_clause = Expression(1, OP.EQ, 1) for term in terms: term = str(term) where_clause_part = Expression(1, OP.EQ, 0) for k in keys: if k == 'id': if re.match('[0-9]+[a-z]?', term): where_clause_part |= (Projects.id == term) where_clause_part |= (Projects.id.contains(term)) else: where_clause_part |= (getattr(Projects, k).contains(term)) where_clause &= (where_clause_part) objs = Projects.select().where(where_clause).order_by(Projects.title) if not objs: message = 'No project entries were retrieved using the terms: \'' message += '\' and \''.join(terms) + '\'' raise cherrypy.HTTPError('404 No Valid Projects Located', message) return [QueryBase.format_project_block(obj) for obj in objs]
def generate_project_list(): """Generate project objects with linkages.""" project_list = {} proj_collection = (Projects.select().order_by(Projects.id).where( Projects.deleted.is_null())) instrument_collection = (ProjectInstrument.select().order_by( ProjectInstrument.instrument)) user_collection = (ProjectUser.select().order_by(ProjectUser.user)) projects_with_links = prefetch(proj_collection, instrument_collection, user_collection) for proj in projects_with_links: proj_entry = ProjQueryBase.format_project_block(proj) proj_entry['abstract'] = proj.abstract proj_entry['instruments'] = [ inst.instrument.id for inst in proj.instruments ] proj_entry['users'] = [ user_entry.user.id for user_entry in proj.users ] project_list[proj.id] = proj_entry return project_list
def get_select_query(cls, time_delta, obj_cls, time_field): """Return the select query based on kwargs provided.""" # The alias() method does return a class # pylint: disable=invalid-name ReleaseUsers = Users.alias() SIPUsers = Users.alias() SAPUsers = Users.alias() SIPProjects = Projects.alias() SAPProjects = Projects.alias() # pylint: enable=invalid-name return (Transactions.select().join( TransactionUser, JOIN.LEFT_OUTER, on=(TransactionUser.transaction == Transactions.id)).join( DOITransaction, JOIN.LEFT_OUTER, on=(DOITransaction.transaction == TransactionUser.uuid)).join( Relationships, JOIN.LEFT_OUTER, on=(Relationships.uuid == TransactionUser.relationship )).join( ReleaseUsers, JOIN.LEFT_OUTER, on=(TransactionUser.user == ReleaseUsers.id)).join( TransSIP, JOIN.LEFT_OUTER, on=(TransSIP.id == Transactions.id)).join( TransSAP, JOIN.LEFT_OUTER, on=(TransSAP.id == Transactions.id)). join( SIPUsers, JOIN.LEFT_OUTER, on=(TransSIP.submitter == SIPUsers.id)).join( SAPUsers, JOIN.LEFT_OUTER, on=(TransSAP.submitter == SAPUsers.id)).join( Instruments, JOIN.LEFT_OUTER, on=(Instruments.id == TransSIP.instrument)).join( SIPProjects, JOIN.LEFT_OUTER, on=(SIPProjects.id == TransSIP.project)).join( SAPProjects, JOIN.LEFT_OUTER, on=(SAPProjects.id == TransSAP.project)). join(InstrumentGroup, JOIN.LEFT_OUTER, on=(InstrumentGroup.instrument == TransSIP.instrument )).join( Groups, JOIN.LEFT_OUTER, on=(Groups.id == InstrumentGroup.group)).join( Files, JOIN.LEFT_OUTER, on=(Files.transaction == Transactions.id)). join(TransactionKeyValue, JOIN.LEFT_OUTER, on=(TransactionKeyValue.transaction == Transactions.id )).join( Keys, JOIN.LEFT_OUTER, on=(TransactionKeyValue.key == Keys.id)).join( Values, JOIN.LEFT_OUTER, on=(TransactionKeyValue.value == Values.id)). where((getattr(Files, time_field) > time_delta) | (getattr(DOITransaction, time_field) > time_delta) | (getattr(Transactions, time_field) > time_delta) | (getattr(TransactionUser, time_field) > time_delta) | (getattr(Relationships, time_field) > time_delta) | (getattr(ReleaseUsers, time_field) > time_delta) | (getattr(SIPUsers, time_field) > time_delta) | (getattr(SAPUsers, time_field) > time_delta) | (getattr(SAPProjects, time_field) > time_delta) | (getattr(SIPProjects, time_field) > time_delta) | (getattr(TransSIP, time_field) > time_delta) | (getattr(TransSAP, time_field) > time_delta) | (getattr(Instruments, time_field) > time_delta) | (getattr(InstrumentGroup, time_field) > time_delta) | (getattr(Groups, time_field) > time_delta) | (getattr(TransactionKeyValue, time_field) > time_delta) | (getattr(Keys, time_field) > time_delta) | (getattr(Values, time_field) > time_delta)).order_by( Transactions.id).distinct())
def format_user_block(user_entry, option=None): """Construct a dictionary from a given user instance in the metadata stack.""" user_hash = user_entry.to_hash() project_xref = ProjectUser() where_exp = project_xref.where_clause({'user': user_entry.id}) project_person_query = (ProjectUser.select().where(where_exp)).dicts() project_list = [proj['project'] for proj in project_person_query] clean_projects = {} if project_list: projects = Projects.select( Projects.id, Projects.title, Projects.short_name, Projects.project_type, Projects.science_theme).where( Projects.id << project_list).dicts() for proj in projects: proj_id = proj['id'] clean_projects[proj_id] = proj display_name = u'[User ID {0}] {1} {2} <{3}>'.format( user_entry.id, user_hash.get('first_name'), user_hash.get('last_name'), user_hash.get('email_address')) if option != 'simple': return_block = { 'category': user_hash.get('last_name')[:1], 'user_id': user_hash.get('_id'), 'person_id': user_hash.get('_id'), 'first_name': user_hash.get('first_name'), 'last_name': user_hash.get('last_name'), 'network_id': user_hash.get('network_id'), 'email_address': user_hash.get('email_address'), 'last_updated': user_hash.get('updated'), 'display_name': display_name, 'simple_display_name': u'{0} {1}'.format(user_hash.get('first_name'), user_hash.get('last_name')), 'emsl_employee': False, 'projects': clean_projects } else: return_block = { 'user_id': user_hash.get('_id'), 'person_id': user_hash.get('_id'), 'first_name': user_hash.get('first_name'), 'last_name': user_hash.get('last_name'), 'display_name': u'{0} {1}'.format(user_hash.get('first_name'), user_hash.get('last_name')), 'email_address': user_hash.get('email_address'), 'emsl_employee': False } return_block['emsl_employee'] = QueryBase._is_admin_user(user_entry) return return_block