Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
File: utils.py Progetto: mhaya/weko
def reset_redis_cache(cache_key, value):
    """Delete and then reset a cache value to Redis."""
    try:
        datastore = RedisStore(redis.StrictRedis.from_url(
            current_app.config['CACHE_REDIS_URL']))
        datastore.delete(cache_key)
        datastore.put(cache_key, value.encode('utf-8'))
    except Exception as e:
        current_app.logger.error('Could not reset redis value', e)
        raise
Esempio n. 5
0
File: views.py Progetto: mhaya/weko
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)
Esempio n. 6
0
    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
Esempio n. 7
0
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)
Esempio n. 8
0
def delete_schema_cache(schema_name):
    """
    delete schema cache on redis
    :param schema_name:
    :return:
    """
    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)
        datastore.delete(cache_key)
    except BaseException:
        pass
Esempio n. 9
0
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)
Esempio n. 10
0
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)
Esempio n. 11
0
File: schema.py Progetto: mhaya/weko
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
Esempio n. 12
0
def upt_activity_item(app, item_id):
    """
    Connect to the item_created signal.
    :param app:
    :param item_id:
    :return:
    """
    if 'activity_info' in session:
        activity = session['activity_info']
        workactivity = WorkActivity()
        rtn = workactivity.upt_activity_item(activity, item_id.object_uuid)
        if rtn:
            del session['activity_info']
            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_id = activity.get('activity_id', None)
            if activity_id and sessionstore.redis.exists('activity_item_' +
                                                         activity_id):
                sessionstore.delete('activity_item_' + activity_id)
Esempio n. 13
0
File: admin.py Progetto: mhaya/weko
    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)
Esempio n. 14
0
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)
Esempio n. 15
0
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)
Esempio n. 16
0
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)
Esempio n. 17
0
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'))
Esempio n. 18
0
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
Esempio n. 19
0
File: api.py Progetto: mhaya/weko
    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