def _get_source_by_issn(self, issnModel: SourceRawData) -> SourceRecord: """ get the source by the issn si el issn no esta en ningun Source, crea uno nuevo, usando la informacion de el modelo ISSN """ issn = issnModel.identifier data = issnModel.issn_data # print("buscando el issn {0}".format(issn)) pid, source = SourceRecord.get_source_by_pid(issn) if source: return source # print("no existe, creando source {0}".format(issn)) for item in data["@graph"]: if item['@id'] == 'resource/ISSN/' + issn + '#KeyTitle': title = item["value"] # print(title) data = dict() data['source_type'] = SourceType.SERIAL.value data['name'] = title data['source_status'] = SourceStatus.UNOFFICIAL.value data['title'] = title data['identifiers'] = [{'idtype': 'pissn', 'value': issn}] user = get_default_user() msg, source = SourceRecord.new_source_revision(data, user.id) if source: return source return None
def sync_records(cls): issn_list = SourceRawData.query.all() for issn in issn_list: record = IssnDataParser.parse(issn.identifier) if record: user = get_default_user() record['source_status'] = SourceStatus.UNOFFICIAL.value record['source_type'] = SourceType.JOURNAL.value print(record) SourceRecord.new_source_revision(record, user_id=user.id, comment='issn.org import')
def delete_all_sources(): ts = TermSources.query.all() for t in ts: db.session.delete(t) db.session.commit() SourceVersion.query.delete() Repository.query.delete() # TODO: delete persistentidentifiers and record metadata s = Source.query.all() for so in s: SourceRecord.delete(data={pids.SOURCE_UUID_FIELD: so.uuid}) SourceRecord.delete_all_pids_without_object( so.data[pids.IDENTIFIERS_FIELD]) db.session.delete(so) db.session.commit()
def repo_schedule_harvest(uuid): """ schedule a harvest of a source params: uuid: Source uuid """ try: source = SourceRecord.get_record(uuid) if not source: raise Exception('Not source found') repository = Repository.query.filter_by(source_uuid=source.id).first() if not repository: raise Exception('Not repository found') if not request.is_json: raise Exception("No JSON data provided") input_data = request.json schedule_datetime = datetime.datetime.now() if 'datetime' in input_data: schedule_datetime = input_data['datetime'] harvester_class = 'iroko.harvester.oai.harvester.OaiHarvester' if 'harvester_class' in input_data: harvester_class = input_data['harvester_class'] # TODO: how instantiate an object from a class name. oai_harvester = OaiHarvester(repository) HarvestersTaskManager.schedule_harvest(oai_harvester, schedule_datetime) return iroko_json_response(IrokoResponseStatus.SUCCESS, 'ok', 'repositories', None) except Exception as e: msg = str(e) return iroko_json_response(IrokoResponseStatus.ERROR, msg, None, None)
def get_editor_source_versions(uuid): try: # listar las versiones de este editor que no se han revisado para que pueda cambiarlas source = SourceRecord.get_record(uuid) # print('source> ', source) if not source: raise Exception('Not source found') if source.user_has_edit_permission(current_user): versions = source.get_editor_versions_not_reviewed() return iroko_json_response( IrokoResponseStatus.SUCCESS, \ 'ok', 'source', \ { 'data': source, 'versions': source_version_schema_many.dump(versions), 'count': 1 } ) except PermissionDenied as err: msg = 'Permission denied for changing source' return iroko_json_response(IrokoResponseStatus.ERROR, msg, None, None) except Exception as e: return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)
def source_unpublish(uuid): # pone source_status = SourceStatus.TO_REVIEW try: source = SourceRecord.get_record(uuid) if not source: raise Exception('Not source found') if source.user_has_manager_permission(current_user): source['source_status'] = SourceStatus.TO_REVIEW.value source.update() # TODO: aqui hay un error con los get managers # notification = NotificationSchema() # notification.classification = NotificationType.INFO # notification.description = _('Se ha despublicado la fuente: {0}.'.format(source[ # 'name'])) # notification.emiter = _('Sistema') # # for user in source.get_managers: # notification.receiver_id = user # Notifications.new_notification(notification) # print('************************** to response') return iroko_json_response(IrokoResponseStatus.SUCCESS, 'ok', 'source', source) except PermissionDenied as err: msg = 'Permission denied for changing source' return iroko_json_response(IrokoResponseStatus.ERROR, msg, None, None) except Exception as e: raise e return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)
def add_source_to_user_profile(cls, user_id, source_uuid, role): # print('add_source_to_user_profile', user_id, source_uuid, role) user_profile = cls.get_or_create_by_userid(user_id) if not user_profile: raise Exception('No user_id={0}'.format(user_id)) pid, source = SourceRecord.get_source_by_pid(source_uuid) if not pid or not source: raise Exception('No source_uuid={0}'.format(source_uuid)) add = True if user_profile.json_metadata: data = dict(user_profile.json_metadata) else: data = dict() if 'sources' not in data: data['sources'] = [] for s in data['sources']: if s['source_uuid'] == source_uuid: s['role'] = role add = False if add: data['sources'].append({'source_uuid': source_uuid, 'role': role}) user_profile.json_metadata = dict(data) db.session.commit()
def get_users_source_editor(uuid): """ get the lists of user with permission of editor and manager :param uuid: source uuid :return: """ try: source = SourceRecord.get_record(uuid) if not source: raise Exception('Not source found') if source.user_has_manager_permission(current_user): ids = get_user_ids_for_source_from_action('source_editor_actions', uuid) users = User.query.filter(User.id.in_(ids)).all() return iroko_json_response( IrokoResponseStatus.SUCCESS, 'ok', 'permission', { 'action': 'editor', 'source': uuid, 'users': user_schema_many.dump(users) }) except Exception as e: return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)
def source_publish(uuid): # inserta un nuevo sourceVersion de un source que ya existe # input_data = request.json # updata el input_data en el SourceRecord try: if not request.is_json: raise Exception("No JSON data provided") input_data = request.json user_id = current_user.id comment = 'no comment' if 'comment' in input_data: comment = input_data['comment'] source = SourceRecord.get_record(uuid) if not source: raise Exception('Not source found') if source.user_has_manager_permission(current_user): data = dict(input_data['data']) data['source_status'] = SourceStatus.APPROVED.value source_version = IrokoSourceVersions.new_version(source.id, data, user_id=user_id, comment=comment, is_current=True) if not source_version: raise Exception('Not source for changing found') source.update(data) # TODO: aqui hay un error con los get managers # notification = NotificationSchema() # notification.classification = NotificationType.INFO # notification.description = _('Se ha publicado una nueva version de la fuente: {0}:{ # 1}.'.format(source['name'], source.id)) # notification.emiter = _('Sistema') # # for user in source.get_managers: # notification.receiver_id = user # Notifications.new_notification(notification) # print('************************** to response') return iroko_json_response(IrokoResponseStatus.SUCCESS, 'ok', 'source', source) except PermissionDenied as err: msg = 'Permission denied for changing source' except PIDObjectAlreadyAssigned as err: msg = 'El Identificador persistente ya existe: ' + str(err) # print('*******', msg) except Exception as e: msg = str(e) # print('*******', msg) return iroko_json_response(IrokoResponseStatus.ERROR, msg, None, None)
def source_new_version(uuid): # inserta un nuevo sourceVersion de un source que ya existe # input_data = request.json # source = Sources.get_source_by_id(uuid=uuid) # Sources.insert_new_source_version(input_data, source, False) try: if not request.is_json: raise Exception("No JSON data provided") input_data = request.json user_id = current_user.id comment = 'no comment' if 'comment' in input_data: comment = input_data['comment'] source = SourceRecord.get_record(uuid) if not source: raise Exception('Not source found') if source.user_has_edit_permission(current_user): # si no esta aprobada significa que siempre es la current. # si esta aprobada el proceso es otro # print(input_data) data = dict(input_data['data']) # data['source_status'] = SourceStatus.TO_REVIEW.value # # source.update(data) source_version = IrokoSourceVersions.new_version(source.id, data, user_id=user_id, comment=comment, is_current=False) if not source_version: raise Exception('Not source for changing found') # notification = NotificationSchema() # notification.classification = NotificationType.INFO # notification.description = _('Editor has change this source: {0}.'.format(source[ # 'name'])) # notification.emiter = _('Sistema') # # for user in source.get_managers: # notification.receiver_id = user # Notifications.new_notification(notification) return iroko_json_response( IrokoResponseStatus.SUCCESS, 'ok', 'source_version', source_version_schema.dump(source_version)) except PermissionDenied as err: msg = 'Permission denied for changing source' return iroko_json_response(IrokoResponseStatus.ERROR, msg, None, None) except Exception as e: raise e
def fix_data_field(self, result, version: SourceVersion, **kwargs): source = SourceRecord.get_record(version.source_uuid) # print('************* POST DUMP ***************') # print(source.model.json['source_type'], ) if source.model.json['source_type'] == SourceType.JOURNAL.value: data = journal_data_schema.dump(version.data) else: data = source_base_data_schema.dump(version.data) result['data'] = data # print('************* POST DUMP ***************') return result
def init_term_sources(): path = current_app.config['INIT_JOURNALS_JSON_PATH'] path_tax = current_app.config['INIT_TAXONOMY_JSON_PATH'] with open(path) as fsource, open(path_tax) as ftax: data = json.load(fsource, object_hook=remove_nulls) tax = json.load(ftax) inserted = {} if isinstance(data, dict): for k, record in data.items(): SourceRecord.get() if not inserted.__contains__(record['title']): inserted[record['title']] = record['title'] source = Source.query.filter_by( name=record['title']).first() if record.__contains__('institution'): add_term_source(source, record, record['institution'], tax, 'institutions') if record.__contains__('licence'): add_term_source(source, record, record['licence'], tax, 'licences') # if record.__contains__('source_category'): # add_term_source(source, record, record['source_category'], tax, # 'grupo_mes') # for subid in record["subjects"]: # add_term_source(source, record, subid, tax, 'subjects') # for ref in record["referecences"]: # if ref.__contains__('url'): # add_term_source(source, record, ref['name'], tax, 'data_bases', # {'url': ref['url']}) # else: # add_term_source(source, record, ref['name'], tax, 'data_bases') db.session.commit()
def get_source_by_pid(): """Get a source by any PID received as a argument, including UUID""" try: pidvalue = request.args.get('value') pid, source = SourceRecord.get_source_by_pid(pidvalue) if not source or not pid: raise Exception('Source not found') return source_v1_response(pid, source) # return iroko_json_response(IrokoResponseStatus.SUCCESS, \ # 'ok', 'source', \ # {'data': source}) except Exception as e: return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)
def set_source_editor(user, uuid, allow=False): """ Set user as editor of a source :param uuid: source uuid :param user: user id :param allow: if allow or deny :return: """ try: offset = request.args.get('offset') if request.args.get( 'offset') else 3 source = SourceRecord.get_record(uuid) if not source: raise Exception('Not source found') userObj = User.query.filter_by(id=user).first() if not userObj: raise Exception('User not found') if source.user_has_manager_permission(current_user): with db.session.begin_nested(): ActionUsers.query.filter_by(user_id=user, action='source_editor_actions', argument=uuid).delete() if allow: db.session.add( ActionUsers.allow(ObjectSourceEditor(uuid), user=userObj)) else: db.session.add( ActionUsers.deny(ObjectSourceEditor(uuid), user=userObj)) db.session.commit() return iroko_json_response( IrokoResponseStatus.SUCCESS, 'ok', 'permission', { 'source': uuid, 'user': user, 'permission': 'editor', 'allow': allow }) raise PermissionDenied() except PermissionDenied as err: msg = 'Permission denied' return iroko_json_response(IrokoResponseStatus.ERROR, msg, None, None) except Exception as e: return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)
def repo_status(uuid): """ get the status of a repository params: uuid: Source uuid """ try: source = SourceRecord.get_record(uuid) if not source: raise Exception('Not source found') repository = Repository.query.filter_by(source_uuid=source.id).first() if not repository: raise Exception('Not repository found') # TODO: get all the process info of the record return iroko_json_response(IrokoResponseStatus.SUCCESS, 'ok', 'repositories', None) except Exception as e: msg = str(e) return iroko_json_response(IrokoResponseStatus.ERROR, msg, None, None)
def get_source_by_uuid(uuid): """Get all source versions by source UUID, with permission checking""" # source = SourcesDeprecated.get_source_by_id(uuid=uuid) # return iroko_json_response(IrokoResponseStatus.SUCCESS, \ # 'ok','versions', \ # source_schema.dump(source)) try: source = SourceRecord.get_record(uuid) # SourcesDeprecated.get_source_by_id(uuid=uuid) if not source: raise Exception('Source not found') terms = helper_get_classifications_string(source) # # # print(source.model.json) # if 'classifications' in source.model.json: # for term in source.model.json['classifications']: # if 'id' in term: # terms = terms + str(term['id']) + ',' # if terms: # terms = terms[0:-1] if user_has_editor_or_manager_permissions({ 'terms': terms, 'uuid': uuid }): # # # print(source.data) # for v in source.term_sources: # # print(v.term_id, v.sources_id, v.data) # # # print(v.data) versions = IrokoSourceVersions.get_versions(uuid) return iroko_json_response( IrokoResponseStatus.SUCCESS, \ 'ok', 'versions', \ source_version_schema_many.dump(versions) ) raise PermissionDenied('No tiene permiso') except Exception as e: # print(traceback.format_exc()) return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)
def get_source_by_issn(issn): """Get a source by any PID received as a argument, including UUID""" # print('eres la api correcta?') # print('eres la api correcta?') try: # print('eres la api correcta?') # pid, source = SourceRecord.create_or_get_source_by_issn(issn) pid, source = SourceRecord.get_source_by_pid(issn) if not source or not pid: raise Exception('Source not found') return source_v1_response(pid, source) # return iroko_json_response(IrokoResponseStatus.SUCCESS, \ # 'ok', 'source', \ # {'data': source}) except Exception as e: return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)
def get_source_versions(uuid): """Get all source versions by source UUID, with permission checking""" try: source = SourceRecord.get_record(uuid) if not source: raise Exception('Source not found') if source.user_has_edit_permission(current_user): versions = IrokoSourceVersions.get_versions(uuid) dd = source_version_schema_many.dump(versions) # print(dd) return iroko_json_response( IrokoResponseStatus.SUCCESS, \ 'ok', 'versions', \ dd ) except Exception as e: # print() return iroko_json_response(IrokoResponseStatus.ERROR, traceback.format_exc(), None, None)
def get_source_by_uuid_perm(uuid): """Get a source by any PID received as a argument, including UUID""" try: pid, source = SourceRecord.get_source_by_pid(uuid) if not source or not pid: raise Exception('Source not found') print(dict(source)) try: if source.user_has_manager_permission(current_user): return iroko_json_response( IrokoResponseStatus.SUCCESS, \ 'ok', 'source', \ { 'record': source_v1.transform_record(pid, source), 'allows': 'publish' } ) except PermissionDenied as err: if source.user_has_edit_permission(current_user): # # print('*********************************') # # print(source) # record = source_v1.preprocess_record(pid, source) # # print(record) # # print('*********************************') # record['allows'] = 'edit' return iroko_json_response( IrokoResponseStatus.SUCCESS, \ 'ok', 'source', \ { 'record': source_v1.transform_record(pid, source), 'allows': 'edit' } ) except Exception as e: print(traceback.format_exc()) return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)
def init_repos(): datadir = current_app.config['IROKO_DATA_DIRECTORY'] path = os.path.join(datadir, 'repos.json') user = get_default_user() with open(path) as repos: raw = json.load(repos, object_hook=remove_nulls) for k, record in raw.items(): source = dict() data = dict() data['source_type'] = SourceType.REPOSITORY.value data['name'] = record['name'] data['title'] = record['name'] ids = [] if 'url' in record: data['url'] = record['url'] ids.append(dict(idtype='url', value=record['url'])) if 'oaiurl' in record: data['oaiurl'] = record['oaiurl'] ids.append(dict(idtype='oaiurl', value=record['oaiurl'])) data[pids.IDENTIFIERS_FIELD] = ids source['data'] = data data['source_status'] = SourceStatus.UNOFFICIAL.value user = get_default_user() data['_save_info'] = { 'user_id': str(user.id), 'comment': 'seed data', 'updated': str(datetime.date.today()) } new_source = SourceRecord.new_source_revision( data, user.id, 'seed data')
def full_source(self, record, **kwargs): source_uuid = record['source_repo']['uuid'] source = SourceRecord.get_record(source_uuid) record['source_repo'] = source return record
def init_journals(): # sources_path = '../../data/journals.json' # delete_all_sources() # print('delete all source and relations') path = current_app.config['INIT_JOURNALS_JSON_PATH'] path_tax = current_app.config['INIT_TAXONOMY_JSON_PATH'] path_oai = current_app.config['INIT_OAIURL_JSON_PATH'] user = User.query.filter_by(email='*****@*****.**').first() with open(path) as fsource, open(path_oai) as foai, open(path_tax) as ftax: data = json.load(fsource, object_hook=remove_nulls) urls = json.load(foai) tax = json.load(ftax) inserted = {} if isinstance(data, dict): for k, record in data.items(): if not inserted.__contains__(record['title']): inserted[record['title']] = record['title'] # print(record['title']) source = dict() data = dict() data['source_type'] = SourceType.JOURNAL.value data['name'] = record['title'] ids = [] _assing_if_exist(data, record, 'title') _assing_if_exist(data, record, 'description') _assing_if_exist(data, record, 'email') _assing_if_exist(data, record, 'logo') _assing_if_exist(data, record, 'seriadas_cubanas') if 'url' in record: data['url'] = record['url'] ids.append(dict(idtype='url', value=record['url'])) if 'rnps' in record: data['rnps'] = {'p': record['rnps'], 'e': ''} ids.append(dict(idtype='prnps', value=record['rnps'])) issn = {} if 'p' in record['issn']: issn['p'] = record['issn']['p'] ids.append( dict(idtype='pissn', value=record['issn']['p'])) if 'e' in record['issn']: issn['e'] = record['issn']['e'] ids.append( dict(idtype='eissn', value=record['issn']['e'])) if 'l' in record['issn']: issn['l'] = record['issn']['l'] ids.append( dict(idtype='lissn', value=record['issn']['l'])) data['issn'] = issn for url in urls: if url['id'] == k: data['oaiurl'] = url['url'] ids.append(dict(idtype='oaiurl', value=url['url'])) data[pids.IDENTIFIERS_FIELD] = ids # SourceRecord.delete_all_pids_without_object(data[pids.IDENTIFIERS_FIELD]) source['data'] = data if 'licence' in record: data['classifications'] = [] name = string_as_identifier( tax['licences'][record['licence']]["name"]) term = Term.query.filter_by(identifier=name).first() data['classifications'].append({ 'id': str(term.uuid), 'description': term.description, 'vocabulary': term.vocabulary_id }) data['source_type'] = SourceType.JOURNAL.value data['source_status'] = SourceStatus.UNOFFICIAL.value user = User.query.filter_by( email='*****@*****.**').first() data['_save_info'] = { 'user_id': str(user.id), 'comment': 'initial version' } new_source = SourceRecord.new_source_revision(data, user)
def view_source_id(uuid): src = SourceRecord.get_record(uuid) # source = source_schema_many.dump(src) return render_template('iroko_theme/sources/source.html', source=src)
def init_journals(): # sources_path = '../../data/journals.json' # delete_all_sources() # print('delete all source and relations') datadir = current_app.config['IROKO_DATA_DIRECTORY'] path = os.path.join(datadir, 'journals.json') path_tax = os.path.join(datadir, 'vocabularies.json') path_oai = os.path.join(datadir, 'oaisources.json') user = User.query.filter_by(email='*****@*****.**').first() org_cache = dict() with open(path) as fsource, open(path_oai) as foai, open(path_tax) as ftax: data = json.load(fsource, object_hook=remove_nulls) urls = json.load(foai) tax = json.load(ftax) inserted = {} if isinstance(data, dict): for k, record in data.items(): if not inserted.__contains__(record['title']): inserted[record['title']] = record['title'] # print(record['title']) source = dict() data = dict() data['source_type'] = SourceType.JOURNAL.value data['name'] = record['title'] ids = [] _assing_if_exist(data, record, 'title') _assing_if_exist(data, record, 'description') _assing_if_exist(data, record, 'email') _assing_if_exist(data, record, 'logo') _assing_if_exist(data, record, 'seriadas_cubanas') if 'url' in record: data['url'] = record['url'] ids.append(dict(idtype='url', value=record['url'])) if 'rnps' in record: data['rnps'] = {'p': record['rnps'], 'e': ''} ids.append(dict(idtype='prnps', value=record['rnps'])) issn = {} issn_org = None if 'p' in record['issn']: issn['p'] = record['issn']['p'] issn_org = IssnDataParser.parse(record['issn']['p']) # ids.append(dict(idtype='issn_p', value=record['issn']['p'])) if 'e' in record['issn']: issn_org = IssnDataParser.parse(record['issn']['e']) issn['e'] = record['issn']['e'] if 'l' in record['issn']: issn_org = IssnDataParser.parse(record['issn']['l']) issn['l'] = record['issn']['l'] data['issn'] = issn if issn_org: data['name'] = issn_org['name'] data['title'] = issn_org['title'] data['aliases'] = issn_org['aliases'] ids.extend(issn_org['identifiers']) for url in urls: if url['id'] == k: data['oaiurl'] = url['url'] ids.append(dict(idtype='oaiurl', value=url['url'])) data[pids.IDENTIFIERS_FIELD] = ids # SourceRecord.delete_all_pids_without_object(data[pids.IDENTIFIERS_FIELD]) source['data'] = data if 'licence' in record: data['classifications'] = [] name = string_as_identifier( tax['licences'][record['licence']]["name"]) term = Term.query.filter_by(identifier=name).first() data['classifications'].append({ 'id': str(term.uuid), 'description': term.description, 'vocabulary': term.vocabulary_id }) if 'institution' in record: # print(tax['institutions'][record['institution']]["name"]) data['organizations'] = [] if "orgaid" in tax['institutions'][ record['institution']]: orgaid = tax['institutions'][ record['institution']]["orgaid"] if orgaid in org_cache: org = org_cache[orgaid] else: org = CuorHelper.query_cuor_by_pid(orgaid) org_cache[orgaid] = org else: name = tax['institutions'][ record['institution']]["name"] if name in org_cache: org = org_cache[name] else: org = CuorHelper.query_cuor_by_label( name, country='Cuba') org_cache[name] = org if org: data['organizations'].append({ 'id': org['id'], 'name': org['metadata']['name'], 'role': 'MAIN' }) parent_id = tax['institutions'][ record['institution']]['parents'][0] if parent_id != '0': if "orgaid" in tax['institutions'][parent_id]: orgaid = tax['institutions'][parent_id][ "orgaid"] if orgaid in org_cache: parent_org = org_cache[orgaid] else: parent_org = CuorHelper.query_cuor_by_pid( orgaid) org_cache[orgaid] = parent_org else: name = tax['institutions'][parent_id]["name"] if name in org_cache: parent_org = org_cache[name] else: parent_org = CuorHelper.query_cuor_by_label( name, country='Cuba') org_cache[name] = parent_org if parent_org: data['organizations'].append({ 'id': parent_org['id'], 'name': parent_org['metadata']['name'], 'role': 'COLABORATOR' }) data['source_type'] = SourceType.JOURNAL.value data['source_status'] = SourceStatus.UNOFFICIAL.value user = get_default_user() data['_save_info'] = { 'user_id': str(user.id), 'comment': 'seed data', 'updated': str(datetime.date.today()) } # TODO: en este caso hace falta hacer patch en vez de update, porque ya issn # trajo datos... new_source = SourceRecord.new_source_revision( data, user.id, 'seed data') # new_source, msg = SourceRecord.create_or_update(data, None, True, True) # # msg, new_source = Sources.insert_new_source(source, # SourceStatus.UNOFFICIAL, user=user) # # if 'oaiurl' in data: # repo = Repository.query.filter_by(source_uuid=new_source.id).first() # if not repo: # repo = Repository() # repo.source_uuid = new_source.id # repo.harvest_endpoint = data['oaiurl'] # repo.harvest_type = HarvestType.OAI # db.session.add(repo) # # IrokoSourceVersions.new_version(new_source.id, data, user=user, # comment='fixing is_current field', is_current=True) # print('-----------------------') # print(new_source) # print('----------------------- sleep 5 seconds') sleep(5)
def get_current_user_sources(status): """ returns the sources of wich current_user is manager param status: 'ALL', 'APPROVED', 'TO_REVIEW', 'UNOFFICIAL' """ # print("## start get sources {0}".format(datetime.datetime.now().strftime("%H:%M:%S"))) try: # count = int(request.args.get('size')) if request.args.get('size') else 10 # page = int(request.args.get('page')) if request.args.get('page') else 1 # # if page < 1: # page = 1 # offset = count * (page - 1) # limit = offset + count # # print(offset) # # print(limit) # print(status) if status == 'ALL': status = None # if role == 'manager': search = SourceRecord.get_sources_search_of_user_as_manager( current_user, status=status) manager = [] if search is not None: # [offset: limit].execute() search_result = search.scan() # TODO: hay que buscar una manera de hacerlo por aqui: # source_v1.serialize_search( # iroko_source_uuid_fetcher, ss # ) # por alguna razon esto no funciona, # la forma en que invenio lo hace incluye en los hits '_version', pero por defecto # eso no esta. # # print(search_result) # # print(source_data_schema_many.dump(search_result)) for hit in search_result: manager.append({ 'id': hit['id'], 'name': hit['name'], 'source_status': hit['source_status'], 'version_to_review': True }) # elif role == 'editor': sources = SourceRecord.get_sources_search_of_user_as_editor( current_user, status=status) editor = [] for hit in sources: editor.append({ 'id': hit['id'], 'name': hit['name'], 'source_status': hit['source_status'], 'version_to_review': True }) sources_terms = get_arguments_for_source_from_action( current_user, 'source_term_manager_actions') terms = Term.query.filter(Term.uuid.in_(sources_terms)).all() sources_orgs = get_arguments_for_source_from_action( current_user, 'source_organization_manager_actions') orgs = [] for org in sources_orgs: orgs.append(CuorHelper.query_cuor_by_uuid(org)) response = iroko_json_response( IrokoResponseStatus.SUCCESS, 'ok', 'sources', { 'manager': manager, 'editor': editor, 'terms': term_schema_many.dump(terms), 'organizations': orgs, 'admin': is_user_sources_admin(current_user) }) # else: # raise Exception("role should be manager or editor") # # print("## iroko_json_response {0}".format(datetime.datetime.now().strftime("%H:%M:%S"))) return response except Exception as e: msg = str(e) return iroko_json_response(IrokoResponseStatus.ERROR, msg, None, None)
def syncronize_miar_journals(self): """ sincronizar lo que hay en el info de miar con el modelo TermSource donde Source es el source dado el issn Term, es el Tems con el nombre de la base de datos en cuestion Source es el que tenga el issn que se recolecto. Si no existe el Source, se debe crear uno nuevo utilizando la informacion que hay en el model ISSN """ issncount = 0 sourcecount = 0 issn_list = SourceRawData.query.all() if issn_list: try: for issn in issn_list: issncount = issncount + 1 data = issn.get_data_field('miar') if type( data ) == str and data == issn.identifier + ' IS NOT LISTED IN MIAR DATABASE': print(issn.identifier + ' IS NOT LISTED IN MIAR DATABASE') continue else: archive_issn_miar = self._parse_journal_information( data) try: # atribute = archive_issn_miar['Indexed\xa0in:'] dbs_split = [] print( '****************************************************************' ) print(archive_issn_miar) print( '****************************************************************' ) # TODO: en algun momento debe ser posible mejorar el parser.. keys = [ # 'Indexed\xa0in:', 'Indexed\u00a0in:', # 'Evaluated\xa0in:', 'Evaluated\u00a0in:' ] # index = miar['Indexed\xa0in:'] for key in keys: if key in archive_issn_miar: dbs_split.extend(archive_issn_miar[key]) print(dbs_split) pid, source = SourceRecord.get_source_by_pid( issn.identifier) print(type(source), source) sourcecount = sourcecount + 1 to_add = [] for dbs in dbs_split: miar = Term.query.filter_by( identifier= 'http://miar.ub.edu/databases/ID/' + dbs.lower().strip()).first() if miar: print("add {0}".format(dbs)) to_add.append(miar) # miar_db_type_terms = Term.query.filter_by( # vocabulary_id=IrokoVocabularyIdentifiers.INDEXES.value).all() # # for miar in miar_db_type_terms: # if miar.identifier == 'http://miar.ub.edu/databases/ID/' + # dbs.lower().strip(): # print("add {0}".format(dbs)) # to_add.append(miar) for t in to_add: print( "----------- !! ADD a Clasfication {0}-{1}-{2}-{3}" .format(t.uuid, t.description, t.vocabulary_id, t.parent_id)) source.add_classification( str(t.uuid), t.description, t.vocabulary_id, dict(url='', initial_cover='', end_cover='')) # add also the parent, meaning the miar_groups if t.parent_id and t.parent_id != 0: parent = Term.query.filter_by( id=t.parent_id).first() print( "----------- !! ADD a parent {0}- {1}". format(parent.uuid, parent.description)) source.add_classification( str(parent.uuid), parent.description, parent.vocabulary_id, dict()) print('***********', dict(source), source) source.new_revision( user_id=get_default_user().id, comment='MIAR Classifications Update') # SourceRecord.new_source_revision(data=source.model.json, # user_id=get_default_user().id, # comment='MIAR Classifications # Update') # # source.update(data=source.model.json, dbcommit=True, reindex=True) # IrokoSourceVersions.new_version( # source.id, # source.model.json, # user_id=get_default_user().id, # comment='MIAR Classifications Update', # is_current=True # ) # source.commit() except Exception: # print("issncount={0}".format(issncount)) # print("sourcecount={0}".format(sourcecount)) print(traceback.format_exc()) continue except Exception as e: # print("issncount={0}".format(issncount)) # print("sourcecount={0}".format(sourcecount)) print(traceback.format_exc()) return None # print("issncount={0}".format(issncount)) # print("sourcecount={0}".format(sourcecount)) return 'success'
def source_new(): """ Create a new Source, If is a regular user, then the user will be Source Editor, but sceiba arguments: pid: must provide a source PID to create something. role: user role in the source. :return: """ try: if not request.is_json: raise Exception("No JSON data provided") input_data = request.json user_id = current_user.id pidvalue = request.args.get('pid') role = request.args.get('role') comment = 'New Inclusion' if 'comment' in input_data: comment = input_data['comment'] data = dict(input_data['data']) # TODO: Si ya esta publicado o si tiene mas de una version entoces se crea una version, data['source_status'] = SourceStatus.TO_REVIEW.value source = SourceRecord.new_source_revision(data, user_id, comment) # print(data) # pid, source = SourceRecord.get_source_by_pid(pidvalue) # if not source or not pid: # pid, source = SourceRecord.get_source_by_pid_in_data(data) if source: with db.session.begin_nested(): db.session.add( ActionUsers.allow(ObjectSourceEditor(source.id), user=current_user)) UserProfile.add_source_to_user_profile(user_id, source['id'], role) # if done: # source_version = IrokoSourceVersions.new_version(source.id, # data, # user_id=user_id, # comment=comment, # is_current=False) # notification = NotificationSchema() # notification.classification = NotificationType.INFO # notification.description = _( # 'Nueva fuente ingresada, requiere revisiĆ³n de un gestor {0} ({1})'.format( # source, source.id)) # notification.emiter = _('Sistema') # # for user_id in source.get_managers: # notification.receiver_id = user_id # Notifications.new_notification(notification) return iroko_json_response( IrokoResponseStatus.SUCCESS, \ 'ok', 'source', \ {'data': source, 'count': 1} ) except Exception as e: return iroko_json_response(IrokoResponseStatus.ERROR, str(e), None, None)