def post_process_serialize_search(self, results, pid_fetcher): """Post process the search results.""" view = request.args.get('view') if view: if view != current_app.config.get( 'SONAR_APP_DEFAULT_ORGANISATION'): results['aggregations'].pop('organisation', {}) else: if current_user_record and not current_user_record.is_superuser: results['aggregations'].pop('organisation', {}) if results['aggregations'].get('year'): results['aggregations']['year']['type'] = 'range' results['aggregations']['year']['config'] = { 'min': 1950, 'max': int(datetime.now().year), 'step': 1 } # Add organisation name for org_term in results.get('aggregations', {}).get('organisation', {}).get('buckets', []): organisation = OrganisationRecord.get_record_by_pid( org_term['key']) if organisation: org_term['name'] = organisation['name'] return super(JSONSerializer, self).post_process_serialize_search(results, pid_fetcher)
def aggregations(self): """Get the search result aggregations.""" aggregations = self._results.aggregations.to_dict() if current_user_record: # Remove organisation facet for non super users if not current_user_record.is_superuser: aggregations.pop('organisation', {}) # Remove user facet for non moderators users if not current_user_record.is_moderator: aggregations.pop('user', {}) # Add organisation name for org_term in aggregations.get('organisation', {}).get('buckets', []): organisation = OrganisationRecord.get_record_by_pid( org_term['key']) if organisation: org_term['name'] = organisation['name'] # Add user name for org_term in aggregations.get('user', {}).get('buckets', []): user = UserRecord.get_record_by_pid(org_term['key']) if user: org_term['name'] = '{last_name}, {first_name}'.format( last_name=user['last_name'], first_name=user['first_name']) return aggregations
def post_process_serialize_search(self, results, pid_fetcher): """Post process the search results.""" view = request.args.get('view') if results['aggregations'].get('year'): results['aggregations']['year']['type'] = 'range' results['aggregations']['year']['config'] = { 'min': 1950, 'max': int(datetime.now().year), 'step': 1 } # Add organisation name for org_term in results.get('aggregations', {}).get('organisation', {}).get('buckets', []): organisation = OrganisationRecord.get_record_by_pid( org_term['key']) if organisation: org_term['name'] = organisation['name'] # Add collection name for org_term in results.get('aggregations', {}).get('collection', {}).get('buckets', []): collection = CollectionRecord.get_record_by_pid(org_term['key']) if collection: org_term['name'] = get_language_value(collection['name']) return super(JSONSerializer, self).post_process_serialize_search(results, pid_fetcher)
def post_process_serialize_search(self, results, pid_fetcher): """Post process the search results.""" if current_user_record: # Remove organisation facet for non super users if not current_user_record.is_superuser: results['aggregations'].pop('organisation', {}) # Remove user facet for non moderators users if not current_user_record.is_moderator: results['aggregations'].pop('user', {}) # Add organisation name for org_term in results.get('aggregations', {}).get('organisation', {}).get('buckets', []): organisation = OrganisationRecord.get_record_by_pid( org_term['key']) if organisation: org_term['name'] = organisation['name'] # Add user name for org_term in results.get('aggregations', {}).get('user', {}).get('buckets', []): user = UserRecord.get_record_by_pid(org_term['key']) if user: org_term['name'] = '{last_name}, {first_name}'.format( last_name=user['last_name'], first_name=user['first_name']) return super(JSONSerializer, self).post_process_serialize_search(results, pid_fetcher)
def import_organisations(file): """Import organisations from JSON file.""" click.secho('Importing organisations from {file}'.format(file=file.name)) indexer = OrganisationIndexer() for record in json.load(file): try: # Check existence in DB db_record = OrganisationRecord.get_record_by_pid(record['code']) if db_record: raise ClickException('Record already exists in DB') # Register record to DB db_record = OrganisationRecord.create(record) db.session.commit() indexer.index(db_record) except Exception as error: click.secho( 'Organisation {org} could not be imported: {error}'.format( org=record, error=str(error)), fg='red') click.secho('Finished', fg='green')
def store_organisation(): """Add organisation record to global variables.""" view = request.view_args.get( 'view', current_app.config.get('SONAR_APP_DEFAULT_ORGANISATION')) if view != current_app.config.get('SONAR_APP_DEFAULT_ORGANISATION'): organisation = OrganisationRecord.get_record_by_pid(view) if not organisation or not organisation.get('isShared'): abort(404) g.organisation = organisation.dumps()
def store_organisation(endpoint, values): """Add organisation record to global variables.""" view = values.pop('view', current_app.config.get('SONAR_APP_DEFAULT_ORGANISATION')) if view != current_app.config.get('SONAR_APP_DEFAULT_ORGANISATION'): organisation = OrganisationRecord.get_record_by_pid(view) if not organisation or not organisation['isShared']: raise Exception('Organisation\'s view is not accessible') g.organisation = organisation.dumps()
def test_create_organisation(app, bucket_location, without_oaiset_signals): """Test create organisation.""" Overdo.create_organisation('test') # Organisation creation OK organisation = OrganisationRecord.get_record_by_pid('test') assert organisation assert organisation['pid'] == 'test' # No organisation key provided with pytest.raises(Exception) as exception: Overdo.create_organisation(None) assert str(exception.value) == 'No key provided'
def get_organisations(record): """Get list of organisations with full data. :param record: Record object. """ organisations = [] for organisation in record.get('organisation', []): organisation_pid = OrganisationRecord.get_pid_by_ref_link( organisation['$ref']) if organisation.get( '$ref') else organisation['pid'] organisations.append( OrganisationRecord.get_record_by_pid(organisation_pid)) return organisations
def _make_organisation(code): data = { 'code': code, 'name': code, 'isShared': True, 'isDedicated': False } record = OrganisationRecord.get_record_by_pid(code) if not record: record = OrganisationRecord.create(data, dbcommit=True) record.reindex() db.session.commit() return record
def populate_files_properties(record): """Add restriction, link and thumbnail to file. :param record: Record object :param file: File dict """ # Load organisation for the record organisation_pid = OrganisationRecord.get_pid_by_ref_link( record['organisation']['$ref']) if record['organisation'].get( '$ref') else record['organisation']['pid'] organisation = OrganisationRecord.get_record_by_pid(organisation_pid) for file in record['_files']: if file.get('type') == 'file': file['restriction'] = get_file_restriction(file, organisation) file['thumbnail'] = get_thumbnail(file, record) file['links'] = get_file_links(file, record)
def import_organisations(file): """Import organisations from JSON file.""" click.secho('Importing organisations from {file}'.format(file=file.name)) directory = os.path.dirname(file.name) indexer = OrganisationIndexer() for record in json.load(file): try: # Check existence in DB db_record = OrganisationRecord.get_record_by_pid(record['code']) if db_record: raise ClickException('Record already exists in DB') files = record.pop('files', []) # Register record to DB db_record = OrganisationRecord.create(record) # Add files for file in files: file_path = os.path.join(directory, file['path']) if os.path.isfile(file_path): with open(file_path, 'rb') as f: db_record.files[file['key']] = BytesIO(f.read()) db_record.commit() db.session.commit() indexer.index(db_record) except Exception as error: click.secho( 'Organisation {org} could not be imported: {error}'.format( org=record, error=str(error)), fg='red') click.secho('Finished', fg='green')
def create_organisation(organisation_key): """Create organisation if not existing and return it. :param str organisation_key: Key (PID) of the organisation. """ if not organisation_key: raise Exception('No key provided') # Get organisation record from database organisation = OrganisationRecord.get_record_by_pid(organisation_key) if not organisation: # Create organisation record organisation = OrganisationRecord.create( { 'code': organisation_key, 'name': organisation_key, 'isShared': False, 'isDedicated': False }, dbcommit=True) organisation.reindex()
def test_import_organisations(app, script_info): """Test import organisations.""" runner = CliRunner() datastore = app.extensions['security'].datastore datastore.create_role(name='admin') # Import ok result = runner.invoke(Cli.import_organisations, ['./tests/ui/organisations/data/valid.json'], obj=script_info) organisation = OrganisationRecord.get_record_by_pid('test') assert organisation assert organisation['pid'] == 'test' # Already existing result = runner.invoke(Cli.import_organisations, ['./tests/ui/organisations/data/valid.json'], obj=script_info) assert result.output.find( 'Organisation {\'code\': \'test\', \'name\': \'Test\'} could not be ' 'imported: Record already exists in DB' )
def _make_organisation(code, is_shared=True): data = { 'code': code, 'name': code, 'isShared': is_shared, 'isDedicated': not is_shared, 'documentsCustomField1': { 'label': [{ 'language': 'eng', 'value': 'Test' }], 'includeInFacets': True } } record = OrganisationRecord.get_record_by_pid(code) if not record: record = OrganisationRecord.create(data, dbcommit=True) record.reindex() db.session.commit() return record
def aggregations(): """Get aggregations list.""" view = request.args.get('view') collection = request.args.get('collection') customFields = [ 'customField1', 'customField2', 'customField3', ] aggregations_list = [ 'document_type', 'controlled_affiliation', 'year', 'collection', 'language', 'author', 'subject', 'organisation', 'subdivision', ] + customFields if view and view != current_app.config.get( 'SONAR_APP_DEFAULT_ORGANISATION'): organisation = OrganisationRecord.get_record_by_pid(view) if organisation and organisation.get('isDedicated') \ and organisation.get('publicDocumentFacets'): aggregations_list = organisation.get('publicDocumentFacets')\ + customFields else: organisation = current_organisation # Remove organisation in dedicated view if 'organisation' in aggregations_list: if view and view != current_app.config.get( 'SONAR_APP_DEFAULT_ORGANISATION'): aggregations_list.remove('organisation') elif current_user_record and not current_user_record.is_superuser: aggregations_list.remove('organisation') # Remove collection in collection context if collection and 'collection' in aggregations_list: aggregations_list.remove('collection') # Custom fields for i in range(1, 4): # Remove custom fields if we are in global view, or the fields is not # configured in organisation. if view == current_app.config.get( 'SONAR_APP_DEFAULT_ORGANISATION' ) or not organisation or not organisation.get( f'documentsCustomField{i}', {}).get('includeInFacets'): aggregations_list.remove(f'customField{i}') elif organisation[f'documentsCustomField{i}'].get('label'): aggregations_list[aggregations_list.index(f'customField{i}')] = { 'key': f'customField{i}', 'name': get_language_value( organisation[f'documentsCustomField{i}']['label']) } # Don't display subdivision in global context if view and 'subdivision' in aggregations_list and \ view == current_app.config.get('SONAR_APP_DEFAULT_ORGANISATION'): aggregations_list.remove('subdivision') return jsonify(aggregations_list)