summaryrefslogtreecommitdiffstats
path: root/kyrias_website/__init__.py
blob: 474663d3d036751e78ebc9e0af4b35fd5f29c552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os

import flask_assets
from flask import Flask
from flask_flatpages import FlatPages
from werkzeug.utils import cached_property


class CustomFlatPages(FlatPages):
    def __init__(self, app=None, name=None):
        super(CustomFlatPages, self).__init__(app=app, name=name)
        self.app = app

    @cached_property
    def _pages(self):
        """Forget all pages and nuke our own cache"""
        try:
            del app.__dict__['journal_entries']
            del app.__dict__['journal_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')

    @cached_property
    def journal_entries(self):
        return sorted(self.journal, key=lambda x: x.meta.get('date'))

    @cached_property
    def journal_tags(self):
        tags = {}
        for entry in self.journal_entries:
            if 'tags' not in entry.meta:
                tags.setdefault('untagged', set()).add(entry)
            else:
                for tag in entry.meta.get('tags'):
                    tags.setdefault(tag, set()).add(entry)

        return tags


app = CustomFlask(__name__)

import kyrias_website.views

if __name__ == '__main__':
    app.run(port=5000)