def register_item_metadata(item): """Upload file content. :argument list_record -- {list} list item import. file_path -- {str} file path. """ item_id = str(item.get('id')) try: pid = PersistentIdentifier.query.filter_by( pid_type='recid', pid_value=item_id ).first() record = WekoDeposit.get_record(pid.object_uuid) _deposit_data = record.dumps().get("_deposit") deposit = WekoDeposit(record, record.model) new_data = dict( **item.get('metadata'), **_deposit_data, **{ '$schema': item.get('$schema'), 'title': handle_get_title(item.get('Title')), } ) item_status = { 'index': new_data['path'], 'actions': 'publish', } if not new_data.get('pid'): new_data = dict(**new_data, **{ 'pid': { 'revision_id': 0, 'type': 'recid', 'value': item_id } }) deposit.update(item_status, new_data) deposit.commit() deposit.publish() with current_app.test_request_context(): first_ver = deposit.newversion(pid) if first_ver: first_ver.publish() db.session.commit() except Exception as ex: db.session.rollback() current_app.logger.error('item id: %s update error.' % item_id) current_app.logger.error(ex) return { 'success': False, 'error': str(ex) } return { 'success': True }
def process_item(record, resync, counter): """Process item.""" event_counter('processed_items', counter) event = ItemEvents.INIT xml = etree.tostring(record, encoding='utf-8').decode() mapper = JPCOARMapper(xml) resyncid = PersistentIdentifier.query.filter_by( pid_type='syncid', pid_value=gen_resync_pid_value(resync, mapper.identifier())).first() if resyncid: r = RecordMetadata.query.filter_by(id=resyncid.object_uuid).first() recid = PersistentIdentifier.query.filter_by( pid_type='recid', object_uuid=resyncid.object_uuid).first() recid.status = PIDStatus.REGISTERED pubdate = dateutil.parser.parse( r.json['pubdate']['attribute_value']).date() dep = WekoDeposit(r.json, r) indexes = dep['path'].copy() event = ItemEvents.UPDATE elif mapper.is_deleted(): return else: dep = WekoDeposit.create({}) PersistentIdentifier.create(pid_type='syncid', pid_value=gen_resync_pid_value( resync, mapper.identifier()), status=PIDStatus.REGISTERED, object_type=dep.pid.object_type, object_uuid=dep.pid.object_uuid) indexes = [] event = ItemEvents.CREATE indexes.append(str(resync.index_id)) if str( resync.index_id) not in indexes else None if mapper.is_deleted(): soft_delete(recid.pid_value) event = ItemEvents.DELETE else: json = mapper.map() json['$schema'] = '/items/jsonschema/' + str(mapper.itemtype.id) dep['_deposit']['status'] = 'draft' dep.update({'actions': 'publish', 'index': indexes}, json) dep.commit() dep.publish() # add item versioning pid = PersistentIdentifier.query.filter_by( pid_type='recid', pid_value=dep.pid.pid_value).first() with current_app.test_request_context() as ctx: first_ver = dep.newversion(pid) first_ver.publish() db.session.commit() if event == ItemEvents.CREATE: event_counter('created_items', counter) elif event == ItemEvents.UPDATE: event_counter('updated_items', counter) elif event == ItemEvents.DELETE: event_counter('deleted_items', counter)
def next_action(activity_id='0', action_id=0): """Next action.""" work_activity = WorkActivity() history = WorkActivityHistory() post_json = request.get_json() activity = dict(activity_id=activity_id, action_id=action_id, action_version=post_json.get('action_version'), action_status=ActionStatusPolicy.ACTION_DONE, commond=post_json.get('commond')) action = Action().get_action_detail(action_id) action_endpoint = action.action_endpoint if action_endpoint == 'begin_action': return jsonify(code=0, msg=_('success')) if action_endpoint == 'end_action': work_activity.end_activity(activity) return jsonify(code=0, msg=_('success')) if action_endpoint == 'item_login': register_hdl(activity_id) activity_detail = work_activity.get_activity_detail(activity_id) item_id = None recid = None record = None pid_without_ver = None if activity_detail and activity_detail.item_id: item_id = activity_detail.item_id current_pid = PersistentIdentifier.get_by_object(pid_type='recid', object_type='rec', object_uuid=item_id) recid = get_record_identifier(current_pid.pid_value) record = WekoDeposit.get_record(item_id) if record: pid_without_ver = get_record_without_version(current_pid) deposit = WekoDeposit(record, record.model) if post_json.get('temporary_save') == 1 \ and action_endpoint != 'identifier_grant': if 'journal' in post_json: work_activity.create_or_update_action_journal( activity_id=activity_id, action_id=action_id, journal=post_json.get('journal')) else: work_activity.upt_activity_action_comment( activity_id=activity_id, action_id=action_id, comment=post_json.get('commond')) return jsonify(code=0, msg=_('success')) elif post_json.get('journal'): work_activity.create_or_update_action_journal( activity_id=activity_id, action_id=action_id, journal=post_json.get('journal')) if action_endpoint == 'approval' and item_id: item = ItemsMetadata.get_record(id_=item_id) pid_identifier = PersistentIdentifier.get_by_object( pid_type='depid', object_type='rec', object_uuid=item.id) record_class = import_string('weko_deposit.api:WekoRecord') resolver = Resolver(pid_type='recid', object_type='rec', getter=record_class.get_record) _pid, _approval_record = resolver.resolve(pid_identifier.pid_value) action_feedbackmail = work_activity.get_action_feedbackmail( activity_id=activity_id, action_id=ITEM_REGISTRATION_ACTION_ID) if action_feedbackmail: FeedbackMailList.update( item_id=item_id, feedback_maillist=action_feedbackmail.feedback_maillist) if not recid and pid_without_ver: FeedbackMailList.update( item_id=pid_without_ver.object_uuid, feedback_maillist=action_feedbackmail.feedback_maillist) if record: deposit.update_feedback_mail() deposit.update_jpcoar_identifier() # TODO: Make private as default. # UpdateItem.publish(pid, approval_record) if action_endpoint == 'item_link' and record: current_pid = PersistentIdentifier.get_by_object(pid_type='recid', object_type='rec', object_uuid=item_id) if not pid_without_ver: pid_without_ver = get_record_without_version(current_pid) item_link = ItemLink(pid_without_ver.pid_value) relation_data = post_json.get('link_data') if relation_data: errors = item_link.update(relation_data) if errors: return jsonify(code=-1, msg=_(errors)) # save pidstore_identifier to ItemsMetadata identifier_select = post_json.get('identifier_grant') if 'identifier_grant' == action_endpoint and identifier_select: idf_grant_jalc_doi_manual = post_json.get( 'identifier_grant_jalc_doi_suffix') idf_grant_jalc_cr_doi_manual = post_json.get( 'identifier_grant_jalc_cr_doi_suffix') idf_grant_jalc_dc_doi_manual = post_json.get( 'identifier_grant_jalc_dc_doi_suffix') idf_grant_ndl_jalc_doi_manual = post_json.get( 'identifier_grant_ndl_jalc_doi_suffix') # If is action identifier_grant, then save to to database identifier_grant = { 'action_identifier_select': identifier_select, 'action_identifier_jalc_doi': idf_grant_jalc_doi_manual, 'action_identifier_jalc_cr_doi': idf_grant_jalc_cr_doi_manual, 'action_identifier_jalc_dc_doi': idf_grant_jalc_dc_doi_manual, 'action_identifier_ndl_jalc_doi': idf_grant_ndl_jalc_doi_manual } work_activity.create_or_update_action_identifier( activity_id=activity_id, action_id=action_id, identifier=identifier_grant) error_list = item_metadata_validation(item_id, identifier_select) if post_json.get('temporary_save') == 1: return jsonify(code=0, msg=_('success')) if isinstance(error_list, str): return jsonify(code=-1, msg=_(error_list)) sessionstore = RedisStore( redis.StrictRedis.from_url('redis://{host}:{port}/1'.format( host=os.getenv('INVENIO_REDIS_HOST', 'localhost'), port=os.getenv('INVENIO_REDIS_PORT', '6379')))) if error_list: sessionstore.put('updated_json_schema_{}'.format(activity_id), json.dumps(error_list).encode('utf-8'), ttl_secs=300) return previous_action(activity_id=activity_id, action_id=action_id, req=-1) else: if sessionstore.redis.exists( 'updated_json_schema_{}'.format(activity_id)): sessionstore.delete( 'updated_json_schema_{}'.format(activity_id)) if identifier_select != IDENTIFIER_GRANT_SELECT_DICT['NotGrant'] \ and item_id is not None: record_without_version = item_id if record and pid_without_ver and not recid: record_without_version = pid_without_ver.object_uuid saving_doi_pidstore(item_id, record_without_version, post_json, int(identifier_select)) rtn = history.create_activity_history(activity) if not rtn: return jsonify(code=-1, msg=_('error')) # next action work_activity.upt_activity_action_status( activity_id=activity_id, action_id=action_id, action_status=ActionStatusPolicy.ACTION_DONE) work_activity.upt_activity_action_comment(activity_id=activity_id, action_id=action_id, comment='') flow = Flow() next_flow_action = flow.get_next_flow_action( activity_detail.flow_define.flow_id, action_id) if next_flow_action and len(next_flow_action) > 0: next_action_endpoint = next_flow_action[0].action.action_endpoint if 'end_action' == next_action_endpoint: new_activity_id = None if record: deposit.publish() updated_item = UpdateItem() # publish record without version ID when registering newly if recid: # new record attached version ID ver_attaching_record = deposit.newversion(current_pid) new_activity_id = ver_attaching_record.model.id ver_attaching_deposit = WekoDeposit( ver_attaching_record, ver_attaching_record.model) ver_attaching_deposit.publish() record_bucket_id = merge_buckets_by_records( current_pid.object_uuid, ver_attaching_record.model.id, sub_bucket_delete=True) if not record_bucket_id: return jsonify(code=-1, msg=_('error')) # Record without version: Make status Public as default updated_item.publish(record) else: # update to record without version ID when editing new_activity_id = record.model.id if pid_without_ver: record_without_ver = WekoDeposit.get_record( pid_without_ver.object_uuid) deposit_without_ver = WekoDeposit( record_without_ver, record_without_ver.model) deposit_without_ver['path'] = deposit.get('path', []) parent_record = deposit_without_ver.\ merge_data_to_record_without_version(current_pid) deposit_without_ver.publish() set_bucket_default_size(new_activity_id) merge_buckets_by_records(new_activity_id, pid_without_ver.object_uuid) updated_item.publish(parent_record) delete_unregister_buckets(new_activity_id) activity.update( action_id=next_flow_action[0].action_id, action_version=next_flow_action[0].action_version, item_id=new_activity_id, ) work_activity.end_activity(activity) else: next_action_id = next_flow_action[0].action_id work_activity.upt_activity_action( activity_id=activity_id, action_id=next_action_id, action_status=ActionStatusPolicy.ACTION_DOING) # delete session value if session.get('itemlogin_id'): del session['itemlogin_id'] del session['itemlogin_activity'] del session['itemlogin_item'] del session['itemlogin_steps'] del session['itemlogin_action_id'] del session['itemlogin_cur_step'] del session['itemlogin_record'] del session['itemlogin_res_check'] del session['itemlogin_pid'] del session['itemlogin_community_id'] return jsonify(code=0, msg=_('success'))
def process_item(record, harvesting, counter): """Process item.""" event_counter('processed_items', counter) event = ItemEvents.INIT xml = etree.tostring(record, encoding='utf-8').decode() if harvesting.metadata_prefix == 'oai_dc': mapper = DCMapper(xml) elif harvesting.metadata_prefix == 'jpcoar' or \ harvesting.metadata_prefix == 'jpcoar_1.0': mapper = JPCOARMapper(xml) elif harvesting.metadata_prefix == 'oai_ddi25' or \ harvesting.metadata_prefix == 'ddi': mapper = DDIMapper(xml) else: return hvstid = PersistentIdentifier.query.filter_by( pid_type='hvstid', pid_value=mapper.identifier()).first() if hvstid: r = RecordMetadata.query.filter_by(id=hvstid.object_uuid).first() recid = PersistentIdentifier.query.filter_by( pid_type='recid', object_uuid=hvstid.object_uuid).first() recid.status = PIDStatus.REGISTERED pubdate = dateutil.parser.parse( r.json['pubdate']['attribute_value']).date() dep = WekoDeposit(r.json, r) indexes = dep['path'].copy() event = ItemEvents.UPDATE elif mapper.is_deleted(): return else: dep = WekoDeposit.create({}) PersistentIdentifier.create(pid_type='hvstid', pid_value=mapper.identifier(), object_type=dep.pid.object_type, object_uuid=dep.pid.object_uuid) indexes = [] event = ItemEvents.CREATE if int(harvesting.auto_distribution): for i in map_indexes(mapper.specs(), harvesting.index_id): indexes.append(str(i)) if i not in indexes else None else: indexes.append(str(harvesting.index_id)) if str( harvesting.index_id) not in indexes else None if hvstid and pubdate >= mapper.datestamp() and \ indexes == dep['path'] and harvesting.update_style == '1': return if mapper.is_deleted(): soft_delete(recid.pid_value) event = ItemEvents.DELETE else: json = mapper.map() json['$schema'] = '/items/jsonschema/' + str(mapper.itemtype.id) dep['_deposit']['status'] = 'draft' dep.update({'actions': 'publish', 'index': indexes}, json) dep.commit() dep.publish() # add item versioning pid = PersistentIdentifier.query.filter_by( pid_type='recid', pid_value=dep.pid.pid_value).first() idt_list = mapper.identifiers from weko_workflow.utils import IdentifierHandle idt = IdentifierHandle(pid.object_uuid) for it in idt_list: if not it.get('type'): continue pid_type = it['type'].lower() pid_obj = idt.get_pidstore(pid_type) if not pid_obj: idt.register_pidstore(pid_type, it['identifier']) with current_app.test_request_context() as ctx: first_ver = dep.newversion(pid) first_ver.publish() harvesting.item_processed = harvesting.item_processed + 1 db.session.commit() if event == ItemEvents.CREATE: event_counter('created_items', counter) elif event == ItemEvents.UPDATE: event_counter('updated_items', counter) elif event == ItemEvents.DELETE: event_counter('deleted_items', counter)
def next_action(activity_id='0', action_id=0): """Next action.""" post_json = request.get_json() activity = dict(activity_id=activity_id, action_id=action_id, action_version=post_json.get('action_version'), action_status=ActionStatusPolicy.ACTION_DONE, commond=post_json.get('commond')) work_activity = WorkActivity() history = WorkActivityHistory() action = Action().get_action_detail(action_id) action_endpoint = action.action_endpoint if (1 == post_json.get('temporary_save') and action_endpoint != 'identifier_grant'): if 'journal' in post_json: work_activity.create_or_update_action_journal( activity_id=activity_id, action_id=action_id, journal=post_json.get('journal')) else: work_activity.upt_activity_action_comment( activity_id=activity_id, action_id=action_id, comment=post_json.get('commond')) return jsonify(code=0, msg=_('success')) elif post_json.get('journal'): work_activity.create_or_update_action_journal( activity_id=activity_id, action_id=action_id, journal=post_json.get('journal')) if 'begin_action' == action_endpoint: return jsonify(code=0, msg=_('success')) if 'end_action' == action_endpoint: work_activity.end_activity(activity) return jsonify(code=0, msg=_('success')) if 'approval' == action_endpoint: activity_obj = WorkActivity() activity_detail = activity_obj.get_activity_detail(activity_id) item = None if activity_detail is not None and activity_detail.item_id is not None: item = ItemsMetadata.get_record(id_=activity_detail.item_id) pid_identifier = PersistentIdentifier.get_by_object( pid_type='depid', object_type='rec', object_uuid=item.id) record_class = import_string('weko_deposit.api:WekoRecord') resolver = Resolver(pid_type='recid', object_type='rec', getter=record_class.get_record) pid, approval_record = resolver.resolve(pid_identifier.pid_value) # TODO: Make private as default. # UpdateItem.publish(pid, approval_record) if 'item_link' == action_endpoint: relation_data = post_json.get('link_data'), activity_obj = WorkActivity() activity_detail = activity_obj.get_activity_detail(activity_id) item = ItemsMetadata.get_record(id_=activity_detail.item_id) pid_identifier = PersistentIdentifier.get_by_object( pid_type='depid', object_type='rec', object_uuid=item.id) record_class = import_string('weko_deposit.api:WekoRecord') resolver = Resolver(pid_type='recid', object_type='rec', getter=record_class.get_record) pid, item_record = resolver.resolve(pid_identifier.pid_value) updated_item = UpdateItem() updated_item.set_item_relation(relation_data, item_record) # save pidstore_identifier to ItemsMetadata idf_grant = post_json.get('identifier_grant') if 'identifier_grant' == action_endpoint and idf_grant is not None: idf_grant_jalc_doi_manual = post_json.get( 'identifier_grant_jalc_doi_suffix') idf_grant_jalc_cr_doi_manual = post_json.get( 'identifier_grant_jalc_cr_doi_suffix') idf_grant_jalc_dc_doi_manual = post_json.get( 'identifier_grant_jalc_dc_doi_suffix') # If is action identifier_grant, then save to to database identifier_grant = { 'action_identifier_select': idf_grant, 'action_identifier_jalc_doi': idf_grant_jalc_doi_manual, 'action_identifier_jalc_cr_doi': idf_grant_jalc_cr_doi_manual, 'action_identifier_jalc_dc_doi': idf_grant_jalc_dc_doi_manual } work_activity.create_or_update_action_identifier( activity_id=activity_id, action_id=action_id, identifier=identifier_grant) if post_json.get('temporary_save') != 1: pidstore_identifier_mapping(post_json, int(idf_grant), activity_id) else: return jsonify(code=0, msg=_('success')) rtn = history.create_activity_history(activity) if rtn is None: return jsonify(code=-1, msg=_('error')) # next action activity_detail = work_activity.get_activity_detail(activity_id) work_activity.upt_activity_action_status( activity_id=activity_id, action_id=action_id, action_status=ActionStatusPolicy.ACTION_DONE) work_activity.upt_activity_action_comment(activity_id=activity_id, action_id=action_id, comment='') flow = Flow() next_flow_action = flow.get_next_flow_action( activity_detail.flow_define.flow_id, action_id) if next_flow_action and len(next_flow_action) > 0: next_action_endpoint = next_flow_action[0].action.action_endpoint if 'end_action' == next_action_endpoint: if activity_detail is not None and \ activity_detail.item_id is not None: record = WekoDeposit.get_record(activity_detail.item_id) if record is not None: deposit = WekoDeposit(record, record.model) deposit.publish() # For current item: Make status Public as default updated_item = UpdateItem() updated_item.publish(record) # For previous item: Update status to Private current_pid = PersistentIdentifier.get_by_object( pid_type='recid', object_type='rec', object_uuid=activity_detail.item_id) current_pv = PIDVersioning(child=current_pid) if current_pv.exists and current_pv.previous is not None: prev_record = WekoDeposit.get_record( current_pv.previous.object_uuid) if prev_record is not None: updated_item.update_status(prev_record) activity.update( action_id=next_flow_action[0].action_id, action_version=next_flow_action[0].action_version, ) work_activity.end_activity(activity) else: next_action_id = next_flow_action[0].action_id work_activity.upt_activity_action( activity_id=activity_id, action_id=next_action_id, action_status=ActionStatusPolicy.ACTION_DOING) return jsonify(code=0, msg=_('success'))