-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
188 lines (165 loc) · 6.06 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from functools import partial
from flask import Flask, request, render_template, redirect, Response, url_for
from flask_caching import Cache
from werkzeug.exceptions import NotFound, Gone
import logging
from cachier import cachier
from index_admin import create_index_admin
from utils import register_redirects
from bot_api import create_botapi
from views.land import get_featured, get_covid, get_recents
from views.search import SearchForm, search_document, search_corpus
from views.read import Read, Diamonds
from views.standard_messages import standard_messages
from views import exceptions
from views.sitemapping import SiteMap
from settings import (
DEBUG,
FEATURED,
LANGUAGE_DOMAIN,
DEAD_SIMPLE_REDIRECTS,
BOTAPI,
CACHE_CONFIG,
FS_CACHE_DIR,
FS_CACHE_STALE_AFTER,
)
logger = logging.getLogger(__name__)
diamonds = Diamonds()
read = Read()
fs_cache = partial(
cachier,
cache_dir=FS_CACHE_DIR,
stale_after=FS_CACHE_STALE_AFTER,
next_time=not DEBUG,
)
def create_app():
app = Flask(__name__)
cache = Cache(app, config=CACHE_CONFIG)
cache.init_app(app)
exceptions.register_app(app)
register_redirects(app, DEAD_SIMPLE_REDIRECTS)
@app.route("/")
@fs_cache()
def landing():
content = {
"messenger": standard_messages,
"title": standard_messages["This_Domain"],
"description": standard_messages["og_description"],
"r_path": f"/eu/search",
"form": SearchForm.default(),
"filter_visibility": "w3-hide",
"featured_acts": get_featured("eu", FEATURED["eu"]),
"covid_acts": get_covid(),
"recent_acts": get_recents(),
"languages_and_domains": LANGUAGE_DOMAIN,
"url_path": "",
"url": standard_messages["lexparency_url"],
}
return render_template("land.html", **content)
@app.route("/<obsolete_document_path>/")
def handle_obsolete_document_path(obsolete_document_path):
"""Guess what! Even more legacy path handling."""
if obsolete_document_path == "eu":
return redirect(url_for("landing"), 307)
raise Gone(obsolete_document_path)
@app.route("/sitemap.xml")
@fs_cache()
def sitemap():
r = Response(
response=SiteMap.build("eu").dump().decode("ascii"),
status=200,
mimetype="application/xml",
)
r.headers["Content-Type"] = "text/xml; charset=utf-8"
return r
app.register_blueprint(create_botapi(cache.cached()), url_prefix=f"/{BOTAPI}")
app.register_blueprint(create_index_admin())
@app.route("/eu/search")
def corpus_search():
if request.args:
if "search_words" in request.args:
if request.args["search_words"].strip() == "":
return redirect(request.path, 307)
else:
return redirect(request.path, 307)
return search_corpus(request.path, request.args)
@app.route("/<domain>/<document_id>/search")
def document_search(domain, document_id):
id_local = diamonds.get_canonical(document_id)
all_versions = request.args.get("all_versions") is not None
return render_template(
"search_document.html",
all_versions=(all_versions * "checked"),
**search_document(
request.path,
domain,
id_local,
words=request.args.get("search_words", ""),
page=int(request.args.get("page", 1)),
all_versions=all_versions,
),
document_id=document_id,
version="",
)
@app.route("/<domain>/<id_local>/<sub_id>/EN/<version>")
@app.route("/<domain>/<id_local>/<sub_id>/EN/")
def legacy(domain, id_local, sub_id, version="latest"):
try:
read(domain, id_local, sub_id, version)
except NotFound:
raise Gone(request.path)
return redirect(
Read.get_standard_path(domain, id_local, sub_id, version), code=301
)
# noinspection PyUnusedLocal
@app.route("/<domain>/<document_id>/en/<sub_id>/")
@app.route("/<domain>/<document_id>/en/<sub_id>")
def legacy_gones(domain, document_id, sub_id):
raise Gone(request.path)
@app.route("/<id_local>/<sub_id>/latest")
def paying_my_dues(id_local, sub_id):
"""This is just because I messed up previous canonical tags :/
So, this is a stupid and annoying type of legacy handling.
But it's necessary.
"""
if sub_id.upper().startswith("ARTI"):
sub_id = "ART_" + sub_id.split(" ")[1]
elif sub_id.startswith("Erw") or sub_id.startswith("Rec"):
sub_id = "PRE"
elif sub_id.upper().startswith("ANHANG"):
splitted = sub_id.split(" ")
if len(splitted) == 2:
sub_id = "ANX_" + splitted[1]
else:
sub_id = "ANX"
elif sub_id.lower().startswith("fin"):
sub_id = "FIN"
return redirect(f"/eu/{id_local}/{sub_id}/", 301)
@app.route("/" + standard_messages["impress_path"])
def about():
return render_template(
f"about.html",
title=standard_messages["title_about"],
description=standard_messages["title_about"],
messenger=standard_messages,
)
@app.route("/bot_api_documentation")
def bot_api_documentation():
return render_template(
f"bot_api_documentation.html",
title="Bot-API Documentation",
description="Bot-API",
messenger=standard_messages,
)
@app.route("/" + standard_messages["data_protection_path"])
def data_protection():
return render_template(
f"data_protection.html",
title=standard_messages["data_protection_title"],
description=standard_messages["data_protection_title"],
messenger=standard_messages,
)
Read.register(app)
return app
if __name__ == "__main__":
create_app().run(debug=DEBUG)