diff options
author | Christian Franke <nobody@nowhere.ws> | 2016-04-05 11:26:14 -0300 |
---|---|---|
committer | Christian Franke <nobody@nowhere.ws> | 2016-04-05 11:26:14 -0300 |
commit | f1989fe72df0a7cb4394ebe2ab6fe5d8ff408a38 (patch) | |
tree | fb3c6eddd44549fc2a1d368ea6b33cd71ffbfbca | |
parent | c5cc5086d00786cfb8696655ffbd95922dc5d68c (diff) |
Add tabular calendar
-rw-r--r-- | scripts/calendar_plugin2.py | 112 | ||||
-rw-r--r-- | scripts/plugin.py | 2 | ||||
-rw-r--r-- | scripts/template.conf | 1 | ||||
-rw-r--r-- | template/calendar_event.j2 | 4 | ||||
-rw-r--r-- | template/pages/termine/content.html | 2 | ||||
-rw-r--r-- | template/tabcalendar.j2 | 35 |
6 files changed, 153 insertions, 3 deletions
diff --git a/scripts/calendar_plugin2.py b/scripts/calendar_plugin2.py new file mode 100644 index 0000000..7138655 --- /dev/null +++ b/scripts/calendar_plugin2.py @@ -0,0 +1,112 @@ +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([]) + + 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()) diff --git a/scripts/plugin.py b/scripts/plugin.py index 08f0461..b6f21ff 100644 --- a/scripts/plugin.py +++ b/scripts/plugin.py @@ -1,3 +1,5 @@ +import sys + class PluginManager(object): def __init__(self): self.plugins = [] diff --git a/scripts/template.conf b/scripts/template.conf index 105f94a..8e6192a 100644 --- a/scripts/template.conf +++ b/scripts/template.conf @@ -8,4 +8,5 @@ target = ../public/ [Plugins] calendar_plugin=load +calendar_plugin2=load news=load diff --git a/template/calendar_event.j2 b/template/calendar_event.j2 index 106b65a..c8418f1 100644 --- a/template/calendar_event.j2 +++ b/template/calendar_event.j2 @@ -1,7 +1,7 @@ {%- if event.recurring %} -<p class="recurring"> +<p class="recurring" {% if 'id' in event %}id="event-{{event.id}}"{% endif %}> {%- else %} -<p class="onetime"> +<p class="onetime" {% if 'id' in event %}id="event-{{event.id}}"{% endif %}> {%- endif -%} <span class="date"> {%- if event.multiday %} diff --git a/template/pages/termine/content.html b/template/pages/termine/content.html index ab2c051..d14eb88 100644 --- a/template/pages/termine/content.html +++ b/template/pages/termine/content.html @@ -11,7 +11,7 @@ </div> <div class="row"> <div class="col-md-8"> -$calendar +$tabcalendar </div> <div class="col-md-4"> <a href="img/hallway.jpg"><img class="img-responsive" src="img/hallway_klein.jpg" alt="Unser Flur mit Buero, Lager, Hacklab."></a> diff --git a/template/tabcalendar.j2 b/template/tabcalendar.j2 new file mode 100644 index 0000000..9546e06 --- /dev/null +++ b/template/tabcalendar.j2 @@ -0,0 +1,35 @@ +<div class="calendar-table"> +{%- for month in months %} +<p>{{ month.date.strftime('%B %Y').decode('utf-8') }}</p> +<table> +<tr> + <th>Montag</th> + <th>Dienstag</th> + <th>Mittwoch</th> + <th>Donnerstag</th> + <th>Freitag</th> + <th>Samstag</th> + <th>Sonntag</th> +</tr> +{%- for week in month.weeks %} +<tr> +{%- for i in range(week[0].weekday) %} +<td></td> +{%- endfor %} +{%- for day in week %} +<td> +{%- for event in day.events %} +<a href="#event-{{ event.id }}">{{ event.name }}</a> +{%- if not loop.last %} +<br/> +{%- endif %} +{%- endfor %} +</td> +{%- endfor %} +</tr> +{%- endfor %} +</table> +{%- endfor %} +</div> +{% set class = 'expanded' %} +{% include 'calendar.j2' %} |