summaryrefslogtreecommitdiff
path: root/scripts/calendar_plugin.py
blob: c277a9f2a58480d60c948844a6bf02172d0a6213 (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
import template
import plugin
import string
import os

import locale
from datetime import datetime,timedelta
import jinja2
import json

class CalendarPlugin:
    def __init__(self):
        calpath = os.path.join(template.path, 'calendar.json')
        with open(calpath, 'r') as calfile:
            events = json.load(calfile)
        for event in events:
            event['start'] = datetime.strptime(event['start'], '%Y-%m-%dT%H:%M:%S')
            event['end'] = datetime.strptime(event['end'], '%Y-%m-%dT%H:%M:%S')
            event['multiday'] = (event['end'] - event['start']) > timedelta(days=1)
        self.events = sorted(events, key=lambda x:x['start'])
        template_loader = jinja2.FileSystemLoader(searchpath=os.path.join(template.path))
        self.template_env = jinja2.Environment(loader=template_loader)
        locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')

    def _get_calendar(self):
        return self.template_env.get_template('calendar.j2').render({
            'events': self.events,
            'class': 'calendar-main'
        }).encode('utf-8')

    def _get_calendar_head(self):
        return self.template_env.get_template('calendar_head.j2').render({
            'events': self.events,
            'class': 'calendar-head',
            'cutoff': datetime.now() + timedelta(days=7)
        }).encode('utf-8')

    def process_content(self, page_content):
        return_value = {}
        for filename, content in page_content.iteritems():
            t = string.Template(content)
            return_value[filename] = t.safe_substitute(
                calendar = self._get_calendar(),
                calendar_head = self._get_calendar_head()
            )
        return return_value

plugin.plugin_manager.register(CalendarPlugin())