Commit 743b31c6 authored by Pietro Albini's avatar Pietro Albini

Complete the backend for downloads and add dummy pages

This commit completes the few bits left for the downloads backend, and
adds dummy content to the pages powered by it.

The "landing" page now features an ugly form for downloading, and the
"thank you" page shows the raw variables instead. The actual content
will be added in future commits.
parent 16bb2fad
......@@ -35,6 +35,7 @@ setuptools.setup(
"click",
"gunicorn",
"requests",
"itsdangerous",
],
packages = [
......
......@@ -44,6 +44,12 @@ def create_app(data_path):
app.config["CACHE_PATH"] = os.path.join(data_path, "cache")
cache.install_cache(app)
app.download = download.Downloads(data_path)
app.register_blueprint(
app.download.prepare_blueprint(app),
url_prefix="/download",
)
app.register_blueprint(pages.prepare_blueprint(app))
pages.prepare_navbar(app)
......
......@@ -19,6 +19,8 @@ import json
import os
import random
import flask
import itsdangerous
import requests
import pkg_resources
......@@ -172,3 +174,61 @@ class Downloads:
url = url.replace("{%s}" % key, value)
return url
def prepare_blueprint(self, app):
"""Prepare a blueprint with the download pages"""
signer = itsdangerous.URLSafeSerializer(app.secret_key)
bp = flask.Blueprint("download", __name__)
@bp.route("/<distro>")
def landing(distro):
return flask.render_template(
"download/landing.html",
distro_name = distro,
distro = self.config["distros"][distro],
releases = self.config["releases"],
)
@bp.route("/+redirect", methods=["POST"])
def redirect():
# Return a Bad Request if not all the fields are present
try:
distro = flask.request.form["distro"]
release = flask.request.form["release"]
arch = flask.request.form["arch"]
except KeyError:
flask.abort(400)
# Validate the input
if distro not in self.config["distros"]:
flask.abort(400)
if release not in self.config["distros"][distro]["releases"]:
flask.abort(400)
if arch not in self.config["distros"][distro]["archs"]:
flask.abort(400)
torrent = "torrent" in flask.request.form
payload = signer.dumps({
"url": self.url_for(distro, release, arch, torrent),
"md5": self.md5sums["%s:%s:%s" % (distro, release, arch)],
"name": "%s %s%s" % (
self.config["distros"][distro]["pretty-name"],
self.config["releases"][release]["version"],
" LTS" if self.config["releases"][release]["lts"] else "",
),
"theme": distro,
})
return flask.redirect(flask.url_for(".thanks", payload=payload))
@bp.route("/grazie/<payload>")
def thanks(payload):
try:
content = signer.loads(payload)
except itsdangerous.BadSignature:
flask.abort(400)
return flask.render_template("download/thanks.html", **content)
return bp
{# Source code of the Ubuntu-it website
# Copyright (C) 2016 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/>.
#}
{% extends "layout.html" %}
{% block title %}Scarica {{ distro["pretty-name"] }}{% endblock %}
{% block content %}
<div class="row">
<div class="twelve-col">
<h2>/!\ Pagina temporanea /!\</h2>
<p>Distro name: <b>{{ distro["pretty-name"] }}</b></p>
</div>
</div>
<div class="row">
<div class="twelve-col">
<form action="{{ url_for(".redirect") }}" method="post">
<input type="hidden" name="distro" value="{{ distro_name }}">
Version:
<select name="release">
{% for release in distro["releases"] %}
<option value="{{ release }}">
{{ distro["pretty-name"] }}
{{ releases[release]["version"] }}
{% if releases[release]["lts"] %}LTS{% endif %}
</option>
{% endfor %}
</select>
Arch:
<select name="arch">
{% for arch in distro["archs"] %}
<option value="{{ arch }}">{{ arch }}</option>
{% endfor %}
</select>
<input type="checkbox" name="torrent" value="1"> .torrent<br>
<button type="submit">Download</button>
</form>
</div>
</div>
{% endblock %}
{# Source code of the Ubuntu-it website
# Copyright (C) 2016 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/>.
#}
{% extends "layout.html" %}
{% block title %}Grazie per aver scaricato {{ name }}{% endblock %}
{% block content %}
<div class="row">
<div class="twelve-col">
<h2>/!\ Pagina temporanea /!\</h2>
</div>
</div>
<div class="row">
<div class="twelve-col">
Name:<br>
<pre>{{ name }}</pre><br>
Download URL:<br>
<pre>{{ url }}</pre><br>
MD5:
<pre>{{ md5 }}</pre><br>
Theme:
<pre>{{ theme }}</pre>
</div>
</div>
{% endblock %}
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