Skip to content

Commit deb078a

Browse files
committed
Added site map and / redirects to site map
1 parent a0fec22 commit deb078a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

backend/PyMatcha/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,33 @@ def no_jwt_callback(error_message):
275275
# import tasks here to be registered by celery
276276

277277
import PyMatcha.utils.tasks # noqa
278+
from flask import url_for, redirect
279+
280+
281+
def has_no_empty_params(rule):
282+
defaults = rule.defaults if rule.defaults is not None else ()
283+
arguments = rule.arguments if rule.arguments is not None else ()
284+
return len(defaults) >= len(arguments)
285+
286+
287+
@application.route("/")
288+
def home():
289+
return redirect(url_for("site_map"))
290+
291+
292+
@application.route("/site-map")
293+
def site_map():
294+
links = []
295+
for rule in application.url_map.iter_rules():
296+
# Filter out rules we can't navigate to in a browser
297+
# and rules that require parameters
298+
if has_no_empty_params(rule):
299+
url = url_for(rule.endpoint, **(rule.defaults or {}))
300+
methods = ""
301+
for m in rule.methods:
302+
if m == "HEAD" or m == "OPTIONS":
303+
continue
304+
methods += f"{m}"
305+
links.append(f"{methods} {url}")
306+
# links is now a list of url, endpoint tuples
307+
return jsonify(links), 200

0 commit comments

Comments
 (0)