Пример #1
0
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)
Пример #2
0
def test_sessionstore_default_ttl_secs(app):
    """Test the `default_ttl_secs` field for simplekv sessionstore backends
    using the TimeToLive-mixin
    (http://pythonhosted.org/simplekv/index.html#simplekv.TimeToLiveMixin)"""
    ttl_seconds = 1
    ttl_delta = datetime.timedelta(0, ttl_seconds)

    sessionstore = RedisStore(redis.StrictRedis())
    sessionstore.default_ttl_secs = ttl_seconds

    ext = InvenioAccounts(app, sessionstore=sessionstore)
    app.register_blueprint(blueprint)

    # Verify that the backend supports ttl
    assert ext.sessionstore.ttl_support

    user = testutils.create_test_user()
    with app.test_client() as client:
        testutils.login_user_via_view(client, user=user)
        sid = testutils.unserialize_session(flask.session.sid_s)
        while not sid.has_expired(ttl_delta):
            pass
        # When we get here the session should have expired.
        # But the client is still authenticated.
        assert testutils.client_authenticated(client)
Пример #3
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)
Пример #4
0
def test_sessionstore_default_ttl_secs(app):
    """Test the `default_ttl_secs` field for simplekv sessionstore backends
    using the TimeToLive-mixin
    (http://pythonhosted.org/simplekv/index.html#simplekv.TimeToLiveMixin)"""
    ttl_seconds = 1
    ttl_delta = datetime.timedelta(0, ttl_seconds)

    sessionstore = RedisStore(redis.StrictRedis())
    sessionstore.default_ttl_secs = ttl_seconds

    ext = InvenioAccounts(app, sessionstore=sessionstore)
    app.register_blueprint(blueprint)

    # Verify that the backend supports ttl
    assert ext.sessionstore.ttl_support

    user = testutils.create_test_user()
    with app.test_client() as client:
        testutils.login_user_via_view(client, user=user)
        sid = testutils.unserialize_session(flask.session.sid_s)
        while not sid.has_expired(ttl_delta):
            pass
        # When we get here the session should have expired.
        # But the client is still authenticated.
        assert testutils.client_authenticated(client)
Пример #5
0
    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
Пример #6
0
def shib_sp_login():
    """The request from shibboleth sp.

    :return: confirm page when relation is empty
    """
    try:
        if not current_app.config['SHIB_ACCOUNTS_LOGIN_ENABLED']:
            return url_for_security('login')
        shib_session_id = request.form.get('SHIB_ATTR_SESSION_ID', None)
        if shib_session_id is None or len(shib_session_id) == 0:
            return url_for_security('login')
        shib_attr, error = parse_attributes()
        if error:
            return url_for_security('login')
        datastore = RedisStore(
            redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL']))
        ttl_sec = int(current_app.config['SHIB_ACCOUNTS_LOGIN_CACHE_TTL'])
        datastore.put(config.SHIB_CACHE_PREFIX + shib_session_id,
                      bytes(json.dumps(shib_attr), encoding='utf-8'),
                      ttl_secs=ttl_sec)
        shib_user = ShibUser(shib_attr)
        rst = shib_user.get_relation_info()
        """ check the relation of shibboleth user with weko account"""
        next_url = 'weko_accounts.shib_auto_login'
        if rst is None:
            """relation is not existed, cache shibboleth info to redis."""
            next_url = 'weko_accounts.shib_login'
        query_string = {
            'SHIB_ATTR_SESSION_ID': shib_session_id,
            '_method': 'GET'
        }
        return url_for(next_url, **query_string)
    except BaseException:
        current_app.logger.error('Unexpected error: ', sys.exc_info()[0])
    return url_for_security('login')
Пример #7
0
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
Пример #8
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
Пример #9
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)
Пример #10
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
Пример #11
0
def shib_sp_login():
    """The request from shibboleth sp.

    :return: confirm page when relation is empty
    """
    _shib_enable = current_app.config['WEKO_ACCOUNTS_SHIB_LOGIN_ENABLED']
    _shib_username_config = current_app.config[
        'WEKO_ACCOUNTS_SHIB_ALLOW_USERNAME_INST_EPPN']
    try:
        shib_session_id = request.form.get('SHIB_ATTR_SESSION_ID', None)
        if not shib_session_id and not _shib_enable:
            flash(_("Missing SHIB_ATTR_SESSION_ID!"), category='error')
            return redirect(url_for_security('login'))

        shib_attr, error = parse_attributes()

        # Check SHIB_ATTR_EPPN and SHIB_ATTR_USER_NAME:
        if error or not (shib_attr.get('shib_eppn', None)
                         or _shib_username_config
                         and shib_attr.get('shib_user_name')):
            flash(_("Missing SHIB_ATTRs!"), category='error')
            return _redirect_method()

        datastore = RedisStore(
            redis.StrictRedis.from_url(current_app.config['CACHE_REDIS_URL']))
        ttl_sec = int(current_app.config['WEKO_ACCOUNTS_SHIB_LOGIN_CACHE_TTL'])
        datastore.put(current_app.config['WEKO_ACCOUNTS_SHIB_CACHE_PREFIX'] +
                      shib_session_id,
                      bytes(json.dumps(shib_attr), encoding='utf-8'),
                      ttl_secs=ttl_sec)

        shib_user = ShibUser(shib_attr)
        # Check the relation of shibboleth user with weko account.
        rst = shib_user.get_relation_info()

        next_url = 'weko_accounts.shib_auto_login'
        if not rst:
            # Relation is not existed, cache shibboleth info to redis.
            next_url = 'weko_accounts.shib_login'

        query_string = {
            'SHIB_ATTR_SESSION_ID': shib_session_id,
            '_method': 'GET'
        }
        return url_for(next_url, **query_string)
    except BaseException:
        current_app.logger.error('Unexpected error: ', sys.exc_info()[0])
        return _redirect_method()
Пример #12
0
def extensions_load(app):
    db.init_app(app)
    migrate.init_app(app, db)
    JWTManager(app)
    redis_store = FlaskRedis()
    redis_store.init_app(app)
    app.config['JWT_BLACKLIST_STORE'] = RedisStore(redis_store)
Пример #13
0
def create_app():
    """Application creation factory."""
    from myweatherapp.views import ui_blueprint
    from myweatherapp.api import api_blueprint

    # Config
    app._static_folder = 'static'
    app.config.update(CFG_SITE_NAME='myweatherapp', )
    app.config.from_pyfile('config.py', silent=True)
    app.config['cache'] = RedisStore(redis.StrictRedis())

    @app.errorhandler(404)
    def page_not_found(e):
        return render_template('404.html'), 404

    @app.context_processor
    def override_url_for():
        return dict(url_for=dated_url_for)

    def dated_url_for(endpoint, **values):
        if endpoint == 'static':
            filename = values.get('filename', None)
            if filename:
                file_path = os.path.join(app.root_path, endpoint, filename)
                values['q'] = int(os.stat(file_path).st_mtime)
        return url_for(endpoint, **values)

    # Blueprints
    app.register_blueprint(ui_blueprint)
    app.register_blueprint(api_blueprint, url_prefix='/api')

    return app
Пример #14
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)
Пример #15
0
    def setUp(self):
        self.redis = redis.StrictRedis(
                host=testconf.get('redis', 'host'),
                port=int(testconf.get('redis', 'port'))
        )
        self.redis.flushdb()

        self.store = RedisStore(self.redis)
Пример #16
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)
Пример #17
0
 def __init__(self, app=None):
     """Extension initialization."""
     if app:
         self.init_app(app)
         store = RedisStore(
             redis.StrictRedis(host=app.config.get(
                 'ACCESS_SESSION_REDIS_HOST', 'localhost')))
         KVSessionExtension(store, app)
Пример #18
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)
Пример #19
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)
def create_app(config_name):

    application = Flask(__name__,
                        static_folder='static/',
                        static_url_path=configs[config_name].ASSET_PATH)

    init_app(application,
             configs[config_name],
             bootstrap=bootstrap,
             data_api_client=data_api_client,
             login_manager=login_manager)

    if application.config['REDIS_SESSIONS']:
        vcap_services = parse_vcap_services()
        redis_opts = {
            'ssl': application.config['REDIS_SSL'],
            'ssl_ca_certs': application.config['REDIS_SSL_CA_CERTS'],
            'ssl_cert_reqs': application.config['REDIS_SSL_HOST_REQ']
        }
        if vcap_services and 'redis' in vcap_services:
            redis_opts['host'] = vcap_services['redis'][0]['credentials'][
                'hostname']
            redis_opts['port'] = vcap_services['redis'][0]['credentials'][
                'port']
            redis_opts['password'] = vcap_services['redis'][0]['credentials'][
                'password']
        else:
            redis_opts['host'] = application.config['REDIS_SERVER_HOST']
            redis_opts['port'] = application.config['REDIS_SERVER_PORT']
            redis_opts['password'] = application.config[
                'REDIS_SERVER_PASSWORD']

        session_store = RedisStore(redis.StrictRedis(**redis_opts))
        KVSessionExtension(session_store, application)

    application.permanent_session_lifetime = timedelta(hours=12)
    from .main import main as main_blueprint
    from .status import status as status_blueprint

    url_prefix = application.config['URL_PREFIX']
    application.register_blueprint(status_blueprint, url_prefix=url_prefix)
    application.register_blueprint(main_blueprint, url_prefix=url_prefix)
    login_manager.login_view = 'main.render_login'
    main_blueprint.config = application.config.copy()

    init_frontend_app(application, data_api_client, login_manager)

    application.add_template_filter(parse_document_upload_time)

    @application.context_processor
    def extra_template_variables():
        return {
            'generic_contact_email':
            application.config['GENERIC_CONTACT_EMAIL'],
        }

    return application
Пример #21
0
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
Пример #22
0
    def create_app(self):
        from app.app import create_app

        app = create_app(__name__)
        app.config['TESTING'] = True
        self.r = fakeredis.FakeStrictRedis()
        app.extensions['redis'] = self.r
        app.config['JWT_BLACKLIST_STORE'] = RedisStore(self.r)

        return app
Пример #23
0
    def put(self, **kwargs):
        """"""
        try:
            data = request.get_json()
            pid = kwargs.get('pid_value').value

            # item metadata cached on Redis by pid
            import redis
            from simplekv.memory.redisstore import RedisStore
            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=pid)
            ttl_sec = int(current_app.config['WEKO_DEPOSIT_ITEMS_CACHE_TTL'])
            datastore.put(cache_key, json.dumps(data), ttl_secs=ttl_sec)
        except BaseException:
            abort(400, "Failed to register item")

        return jsonify({'status': 'success'})
Пример #24
0
def iframe_save_model():
    """Renders an item register view.

    :return: The rendered template.
    """
    try:
        data = request.get_json()
        activity_session = session['activity_info']
        activity_id = activity_session.get('activity_id', None)
        if activity_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'))))
            sessionstore.put('activity_item_' + activity_id,
                             json.dumps(data).encode('utf-8'),
                             ttl_secs=60 * 60 * 24 * 7)
    except Exception as ex:
        current_app.logger.exception(str(ex))
        return jsonify(code=1, msg='Model save error')
    return jsonify(code=0, msg='Model save success')
Пример #25
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)
Пример #26
0
def run_server():
    print "Starting server..."

    if not app.debug:
        Flask.logger = getLogger("production")
    else:
        Flask.logger = getLogger("develop")
    app.logger.info("Starting server on: " + app.config['address'] + ":" + str(app.config['port']))
    app.storekv = RedisStore(redis.StrictRedis())
    http_server = WSGIServer((app.config['address'], app.config['port']), app,
                             handler_class=WebSocketHandler)  # @IgnorePep8
    http_server.serve_forever()
Пример #27
0
    def store(self):
        from simplekv.memory.redisstore import RedisStore
        r = StrictRedis()

        try:
            r.get('anything')
        except ConnectionError:
            pytest.skip('Could not connect to redis server')

        r.flushdb()
        yield RedisStore(r)
        r.flushdb()
Пример #28
0
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)
Пример #29
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)
Пример #30
0
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)
Пример #31
0
def default_session_store_factory(app):
    """Session store factory.

    If ``ACCOUNTS_SESSION_REDIS_URL`` is set, it returns a
    :class:`simplekv.memory.redisstore.RedisStore` otherwise
    a :class:`simplekv.memory.DictStore`.
    """
    accounts_session_redis_url = app.config.get('ACCOUNTS_SESSION_REDIS_URL')
    if accounts_session_redis_url:
        import redis
        from simplekv.memory.redisstore import RedisStore
        return RedisStore(
            redis.StrictRedis.from_url(accounts_session_redis_url))
    from simplekv.memory import DictStore
    return DictStore()