mirror of
https://github.com/FAUSheppy/flask-json-dream-website
synced 2025-12-06 00:01:36 +01:00
implement dynamic adding of default-type subpages
This commit is contained in:
3
content.example/subpages.json
Normal file
3
content.example/subpages.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"example" : "example_subpage_content.html"
|
||||
}
|
||||
37
server.py
37
server.py
@@ -3,6 +3,7 @@ import json
|
||||
import os
|
||||
import flask
|
||||
import argparse
|
||||
import jinja2
|
||||
|
||||
import caldav
|
||||
import datetime as dt
|
||||
@@ -40,6 +41,11 @@ EMPTY_STRING = ""
|
||||
READ = "r"
|
||||
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.config.from_object("config")
|
||||
|
||||
@@ -144,6 +150,15 @@ def people():
|
||||
return flask.render_template("people.html", conf=app.config,
|
||||
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")
|
||||
def news():
|
||||
'''Display news-articles based on a UID-parameter'''
|
||||
@@ -244,6 +259,8 @@ def siteMap():
|
||||
|
||||
@app.before_first_request
|
||||
def init():
|
||||
'''Before first request configuration'''
|
||||
|
||||
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["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()
|
||||
|
||||
# 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__":
|
||||
|
||||
parser = argparse.ArgumentParser(description='Projects Showcase',
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
<head>
|
||||
|
||||
{% include 'head.html' %}
|
||||
<title>Stammtisch</title>
|
||||
|
||||
</head>
|
||||
<body class="h-100 bg-special">
|
||||
@@ -12,7 +11,7 @@
|
||||
<div class="container mt-5 mb-5 h-100">
|
||||
<div class="row impressum mt-5"></div>
|
||||
<div class="col-lg-12">
|
||||
{% include 'stammtisch_text.html' %}
|
||||
{{ markupText }}
|
||||
<div class="pb-3"></div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user