Commits

Luke Plant committed 25fc2c0

Added 'this sunday' functionality

Comments (0)

Files changed (6)

christchurch/static/christchurch/css/style.css

 h2 { font-size: 1.1em; }
 h3 { font-size: 1.0em; }
 
+/* iframes */
+
+body.iframecontent {
+    background-color: white;
+    background-image: none;
+    color: black;
+}
+
+body.iframecontent .iframeinner
+{
+    /* allow room for h2 indent */
+    padding: 0 1em;
+}
+
 /* NAVBAR */
 #navbarcont {
     margin: 0;

christchurch/templates/christchurch/iframe.html

+<!DOCTYPE html>
+<html>
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+  <title>{{ title }}</title>
+  <link rel="StyleSheet" href="/static/christchurch/css/style.css" type="text/css">
+</head>
+<body class="iframecontent">
+  <div class="iframeinner">
+    {% block content %}
+    {% endblock %}
+  </div>
+</body>
+</html>

christchurch/templates/christchurch/thissunday.html

+{% extends "christchurch/iframe.html" %}
+{% block content %}
+{% if message %}
+  {{ message }}
+{% else %}
+  {% for event in events %}
+    {% ifchanged event.start.date %}
+      <h2>{{ event.start|date:"F j, Y" }}</h2>
+    {% endifchanged %}
+  <p><b>{{ event.nice_time }}:</b> {{ event.speaker }}</p>
+  {% endfor %}
+{% endif %}
+{% endblock %}

christchurch/urls.py

 admin.autodiscover()
 
 urlpatterns = patterns('',
-    url(r'^admin/', include(admin.site.urls)),
-    url(r'^', include('cms.urls')),
+                       url(r'^thissunday/', 'christchurch.views.this_sunday'),
+                       url(r'^admin/', include(admin.site.urls)),
+                       url(r'^', include('cms.urls')),
+                       # Sermons views included via apphooks
 )
 
 if settings.DEVBOX:

christchurch/views.py

+from datetime import datetime, timedelta
+from dateutil import tz
+import pytz
+import re
+
+from django.shortcuts import render
+import requests
+import vobject
+
+
+PREACHING_ICAL = 'http://www.google.com/calendar/ical/c3kc8arf6hr51dh146dnsiq040%40group.calendar.google.com/public/basic.ics'
+
+MIDWEEK_ICAL = 'https://www.google.com/calendar/ical/rd8ant2lkdackckjk587kfr68g%40group.calendar.google.com/public/basic.ics'
+# Some bad data like this
+bad_dates = re.compile(r'CREATED:0000\d{4}T\d*Z\r\n')
+
+local_timezone = pytz.timezone('Europe/London')
+
+def get_calendar(url):
+    data = requests.get(url).content
+    data = bad_dates.sub('', data)
+    return vobject.readOne(data)
+
+
+class Event(object):
+    def __init__(self, start, speaker):
+        self.start = start
+        self.speaker = speaker
+
+    def nice_time(self):
+        if self.start.hour < 11:
+            return "Morning"
+        elif self.start.hour >= 17:
+            return "Evening"
+        else:
+            return "Afternoon"
+
+    def __cmp__(self, other):
+        return cmp(self.start, other.start)
+
+def this_sunday(request):
+    try:
+        cal = get_calendar(PREACHING_ICAL)
+    except Exception:
+        raise
+        cal = None
+
+    c = {}
+    if cal is not None:
+        events = []
+        for vevent in cal.vevent_list:
+            d = vevent.dtstart.value
+            if d.tzinfo is None:
+                d = d.replace(tzinfo=tz.tzutc())
+
+            d = d.astimezone(local_timezone)
+            # Clock should 'tick over' to the next week at the end of Sunday,
+            # not part way through, so truncate hour to zero
+            today = datetime.now(local_timezone).replace(hour=0)
+            if d > today and d < today + timedelta(7):
+                events.append(Event(d, vevent.summary.value))
+        events.sort()
+        c['events'] = events
+    else:
+        c['message'] = "Calendar not available"
+
+    return render(request, "christchurch/thissunday.html", c)
+
+
 django-appmedia
 django-classy-tags>=0.2.2
 django==1.3
+vobject
+requests