Пример #1
0
def app():
    """Flask app fixture."""
    app = Flask('myapp')
    app.config.update(I18N_LANGUAGES=[('en', 'English'), ('de', 'German')], )
    Babel(app)
    InvenioI18N(app)
    app.register_blueprint(create_blueprint_from_app(app))
    return app
Пример #2
0
def app():
    """Flask app fixture."""
    app = Flask("myapp")
    app.config.update(I18N_LANGUAGES=[("en", "English"), ("de", "German")], )
    Babel(app)
    InvenioI18N(app)
    app.register_blueprint(create_blueprint_from_app(app))
    return app
Пример #3
0
def test_lang_view(app):
    """Test extension initalization."""
    app.config.update(
        I18N_LANGUAGES=[
            ('da', 'Danish'),
        ],
        I18N_SESSION_KEY='my_session_key',
        SECRET_KEY='CHANGEME',
    )
    InvenioI18N(app)
    app.register_blueprint(create_blueprint_from_app(app))

    @app.route("/")
    def index():
        return get_locale().language

    with app.test_request_context():
        da_lang_url = url_for('invenio_i18n.set_lang', lang_code='da')
        en_lang_url = url_for('invenio_i18n.set_lang', lang_code='en')
        es_lang_url = url_for('invenio_i18n.set_lang', lang_code='es')

    with app.test_client() as client:
        # Set language to danish
        res = client.get(da_lang_url)
        assert res.status_code == 302
        assert res.location == 'http://localhost/'
        assert session[app.config['I18N_SESSION_KEY']] == 'da'

        res = client.get("/")
        assert res.get_data(as_text=True) == 'da'

        # Set language to english
        res = client.get(en_lang_url)
        assert res.status_code == 302
        assert res.location == 'http://localhost/'
        assert session[app.config['I18N_SESSION_KEY']] == 'en'

        res = client.get("/")
        assert res.get_data(as_text=True) == 'en'

        # Try to set invalid language.
        res = client.get(es_lang_url)
        assert res.status_code == 404
Пример #4
0
def test_lang_view(app):
    """Test extension initalization."""
    app.config.update(
        I18N_LANGUAGES=[
            ("da", "Danish"),
        ],
        I18N_SESSION_KEY="my_session_key",
        SECRET_KEY="CHANGEME",
    )
    InvenioI18N(app)
    app.register_blueprint(create_blueprint_from_app(app))

    @app.route("/")
    def index():
        return get_locale().language

    with app.test_request_context():
        da_lang_url = url_for("invenio_i18n.set_lang", lang_code="da")
        en_lang_url = url_for("invenio_i18n.set_lang", lang_code="en")
        es_lang_url = url_for("invenio_i18n.set_lang", lang_code="es")

    with app.test_client() as client:
        # Set language to danish
        res = client.get(da_lang_url)
        assert res.status_code == 302
        assert res.location == "/"
        assert session[app.config["I18N_SESSION_KEY"]] == "da"

        res = client.get("/")
        assert res.get_data(as_text=True) == "da"

        # Set language to english
        res = client.get(en_lang_url)
        assert res.status_code == 302
        assert res.location == "/"
        assert session[app.config["I18N_SESSION_KEY"]] == "en"

        res = client.get("/")
        assert res.get_data(as_text=True) == "en"

        # Try to set invalid language.
        res = client.get(es_lang_url)
        assert res.status_code == 404
Пример #5
0
def test_lang_view_redirect(app):
    """Test extension initalization."""
    app.config.update(
        I18N_LANGUAGES=[
            ('da', 'Danish'),
        ],
        SECRET_KEY='CHANGEME',
    )
    InvenioI18N(app)
    app.register_blueprint(create_blueprint_from_app(app))

    @app.route("/page/")
    def page():
        return get_locale().language

    with app.test_request_context():
        da_lang_url = url_for('invenio_i18n.set_lang', lang_code='da')
        next_url = url_for('page')

    with app.test_client() as client:
        # Request body
        res = client.get(da_lang_url, data={'next': next_url})
        assert res.status_code == 302
        assert res.location == 'http://localhost/page/'
        assert session[app.config['I18N_SESSION_KEY']] == 'da'

        # Query string
        res = client.get(da_lang_url + "?next={0}".format(next_url))
        assert res.location == 'http://localhost/page/'

        # Referrer header
        res = client.get(da_lang_url, headers={'Referer': next_url})
        assert res.location == 'http://localhost/page/'

        # Unsafe redirects
        res = client.get(da_lang_url + "?next=http://example.org")
        assert res.location == 'http://localhost/'
        res = client.get(da_lang_url,
                         headers={'Referer': 'http://example.org'})
        assert res.location == 'http://localhost/'
Пример #6
0
def test_lang_view_redirect(app):
    """Test extension initalization."""
    app.config.update(
        I18N_LANGUAGES=[
            ("da", "Danish"),
        ],
        SECRET_KEY="CHANGEME",
    )
    InvenioI18N(app)
    app.register_blueprint(create_blueprint_from_app(app))

    @app.route("/page/")
    def page():
        return get_locale().language

    with app.test_request_context():
        da_lang_url = url_for("invenio_i18n.set_lang", lang_code="da")
        next_url = url_for("page")

    with app.test_client() as client:
        # Request body
        res = client.post(da_lang_url, data={"next": next_url})
        assert res.status_code == 302
        assert res.location == "/page/"
        assert session[app.config["I18N_SESSION_KEY"]] == "da"

        # Query string
        res = client.get(da_lang_url + "?next={0}".format(next_url))
        assert res.location == "/page/"

        # Referrer header
        res = client.get(da_lang_url, headers={"Referer": next_url})
        assert res.location == "/page/"

        # Unsafe redirects
        res = client.get(da_lang_url + "?next=http://example.org")
        assert res.location == "/"
        res = client.get(da_lang_url, headers={"Referer": "http://example.org"})
        assert res.location == "/"
Пример #7
0
.. code-block:: console

    $ ./app-teardown.sh

SPHINX-END
"""

from __future__ import absolute_import, print_function

from flask import Flask, render_template
from flask_babelex import lazy_gettext as _

from invenio_i18n import InvenioI18N
from invenio_i18n.views import create_blueprint_from_app

# Create Flask application
app = Flask(__name__)
app.config.update(
    SECRET_KEY='CHANGE_ME',
    BABEL_DEFAULT_LOCALE='en',
    I18N_LANGUAGES=[('da', _('Danish')), ('es', _('Spanish'))],
)
InvenioI18N(app)
app.register_blueprint(create_blueprint_from_app(app))


@app.route('/')
def index():
    return render_template('invenio_i18n/page.html')