def get_permission(record, fjson): """Get download file permission. @param record: @param fjson: @return: """ user_id = current_user.get_id() record_id = record.get('recid') file_name = fjson.get('filename') current_time = dt.now() duration = current_time - \ timedelta(days=current_app.config['WEKO_RECORDS_UI_DOWNLOAD_DAYS']) list_permission = FilePermission.find_list_permission_by_date( user_id, record_id, file_name, duration) if list_permission: permission = list_permission[0] if permission.status == 1: return permission else: activity_id = permission.usage_application_activity_id activity = WorkActivity() steps = activity.get_activity_steps(activity_id) if steps: for step in steps: if step and step['Status'] == 'action_canceled': return None return permission else: return None
def get_index_id(activity_id): """Get index ID base on activity id. :param activity_id: :return: """ from weko_workflow.api import WorkActivity, WorkFlow activity = WorkActivity() activity_detail = activity.get_activity_detail(activity_id) workflow = WorkFlow() workflow_detail = workflow.get_workflow_by_id(activity_detail.workflow_id) index_tree_id = workflow_detail.index_tree_id if index_tree_id: from .api import Indexes index_result = Indexes.get_index(index_tree_id) if not index_result: index_tree_id = None else: index_tree_id = None return index_tree_id
def corresponding_activity_list(): """Get corresponding usage & output activity list. :return: activity list """ result = {} work_activity = WorkActivity() if "get_corresponding_usage_activities" in dir(work_activity): usage_application_list, output_report_list = work_activity. \ get_corresponding_usage_activities(current_user.get_id()) result = { 'usage_application': usage_application_list, 'output_report': output_report_list } return jsonify(result)
def handle_workflow(item: dict): """Handle workflow. :argument title -- {dict or list} title. :return return -- title string. """ pid = PersistentIdentifier.query.filter_by( pid_type='recid', pid_value=item.get('id')).first() if pid: activity = WorkActivity() wf_activity = activity.get_workflow_activity_by_item_id( pid.object_uuid) if wf_activity: return else: workflow = WorkFlow.query.filter_by( itemtype_id=item.get('item_type_id')).first() if workflow: return else: create_work_flow(item.get('item_type_id'))
def prepare_edit_item(): """Prepare_edit_item. Host the api which provide 2 service: Create new activity for editing flow Check permission: check if user is owner/admin/shared user request: header: Content type must be json data: pid_value: pid_value return: The result json: code: status code, msg: meassage result, data: url redirect """ def _get_workflow_by_item_type_id(item_type_name_id, item_type_id): """Get workflow settings by item type id.""" workflow = WorkFlow.query.filter_by(itemtype_id=item_type_id).first() if not workflow: item_type_list = ItemTypes.get_by_name_id(item_type_name_id) id_list = [x.id for x in item_type_list] workflow = (WorkFlow.query.filter( WorkFlow.itemtype_id.in_(id_list)).order_by( WorkFlow.itemtype_id.desc()).order_by( WorkFlow.flow_id.asc()).first()) return workflow if request.headers['Content-Type'] != 'application/json': """Check header of request""" return jsonify(code=-1, msg=_('Header Error')) post_activity = request.get_json() pid_value = post_activity.get('pid_value') if pid_value: try: record = WekoRecord.get_record_by_pid(pid_value) owner = str(record.get('owner')) shared_id = str(record.get('weko_shared_id')) user_id = str(get_current_user()) is_admin = get_user_roles() activity = WorkActivity() pid_object = PersistentIdentifier.get('recid', pid_value) latest_pid = PIDVersioning(child=pid_object).last_child # check user's permission if user_id != owner and not is_admin[0] and user_id != shared_id: return jsonify( code=-1, msg=_(r"You are not allowed to edit this item.")) lists = ItemTypes.get_latest() if not lists: return jsonify(code=-1, msg=_(r"You do not even have an Itemtype.")) item_type_id = record.get('item_type_id') item_type = ItemTypes.get_by_id(item_type_id) if not item_type: return jsonify(code=-1, msg=_(r"This itemtype isn't found.")) # check item is being editied item_id = latest_pid.object_uuid workflow_activity = activity.get_workflow_activity_by_item_id( item_id) if not workflow_activity: # get workflow of first record attached version ID: x.1 workflow_activity = activity.get_workflow_activity_by_item_id( pid_object.object_uuid) # if workflow of the item is not found # use default settings of item type to which the item belongs else: # show error when has stt is Begin or Doing if workflow_activity.action_status == \ ActionStatusPolicy.ACTION_BEGIN \ or workflow_activity.action_status == \ ActionStatusPolicy.ACTION_DOING: return jsonify(code=-1, msg=_(r"The workflow is being edited.")) # prepare params for new workflow activity if workflow_activity: post_activity['workflow_id'] = workflow_activity.workflow_id post_activity['flow_id'] = workflow_activity.flow_id else: workflow = _get_workflow_by_item_type_id( item_type.name_id, item_type_id) if not workflow: return jsonify(code=-1, msg=_('Workflow setting does not exist.')) post_activity['workflow_id'] = workflow.id post_activity['flow_id'] = workflow.flow_id post_activity['itemtype_id'] = item_type_id getargs = request.args community = getargs.get('community', None) # Create a new version of a record. record = WekoDeposit.get_record(item_id) if not record: return jsonify(code=-1, msg=_('Record does not exist.')) deposit = WekoDeposit(record, record.model) draft_record = deposit.newversion(pid_object) if not draft_record: return jsonify(code=-1, msg=_('An error has occurred.')) # Create snapshot bucket for draft record from invenio_records_files.models import RecordsBuckets try: with db.session.begin_nested(): from weko_workflow.utils import delete_bucket draft_deposit = WekoDeposit(draft_record, draft_record.model) snapshot = record.files.bucket.snapshot(lock=False) snapshot.locked = False draft_deposit['_buckets'] = {'deposit': str(snapshot.id)} draft_record_bucket = RecordsBuckets.create( record=draft_record.model, bucket=snapshot) # Remove duplicated buckets draft_record_buckets = RecordsBuckets.query.filter_by( record_id=draft_record.model.id).all() for record_bucket in draft_record_buckets: if record_bucket != draft_record_bucket: delete_bucket_id = record_bucket.bucket_id RecordsBuckets.query.filter_by( bucket_id=delete_bucket_id).delete() delete_bucket(delete_bucket_id) draft_deposit.commit() except Exception as ex: db.session.rollback() current_app.logger.exception(str(ex)) return jsonify(code=-1, msg=_('An error has occurred.')) # Create a new workflow activity. rtn = activity.init_activity(post_activity, community, draft_record.model.id) if rtn: # GOTO: TEMPORARY EDIT MODE FOR IDENTIFIER identifier_actionid = get_actionid('identifier_grant') if workflow_activity: identifier = activity.get_action_identifier_grant( workflow_activity.activity_id, identifier_actionid) else: identifier = activity.get_action_identifier_grant( '', identifier_actionid) if identifier: if identifier.get('action_identifier_select') > \ IDENTIFIER_GRANT_DOI: identifier['action_identifier_select'] = \ IDENTIFIER_GRANT_CAN_WITHDRAW elif identifier.get('action_identifier_select') == \ IDENTIFIER_GRANT_IS_WITHDRAWING: identifier['action_identifier_select'] = \ IDENTIFIER_GRANT_WITHDRAWN activity.create_or_update_action_identifier( rtn.activity_id, identifier_actionid, identifier) mail_list = FeedbackMailList.get_mail_list_by_item_id( item_id=pid_object.object_uuid) if mail_list: activity.create_or_update_action_feedbackmail( activity_id=rtn.activity_id, action_id=ITEM_REGISTRATION_ACTION_ID, feedback_maillist=mail_list) if community: comm = GetCommunity.get_community_by_id(community) url_redirect = url_for('weko_workflow.display_activity', activity_id=rtn.activity_id, community=comm.id) else: url_redirect = url_for('weko_workflow.display_activity', activity_id=rtn.activity_id) return jsonify(code=0, msg='success', data={'redirect': url_redirect}) except Exception as e: current_app.logger.error('Unexpected error: ', str(e)) return jsonify(code=-1, msg=_('An error has occurred.'))
def prepare_edit_item(): """Prepare_edit_item. Host the api which provide 2 service: Create new activity for editing flow Check permission: check if user is owner/admin/shared user request: header: Content type must be json data: pid_value: pid_value return: The result json: code: status code, msg: meassage result, data: url redirect """ if request.headers['Content-Type'] != 'application/json': """Check header of request""" return jsonify(code=-1, msg=_('Header Error')) post_activity = request.get_json() pid_value = post_activity.get('pid_value') if pid_value: try: record = WekoRecord.get_record_by_pid(pid_value) owner = str(record.get('owner')) shared_id = str(record.get('weko_shared_id')) user_id = str(get_current_user()) is_admin = get_user_roles() activity = WorkActivity() pid_object = PersistentIdentifier.get('recid', pid_value) # check item is being editied item_id = pid_object.object_uuid workflow_action_stt = \ activity.get_workflow_activity_status_by_item_id( item_id=item_id) # show error when has stt is Begin or Doing if workflow_action_stt is not None and \ (workflow_action_stt == ActionStatusPolicy.ACTION_BEGIN or workflow_action_stt == ActionStatusPolicy.ACTION_DOING): return jsonify(code=-13, msg=_('The workflow is being edited. ')) if user_id != owner and not is_admin[0] and user_id != shared_id: return jsonify(code=-1, msg=_('You are not allowed to edit this item.')) lists = ItemTypes.get_latest() if not lists: return jsonify(code=-1, msg=_('You do not even have an itemtype.')) item_type_id = record.get('item_type_id') item_type = ItemTypes.get_by_id(item_type_id) if item_type is None: return jsonify(code=-1, msg=_('This itemtype not found.')) upt_current_activity = activity.upt_activity_detail( item_id=pid_object.object_uuid) if upt_current_activity is not None: post_activity['workflow_id'] = upt_current_activity.workflow_id post_activity['flow_id'] = upt_current_activity.flow_id post_activity['itemtype_id'] = item_type_id getargs = request.args community = getargs.get('community', None) # Create a new version of a record. record = WekoDeposit.get_record(item_id) if record is None: return jsonify(code=-1, msg=_('Record does not exist.')) deposit = WekoDeposit(record, record.model) new_record = deposit.newversion(pid_object) if new_record is None: return jsonify(code=-1, msg=_('An error has occurred.')) rtn = activity.init_activity(post_activity, community, new_record.model.id) if rtn: # GOTO: TEMPORARY EDIT MODE FOR IDENTIFIER identifier_actionid = get_actionid('identifier_grant') identifier = activity.get_action_identifier_grant( upt_current_activity.activity_id, identifier_actionid) if identifier: if identifier.get('action_identifier_select') > \ IDENTIFIER_GRANT_DOI: identifier['action_identifier_select'] = \ IDENTIFIER_GRANT_CAN_WITHDRAW elif identifier.get('action_identifier_select') == \ IDENTIFIER_GRANT_IS_WITHDRAWING: identifier['action_identifier_select'] = \ IDENTIFIER_GRANT_WITHDRAWN activity.create_or_update_action_identifier( rtn.activity_id, identifier_actionid, identifier) if community: comm = GetCommunity.get_community_by_id(community) url_redirect = url_for( 'weko_workflow.display_activity', activity_id=rtn.activity_id, community=comm.id) else: url_redirect = url_for( 'weko_workflow.display_activity', activity_id=rtn.activity_id) return jsonify(code=0, msg='success', data={'redirect': url_redirect}) except Exception as e: current_app.logger.error('Unexpected error: ', str(e)) return jsonify(code=-1, msg=_('An error has occurred.'))
def search(): """Index Search page ui.""" search_type = request.args.get('search_type', '0') getArgs = request.args community_id = "" ctx = {'community': None} cur_index_id = search_type if search_type not in ( '0', '1', ) else None if 'community' in getArgs: from weko_workflow.api import GetCommunity comm = GetCommunity.get_community_by_id(request.args.get('community')) ctx = {'community': comm} community_id = comm.id # Get index style style = IndexStyle.get( current_app.config['WEKO_INDEX_TREE_STYLE_OPTIONS']['id']) width = style.width if style else '3' # add at 1206 for search management sort_options, display_number = SearchSetting.get_results_setting() disply_setting = dict(size=display_number) detail_condition = get_search_detail_keyword('') height = style.height if style else None index_link_list = [] for index in Index.query.all(): if index.index_link_enabled and index.public_state: if hasattr(current_i18n, 'language'): if current_i18n.language == 'ja' and index.index_link_name: index_link_list.append((index.id, index.index_link_name)) else: index_link_list.append( (index.id, index.index_link_name_english)) else: index_link_list.append( (index.id, index.index_link_name_english)) if 'item_link' in getArgs: activity_id = request.args.get('item_link') from weko_workflow.api import WorkActivity workFlowActivity = WorkActivity() activity_detail, item, steps, action_id, cur_step, temporary_comment, approval_record, \ step_item_login_url, histories, res_check, pid, community_id, ctx \ = workFlowActivity.get_activity_index_search(activity_id=activity_id) return render_template('weko_workflow/activity_detail.html', render_widgets=True, activity=activity_detail, item=item, steps=steps, action_id=action_id, cur_step=cur_step, temporary_comment=temporary_comment, record=approval_record, step_item_login_url=step_item_login_url, histories=histories, res_check=res_check, pid=pid, index_id=cur_index_id, community_id=community_id, width=width, height=height, **ctx) else: journal_info = None check_site_license_permission() send_info = {} send_info['site_license_flag'] = True \ if hasattr(current_user, 'site_license_flag') else False send_info['site_license_name'] = current_user.site_license_name \ if hasattr(current_user, 'site_license_name') else '' if search_type in ('0', '1', '2'): searched.send(current_app._get_current_object(), search_args=getArgs, info=send_info) if search_type == '2': cur_index_id = request.args.get('q', '0') journal_info = get_journal_info(cur_index_id) return render_template(current_app.config['SEARCH_UI_SEARCH_TEMPLATE'], render_widgets=True, index_id=cur_index_id, community_id=community_id, sort_option=sort_options, disply_setting=disply_setting, detail_condition=detail_condition, width=width, height=height, index_link_enabled=style.index_link_enabled, index_link_list=index_link_list, journal_info=journal_info, **ctx)
def search(): """Index Search page ui.""" search_type = request.args.get('search_type', WEKO_SEARCH_TYPE_DICT['FULL_TEXT']) get_args = request.args community_id = "" ctx = {'community': None} cur_index_id = search_type if search_type not in \ (WEKO_SEARCH_TYPE_DICT['FULL_TEXT'], WEKO_SEARCH_TYPE_DICT[ 'KEYWORD'], ) else None if 'community' in get_args: from weko_workflow.api import GetCommunity comm = GetCommunity.get_community_by_id(request.args.get('community')) ctx = {'community': comm} community_id = comm.id # Get the design for widget rendering page, render_widgets = get_design_layout( community_id or current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) # Get index style style = IndexStyle.get( current_app.config['WEKO_INDEX_TREE_STYLE_OPTIONS']['id']) width = style.width if style else '3' # add at 1206 for search management sort_options, display_number = SearchSetting.get_results_setting() ts = time.time() disply_setting = dict(size=display_number, timestamp=ts) detail_condition = get_search_detail_keyword('') export_settings = AdminSettings.get('item_export_settings') or \ AdminSettings.Dict2Obj( current_app.config['WEKO_ADMIN_DEFAULT_ITEM_EXPORT_SETTINGS']) height = style.height if style else None if 'item_link' in get_args: from weko_workflow.api import WorkActivity activity_id = request.args.get('item_link') workflow_activity = WorkActivity() activity_detail, item, steps, action_id, cur_step, temporary_comment,\ approval_record, step_item_login_url, histories, res_check, pid, \ community_id, ctx = workflow_activity.get_activity_index_search( activity_id=activity_id) # Get ex-Item Links recid = item['pid'].get('value') if item.get('pid') else None if recid: pid_without_ver = recid.split('.')[0] item_link = ItemLink.get_item_link_info(pid_without_ver) ctx['item_link'] = item_link return render_template( 'weko_workflow/activity_detail.html', page=page, render_widgets=render_widgets, activity=activity_detail, item=item, steps=steps, action_id=action_id, cur_step=cur_step, temporary_comment=temporary_comment, record=approval_record, step_item_login_url=step_item_login_url, histories=histories, res_check=res_check, pid=pid, index_id=cur_index_id, community_id=community_id, width=width, height=height, allow_item_exporting=export_settings.allow_item_exporting, is_permission=check_permission(), is_login=bool(current_user.get_id()), **ctx) else: journal_info = None index_display_format = '1' check_site_license_permission() send_info = dict() send_info['site_license_flag'] = True \ if hasattr(current_user, 'site_license_flag') else False send_info['site_license_name'] = current_user.site_license_name \ if hasattr(current_user, 'site_license_name') else '' if search_type in WEKO_SEARCH_TYPE_DICT.values(): searched.send(current_app._get_current_object(), search_args=get_args, info=send_info) if search_type == WEKO_SEARCH_TYPE_DICT['INDEX']: cur_index_id = request.args.get('q', '0') journal_info = get_journal_info(cur_index_id) index_info = Indexes.get_index(cur_index_id) if index_info: index_display_format = index_info.display_format if index_display_format == '2': disply_setting = dict(size=100, timestamp=ts) if hasattr(current_i18n, 'language'): index_link_list = get_index_link_list(current_i18n.language) else: index_link_list = get_index_link_list() return render_template( current_app.config['SEARCH_UI_SEARCH_TEMPLATE'], page=page, render_widgets=render_widgets, index_id=cur_index_id, community_id=community_id, sort_option=sort_options, disply_setting=disply_setting, detail_condition=detail_condition, width=width, height=height, index_link_enabled=style.index_link_enabled, index_link_list=index_link_list, journal_info=journal_info, index_display_format=index_display_format, allow_item_exporting=export_settings.allow_item_exporting, is_permission=check_permission(), is_login=bool(current_user.get_id()), **ctx)