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 <mmoese@suse.com>
pull/53/head
Michael Moese 2 years ago
parent fd8f75a1b1
commit f93d9483b8

@ -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('/<path:path>.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)

@ -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")

@ -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'

@ -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('/<path:path>.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)

@ -16,7 +16,7 @@
<div id="sidebar">
<header>
<div class="site-title">
<a href="{{ url_for("index") }}">
<a href="{{ url_for("main_blueprint.index") }}">
<img src="{{ url_for('static', filename='logo.png') }}" alt="Fdgl - Freunde des geordneten Lärms e.V." height="150"/>
<br/>FdgL e.V.
</a>
@ -27,12 +27,12 @@
</div>
</header>
<nav id="sidebar-nav-links">
<a class="page-link" href="{{ url_for('index') }}">News</a>
<a class="page-link" href="{{ url_for('verein') }}">Verein</a>
<a class="page-link" href="{{ url_for('view_events') }}">Events</a>
<a class="page-link" href="{{ url_for('kontakt') }}">Kontakt</a>
<a class="page-link" href="{{ url_for('datenschutz') }}">Datenschutz</a>
<a class="page-link" href="{{ url_for('impressum') }}">Impressum</a>
<a class="page-link" href="{{ url_for('main_blueprint.index') }}">News</a>
<a class="page-link" href="{{ url_for('main_blueprint.verein') }}">Verein</a>
<a class="page-link" href="{{ url_for('main_blueprint.view_events') }}">Events</a>
<a class="page-link" href="{{ url_for('main_blueprint.kontakt') }}">Kontakt</a>
<a class="page-link" href="{{ url_for('main_blueprint.datenschutz') }}">Datenschutz</a>
<a class="page-link" href="{{ url_for('main_blueprint.impressum') }}">Impressum</a>
</nav>
</div>
<main class="container">

@ -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

Loading…
Cancel
Save