add three calendars to the channel_calendar view
authorTero Marttila <terom@fixme.fi>
Wed, 11 Feb 2009 22:56:43 +0200
changeset 112 090192b64d7e
parent 111 95c0c49d76aa
child 113 9fc0eb751b6a
add three calendars to the channel_calendar view
handlers.py
helpers.py
log_source.py
static/irclogs.css
templates/channel_calendar.tmpl
--- a/handlers.py	Wed Feb 11 22:24:55 2009 +0200
+++ b/handlers.py	Wed Feb 11 22:56:43 2009 +0200
@@ -181,17 +181,12 @@
         day     = 1
     ))
 
-    # get set of days available
-    days = set(channel.source.get_month_days(target))
-
     # display calendar
     return templates.render_to_response("channel_calendar",
         req             = request,
         prefs           = request.prefs,
         channel         = channel,
-        calendar        = calendar.Calendar(),
-        month           = target.date(),
-        days            = days,
+        month           = target,
     )
 
 @preferences.handler(prefs.formatter, prefs.timezone, prefs.count)
--- a/helpers.py	Wed Feb 11 22:24:55 2009 +0200
+++ b/helpers.py	Wed Feb 11 22:56:43 2009 +0200
@@ -13,6 +13,12 @@
         Our set of helpers, inheriting from base helpers
     """
 
+    # set contructor...
+    set = set
+
+    # reference to calendar instance
+    calendar = calendar.Calendar()
+
     def tz_name (self, tz) :
         """
             Returns a string describing the given timezone
@@ -72,49 +78,53 @@
 
         return (month.year == today.year and month.month == today.month)
 
-    def prev_month_year (self, month) :
-        """
-            Returns the year of the month before the given one
+    @staticmethod
+    def _wrap_year (year, month) :
         """
+            Wraps month to between [1, 12], spilling overflow/underflow by to year.
 
-        if month.month == 1 :
-            return month.year - 1
-
+            Returns (year, month)
+        """
+        
+        # underflow?
+        if month == 0 :
+            # wrap to previous year
+            return (year - 1, 12)
+        
+        # overflow?
+        elif month == 13 :
+            # wrap to next year
+            return (year + 1, 1)
+        
+        # sane value
+        elif 1 <= month <= 12 :
+            return (year, month)
+        
+        # insane value
         else :
-            return month.year
-            
-    def next_month_year (self, month) :
-        """
-            Returns the year of the month after the given one
-        """
-
-        if month.month == 12 :
-            return month.year + 1
-
-        else :
-            return month.year
+            assert False, "invalid year/month: %d/%d" % (year, month)
 
     def prev_month (self, month) :
         """
-            Returns the month before the given one
+            Returns the month preceding the given one (as a datetime.datetime)
         """
-
-        if month.month == 1 :
-            return 12
-
-        else :
-            return month.month - 1
+        
+        # previous month
+        y, m = self._wrap_year(month.year, month.month - 1)
+        
+        # build datetime
+        return datetime.datetime(year=y, month=m, day=1, tzinfo=month.tzinfo)
 
     def next_month (self, month) :
         """
-            Returns the month after the given one
+            Returns the month following the given one (as a datetime.datetime)
         """
-
-        if month.month == 12 :
-            return 1
-
-        else :
-            return month.month + 1
+        
+        # previous month
+        y, m = self._wrap_year(month.year, month.month + 1)
+        
+        # build datetime
+        return datetime.datetime(year=y, month=m, day=1, tzinfo=month.tzinfo)
     
     def fmt_time (self, time=None) :
         """
--- a/log_source.py	Wed Feb 11 22:24:55 2009 +0200
+++ b/log_source.py	Wed Feb 11 22:56:43 2009 +0200
@@ -627,7 +627,7 @@
         """
             Returns a set of dates for which logfiles are available in the given datetime's month
         """
-        
+
         # iterate over month's days
         for dt in self._iter_month_days(month) :
             # date in our target timezone
--- a/static/irclogs.css	Wed Feb 11 22:24:55 2009 +0200
+++ b/static/irclogs.css	Wed Feb 11 22:56:43 2009 +0200
@@ -145,7 +145,7 @@
 
 /* Calendar */
 table.calendar {
-    
+    display: inline;
 }
 
 table.calendar th {
--- a/templates/channel_calendar.tmpl	Wed Feb 11 22:24:55 2009 +0200
+++ b/templates/channel_calendar.tmpl	Wed Feb 11 22:56:43 2009 +0200
@@ -1,23 +1,26 @@
 <%inherit file="channel.tmpl" />
 
-<%def name="month_table(cal, month, dates)">
+<%def name="month_table(month)">
+## the set of available days
+<% log_dates = h.set(channel.source.get_month_days(month)) %>
+## the calendar table
 <table class="calendar">
 ## table header - month name
     <tr class="month-header">
         <th colspan="7"${' class="this-month"' if h.is_this_month(month) else ''}>
-            <a href="${urls.channel_calendar.build(req, channel=channel, year=h.next_month_year(month), month=h.next_month(month))}" class="next-month">&raquo;</a>
-            <a href="${urls.channel_calendar.build(req, channel=channel, year=h.prev_month_year(month), month=h.prev_month(month))}" class="prev-month">&laquo;</a>
+            <a href="${urls.channel_calendar.build(req, channel=channel, year=h.next_month(month).year, month=h.next_month(month).month)}" class="next-month">&raquo;</a>
+            <a href="${urls.channel_calendar.build(req, channel=channel, year=h.prev_month(month).year, month=h.prev_month(month).month)}" class="prev-month">&laquo;</a>
             <span id="month-name">${h.fmt_month(month)}</span>
         </th>
     </tr>
 ## month header - weekday names    
     <tr class="week-header">
-    % for weekday in cal.iterweekdays() :
+    % for weekday in h.calendar.iterweekdays() :
         <th>${h.fmt_weekday(weekday)}</th>
     % endfor
     </tr>
 ## iterate over the weeks
-% for week in cal.monthdays2calendar(month.year, month.month) :
+% for week in h.calendar.monthdays2calendar(month.year, month.month) :
     <tr>
     ## iterate over the week's days
     % for day, weekday in week :
@@ -28,9 +31,9 @@
         ## build date
         <% date = h.build_date(month, day) %>\
         ## render cell
-        <td${' id="today"' if h.is_today(date) else ''}${' class="empty"' if date not in dates else ''}>\
+        <td${' id="today"' if h.is_today(date) else ''}${' class="empty"' if date not in log_dates else ''}>\
         ## link to logs for this day?
-        % if date in dates :
+        % if date in log_dates :
 <a href="${urls.channel_date.build(req, channel=channel, date=date)}">${day}</a>\
         % else :
 ${day}\
@@ -43,5 +46,8 @@
 </table>
 </%def>
 
-${month_table(calendar, month, days)}
+## three months
+${month_table(h.prev_month(month))}
+${month_table(month              )}
+${month_table(h.next_month(month))}