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 _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 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 _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