From f93d9483b8dc48488fdd21a59e4511f77e6cfba1 Mon Sep 17 00:00:00 2001 From: Michael Moese Date: Fri, 1 Jul 2022 15:19:37 +0200 Subject: [PATCH] Make use of flask blueprints Create a first main blueprint that contains all the routes for the static endpoints. Also move app initialization into crate_app() function Signed-off-by: Michael Moese --- app.py | 76 +++++++++++------------------------------ common.py | 7 ++++ config.py | 1 + main_blueprint.py | 65 +++++++++++++++++++++++++++++++++++ templates/template.html | 14 ++++---- utils.py | 4 +-- 6 files changed, 101 insertions(+), 66 deletions(-) create mode 100644 common.py create mode 100644 main_blueprint.py diff --git a/app.py b/app.py index 107fb1d..aef5583 100755 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: GPL-3.0 # vim: set sw=4 ts=4 ex from flask import Flask, render_template, render_template_string, url_for +from flask import Blueprint from flask_flatpages import FlatPages from flask_flatpages.utils import pygmented_markdown from flask_fontawesome import FontAwesome @@ -10,73 +11,34 @@ import locale from config import Config from fotogrid import fotogrid -from feed import generate_feed -from utils import markdown_with_jinja_renderer, format_date_string, sort_date_from_string, is_future_date +from utils import markdown_with_jinja_renderer, format_date_string +from utils sort_date_from_string, is_future_date -# create app -app = Flask(__name__) +from common import pages, events -# load configuration from config file config = Config -app.config.from_object(config) - -# register fotogrid with jinja -app.jinja_env.globals.update(fotogrid=fotogrid) -app.jinja_env.globals.update(format_date_string=format_date_string) - -pages = FlatPages(app, name="pages") -events = FlatPages(app, name="events") -fa = FontAwesome(app) - - -@app.route('/.html') -def page(path): - page = pages.get_or_404(path) - return render_template('page.html', page=page) - - -@app.route('/verein.html') -def verein(): - return render_template('verein.html') - -@app.route('/events.html') -def view_events(): - posts = [p for p in events if "date" in p.meta and is_future_date(p.meta["date"])] - sorted_events = sorted(posts, reverse=False, key=lambda event: - sort_date_from_string(event.meta["date"])) - return render_template('events.html', events=sorted_events) - - -@app.route('/kontakt.html') -def kontakt(): - return render_template('kontakt.html') - - -@app.route('/datenschutz.html') -def datenschutz(): - return render_template('datenschutz.html') - - -@app.route('/impressum.html') -def impressum(): - return render_template('impressum.html') +# create app +def create_app(): + app = Flask(__name__) + # load configuration from config file + app.config.from_object(config) + pages.init_app(app) + events.init_app(app) -@app.route('/') -@app.route('/index.html') -def index(): - posts = [p for p in pages if "date" in p.meta] - sorted_pages = sorted(posts, reverse=True, key=lambda page: - sort_date_from_string(page.meta["date"])) - return render_template('index.html', pages=sorted_pages) + # register fotogrid with jinja + app.jinja_env.globals.update(fotogrid=fotogrid) + app.jinja_env.globals.update(format_date_string=format_date_string) + fa = FontAwesome(app) -@app.route('/rss') -def rss(): - return generate_feed(pages) + from main_blueprint import main_blueprint + app.register_blueprint(main_blueprint) + return app if __name__ == '__main__': + app = create_app() locale.setlocale(locale.LC_TIME, config.LOCALE) app.run(host=config.HOST, port=config.PORT) diff --git a/common.py b/common.py new file mode 100644 index 0000000..4bf5518 --- /dev/null +++ b/common.py @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-3.0 +# vim: set sw=4 ts=4 ex + +from flask_flatpages import FlatPages + +pages = FlatPages(name="pages") +events = FlatPages(name="events") diff --git a/config.py b/config.py index 29a05d2..062a7f1 100644 --- a/config.py +++ b/config.py @@ -2,6 +2,7 @@ # vim: set sw=4 ts=4 ex from utils import markdown_with_jinja_renderer + class Config: """ configuration for application """ FLATPAGES_PAGES_EXTENSION = '.md' diff --git a/main_blueprint.py b/main_blueprint.py new file mode 100644 index 0000000..46ae748 --- /dev/null +++ b/main_blueprint.py @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: GPL-3.0 +# vim: set sw=4 ts=4 ex +from flask import Flask, render_template, render_template_string, url_for +from flask import Blueprint +from flask_flatpages import FlatPages +from flask_flatpages.utils import pygmented_markdown +from flask_fontawesome import FontAwesome +from datetime import datetime +import locale + +from config import Config +from fotogrid import fotogrid +from utils import markdown_with_jinja_renderer, format_date_string +from utils import sort_date_from_string, is_future_date +from common import pages, events + +main_blueprint = Blueprint('main_blueprint', __name__) + + +@main_blueprint.route('/.html') +def page(path): + page = pages.get_or_404(path) + return render_template('page.html', page=page) + + +@main_blueprint.route('/verein') +def verein(): + return render_template('verein.html') + + +@main_blueprint.route('/events.html') +def view_events(): + posts = [p for p in events if "date" in p.meta and is_future_date(p.meta["date"])] + sorted_events = sorted(posts, reverse=False, key=lambda event: + sort_date_from_string(event.meta["date"])) + return render_template('events.html', events=sorted_events) + + +@main_blueprint.route('/kontakt.html') +def kontakt(): + return render_template('kontakt.html') + + +@main_blueprint.route('/datenschutz.html') +def datenschutz(): + return render_template('datenschutz.html') + + +@main_blueprint.route('/impressum.html') +def impressum(): + return render_template('impressum.html') + + +@main_blueprint.route('/') +@main_blueprint.route('/index.html') +def index(): + posts = [p for p in pages if "date" in p.meta] + sorted_pages = sorted(posts, reverse=True, key=lambda page: + sort_date_from_string(page.meta["date"])) + return render_template('index.html', pages=sorted_pages) + + +@main_blueprint.route('/rss') +def rss(): + return generate_feed(pages) diff --git a/templates/template.html b/templates/template.html index 5d1207b..7554e3d 100644 --- a/templates/template.html +++ b/templates/template.html @@ -16,7 +16,7 @@
diff --git a/utils.py b/utils.py index f50206a..953f79a 100644 --- a/utils.py +++ b/utils.py @@ -4,12 +4,14 @@ from flask import render_template_string from flask_flatpages.utils import pygmented_markdown from datetime import datetime + # directly support jinja within markdown blogposts # https://flask-flatpages.readthedocs.io/en/v0.7.1/ def markdown_with_jinja_renderer(text): prerendered_body = render_template_string(text) return pygmented_markdown(prerendered_body) + # function to reformat date from blog posts for better # visualization on the pages def format_date_string(date_string): @@ -33,5 +35,3 @@ def is_future_date(date_string): pass return True - -