1.1 --- a/EventAggregatorSupport.py Mon Mar 23 01:52:13 2009 +0100
1.2 +++ b/EventAggregatorSupport.py Tue Mar 24 01:05:50 2009 +0100
1.3 @@ -11,6 +11,7 @@
1.4 from MoinMoin.Page import Page
1.5 from MoinMoin import search, version
1.6 import calendar
1.7 +import datetime
1.8 import re
1.9
1.10 __version__ = "0.1"
1.11 @@ -112,44 +113,82 @@
1.12 else:
1.13 return None
1.14
1.15 +def getCurrentMonth():
1.16 +
1.17 + "Return the current month as a (year, month) tuple."
1.18 +
1.19 + today = datetime.date.today()
1.20 + return (today.year, today.month)
1.21 +
1.22 +def monthupdate(date, n):
1.23 +
1.24 + "Return 'date' updated by 'n' months."
1.25 +
1.26 + if n < 0:
1.27 + fn = prevmonth
1.28 + else:
1.29 + fn = nextmonth
1.30 +
1.31 + i = 0
1.32 + while i < abs(n):
1.33 + date = fn(date)
1.34 + i += 1
1.35 +
1.36 + return date
1.37 +
1.38 def daterange(first, last):
1.39 +
1.40 + "Get the range of dates starting at 'first' and ending on 'last'."
1.41 +
1.42 results = []
1.43 + step = last > first and 1 or -1
1.44
1.45 months_only = len(first) == 2
1.46 start_year = first[0]
1.47 end_year = last[0]
1.48
1.49 - for year in range(start_year, end_year + 1):
1.50 - if year < end_year:
1.51 + for year in range(start_year, end_year + step, step):
1.52 + if step == 1 and year < end_year:
1.53 end_month = 12
1.54 + elif step == -1 and year > end_year:
1.55 + end_month = 1
1.56 else:
1.57 end_month = last[1]
1.58
1.59 - if year > start_year:
1.60 + if step == 1 and year > start_year:
1.61 start_month = 1
1.62 + elif step == -1 and year < start_year:
1.63 + start_month = 12
1.64 else:
1.65 start_month = first[1]
1.66
1.67 - for month in range(start_month, end_month + 1):
1.68 + for month in range(start_month, end_month + step, step):
1.69 if months_only:
1.70 results.append((year, month))
1.71 else:
1.72 - if month < end_month:
1.73 + if step == 1 and month < end_month:
1.74 _wd, end_day = calendar.monthrange(year, month)
1.75 + elif step == -1 and month > end_month:
1.76 + end_day = 1
1.77 else:
1.78 end_day = last[2]
1.79
1.80 - if month > start_month:
1.81 + if step == 1 and month > start_month:
1.82 start_day = 1
1.83 + elif step == -1 and month < start_month:
1.84 + _wd, start_day = calendar.monthrange(year, month)
1.85 else:
1.86 start_day = first[2]
1.87
1.88 - for day in range(start_day, end_day + 1):
1.89 + for day in range(start_day, end_day + step, step):
1.90 results.append((year, month, day))
1.91
1.92 return results
1.93
1.94 def nextdate(date):
1.95 +
1.96 + "Return the date following the given 'date'."
1.97 +
1.98 year, month, day = date
1.99 _wd, end_day = calendar.monthrange(year, month)
1.100 if day == end_day:
1.101 @@ -160,6 +199,40 @@
1.102 else:
1.103 return (year, month, day + 1)
1.104
1.105 +def prevdate(date):
1.106 +
1.107 + "Return the date preceding the given 'date'."
1.108 +
1.109 + year, month, day = date
1.110 + if day == 1:
1.111 + if month == 1:
1.112 + return (year - 1, 12, 31)
1.113 + else:
1.114 + _wd, end_day = calendar.monthrange(year, month - 1)
1.115 + return (year, month - 1, end_day)
1.116 + else:
1.117 + return (year, month, day - 1)
1.118 +
1.119 +def nextmonth(date):
1.120 +
1.121 + "Return the (year, month) tuple following 'date'."
1.122 +
1.123 + year, month = date
1.124 + if month == 12:
1.125 + return (year + 1, 1)
1.126 + else:
1.127 + return year, month + 1
1.128 +
1.129 +def prevmonth(date):
1.130 +
1.131 + "Return the (year, month) tuple preceding 'date'."
1.132 +
1.133 + year, month = date
1.134 + if month == 1:
1.135 + return (year - 1, 12)
1.136 + else:
1.137 + return year, month - 1
1.138 +
1.139 def getEvents(request, category_names, calendar_start=None, calendar_end=None):
1.140
1.141 """
3.1 --- a/macros/EventAggregator.py Mon Mar 23 01:52:13 2009 +0100
3.2 +++ b/macros/EventAggregator.py Tue Mar 24 01:05:50 2009 +0100
3.3 @@ -42,7 +42,25 @@
3.4 else:
3.5 return (255, 255, 255)
3.6
3.7 -# Macro function.
3.8 +# Macro functions.
3.9 +
3.10 +def getMonth(arg):
3.11 + if arg in ("current", "next", "last"):
3.12 + date = EventAggregatorSupport.getCurrentMonth()
3.13 + if arg == "next":
3.14 + date = EventAggregatorSupport.monthupdate(date, 1)
3.15 + elif arg == "last":
3.16 + date = EventAggregatorSupport.monthupdate(date, -1)
3.17 + else:
3.18 + date = EventAggregatorSupport.getMonth(arg)
3.19 + return date
3.20 +
3.21 +def getFormMonth(request, calendar_name, argname):
3.22 + arg = request.form.get("%s-%s" % (calendar_name, argname), [None])[0]
3.23 + if arg is not None:
3.24 + return getMonth(arg)
3.25 + else:
3.26 + return None
3.27
3.28 def execute(macro, args):
3.29
3.30 @@ -52,7 +70,14 @@
3.31 optional named arguments of the following forms:
3.32
3.33 start=YYYY-MM shows event details starting from the specified month
3.34 + start=current shows event details starting with the current month
3.35 + start=next shows event details starting with next month
3.36 end=YYYY-MM shows event details ending at the specified month
3.37 + end=current shows event details ending with the current month
3.38 + end=last shows event details ending with last month
3.39 + span=N shows event details for N months starting with the
3.40 + specified start month, or ending with the specified end
3.41 + month
3.42
3.43 mode=calendar shows a calendar view of events
3.44 mode=list shows a list of events by month
3.45 @@ -60,6 +85,9 @@
3.46
3.47 names=daily shows the name of an event on every day of that event
3.48 names=weekly shows the name of an event once per week
3.49 +
3.50 + calendar=NAME uses the given NAME to provide request parameters which
3.51 + can be used to control the calendar view
3.52 """
3.53
3.54 request = macro.request
3.55 @@ -81,21 +109,62 @@
3.56 category_names = []
3.57 calendar_start = None
3.58 calendar_end = None
3.59 + span = None
3.60 mode = "calendar"
3.61 name_usage = "daily"
3.62 + calendar_name = None
3.63
3.64 for arg in parsed_args:
3.65 if arg.startswith("start="):
3.66 - calendar_start = EventAggregatorSupport.getMonth(arg[6:])
3.67 + calendar_start = getMonth(arg[6:])
3.68 +
3.69 elif arg.startswith("end="):
3.70 - calendar_end = EventAggregatorSupport.getMonth(arg[4:])
3.71 + calendar_end = getMonth(arg[4:])
3.72 +
3.73 + elif arg.startswith("span="):
3.74 + span = abs(int(arg[5:]))
3.75 +
3.76 elif arg.startswith("mode="):
3.77 mode = arg[5:]
3.78 +
3.79 elif arg.startswith("names="):
3.80 name_usage = arg[6:]
3.81 +
3.82 + elif arg.startswith("calendar="):
3.83 + calendar_name = arg[9:]
3.84 +
3.85 else:
3.86 category_names.append(arg)
3.87
3.88 + # Find request parameters to override settings.
3.89 +
3.90 + if calendar_name is not None:
3.91 + new_calendar_start = getFormMonth(request, calendar_name, "start")
3.92 + new_calendar_end = getFormMonth(request, calendar_name, "end")
3.93 + if new_calendar_start is not None or new_calendar_end is not None:
3.94 + calendar_start = new_calendar_start
3.95 + calendar_end = new_calendar_end
3.96 +
3.97 + # Calculate the window of interest.
3.98 +
3.99 + if span is not None:
3.100 + if calendar_start is not None:
3.101 + calendar_end = EventAggregatorSupport.monthupdate(calendar_start, max(0, span - 1))
3.102 + elif calendar_end is not None:
3.103 + calendar_start = EventAggregatorSupport.monthupdate(calendar_end, min(0, -span + 1))
3.104 + else:
3.105 + calendar_start = EventAggregatorSupport.getCurrentMonth()
3.106 + calendar_end = EventAggregatorSupport.monthupdate(calendar_start, max(0, span - 1))
3.107 +
3.108 + # Some useful navigation months.
3.109 +
3.110 + if calendar_name is not None:
3.111 + previous_year, previous_month = EventAggregatorSupport.prevmonth(calendar_start)
3.112 + next_year, next_month = EventAggregatorSupport.nextmonth(calendar_start)
3.113 + next_set_year, next_set_month = EventAggregatorSupport.nextmonth(calendar_end)
3.114 +
3.115 + # Get the events.
3.116 +
3.117 events, shown_events, all_shown_events, earliest, latest = \
3.118 EventAggregatorSupport.getEvents(request, category_names, calendar_start, calendar_end)
3.119
3.120 @@ -125,13 +194,44 @@
3.121
3.122 output.append(fmt.table_row(on=1))
3.123 output.append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "7"}))
3.124 - output.append(fmt.span(on=1))
3.125 - output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels
3.126 - output.append(fmt.span(on=0))
3.127 - output.append(fmt.text(" "))
3.128 - output.append(fmt.span(on=1))
3.129 - output.append(fmt.text(year))
3.130 - output.append(fmt.span(on=0))
3.131 +
3.132 + # Either write a month heading or produce a link for navigable
3.133 + # calendars.
3.134 +
3.135 + month_label = _(month_labels[month - 1]) # zero-based labels
3.136 +
3.137 + if calendar_name is not None:
3.138 + previous_months_link = "%s-end=%04d-%02d" % (calendar_name, previous_year, previous_month)
3.139 + previous_month_link = "%s-start=%04d-%02d" % (calendar_name, previous_year, previous_month)
3.140 +
3.141 + output.append(fmt.span(on=1, css_class="previous-month"))
3.142 + output.append(page.link_to_raw(request, wikiutil.escape("<<"), previous_months_link))
3.143 + output.append(fmt.text(" "))
3.144 + output.append(page.link_to_raw(request, wikiutil.escape("<"), previous_month_link))
3.145 + output.append(fmt.span(on=0))
3.146 +
3.147 + next_months_link = "%s-start=%04d-%02d" % (calendar_name, next_set_year, next_set_month)
3.148 + next_month_link = "%s-start=%04d-%02d" % (calendar_name, next_year, next_month)
3.149 +
3.150 + output.append(fmt.span(on=1, css_class="next-month"))
3.151 + output.append(page.link_to_raw(request, wikiutil.escape(">"), next_month_link))
3.152 + output.append(fmt.text(" "))
3.153 + output.append(page.link_to_raw(request, wikiutil.escape(">>"), next_months_link))
3.154 + output.append(fmt.span(on=0))
3.155 +
3.156 + full_month_label = "%s %s" % (month_label, year)
3.157 + month_link = "%s-start=%04d-%02d" % (calendar_name, year, month)
3.158 + output.append(page.link_to_raw(request, wikiutil.escape(full_month_label), month_link))
3.159 +
3.160 + else:
3.161 + output.append(fmt.span(on=1))
3.162 + output.append(fmt.text(month_label))
3.163 + output.append(fmt.span(on=0))
3.164 + output.append(fmt.text(" "))
3.165 + output.append(fmt.span(on=1))
3.166 + output.append(fmt.text(year))
3.167 + output.append(fmt.span(on=0))
3.168 +
3.169 output.append(fmt.table_cell(on=0))
3.170 output.append(fmt.table_row(on=0))
3.171