-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
114 lines (83 loc) · 3.23 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from typing import List, Dict
from urllib.parse import urlparse
import htmlmin
from flask import Flask, render_template, abort, redirect, request
from flask_assets import Environment
from webassets import Bundle
from parliamone import config, utils
from parliamone.utils import count_questions
app = Flask(__name__)
app.jinja_options["autoescape"] = lambda _: True
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
QUESTIONS: Dict[str, List[str]] = {}
def load_questions():
global QUESTIONS
QUESTIONS = utils.load_questions()
if config.HTTPS or config.CANONICAL_DOMAIN:
@app.before_request
def redirect_to_canonical_domain():
url_parts = urlparse(request.url)
if url_parts.netloc != config.CANONICAL_DOMAIN:
url_parts = url_parts._replace(netloc=config.CANONICAL_DOMAIN)
if config.HTTPS and url_parts.scheme != "https":
url_parts = url_parts._replace(scheme="https")
new_url = url_parts.geturl()
if new_url != request.url:
return redirect(new_url, code=301)
@app.after_request
def minify_html(response):
if not app.debug and response.content_type.startswith("text/html"):
response.set_data(
htmlmin.minify(response.get_data(as_text=True),
remove_comments=True,
remove_empty_space=True)
)
return response
assets = Environment(app)
# js = Bundle(...,
# filters='jsmin', output='static/js/main.min.js')
# assets.register('js_all', js)
sass = Bundle('sass/main.scss', filters='libsass', output='css/sass.css')
all_css = Bundle(sass, filters='rcssmin', output="css/main.min.css")
assets.register('css_all', all_css)
@app.route("/")
def home():
count = count_questions(QUESTIONS)
questions_count_more_than = (count // 50) * 50
return render_template("home.html.j2",
categories=[
{"slug": utils.get_category_slug(category),
"title": utils.get_category_title(category)}
for category in sorted(QUESTIONS)
],
categories_count=len(QUESTIONS),
questions_count_more_than=questions_count_more_than)
@app.route("/domande/<slug>")
def category_page(slug):
category = slug.replace("_", " ")
if category not in QUESTIONS:
abort(404)
title = utils.get_category_title(category)
questions = QUESTIONS[category]
questions_a = questions[::2]
questions_b = questions[1::2]
return render_template("category.html.j2",
category_title=title,
sub_title=title,
category_lower_title=category,
questions_a=questions_a,
questions_b=questions_b)
@app.route("/a-proposito")
def about():
return render_template("about.html.j2")
# Admin
@app.route("/reload", methods=["POST"])
def reload():
token = request.args.get('token')
if token != config.TOKEN:
abort(403)
load_questions()
return f"loaded {count_questions(QUESTIONS)} questions in {len(QUESTIONS)} categories."
#
load_questions()