def shib_login(): """Get shibboleth user login page. :return: confirm user page when relation is empty """ try: shib_session_id = request.args.get('SHIB_ATTR_SESSION_ID', None) if shib_session_id is None or len(shib_session_id) == 0: return redirect(url_for_security('login')) datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = config.SHIB_CACHE_PREFIX + shib_session_id if not datastore.redis.exists(cache_key): return redirect(url_for_security('login')) cache_val = datastore.get(cache_key) if cache_val is None: datastore.delete(cache_key) return redirect(url_for_security('login')) cache_val = json.loads(str(cache_val, encoding='utf-8')) session['shib_session_id'] = shib_session_id csrf_random = generate_random_str(length=64) session['csrf_random'] = csrf_random return render_template(config.WEKO_ACCOUNTS_CONFIRM_USER_TEMPLATE, csrf_random=csrf_random, email=cache_val['shib_mail'] if len(cache_val['shib_mail']) > 0 else '') except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def iframe_items_index(pid_value=0): try: if pid_value == 0: return redirect(url_for('.iframe_index')) if request.method == 'GET': return render_template( 'weko_items_ui/iframe/item_index.html', pid_value=pid_value) if request.headers['Content-Type'] != 'application/json': flash(_('invalide request'), 'error') return render_template( 'weko_items_ui/iframe/item_index.html') data = request.get_json() 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 request.method == 'PUT': """update index of item info.""" item_str = sessionstore.get('item_index_{}'.format(pid_value)) sessionstore.delete('item_index_{}'.format(pid_value)) current_app.logger.debug(item_str) item = json.loads(item_str) item['index'] = data current_app.logger.debug(item) elif request.method == 'POST': """update item data info.""" current_app.logger.debug(data) sessionstore.put('item_index_{}'.format(pid_value), json.dumps(data), ttl_secs=300) return jsonify(data) except: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def shib_auto_login(): """Create new account and auto login when shibboleth user first login. :return: next url """ try: is_auto_bind = False shib_session_id = request.args.get('SHIB_ATTR_SESSION_ID', None) if shib_session_id is None: shib_session_id = session['shib_session_id'] is_auto_bind = True if shib_session_id is None or len(shib_session_id) == 0: return redirect(url_for_security('login')) datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = config.SHIB_CACHE_PREFIX + shib_session_id if not datastore.redis.exists(cache_key): return redirect(url_for_security('login')) cache_val = datastore.get(cache_key) if cache_val is None: datastore.delete(cache_key) return redirect(url_for_security('login')) cache_val = json.loads(str(cache_val, encoding='utf-8')) shib_user = ShibUser(cache_val) if not is_auto_bind: shib_user.get_relation_info() else: shib_user.new_relation_info() if shib_user.shib_user is not None: shib_user.shib_user_login() datastore.delete(cache_key) return redirect(session['next'] if 'next' in session else '/') except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def get_journals(): """Get journals.""" key = request.values.get('key') if not key: return jsonify({}) datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config['WEKO_WORKFLOW_OAPOLICY_SEARCH'].format( keyword=key) if datastore.redis.exists(cache_key): data = datastore.get(cache_key) multiple_result = json.loads(data.decode('utf-8'), object_pairs_hook=OrderedDict) else: multiple_result = search_romeo_jtitles(key, 'starts') if key else {} try: datastore.put( cache_key, json.dumps(multiple_result).encode('utf-8'), ttl_secs=int( current_app.config['WEKO_WORKFLOW_OAPOLICY_CACHE_TTL'])) except Exception: pass return jsonify(multiple_result)
def confirm_user(): """Check weko user info. :return: """ try: if request.form.get('csrf_random', '') != session['csrf_random']: flash('csrf_random', category='error') return _redirect_method() shib_session_id = session['shib_session_id'] if not shib_session_id: flash('shib_session_id', category='error') return _redirect_method() datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config[ 'WEKO_ACCOUNTS_SHIB_CACHE_PREFIX'] + shib_session_id if not datastore.redis.exists(cache_key): flash('cache_key', category='error') return _redirect_method() cache_val = datastore.get(cache_key) if not cache_val: flash('cache_val', category='error') datastore.delete(cache_key) return _redirect_method() cache_val = json.loads(str(cache_val, encoding='utf-8')) shib_user = ShibUser(cache_val) account = request.form.get('WEKO_ATTR_ACCOUNT', None) password = request.form.get('WEKO_ATTR_PWD', None) if not shib_user.check_weko_user(account, password): flash('check_weko_user', category='error') datastore.delete(cache_key) return _redirect_method() if not shib_user.bind_relation_info(account): flash('FAILED bind_relation_info!', category='error') return _redirect_method() error = shib_user.check_in() if error: datastore.delete(cache_key) flash(error, category='error') return _redirect_method() if shib_user.shib_user: shib_user.shib_user_login() datastore.delete(cache_key) return redirect(session['next'] if 'next' in session else '/') except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def get_redis_cache(cache_key): """Check and then retrieve the value of a Redis cache key.""" try: datastore = RedisStore(redis.StrictRedis.from_url( current_app.config['CACHE_REDIS_URL'])) if datastore.redis.exists(cache_key): return datastore.get(cache_key).decode('utf-8') except Exception as e: current_app.logger.error('Could get value for ' + cache_key, e) return None
def open(db_name, backend='filesystem', **kwargs): path = os.path.join(os.path.expanduser('~'), '.experimentdb', db_name) if backend == 'filesystem': print("Using filesystem backend") if not os.path.exists(path): try: os.makedirs(path) except Exception as e: print(e) pass return DataBase(FilesystemStore(path)) if backend == 'redis': print("Using redis backend") # find or create a new db index redis_config_store = RedisStore(StrictRedis(db=0, **kwargs)) if db_name in redis_config_store: db_index = int(redis_config_store.get(db_name)) print("Database", db_name, "already exists with index", db_index) else: if 'next_db_index' in redis_config_store: next_db_index = int(redis_config_store.get('next_db_index')) db_index = next_db_index next_db_index += 1 redis_config_store.put('next_db_index', str(next_db_index)) else: db_index = 1 redis_config_store.put('next_db_index', '2') print("New database", db_name, "created with index", db_index) redis_config_store.put(db_name, db_index) return DataBase(RedisStore(StrictRedis(db=db_index, **kwargs)))
def iframe_items_index(pid_value=0): """Iframe items index.""" try: if pid_value == 0: return redirect(url_for('.iframe_index')) record = WekoRecord.get_record_by_pid(pid_value) action = 'private' if record.get('publish_status', '1') == '1' \ else 'publish' if request.method == 'GET': return render_template( 'weko_items_ui/iframe/item_index.html', render_widgets=True, pid_value=pid_value, action=action, activity=session['itemlogin_activity'], item=session['itemlogin_item'], steps=session['itemlogin_steps'], action_id=session['itemlogin_action_id'], cur_step=session['itemlogin_cur_step'], record=session['itemlogin_record'], histories=session['itemlogin_histories'], res_check=session['itemlogin_res_check'], pid=session['itemlogin_pid'], community_id=session['itemlogin_community_id']) if request.headers['Content-Type'] != 'application/json': flash(_('Invalid Request'), 'error') return render_template('weko_items_ui/iframe/item_index.html', render_widgets=True) data = request.get_json() 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 request.method == 'PUT': """update index of item info.""" item_str = sessionstore.get('item_index_{}'.format(pid_value)) sessionstore.delete('item_index_{}'.format(pid_value)) current_app.logger.debug(item_str) item = json.loads(item_str) item['index'] = data current_app.logger.debug(item) elif request.method == 'POST': """update item data info.""" sessionstore.put('item_index_{}'.format(pid_value), json.dumps(data), ttl_secs=300) return jsonify(data) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def items_index(pid_value='0'): """Items index.""" try: if pid_value == '0' or pid_value == 0: return redirect(url_for('.index')) record = WekoRecord.get_record_by_pid(pid_value) action = 'private' if record.get('publish_status', '1') == '1' \ else 'publish' from weko_theme.utils import get_design_layout # Get the design for widget rendering page, render_widgets = get_design_layout( current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) if request.method == 'GET': return render_template( current_app.config['WEKO_ITEMS_UI_INDEX_TEMPLATE'], page=page, render_widgets=render_widgets, pid_value=pid_value, action=action) if request.headers['Content-Type'] != 'application/json': flash(_('Invalid request'), 'error') return render_template( current_app.config['WEKO_ITEMS_UI_INDEX_TEMPLATE'], page=page, render_widgets=render_widgets) data = request.get_json() 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 request.method == 'PUT': """update index of item info.""" item_str = sessionstore.get('item_index_{}'.format(pid_value)) sessionstore.delete('item_index_{}'.format(pid_value)) current_app.logger.debug(item_str) item = json.loads(item_str) item['index'] = data current_app.logger.debug(item) elif request.method == 'POST': """update item data info.""" sessionstore.put('item_index_{}'.format(pid_value), json.dumps(data), ttl_secs=300) return jsonify(data) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def convert_item_metadata(self, index_obj): """ 1. Convert Item Metadata 2. Inject index tree id to dict 3. Set Publish Status :param index_obj: :return: dc """ # if this item has been deleted self.delete_es_index_attempt(self.pid) try: actions = index_obj.get('actions', 'private') datastore = RedisStore(redis.StrictRedis.from_url( current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config[ 'WEKO_DEPOSIT_ITEMS_CACHE_PREFIX'].format( pid_value=self.pid.pid_value) data_str = datastore.get(cache_key) datastore.delete(cache_key) data = json.loads(data_str) except: abort(500, 'Failed to register item') # Get index path index_lst = index_obj.get('index', []) plst = Indexes.get_path_list(index_lst) if not plst or len(index_lst) != len(plst): raise PIDResolveRESTError(description='Any tree index has been deleted') index_lst.clear() for lst in plst: index_lst.append(lst.path) # convert item meta data dc, jrc, is_edit = json_loader(data, self.pid) self.data = data self.jrc = jrc self.is_edit = is_edit # Save Index Path on ES jrc.update(dict(path=index_lst)) dc.update(dict(path=index_lst)) pubs = '1' if 'private' in actions else '0' ps = dict(publish_status=pubs) jrc.update(ps) dc.update(ps) return dc
def shib_auto_login(): """Create new account and auto login when shibboleth user first login. :return: next url """ try: is_auto_bind = False shib_session_id = request.args.get('SHIB_ATTR_SESSION_ID', None) if not shib_session_id: shib_session_id = session['shib_session_id'] is_auto_bind = True if not shib_session_id: return _redirect_method() datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config[ 'WEKO_ACCOUNTS_SHIB_CACHE_PREFIX'] + shib_session_id if not datastore.redis.exists(cache_key): return _redirect_method() cache_val = datastore.get(cache_key) if not cache_val: datastore.delete(cache_key) return _redirect_method() cache_val = json.loads(str(cache_val, encoding='utf-8')) shib_user = ShibUser(cache_val) if not is_auto_bind: shib_user.get_relation_info() else: shib_user.new_relation_info() error = shib_user.check_in() if error: datastore.delete(cache_key) current_app.logger.error(error) flash(error, category='error') return _redirect_method() if shib_user.shib_user: shib_user.shib_user_login() datastore.delete(cache_key) return redirect(session['next'] if 'next' in session else '/') except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def check_validation_error_msg(activity_id): """Check whether sessionstore('updated_json_schema_') is exist. :param activity_id: The identify of Activity. :return: Show error message """ 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 sessionstore.redis.exists( 'updated_json_schema_{}'.format(activity_id)) \ and sessionstore.get('updated_json_schema_{}'.format(activity_id)): session_data = sessionstore.get( 'updated_json_schema_{}'.format(activity_id)) error_list = json.loads(session_data.decode('utf-8')) msg = [] if error_list.get('error_type'): if error_list.get('error_type') == 'no_resource_type': msg.append(_(error_list.get('msg', ''))) else: msg.append(_('PID does not meet the conditions.')) if error_list.get('pmid'): msg.append( _('Since PMID is not subject to DOI registration, please ' 'select another type.')) if error_list.get('doi'): msg.append( _('Prefix/Suffix of Identifier is not consistency with' ' content of Identifier Registration.')) if error_list.get('url'): msg.append( _('Please input location information (URL) for Identifier.')) return jsonify(code=1, msg=msg, error_list=error_list) else: return jsonify(code=0)
def iframe_index(item_type_id=0): """Renders an item register view. :param item_type_id: Item type ID. (Default: 0) :return: The rendered template. """ try: item_type = ItemTypes.get_by_id(item_type_id) if item_type is None: return render_template('weko_items_ui/iframe/error.html', error_type='no_itemtype') json_schema = '/items/jsonschema/{}'.format(item_type_id) schema_form = '/items/schemaform/{}'.format(item_type_id) 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')))) record = {} files = [] endpoints = {} activity_session = session['activity_info'] activity_id = activity_session.get('activity_id', None) if activity_id and sessionstore.redis.exists( 'activity_item_'+activity_id): item_str = sessionstore.get('activity_item_'+activity_id) item_json = json.loads(item_str) if 'metainfo' in item_json: record = item_json.get('metainfo') if 'files' in item_json: files = item_json.get('files') if 'endpoints' in item_json: endpoints = item_json.get('endpoints') need_file = False if 'filename' in json.dumps(item_type.schema): need_file = True return render_template( 'weko_items_ui/iframe/item_edit.html', need_file=need_file, record=record, jsonschema=json_schema, schemaform=schema_form, id=item_type_id, item_save_uri=url_for('.iframe_save_model'), files=files, endpoints=endpoints ) except: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def shib_login(): """Get shibboleth user login page. :return: confirm user page when relation is empty """ try: shib_session_id = request.args.get('SHIB_ATTR_SESSION_ID', None) if not shib_session_id: current_app.logger.error(_("Missing SHIB_ATTR_SESSION_ID!")) flash(_("Missing SHIB_ATTR_SESSION_ID!"), category='error') return _redirect_method() datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config[ 'WEKO_ACCOUNTS_SHIB_CACHE_PREFIX'] + shib_session_id if not datastore.redis.exists(cache_key): current_app.logger.error(_("Missing SHIB_CACHE_PREFIX!")) flash(_("Missing SHIB_CACHE_PREFIX!"), category='error') return _redirect_method() cache_val = datastore.get(cache_key) if not cache_val: current_app.logger.error(_("Missing SHIB_ATTR!")) flash(_("Missing SHIB_ATTR!"), category='error') datastore.delete(cache_key) return _redirect_method() cache_val = json.loads(str(cache_val, encoding='utf-8')) session['shib_session_id'] = shib_session_id csrf_random = generate_random_str(length=64) session['csrf_random'] = csrf_random _datastore = LocalProxy( lambda: current_app.extensions['security'].datastore) user = _datastore.find_user(email=cache_val.get('shib_mail')) return render_template( current_app.config['WEKO_ACCOUNTS_CONFIRM_USER_TEMPLATE'], csrf_random=csrf_random, email=user.email if user else '') except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def cache_schema(schema_name, delete=False): """ Cache the schema to Redis. :param schema_name: :return: """ def get_schema(): try: rec = WekoSchema.get_record_by_name(schema_name) if rec: dstore = dict() dstore['root_name'] = rec.get('root_name') dstore['target_namespace'] = rec.get('target_namespace') dstore['schema_location'] = rec.get('schema_location') dstore['namespaces'] = rec.model.namespaces.copy() dstore['schema'] = json.loads(rec.model.xsd, object_pairs_hook=OrderedDict) rec.model.namespaces.clear() del rec return dstore except BaseException: return None try: # schema cached on Redis by schema name datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config['WEKO_SCHEMA_CACHE_PREFIX'].format( schema_name=schema_name) data_str = datastore.get(cache_key) data = json.loads(data_str.decode('utf-8'), object_pairs_hook=OrderedDict) if delete: datastore.delete(cache_key) except BaseException: try: schema = get_schema() if schema: datastore.put(cache_key, json.dumps(schema)) except BaseException: return get_schema() else: return schema return data
def item_login(item_type_id=0): """Return information that item register need. :param item_type_id: Item type ID. (Default: 0) """ template_url = 'weko_items_ui/iframe/item_edit.html' need_file = False record = {} json_schema = '' schema_form = '' item_save_uri = url_for('weko_items_ui.iframe_save_model') files = [] endpoints = {} try: item_type = ItemTypes.get_by_id(item_type_id) if item_type is None: template_url = 'weko_items_ui/iframe/error.html' json_schema = '/items/jsonschema/{}'.format(item_type_id) schema_form = '/items/schemaform/{}'.format(item_type_id) 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')))) activity_session = session['activity_info'] activity_id = activity_session.get('activity_id', None) if activity_id and sessionstore.redis.exists( 'activity_item_' + activity_id): item_str = sessionstore.get('activity_item_' + activity_id) item_json = json.loads(item_str) if 'metainfo' in item_json: record = item_json.get('metainfo') if 'files' in item_json: files = item_json.get('files') if 'endpoints' in item_json: endpoints = item_json.get('endpoints') if 'filename' in json.dumps(item_type.schema): need_file = True except Exception as e: template_url = 'weko_items_ui/iframe/error.html' current_app.logger.debug(str(e)) return template_url, need_file, record, json_schema, \ schema_form, item_save_uri, files, endpoints
def index(self): cache_key = current_app.config['WEKO_ADMIN_CACHE_PREFIX'].\ format(name='display_stats') current_display_setting = True # Default datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) if datastore.redis.exists(cache_key): curr_display_setting = datastore.get(cache_key).decode('utf-8') current_display_setting = True if curr_display_setting == 'True' \ else False if request.method == 'POST': display_setting = request.form.get('record_stats_radio', 'True') datastore.delete(cache_key) datastore.put(cache_key, display_setting.encode('utf-8')) flash(_('Successfully Changed Settings.')) return redirect(url_for('statssettings.index')) return self.render( current_app.config["WEKO_ADMIN_STATS_SETTINGS_TEMPLATE"], display_stats=current_display_setting)
def confirm_user(): """Check weko user info. :return: """ try: csrf_random = request.form.get('csrf_random', '') if csrf_random != session['csrf_random']: return redirect(url_for_security('login')) shib_session_id = session['shib_session_id'] if shib_session_id is None or len(shib_session_id) == 0: return redirect(url_for_security('login')) datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = config.SHIB_CACHE_PREFIX + shib_session_id if not datastore.redis.exists(cache_key): return redirect(url_for_security('login')) cache_val = datastore.get(cache_key) if cache_val is None: datastore.delete(cache_key) return redirect(url_for_security('login')) cache_val = json.loads(str(cache_val, encoding='utf-8')) shib_user = ShibUser(cache_val) account = request.form.get('WEKO_ATTR_ACCOUNT', None) password = request.form.get('WEKO_ATTR_PWD', None) if not shib_user.check_weko_user(account, password): datastore.delete(cache_key) return redirect(url_for_security('login')) shib_user.bind_relation_info(account) if shib_user.shib_user is not None: shib_user.shib_user_login() datastore.delete(cache_key) return redirect(session['next'] if 'next' in session else '/') except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def default_view_method(pid, record, template=None): """Default view method. Sends ``record_viewed`` signal and renders template. """ 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 '' record_viewed.send(current_app._get_current_object(), pid=pid, record=record, info=send_info) item_type_id = record.get('item_type_id') lists = ItemTypes.get_latest() if lists is None or len(lists) == 0: return render_template( current_app.config['WEKO_ITEMS_UI_ERROR_TEMPLATE']) item_type = ItemTypes.get_by_id(item_type_id) if item_type is None: return redirect( url_for('.index', item_type_id=lists[0].item_type[0].id)) json_schema = '/items/jsonschema/{}'.format(item_type_id) schema_form = '/items/schemaform/{}'.format(item_type_id) 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')))) files = to_files_js(record) record = record.item_metadata endpoints = {} activity_session = session['activity_info'] activity_id = activity_session.get('activity_id', None) if activity_id and sessionstore.redis.exists('activity_item_' + activity_id): item_str = sessionstore.get('activity_item_' + activity_id) item_json = json.loads(item_str) if 'metainfo' in item_json: record = item_json.get('metainfo') if 'files' in item_json: files = item_json.get('files') if 'endpoints' in item_json: endpoints = item_json.get('endpoints') need_file, need_billing_file = is_schema_include_key(item_type.schema) return render_template( template, need_file=need_file, need_billing_file=need_billing_file, record=record, jsonschema=json_schema, schemaform=schema_form, lists=lists, links=to_links_js(pid), id=item_type_id, files=files, pid=pid, item_save_uri=url_for('weko_items_ui.iframe_save_model'), endpoints=endpoints)
def iframe_items_index(pid_value='0'): """Iframe items index.""" try: if pid_value == '0' or pid_value == 0: return redirect(url_for('.iframe_index')) record = WekoRecord.get_record_by_pid(pid_value) action = 'private' if record.get('publish_status', '1') == '1' \ else 'publish' community_id = session.get('itemlogin_community_id') ctx = {'community': None} if community_id: comm = GetCommunity.get_community_by_id(community_id) ctx = {'community': comm} if request.method == 'GET': cur_activity = session['itemlogin_activity'] # If enable auto set index feature # and activity is usage application item type steps = session['itemlogin_steps'] contain_application_endpoint = False for step in steps: if step.get('ActionEndpoint') == 'item_login_application': contain_application_endpoint = True enable_auto_set_index = current_app.config.get( 'WEKO_WORKFLOW_ENABLE_AUTO_SET_INDEX_FOR_ITEM_TYPE') if enable_auto_set_index and contain_application_endpoint: index_id = get_index_id(cur_activity.activity_id) update_index_tree_for_record(pid_value, index_id) return redirect(url_for('weko_workflow.iframe_success')) # Get the design for widget rendering from weko_theme.utils import get_design_layout page, render_widgets = get_design_layout( community_id or current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) return render_template('weko_items_ui/iframe/item_index.html', page=page, render_widgets=render_widgets, pid_value=pid_value, action=action, activity=session['itemlogin_activity'], item=session['itemlogin_item'], steps=session['itemlogin_steps'], action_id=session['itemlogin_action_id'], cur_step=session['itemlogin_cur_step'], record=session['itemlogin_record'], histories=session['itemlogin_histories'], res_check=session['itemlogin_res_check'], pid=session['itemlogin_pid'], community_id=community_id, **ctx) if request.headers['Content-Type'] != 'application/json': flash(_('Invalid Request'), 'error') from weko_theme.utils import get_design_layout page, render_widgets = get_design_layout( current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) return render_template('weko_items_ui/iframe/item_index.html', page=page, render_widgets=render_widgets, community_id=community_id, **ctx) data = request.get_json() 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 request.method == 'PUT': """update index of item info.""" item_str = sessionstore.get('item_index_{}'.format(pid_value)) sessionstore.delete('item_index_{}'.format(pid_value)) item = json.loads(item_str) item['index'] = data elif request.method == 'POST': """update item data info.""" sessionstore.put('item_index_{}'.format(pid_value), json.dumps(data), ttl_secs=300) return jsonify(data) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def default_view_method(pid, record, filename=None, template=None, **kwargs): """Display default view. Sends record_viewed signal and renders template. :param pid: PID object. :param record: Record object. :param filename: File name. :param template: Template to render. :param kwargs: Additional view arguments based on URL rule. :returns: The rendered template. """ # Get PID version object to retrieve all versions of item pid_ver = PIDVersioning(child=pid) if not pid_ver.exists or pid_ver.is_last_child: abort(404) active_versions = list(pid_ver.children or []) all_versions = list( pid_ver.get_children(ordered=True, pid_status=None) or []) try: if WekoRecord.get_record(id_=active_versions[-1].object_uuid )['_deposit']['status'] == 'draft': active_versions.pop() if WekoRecord.get_record(id_=all_versions[-1].object_uuid )['_deposit']['status'] == 'draft': all_versions.pop() except Exception: pass if active_versions: # active_versions.remove(pid_ver.last_child) active_versions.pop() check_site_license_permission() check_items_settings() 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 '' record_viewed.send(current_app._get_current_object(), pid=pid, record=record, info=send_info) community_arg = request.args.get('community') community_id = "" ctx = {'community': None} if community_arg: from weko_workflow.api import GetCommunity comm = GetCommunity.get_community_by_id(community_arg) 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' height = style.height if style else None detail_condition = get_search_detail_keyword('') # Add Item Reference data to Record Metadata pid_without_ver = record.get("recid").split('.')[0] res = ItemLink.get_item_link_info(pid_without_ver) if res: record["relation"] = res else: record["relation"] = {} google_scholar_meta = _get_google_scholar_meta(record) pdfcoverpage_set_rec = PDFCoverPageSettings.find(1) # Check if user has the permission to download original pdf file # and the cover page setting is set and its value is enable (not disabled) can_download_original = check_original_pdf_download_permission(record) \ and pdfcoverpage_set_rec and pdfcoverpage_set_rec.avail != 'disable' # Get item meta data record['permalink_uri'] = None permalink = get_record_permalink(record) if not permalink: record['permalink_uri'] = request.url else: record['permalink_uri'] = permalink can_update_version = has_update_version_role(current_user) datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config['WEKO_ADMIN_CACHE_PREFIX'].\ format(name='display_stats') if datastore.redis.exists(cache_key): curr_display_setting = datastore.get(cache_key).decode('utf-8') display_stats = True if curr_display_setting == 'True' else False else: display_stats = True groups_price = get_groups_price(record) billing_files_permission = get_billing_file_download_permission( groups_price) if groups_price else None billing_files_prices = get_min_price_billing_file_download( groups_price, billing_files_permission) if groups_price else None from weko_theme.utils import get_design_layout # Get the design for widget rendering page, render_widgets = get_design_layout( request.args.get('community') or current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) if hasattr(current_i18n, 'language'): index_link_list = get_index_link_list(current_i18n.language) else: index_link_list = get_index_link_list() files_thumbnail = [] if record.files: files_thumbnail = ObjectVersion.get_by_bucket( record.get('_buckets').get('deposit')).\ filter_by(is_thumbnail=True).all() files = [] for f in record.files: if check_file_permission(record, f.data) or is_open_restricted(f.data): files.append(f) # Flag: can edit record can_edit = True if pid == get_record_without_version(pid) else False open_day_display_flg = current_app.config.get('OPEN_DATE_DISPLAY_FLG') return render_template(template, pid=pid, pid_versioning=pid_ver, active_versions=active_versions, all_versions=all_versions, record=record, files=files, display_stats=display_stats, filename=filename, can_download_original_pdf=can_download_original, is_logged_in=current_user and current_user.is_authenticated, can_update_version=can_update_version, page=page, render_widgets=render_widgets, community_id=community_id, width=width, detail_condition=detail_condition, height=height, index_link_enabled=style.index_link_enabled, index_link_list=index_link_list, google_scholar_meta=google_scholar_meta, billing_files_permission=billing_files_permission, billing_files_prices=billing_files_prices, files_thumbnail=files_thumbnail, can_edit=can_edit, open_day_display_flg=open_day_display_flg, **ctx, **kwargs)
def display_activity(activity_id=0): """Display activity.""" activity = WorkActivity() activity_detail = activity.get_activity_detail(activity_id) item = None if activity_detail is not None and activity_detail.item_id is not None: try: item = ItemsMetadata.get_record(id_=activity_detail.item_id) except NoResultFound as ex: current_app.logger.exception(str(ex)) item = None steps = activity.get_activity_steps(activity_id) history = WorkActivityHistory() histories = history.get_activity_history_list(activity_id) workflow = WorkFlow() workflow_detail = workflow.get_workflow_by_id(activity_detail.workflow_id) if activity_detail.activity_status == \ ActivityStatusPolicy.ACTIVITY_FINALLY \ or activity_detail.activity_status == \ ActivityStatusPolicy.ACTIVITY_CANCEL: activity_detail.activity_status_str = _('End') else: activity_detail.activity_status_str = \ request.args.get('status', 'ToDo') cur_action = activity_detail.action action_endpoint = cur_action.action_endpoint action_id = cur_action.id temporary_comment = activity.get_activity_action_comment( activity_id=activity_id, action_id=action_id) # display_activity of Identifier grant idf_grant_data = None if 'identifier_grant' == action_endpoint and item: path = WekoRecord.get_record(item.id).get('path') if len(path) > 1: community_id = 'Root Index' else: index_address = path.pop(-1).split('/') index_id = Index.query.filter_by(id=index_address.pop()).one() community_id = get_community_id_by_index( index_id.index_name_english) idf_grant_data = Identifier.query.filter_by( repository=community_id).one_or_none() # valid date pidstore_identifier data if idf_grant_data is not None: if not idf_grant_data.jalc_doi: idf_grant_data.jalc_doi = '<Empty>' if not idf_grant_data.jalc_crossref_doi: idf_grant_data.jalc_crossref_doi = '<Empty>' if not idf_grant_data.jalc_datacite_doi: idf_grant_data.jalc_datacite_doi = '<Empty>' if not idf_grant_data.cnri: idf_grant_data.cnri = '<Empty>' if not idf_grant_data.suffix: idf_grant_data.suffix = '<Empty>' temporary_idf_grant = 0 temporary_idf_grant_suffix = [] temporary_identifier = activity.get_action_identifier_grant( activity_id=activity_id, action_id=action_id) if temporary_identifier: temporary_idf_grant = temporary_identifier.get( 'action_identifier_select') temporary_idf_grant_suffix.append( temporary_identifier.get('action_identifier_jalc_doi')) temporary_idf_grant_suffix.append( temporary_identifier.get('action_identifier_jalc_cr_doi')) temporary_idf_grant_suffix.append( temporary_identifier.get('action_identifier_jalc_dc_doi')) temporary_journal = activity.get_action_journal(activity_id=activity_id, action_id=action_id) if temporary_journal: temporary_journal = temporary_journal.action_journal cur_step = action_endpoint step_item_login_url = None approval_record = [] pid = None record = {} need_file = False json_schema = '' schema_form = '' item_save_uri = '' files = [] endpoints = {} links = None if 'item_login' == action_endpoint or 'file_upload' == action_endpoint: activity_session = dict(activity_id=activity_id, action_id=activity_detail.action_id, action_version=cur_action.action_version, action_status=ActionStatusPolicy.ACTION_DOING, commond='') session['activity_info'] = activity_session # get item edit page info. step_item_login_url, need_file, record, json_schema, \ schema_form, item_save_uri, files, endpoints = item_login( item_type_id=workflow_detail.itemtype_id) if item: pid_identifier = PersistentIdentifier.get_by_object( pid_type='depid', object_type='rec', object_uuid=item.id) record = item # if 'approval' == action_endpoint: if item: # get record data for the first time access to editing item screen 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) files = to_files_js(approval_record) # get files data after click Save btn 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 sessionstore.redis.exists('activity_item_' + str(activity_id)): item_str = sessionstore.get('activity_item_' + str(activity_id)) item_json = json.loads(item_str.decode('utf-8')) if 'files' in item_json: files = item_json.get('files') if not files: deposit = WekoDeposit.get_record(item.id) if deposit is not None: files = to_files_js(deposit) from weko_deposit.links import base_factory links = base_factory(pid) res_check = check_authority_action(activity_id, action_id) getargs = request.args ctx = {'community': None} community_id = "" if 'community' in getargs: comm = GetCommunity.get_community_by_id(request.args.get('community')) community_id = request.args.get('community') ctx = {'community': comm} community_id = comm.id # be use for index tree and comment page. if 'item_login' == action_endpoint or 'file_upload' == action_endpoint: session['itemlogin_id'] = activity_id session['itemlogin_activity'] = activity_detail session['itemlogin_item'] = item session['itemlogin_steps'] = steps session['itemlogin_action_id'] = action_id session['itemlogin_cur_step'] = cur_step session['itemlogin_record'] = approval_record session['itemlogin_histories'] = histories session['itemlogin_res_check'] = res_check session['itemlogin_pid'] = pid session['itemlogin_community_id'] = community_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, temporary_journal=temporary_journal, temporary_idf_grant=temporary_idf_grant, temporary_idf_grant_suffix=temporary_idf_grant_suffix, idf_grant_data=idf_grant_data, idf_grant_input=IDENTIFIER_GRANT_LIST, idf_grant_method=IDENTIFIER_GRANT_SUFFIX_METHOD, record=approval_record, records=record, step_item_login_url=step_item_login_url, need_file=need_file, jsonschema=json_schema, schemaform=schema_form, id=workflow_detail.itemtype_id, item_save_uri=item_save_uri, files=files, endpoints=endpoints, error_type='item_login_error', links=links, histories=histories, res_check=res_check, pid=pid, community_id=community_id, **ctx)
def default_view_method(pid, record, filename=None, template=None, **kwargs): r"""Display default view. Sends record_viewed signal and renders template. :param pid: PID object. :param record: Record object. :param filename: File name. :param template: Template to render. :param \*\*kwargs: Additional view arguments based on URL rule. :returns: The rendered template. """ check_site_license_permission() check_items_settings() 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 '' record_viewed.send(current_app._get_current_object(), pid=pid, record=record, info=send_info) getargs = request.args community_id = "" ctx = {'community': 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' height = style.height if style else None detail_condition = get_search_detail_keyword('') weko_indexer = WekoIndexer() res = weko_indexer.get_item_link_info(pid=record.get("control_number")) if res is not None: record["relation"] = res else: record["relation"] = {} google_scholar_meta = _get_google_scholar_meta(record) pdfcoverpage_set_rec = PDFCoverPageSettings.find(1) # Check if user has the permission to download original pdf file # and the cover page setting is set and its value is enable (not disabled) can_download_original = check_original_pdf_download_permission(record) \ and pdfcoverpage_set_rec is not None \ and pdfcoverpage_set_rec.avail != 'disable' # Get item meta data record['permalink_uri'] = None pidstore_identifier = get_item_pidstore_identifier(pid.object_uuid) if not pidstore_identifier: record['permalink_uri'] = request.url else: record['permalink_uri'] = pidstore_identifier from invenio_files_rest.permissions import has_update_version_role can_update_version = has_update_version_role(current_user) datastore = RedisStore( redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config['WEKO_ADMIN_CACHE_PREFIX'].\ format(name='display_stats') if datastore.redis.exists(cache_key): curr_display_setting = datastore.get(cache_key).decode('utf-8') display_stats = True if curr_display_setting == 'True' else False else: display_stats = True return render_template(template, pid=pid, record=record, display_stats=display_stats, filename=filename, can_download_original_pdf=can_download_original, is_logged_in=current_user and current_user.is_authenticated, can_update_version=can_update_version, community_id=community_id, width=width, detail_condition=detail_condition, height=height, google_scholar_meta=google_scholar_meta, **ctx, **kwargs)
def display_activity(activity_id=0): """Display activity.""" activity = WorkActivity() activity_detail = activity.get_activity_detail(activity_id) item = None if activity_detail and activity_detail.item_id: try: item = ItemsMetadata.get_record(id_=activity_detail.item_id) except NoResultFound as ex: current_app.logger.exception(str(ex)) item = None steps = activity.get_activity_steps(activity_id) history = WorkActivityHistory() histories = history.get_activity_history_list(activity_id) workflow = WorkFlow() workflow_detail = workflow.get_workflow_by_id(activity_detail.workflow_id) if activity_detail.activity_status == \ ActivityStatusPolicy.ACTIVITY_FINALLY \ or activity_detail.activity_status == \ ActivityStatusPolicy.ACTIVITY_CANCEL: activity_detail.activity_status_str = _('End') else: activity_detail.activity_status_str = \ request.args.get('status', 'ToDo') cur_action = activity_detail.action action_endpoint = cur_action.action_endpoint action_id = cur_action.id temporary_comment = activity.get_activity_action_comment( activity_id=activity_id, action_id=action_id) # display_activity of Identifier grant identifier_setting = None if action_endpoint == 'identifier_grant' and item: community_id = request.args.get('community', None) if not community_id: community_id = 'Root Index' identifier_setting = get_identifier_setting(community_id) # valid date pidstore_identifier data if identifier_setting: text_empty = '<Empty>' if not identifier_setting.jalc_doi: identifier_setting.jalc_doi = text_empty if not identifier_setting.jalc_crossref_doi: identifier_setting.jalc_crossref_doi = text_empty if not identifier_setting.jalc_datacite_doi: identifier_setting.jalc_datacite_doi = text_empty if not identifier_setting.ndl_jalc_doi: identifier_setting.ndl_jalc_doi = text_empty temporary_identifier_select = 0 temporary_identifier_inputs = [] last_identifier_setting = activity.get_action_identifier_grant( activity_id=activity_id, action_id=action_id) if last_identifier_setting: temporary_identifier_select = last_identifier_setting.get( 'action_identifier_select') temporary_identifier_inputs.append( last_identifier_setting.get('action_identifier_jalc_doi')) temporary_identifier_inputs.append( last_identifier_setting.get('action_identifier_jalc_cr_doi')) temporary_identifier_inputs.append( last_identifier_setting.get('action_identifier_jalc_dc_doi')) temporary_identifier_inputs.append( last_identifier_setting.get('action_identifier_ndl_jalc_doi')) temporary_journal = activity.get_action_journal(activity_id=activity_id, action_id=action_id) if temporary_journal: temporary_journal = temporary_journal.action_journal cur_step = action_endpoint step_item_login_url = None approval_record = [] pid = None record = {} need_file = False need_billing_file = False json_schema = '' schema_form = '' item_save_uri = '' files = [] endpoints = {} links = None need_thumbnail = False files_thumbnail = [] allow_multi_thumbnail = False show_autofill_metadata = True is_hidden_pubdate_value = False item_type_name = get_item_type_name(workflow_detail.itemtype_id) if 'item_login' == action_endpoint or 'file_upload' == action_endpoint: activity_session = dict(activity_id=activity_id, action_id=activity_detail.action_id, action_version=cur_action.action_version, action_status=ActionStatusPolicy.ACTION_DOING, commond='') show_autofill_metadata = is_show_autofill_metadata(item_type_name) is_hidden_pubdate_value = is_hidden_pubdate(item_type_name) session['activity_info'] = activity_session # get item edit page info. step_item_login_url, need_file, need_billing_file, \ record, json_schema, schema_form,\ item_save_uri, files, endpoints, need_thumbnail, files_thumbnail, \ allow_multi_thumbnail \ = item_login(item_type_id=workflow_detail.itemtype_id) if item: # Remove the unused local variable # _pid_identifier = PersistentIdentifier.get_by_object( # pid_type='depid', object_type='rec', object_uuid=item.id) record = item 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 sessionstore.redis.exists( 'updated_json_schema_{}'.format(activity_id)) \ and sessionstore.get( 'updated_json_schema_{}'.format(activity_id)): json_schema = (json_schema + "/{}").format(activity_id) # if 'approval' == action_endpoint: if item: # get record data for the first time access to editing item screen 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) files = to_files_js(approval_record) # get files data after click Save btn 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 sessionstore.redis.exists('activity_item_' + str(activity_id)): item_str = sessionstore.get('activity_item_' + str(activity_id)) item_json = json.loads(item_str.decode('utf-8')) if 'files' in item_json: files = item_json.get('files') if not files: deposit = WekoDeposit.get_record(item.id) if deposit: files = to_files_js(deposit) if files and not files_thumbnail: files_thumbnail = [ i for i in files if 'is_thumbnail' in i.keys() and i['is_thumbnail'] ] from weko_deposit.links import base_factory links = base_factory(pid) res_check = check_authority_action(str(activity_id), str(action_id)) getargs = request.args ctx = {'community': None} community_id = "" if 'community' in getargs: comm = GetCommunity.get_community_by_id(request.args.get('community')) ctx = {'community': comm} community_id = comm.id # be use for index tree and comment page. if 'item_login' == action_endpoint or 'file_upload' == action_endpoint: session['itemlogin_id'] = activity_id session['itemlogin_activity'] = activity_detail session['itemlogin_item'] = item session['itemlogin_steps'] = steps session['itemlogin_action_id'] = action_id session['itemlogin_cur_step'] = cur_step session['itemlogin_record'] = approval_record session['itemlogin_histories'] = histories session['itemlogin_res_check'] = res_check session['itemlogin_pid'] = pid session['itemlogin_community_id'] = community_id from weko_theme.utils import get_design_layout # Get the design for widget rendering page, render_widgets = get_design_layout( community_id or current_app.config['WEKO_THEME_DEFAULT_COMMUNITY']) list_license = get_list_licence() if item and item.get('pid'): pid_without_ver = item['pid']['value'].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, temporary_journal=temporary_journal, temporary_idf_grant=temporary_identifier_select, temporary_idf_grant_suffix=temporary_identifier_inputs, idf_grant_data=identifier_setting, idf_grant_input=IDENTIFIER_GRANT_LIST, idf_grant_method=IDENTIFIER_GRANT_SUFFIX_METHOD, record=approval_record, records=record, step_item_login_url=step_item_login_url, need_file=need_file, need_billing_file=need_billing_file, jsonschema=json_schema, schemaform=schema_form, id=workflow_detail.itemtype_id, item_save_uri=item_save_uri, files=files, endpoints=endpoints, error_type='item_login_error', links=links, histories=histories, res_check=res_check, pid=pid, community_id=community_id, need_thumbnail=need_thumbnail, files_thumbnail=files_thumbnail, allow_multi_thumbnail=allow_multi_thumbnail, enable_feedback_maillist=current_app. config['WEKO_WORKFLOW_ENABLE_FEEDBACK_MAIL'], enable_contributor=current_app. config['WEKO_WORKFLOW_ENABLE_CONTRIBUTOR'], show_automatic_metadata_input=show_autofill_metadata, is_hidden_pubdate=is_hidden_pubdate_value, list_license=list_license, **ctx)
def item_login(item_type_id=0): """Return information that item register need. :param item_type_id: Item type ID. (Default: 0) """ template_url = 'weko_items_ui/iframe/item_edit.html' need_file = False need_billing_file = False record = {} json_schema = '' schema_form = '' item_save_uri = url_for('weko_items_ui.iframe_save_model') files = [] endpoints = {} need_thumbnail = False files_thumbnail = [] allow_multi_thumbnail = False try: item_type = ItemTypes.get_by_id(item_type_id) if item_type is None: template_url = 'weko_items_ui/iframe/error.html' json_schema = '/items/jsonschema/{}'.format(item_type_id) schema_form = '/items/schemaform/{}'.format(item_type_id) 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')))) activity_session = session['activity_info'] activity_id = activity_session.get('activity_id', None) if activity_id and sessionstore.redis.exists('activity_item_' + activity_id): item_str = sessionstore.get('activity_item_' + activity_id) item_json = json.loads(item_str) if 'metainfo' in item_json: record = item_json.get('metainfo') if 'files' in item_json: files = item_json.get('files') files_thumbnail = [ i for i in files if 'is_thumbnail' in i.keys() and i['is_thumbnail'] ] if 'endpoints' in item_json: endpoints = item_json.get('endpoints') need_file, need_billing_file = is_schema_include_key(item_type.schema) if 'subitem_thumbnail' in json.dumps(item_type.schema): need_thumbnail = True key = [ i[0].split('.')[0] for i in find_items(item_type.form) if 'subitem_thumbnail' in i[0] ] option = item_type.render.get('meta_list', {}). \ get(key[0].split('[')[0], {}).get('option') if option: allow_multi_thumbnail = option.get('multiple') except Exception as e: template_url = 'weko_items_ui/iframe/error.html' current_app.logger.debug(str(e)) return template_url, need_file, need_billing_file, \ record, json_schema, schema_form, \ item_save_uri, files, endpoints, need_thumbnail, files_thumbnail, \ allow_multi_thumbnail
class DatabaseService(Service): """ Database bindings for leveldb """ def __init__(self, engine): Service.__init__(self, name='database') self.engine = engine self.DB = None self.salt = None self.req_count = 0 self.set_state(Service.INIT) def on_register(self): # TODO: Add authentication support for redis try: redis_instance = redis.StrictRedis( host=os.environ.get('REDIS_URL', 'localhost'), db=self.engine.config['database']['index']) redis_instance.ping() self.DB = RedisStore(redis_instance) except Exception as e: tools.log(e) sys.stderr.write( 'Redis connection cannot be established!\nFalling to SQLAlchemy' ) return False try: self.salt = self.DB.get('salt').decode() if self.salt is None: raise Exception except Exception as e: self.salt = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(5)) self.DB.put('salt', self.salt.encode()) return True @sync def get(self, key): """gets the key in args[0] using the salt""" try: return yaml.load(self.DB.get(self.salt + str(key)).decode()) except Exception as e: return None @sync def put(self, key, value): """ Puts the val in args[1] under the key in args[0] with the salt prepended to the key. """ try: self.DB.put(self.salt + str(key), yaml.dump(value).encode()) return True except Exception as e: return False @sync def exists(self, key): """ Checks if the key in args[0] with the salt prepended is in the database. """ try: return (self.salt + str(key)) in self.DB except KeyError: return False @sync def delete(self, key): """ Removes the entry in the database under the the key in args[0] with the salt prepended. """ try: self.DB.delete(self.salt + str(key)) return True except: return False
def convert_item_metadata(self, index_obj, data=None): """Convert Item Metadat. 1. Convert Item Metadata 2. Inject index tree id to dict 3. Set Publish Status :param index_obj: :return: dc """ # if this item has been deleted self.delete_es_index_attempt(self.pid) try: actions = index_obj.get('actions', 'private') if not data: datastore = RedisStore( redis.StrictRedis.from_url( current_app.config['CACHE_REDIS_URL'])) cache_key = current_app.config[ 'WEKO_DEPOSIT_ITEMS_CACHE_PREFIX'].format( pid_value=self.pid.pid_value) data_str = datastore.get(cache_key) datastore.delete(cache_key) data = json.loads(data_str.decode('utf-8')) except BaseException: abort(500, 'Failed to register item') # Get index path index_lst = index_obj.get('index', []) # Prepare index id list if the current index_lst is a path list if index_lst: index_id_lst = [] for index in index_lst: indexes = str(index).split('/') index_id_lst.append(indexes[len(indexes) - 1]) index_lst = index_id_lst plst = Indexes.get_path_list(index_lst) if not plst or len(index_lst) != len(plst): raise PIDResolveRESTError( description='Any tree index has been deleted') index_lst.clear() for lst in plst: index_lst.append(lst.path) # convert item meta data dc, jrc, is_edit = json_loader(data, self.pid) self.data = data self.jrc = jrc self.is_edit = is_edit # Save Index Path on ES jrc.update(dict(path=index_lst)) # add at 20181121 start sub_sort = {} for pth in index_lst: # es setting sub_sort[pth[-13:]] = "" jrc.update(dict(custom_sort=sub_sort)) dc.update(dict(custom_sort=sub_sort)) dc.update(dict(path=index_lst)) pubs = '1' if 'private' in actions else '0' ps = dict(publish_status=pubs) jrc.update(ps) dc.update(ps) return dc