summaryrefslogtreecommitdiff
path: root/scripts/calendar_plugin2.py
blob: ea44483860b433e6be08e9a2bad970ad461613fa (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import template
import plugin
import string
import os

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

class TabularCalendarPlugin:
    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)
        events = sorted(events, key=lambda x:x['start'])

        i = 0
        for event in events:
            event['id'] = i
            i += 1

        first = events[0]['start']
        last = events[-1]['end']

        calendar_start = first - timedelta(days=first.weekday())
        if calendar_start.month != first.month:
            calendar_start = date(first.year, first.month, 1)
        else:
            calendar_start = date(calendar_start.year,
                                  calendar_start.month,
                                  calendar_start.day)
        calendar_end = last + timedelta(days=6 - last.weekday())
        if calendar_end.month != last.month:
            calendar_end = date(last.year,last.month,last.day)
            while True:
                next_end = calendar_end + timedelta(days=1)
                if next_end.month != calendar_end.month:
                    break
                calendar_end = next_end
        else:
            calendar_end = date(calendar_end.year,
                                calendar_end.month,
                                calendar_end.day)

        months = []
        cur_day = calendar_start
        active_month = None
        active_week = None
        while cur_day <= calendar_end:
            cur_month = cur_day.month
            cur_week = cur_day.isocalendar()[1]

            if cur_month != active_month:
                months.append({
                    'year': cur_day.year,
                    'month': cur_day.month,
                    'date': date(cur_day.year, cur_day.month, 1),
                    'weeks': [[]]
                })
                active_week = cur_week
                active_month = cur_month
            if cur_week != active_week:
                months[-1]['weeks'].append([])
                active_week = cur_week

            week_list = months[-1]['weeks'][-1]
            week_list.append({
                    'year': cur_day.year,
                    'month': cur_day.month,
                    'day': cur_day.day,
                    'weekday': cur_day.weekday(),
                    'events': []
            })
            event_list = week_list[-1]['events']
            day_start = datetime(cur_day.year, cur_day.month, cur_day.day)
            day_end = day_start + timedelta(days=1)

            for event in events:
                if event['start'] >= day_end \
                   or event['end'] <= day_start:
                    continue
                event_list.append(event)

            cur_day += timedelta(days=1)

        self.months = months
        self.events = events
        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('tabcalendar.j2').render({
            'months': self.months,
            'events': self.events,
        }).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(
                tabcalendar = self._get_calendar(),
            )
        return return_value

plugin.plugin_manager.register(TabularCalendarPlugin())