-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
app.py
executable file
·277 lines (241 loc) · 9.71 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2024 Nicco Kunzmann and Open Web Calendar Contributors <https://open-web-calendar.quelltext.eu/>
#
# SPDX-License-Identifier: GPL-2.0-only
from flask import Flask, render_template, make_response, request, jsonify, \
redirect, send_from_directory, Response
from flask_allowedhosts import limit_hosts
from flask_caching import Cache
import json
import os
import tempfile
import requests
import icalendar
import datetime
from dateutil.rrule import rrulestr
from pprint import pprint
import yaml
import traceback
import io
import sys
from convert_to_dhtmlx import ConvertToDhtmlx
from convert_to_ics import ConvertToICS
import pytz
import translate
import mimetypes
# configuration
DEBUG = os.environ.get("APP_DEBUG", "true").lower() == "true"
PORT = int(os.environ.get("PORT", "5000"))
CACHE_REQUESTED_URLS_FOR_SECONDS = int(os.environ.get("CACHE_REQUESTED_URLS_FOR_SECONDS", 600))
ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "").split(",")
if ALLOWED_HOSTS == [""]:
ALLOWED_HOSTS = []
# constants
HERE = os.path.dirname(__file__) or "."
DEFAULT_SPECIFICATION_PATH = os.path.join(HERE, "default_specification.yml")
TEMPLATE_FOLDER_NAME = "templates"
TEMPLATE_FOLDER = os.path.join(HERE, TEMPLATE_FOLDER_NAME)
CALENDARS_TEMPLATE_FOLDER_NAME = "calendars"
CALENDAR_TEMPLATE_FOLDER = os.path.join(TEMPLATE_FOLDER, CALENDARS_TEMPLATE_FOLDER_NAME)
STATIC_FOLDER_NAME = "static"
STATIC_FOLDER_PATH = os.path.join(HERE, STATIC_FOLDER_NAME)
DHTMLX_LANGUAGES_FILE = os.path.join(STATIC_FOLDER_PATH, "js", "dhtmlx", "locale", "languages.json")
DEFAULT_REQUEST_HEADERS = {
"user-agent": "open-web-calendar",
}
# specification
PARAM_SPECIFICATION_URL = "specification_url"
# globals
app = Flask(__name__, template_folder="templates")
# Check Configuring Flask-Cache section for more details
CACHE_CONFIG = {
'CACHE_TYPE': 'FileSystemCache',
'CACHE_DIR': tempfile.mkdtemp(prefix="owc-cache-")}
cache = Cache(app, config=CACHE_CONFIG)
# caching
__URL_CACHE = {}
def cache_url(url, text):
"""Cache the value of a url."""
__URL_CACHE[url] = text
try:
get_text_from_url(url)
finally:
del __URL_CACHE[url]
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
return r
# configuration
def get_configuration():
"""Return the configuration for the browser"""
config = {
"default_specification": get_default_specification(),
"timezones": pytz.all_timezones, # see https://stackoverflow.com/a/13867319
"dhtmlx": {
"languages": translate.dhtmlx_languages()
},
"index": {
"languages": translate.languages_for_the_index_file()
}
}
return config
def set_JS_headers(response):
response = make_response(response)
response.headers['Access-Control-Allow-Origin'] = '*'
# see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowHeaderFromPreflight
response.headers['Access-Control-Allow-Headers'] = request.headers.get("Access-Control-Request-Headers")
response.headers['Content-Type'] = 'text/calendar'
return response
def set_js_headers(func):
"""Set the response headers for a valid CORS request."""
def with_js_response(*args, **kw):
return set_JS_headers(func(*args, **kw))
return with_js_response
def make_js_file_response(content:str) -> Response:
"""Modify the response to set the content type for .js files."""
response = make_response(content)
set_JS_headers(response)
response.headers['Content-Type'] = "text/javascript"
return response
@cache.memoize(
CACHE_REQUESTED_URLS_FOR_SECONDS,
forced_update=lambda: bool(__URL_CACHE))
def get_text_from_url(url):
"""Return the text from a url.
The result is cached CACHE_REQUESTED_URLS_FOR_SECONDS.
"""
if __URL_CACHE:
return __URL_CACHE[url]
return requests.get(url, headers=DEFAULT_REQUEST_HEADERS).content
def get_default_specification():
"""Return the default specification."""
with open(DEFAULT_SPECIFICATION_PATH, encoding="UTF-8") as file:
return yaml.safe_load(file)
def get_specification(query=None):
"""Build the calendar specification."""
if query is None:
query = request.args
specification = get_default_specification()
# get a request parameter, see https://stackoverflow.com/a/11774434
url = query.get(PARAM_SPECIFICATION_URL, None)
if url:
url_specification_response = get_text_from_url(url)
try:
url_specification_values = json.loads(url_specification_response)
except json.JSONDecodeError:
url_specification_values = yaml.safe_load(url_specification_response)
specification.update(url_specification_values)
for parameter in query:
# get a list of arguments
# see https://web.archive.org/web/20230325034825/https://werkzeug.palletsprojects.com/en/0.14.x/datastructures/
value = query.getlist(parameter)
# convert values
for i in range(len(value)):
if value[i] in ("false", "False"):
value[i] = False
elif value[i] in ("true", "True"):
value[i] = True
if len(value) == 1 and type(specification.get(parameter)) != list:
value = value[0]
specification[parameter] = value
return specification
def get_query_string():
return "?" + request.query_string.decode()
def render_app_template(template, specification):
translation_file = os.path.splitext(template)[0]
return render_template(template,
specification=specification,
configuration = get_configuration(),
json=json,
get_query_string=get_query_string,
html=lambda id, **template_replacements: translate.html(specification["language"], translation_file, id, **template_replacements)
)
@app.route("/calendar.<type>", methods=['GET', 'OPTIONS'])
@limit_hosts(allowed_hosts=ALLOWED_HOSTS)
# use query string in cache, see https://stackoverflow.com/a/47181782/1320237
#@cache.cached(timeout=CACHE_TIMEOUT, query_string=True)
def get_calendar(type):
"""Return a calendar."""
specification = get_specification()
if type == "spec":
return jsonify(specification)
if type == "events.json":
strategy = ConvertToDhtmlx(specification, get_text_from_url)
strategy.retrieve_calendars()
return strategy.merge()
if type == "ics":
strategy = ConvertToICS(specification, get_text_from_url)
strategy.retrieve_calendars()
return strategy.merge()
if type == "html":
template_name = specification["template"]
all_template_names = os.listdir(CALENDAR_TEMPLATE_FOLDER)
assert template_name in all_template_names, "Template names must be file names like \"{}\", not \"{}\".".format("\", \"".join(all_template_names), template_name)
template = CALENDARS_TEMPLATE_FOLDER_NAME + "/" + template_name
return render_app_template(template, specification)
raise ValueError("Cannot use extension {}. Please see the documentation or report an error.".format(type))
for folder_name in os.listdir(STATIC_FOLDER_PATH):
folder_path = os.path.join(STATIC_FOLDER_PATH, folder_name)
if not os.path.isdir(folder_path):
continue
@app.route('/' + folder_name + '/<path:path>', endpoint="static/" + folder_name)
def send_static(path, folder_name=folder_name):
return send_from_directory('static/' + folder_name, path)
@app.route("/")
@app.route("/index.html")
@limit_hosts(allowed_hosts=ALLOWED_HOSTS)
def serve_index():
specification = get_specification()
return render_app_template("index.html", specification)
@app.route("/about.html")
@limit_hosts(allowed_hosts=ALLOWED_HOSTS)
def serve_about():
specification = get_specification()
return render_app_template("about.html", specification)
@app.route("/configuration.js")
@limit_hosts(allowed_hosts=ALLOWED_HOSTS)
def serve_configuration():
return make_js_file_response("/* generated */\nconst configuration = {};".format(json.dumps(get_configuration())))
@app.route("/locale_<lang>.js")
@limit_hosts(allowed_hosts=ALLOWED_HOSTS)
def serve_locale(lang):
"""Serve the locale translations for the web frontend DHTMLX."""
return make_js_file_response(render_template("locale.js", locale=json.dumps(translate.dhtmlx(lang), indent=" ")))
@app.errorhandler(500)
def unhandledException(error):
"""Called when an error occurs.
See https://stackoverflow.com/q/14993318
"""
file = io.StringIO()
traceback.print_exception(type(error), error, error.__traceback__, file=file)
return """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>500 Internal Server Error</title>
</head>
<body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
<pre>\r\n{traceback}
</pre>
</body>
</html>
""".format(traceback=file.getvalue()), 500 # return error code from https://stackoverflow.com/a/7824605
@app.errorhandler(403)
def host_not_allowed(error):
return render_template(
"403.html",
hostname=request.host.split(':')[0],
allowed_hosts=", ".join(ALLOWED_HOSTS)
), 403
# make serializable for multiprocessing
#app.__reduce__ = lambda: __name__ + ".app"
if __name__ == "__main__":
app.run(debug=DEBUG, host="0.0.0.0", port=PORT)