diff options
-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) |