diff options
author | Johannes Löthberg <johannes@kyriasis.com> | 2017-06-09 21:50:59 +0100 |
---|---|---|
committer | Johannes Löthberg <johannes@kyriasis.com> | 2017-06-09 22:51:50 +0200 |
commit | 9bee01ce26f7b5db038f2dae3227a9714233c844 (patch) | |
tree | 76a23ea830c989828a719f1ea217671da5798414 | |
parent | 55bb964cf8a106679f369f8c3c7d9874f4711663 (diff) | |
download | kyrias-website-9bee01ce26f7b5db038f2dae3227a9714233c844.tar.xz |
Not much use in wrapping everything in the CustomFlask class, so put
everything at the top level of __init__.py.
Rename CustomFlatPages to JournalFlatPages and only use it for the
journal FlatPages instance. Also move the journal_entries and
journal_tags members to JournalFlatPages, renaming them to just entries
and tags.
Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
-rw-r--r-- | kyrias_website/__init__.py | 55 | ||||
-rw-r--r-- | kyrias_website/views.py | 20 |
2 files changed, 34 insertions, 41 deletions
diff --git a/kyrias_website/__init__.py b/kyrias_website/__init__.py index 474663d..5f07b65 100644 --- a/kyrias_website/__init__.py +++ b/kyrias_website/__init__.py @@ -6,49 +6,28 @@ from flask_flatpages import FlatPages from werkzeug.utils import cached_property -class CustomFlatPages(FlatPages): +class JournalFlatPages(FlatPages): def __init__(self, app=None, name=None): - super(CustomFlatPages, self).__init__(app=app, name=name) - self.app = app + super(JournalFlatPages, self).__init__(app=app, name=name) @cached_property def _pages(self): """Forget all pages and nuke our own cache""" try: - del app.__dict__['journal_entries'] - del app.__dict__['journal_tags'] + del self.__dict__['entries'] + del self.__dict__['tags'] except KeyError: pass - return super(CustomFlatPages, self)._pages - - -class CustomFlask(Flask): - def __init__(self, *args, **kwargs): - super(CustomFlask, self).__init__(*args, **kwargs) - - config = { - 'default': 'kyrias_website.config.BaseConfig', - 'development': 'kyrias_website.config.DevelopmentConfig', - } - - config_name = os.getenv('WEBSITE_CONFIGURATION', 'default') - self.config.from_object(config[config_name]) - - assets = flask_assets.Environment(self) - css = flask_assets.Bundle('base.css', filters='cssmin', output='screen.css') - assets.register('css_all', css) - - self.pages = CustomFlatPages(self, 'pages') - self.journal = CustomFlatPages(self, 'journal') + return super(JournalFlatPages, self)._pages @cached_property - def journal_entries(self): - return sorted(self.journal, key=lambda x: x.meta.get('date')) + def entries(self): + return sorted(self, key=lambda x: x.meta.get('date')) @cached_property - def journal_tags(self): + def tags(self): tags = {} - for entry in self.journal_entries: + for entry in self.entries: if 'tags' not in entry.meta: tags.setdefault('untagged', set()).add(entry) else: @@ -58,7 +37,21 @@ class CustomFlask(Flask): return tags -app = CustomFlask(__name__) +app = Flask(__name__) + +config = { + 'default': 'kyrias_website.config.BaseConfig', + 'development': 'kyrias_website.config.DevelopmentConfig', +} +config_name = os.getenv('WEBSITE_CONFIGURATION', 'development') +app.config.from_object(config[config_name]) + +assets = flask_assets.Environment(app) +css = flask_assets.Bundle('base.css', filters='cssmin', output='screen.css') +assets.register('css_all', css) + +pages = FlatPages(app, 'pages') +journal = JournalFlatPages(app, 'journal') import kyrias_website.views diff --git a/kyrias_website/views.py b/kyrias_website/views.py index 3065aef..14a81eb 100644 --- a/kyrias_website/views.py +++ b/kyrias_website/views.py @@ -1,11 +1,11 @@ -from kyrias_website import app +from kyrias_website import app, pages, journal from flask import render_template # Render all static pages as fallback @app.route('/<path:path>/') def page(path): - page = app.pages.get_or_404(path) + page = pages.get_or_404(path) return render_template('page.html', page=page) @@ -13,27 +13,27 @@ def page(path): @app.route('/index/') @app.route('/') def index(): - current = app.journal_entries[-1] - older = next(iter(app.journal_entries[-2:-1]), None) + current = journal.entries[-1] + older = next(iter(journal.entries[-2:-1]), None) return render_template('entry.html', entry=current, older=older) @app.route('/archive/') def archive(): - return render_template('archive.html', entries=app.journal_entries) + return render_template('archive.html', entries=journal.entries) @app.route('/tags/') def tags(): - return render_template('tags.html', tags=app.journal_tags) + return render_template('tags.html', tags=journal.tags) @app.route('/blog/<path:name>/') def entry(name): - entry = app.journal.get_or_404(name) + entry = journal.get_or_404(name) - index = app.journal_entries.index(entry) - older = next(iter(app.journal_entries[index-1:index]), None) - newer = next(iter(app.journal_entries[index+1:index+2]), None) + index = journal.entries.index(entry) + older = next(iter(journal.entries[index-1:index]), None) + newer = next(iter(journal.entries[index+1:index+2]), None) return render_template('entry.html', entry=entry, older=older, newer=newer) |