Commit 7fb11b74 authored by Pietro Albini's avatar Pietro Albini

aggiungi il supporto ai redirect

parent ff3db682
Pipeline #132 passed with stage
in 0 seconds
......@@ -25,7 +25,7 @@ from . import db
from . import download
from . import navbar
from . import pages
from . import utils
from . import redirects
from . import utils
......@@ -57,6 +57,7 @@ def create_app(data_path):
app.download = download.Downloads(data_path)
app.register_blueprint(redirects.prepare_blueprint(app))
app.register_blueprint(app.download.prepare_blueprint(app), url_prefix="/download")
app.register_blueprint(actions.prepare_blueprint(app), url_prefix="/+actions")
app.register_blueprint(auth.prepare_blueprint(app), url_prefix="/+auth")
......
......@@ -53,6 +53,8 @@ def run(data, gunicorn_config, port, public, workers, debug):
extra_files = [
os.path.join(src_directory, "data/navbar.yml"),
os.path.join(src_directory, "data/permissions.yml"),
os.path.join(src_directory, "data/redirects.yml"),
os.path.join(src_directory, "data/downloads.toml"),
]
app.run(debug=True, port=port, host=host, extra_files=extra_files)
......
# La sezione "internal" contiene i redirect che puntano ad altre pagine sempre
# all'interno del sito di Ubuntu-it. La chiave è l'indirizzo originale, mentre
# il valore deve contenere l'endpoint, ed opzionalmente gli argomenti
# dell'endpoint dentro alla chiave args. Per esempio:
#
# "/old-download/desktop":
# endpoint: download.landing
# args:
# distro: desktop
internal:
"/download/derivate":
endpoint: download.index
"/comunita/orientamento":
endpoint: pages.contribuire_index
# La sezione "external" contiene i redirect che puntano a pagine esterne al
# sito di Ubuntu-it. La chiave è l'indirizzo interno, il valore è l'URL a cui
# gli utenti devono essere reindirizzati
external:
"/scopri-ubuntu/richiedi-cd": "https://wiki.ubuntu-it.org/GruppoPromozione/ProgettoCDUbuntu"
# Source code of the Ubuntu-it website
# Copyright (C) 2018 Pietro Albini <pietroalbini@ubuntu.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; witout even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import flask
import pkg_resources
import yaml
import hashlib
def prepare_blueprint(app):
"""Prepare a blueprint containing all the redirects"""
raw = pkg_resources.resource_string("uitwww", "data/redirects.yml")
config = yaml.load(raw.decode("utf-8"))
bp = flask.Blueprint("redirects", __name__)
for origin, dest in config["external"].items():
endpoint = "ext_" + hashlib.sha1(origin.encode("utf-8")).hexdigest()[:10]
@bp.route(origin, endpoint=endpoint)
def _(_dest=dest):
return flask.redirect(_dest)
for origin, dest in config["internal"].items():
endpoint = "int_" + hashlib.sha1(origin.encode("utf-8")).hexdigest()[:10]
@bp.route(origin, endpoint=endpoint)
def _(_dest=dest):
_dest.setdefault("args", {})
return flask.redirect(
flask.url_for(_dest["endpoint"], **_dest["args"])
)
return bp
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment