implement dynamic adding of default-type subpages

This commit is contained in:
2020-07-27 19:53:19 +02:00
parent c5238c3200
commit 1f28f4a6d8
4 changed files with 41 additions and 2 deletions

View File

@@ -0,0 +1,3 @@
{
"example" : "example_subpage_content.html"
}

View File

@@ -3,6 +3,7 @@ import json
import os import os
import flask import flask
import argparse import argparse
import jinja2
import caldav import caldav
import datetime as dt import datetime as dt
@@ -40,6 +41,11 @@ EMPTY_STRING = ""
READ = "r" READ = "r"
WRITE = "w" WRITE = "w"
# subpages
IDENTIFIER_PREFIX = "PAGE_"
SUBPAGE_CONFIG_FILE = "subpages.json"
SUBPAGE_CONTENT_DIR = "subpages/"
app = flask.Flask("FLASK_JSON_DREAM_WEBSITE", static_folder=None) app = flask.Flask("FLASK_JSON_DREAM_WEBSITE", static_folder=None)
app.config.from_object("config") app.config.from_object("config")
@@ -144,6 +150,15 @@ def people():
return flask.render_template("people.html", conf=app.config, return flask.render_template("people.html", conf=app.config,
people=readJsonDir("people/")) people=readJsonDir("people/"))
@app.route("/content/")
def content():
identifier = IDENTIFIER_PREFIX + flask.request.args.get("id")
if identifier in app.config:
markupText = flask.Markup(flask.render_template(app.config[identifier]))
return flask.render_template("default_content.html", conf=app.config, markupText=markupText)
else:
return (EMPTY_STRING, HTTP_NOT_FOUND)
@app.route("/news") @app.route("/news")
def news(): def news():
'''Display news-articles based on a UID-parameter''' '''Display news-articles based on a UID-parameter'''
@@ -244,6 +259,8 @@ def siteMap():
@app.before_first_request @app.before_first_request
def init(): def init():
'''Before first request configuration'''
app.config["SECTIONS_DIR"] = os.path.join(app.config["CONTENT_DIR"], SECTIONS_DIR) app.config["SECTIONS_DIR"] = os.path.join(app.config["CONTENT_DIR"], SECTIONS_DIR)
app.config["NEWS_DIR"] = os.path.join(app.config["CONTENT_DIR"], NEWS_DIR) app.config["NEWS_DIR"] = os.path.join(app.config["CONTENT_DIR"], NEWS_DIR)
app.config["MAIN_LINKS_DIR"] = os.path.join(app.config["CONTENT_DIR"], MAIN_LINKS_DIR) app.config["MAIN_LINKS_DIR"] = os.path.join(app.config["CONTENT_DIR"], MAIN_LINKS_DIR)
@@ -253,6 +270,26 @@ def init():
app.config["START_TIME"] = dt.datetime.now() app.config["START_TIME"] = dt.datetime.now()
# add additional pages if they exist #
subpageConfigFileTmp = os.path.join(app.config["CONTENT_DIR"], SUBPAGE_CONFIG_FILE)
if os.path.isfile(subpageConfigFileTmp):
# parse subpage config file #
subpages = dict()
with open(subpageConfigFileTmp) as f:
subpages = json.load(f)
# set template paths for identifier in app config #
for identifier in subpages.keys():
app.config[IDENTIFIER_PREFIX + identifier] = subpages[identifier]
# set custom loader to support second template dir #
subpageContentDirTmp = os.path.join(app.config["CONTENT_DIR"], SUBPAGE_CONTENT_DIR)
fsLoader = jinja2.FileSystemLoader([subpageContentDirTmp])
print(subpageContentDirTmp)
choiceLoader = jinja2.ChoiceLoader([ app.jinja_loader, fsLoader])
app.jinja_loader = choiceLoader
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Projects Showcase', parser = argparse.ArgumentParser(description='Projects Showcase',

View File

@@ -3,7 +3,6 @@
<head> <head>
{% include 'head.html' %} {% include 'head.html' %}
<title>Stammtisch</title>
</head> </head>
<body class="h-100 bg-special"> <body class="h-100 bg-special">
@@ -12,7 +11,7 @@
<div class="container mt-5 mb-5 h-100"> <div class="container mt-5 mb-5 h-100">
<div class="row impressum mt-5"></div> <div class="row impressum mt-5"></div>
<div class="col-lg-12"> <div class="col-lg-12">
{% include 'stammtisch_text.html' %} {{ markupText }}
<div class="pb-3"></div> <div class="pb-3"></div>
</div> </div>
</div> </div>