paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - EventAggregator Macro |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2008, 2009 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@0 | 7 | 2005-2008 MoinMoin:ThomasWaldmann. |
paul@0 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 9 | """ |
paul@0 | 10 | |
paul@10 | 11 | from MoinMoin import wikiutil |
paul@10 | 12 | import EventAggregatorSupport |
paul@0 | 13 | import calendar |
paul@0 | 14 | |
paul@27 | 15 | linkToPage = EventAggregatorSupport.linkToPage |
paul@27 | 16 | |
paul@5 | 17 | try: |
paul@5 | 18 | set |
paul@5 | 19 | except NameError: |
paul@5 | 20 | from sets import Set as set |
paul@5 | 21 | |
paul@0 | 22 | Dependencies = ['pages'] |
paul@0 | 23 | |
paul@37 | 24 | # Abstractions. |
paul@37 | 25 | |
paul@37 | 26 | class View: |
paul@37 | 27 | |
paul@37 | 28 | "A view of the event calendar." |
paul@37 | 29 | |
paul@57 | 30 | def __init__(self, page, calendar_name, calendar_start, calendar_end, |
paul@57 | 31 | first, last, category_names, template_name, parent_name, mode): |
paul@37 | 32 | |
paul@37 | 33 | """ |
paul@37 | 34 | Initialise the view with the current 'page', a 'calendar_name' (which |
paul@57 | 35 | may be None), the requested 'calendar_start' and 'calendar_end', and the |
paul@57 | 36 | 'first' and 'last' months of event coverage. |
paul@57 | 37 | |
paul@57 | 38 | The additional 'category_names', 'template_name', 'parent_name' and |
paul@57 | 39 | 'mode' parameters are used to configure the links employed by the view. |
paul@37 | 40 | """ |
paul@37 | 41 | |
paul@37 | 42 | self.page = page |
paul@37 | 43 | self.calendar_name = calendar_name |
paul@57 | 44 | self.calendar_start = calendar_start |
paul@57 | 45 | self.calendar_end = calendar_end |
paul@57 | 46 | self.template_name = template_name |
paul@57 | 47 | self.parent_name = parent_name |
paul@57 | 48 | self.mode = mode |
paul@57 | 49 | |
paul@57 | 50 | self.category_name_parameters = "&".join([("category=%s" % name) for name in category_names]) |
paul@37 | 51 | |
paul@37 | 52 | if self.calendar_name is not None: |
paul@37 | 53 | |
paul@37 | 54 | # Store the view parameters. |
paul@37 | 55 | |
paul@67 | 56 | self.number_of_months = (last - first).months() + 1 |
paul@37 | 57 | |
paul@67 | 58 | self.previous_month_start = first.previous_month() |
paul@67 | 59 | self.next_month_start = first.next_month() |
paul@67 | 60 | self.previous_month_end = last.previous_month() |
paul@67 | 61 | self.next_month_end = last.next_month() |
paul@37 | 62 | |
paul@67 | 63 | self.previous_set_start = first.month_update(-self.number_of_months) |
paul@67 | 64 | self.next_set_start = first.month_update(self.number_of_months) |
paul@67 | 65 | self.previous_set_end = last.month_update(-self.number_of_months) |
paul@67 | 66 | self.next_set_end = last.month_update(self.number_of_months) |
paul@37 | 67 | |
paul@58 | 68 | def getQualifiedParameterName(self, argname): |
paul@58 | 69 | return EventAggregatorSupport.getQualifiedParameterName(self.calendar_name, argname) |
paul@58 | 70 | |
paul@57 | 71 | def getMonthQueryString(self, argname, month, prefix=1): |
paul@37 | 72 | if month is not None: |
paul@57 | 73 | if prefix: |
paul@58 | 74 | argname = self.getQualifiedParameterName(argname) |
paul@67 | 75 | return "%s=%s" % (argname, month) |
paul@37 | 76 | else: |
paul@37 | 77 | return "" |
paul@37 | 78 | |
paul@57 | 79 | def getNavigationLink(self, start, end, mode=None): |
paul@58 | 80 | return "%s&%s&%s=%s" % ( |
paul@57 | 81 | self.getMonthQueryString("start", start), |
paul@57 | 82 | self.getMonthQueryString("end", end), |
paul@58 | 83 | self.getQualifiedParameterName("mode"), mode or self.mode |
paul@57 | 84 | ) |
paul@57 | 85 | |
paul@57 | 86 | def writeDownloadControls(self): |
paul@57 | 87 | page = self.page |
paul@57 | 88 | request = page.request |
paul@57 | 89 | fmt = page.formatter |
paul@57 | 90 | _ = request.getText |
paul@57 | 91 | |
paul@57 | 92 | output = [] |
paul@57 | 93 | |
paul@57 | 94 | # Generate the links. |
paul@57 | 95 | |
paul@74 | 96 | download_all_link = "action=EventAggregatorSummary&doit=1&parent=%s&%s" % ( |
paul@74 | 97 | self.parent_name or "", self.category_name_parameters |
paul@74 | 98 | ) |
paul@57 | 99 | download_link = download_all_link + ("&%s&%s" % ( |
paul@57 | 100 | self.getMonthQueryString("start", self.calendar_start, prefix=0), |
paul@57 | 101 | self.getMonthQueryString("end", self.calendar_end, prefix=0) |
paul@57 | 102 | )) |
paul@57 | 103 | subscribe_all_link = download_all_link + "&format=RSS" |
paul@57 | 104 | subscribe_link = download_link + "&format=RSS" |
paul@57 | 105 | |
paul@57 | 106 | # Write the controls. |
paul@57 | 107 | |
paul@57 | 108 | output.append(fmt.div(on=1, css_class="event-download-controls")) |
paul@57 | 109 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@57 | 110 | output.append(linkToPage(request, page, _("Download this view"), download_link)) |
paul@57 | 111 | output.append(fmt.span(on=0)) |
paul@57 | 112 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@57 | 113 | output.append(linkToPage(request, page, _("Download this calendar"), download_all_link)) |
paul@57 | 114 | output.append(fmt.span(on=0)) |
paul@57 | 115 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@57 | 116 | output.append(linkToPage(request, page, _("Subscribe to this view"), subscribe_link)) |
paul@57 | 117 | output.append(fmt.span(on=0)) |
paul@57 | 118 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@57 | 119 | output.append(linkToPage(request, page, _("Subscribe to this calendar"), subscribe_all_link)) |
paul@57 | 120 | output.append(fmt.span(on=0)) |
paul@57 | 121 | output.append(fmt.div(on=0)) |
paul@57 | 122 | |
paul@57 | 123 | return "".join(output) |
paul@57 | 124 | |
paul@57 | 125 | def writeViewControls(self): |
paul@57 | 126 | page = self.page |
paul@57 | 127 | request = page.request |
paul@57 | 128 | fmt = page.formatter |
paul@57 | 129 | _ = request.getText |
paul@57 | 130 | |
paul@57 | 131 | output = [] |
paul@57 | 132 | |
paul@57 | 133 | calendar_link = self.getNavigationLink( |
paul@57 | 134 | self.calendar_start, self.calendar_end, "calendar" |
paul@57 | 135 | ) |
paul@57 | 136 | list_link = self.getNavigationLink( |
paul@57 | 137 | self.calendar_start, self.calendar_end, "list" |
paul@57 | 138 | ) |
paul@57 | 139 | table_link = self.getNavigationLink( |
paul@57 | 140 | self.calendar_start, self.calendar_end, "table" |
paul@57 | 141 | ) |
paul@57 | 142 | |
paul@57 | 143 | # Write the controls. |
paul@57 | 144 | |
paul@57 | 145 | output.append(fmt.div(on=1, css_class="event-view-controls")) |
paul@57 | 146 | output.append(fmt.span(on=1, css_class="event-view")) |
paul@57 | 147 | output.append(linkToPage(request, page, _("View as calendar"), calendar_link)) |
paul@57 | 148 | output.append(fmt.span(on=0)) |
paul@57 | 149 | output.append(fmt.span(on=1, css_class="event-view")) |
paul@57 | 150 | output.append(linkToPage(request, page, _("View as list"), list_link)) |
paul@57 | 151 | output.append(fmt.span(on=0)) |
paul@57 | 152 | output.append(fmt.span(on=1, css_class="event-view")) |
paul@57 | 153 | output.append(linkToPage(request, page, _("View as table"), table_link)) |
paul@57 | 154 | output.append(fmt.span(on=0)) |
paul@57 | 155 | output.append(fmt.div(on=0)) |
paul@57 | 156 | |
paul@57 | 157 | return "".join(output) |
paul@57 | 158 | |
paul@67 | 159 | def writeMonthHeading(self, year_month): |
paul@37 | 160 | page = self.page |
paul@37 | 161 | request = page.request |
paul@37 | 162 | fmt = page.formatter |
paul@37 | 163 | _ = request.getText |
paul@37 | 164 | |
paul@37 | 165 | output = [] |
paul@37 | 166 | |
paul@67 | 167 | year, month = year_month.as_tuple() |
paul@37 | 168 | month_label = _(EventAggregatorSupport.getMonthLabel(month)) |
paul@37 | 169 | |
paul@37 | 170 | # Prepare navigation links. |
paul@37 | 171 | |
paul@37 | 172 | if self.calendar_name is not None: |
paul@37 | 173 | calendar_name = self.calendar_name |
paul@37 | 174 | |
paul@37 | 175 | # Links to the previous set of months and to a calendar shifted |
paul@37 | 176 | # back one month. |
paul@37 | 177 | |
paul@57 | 178 | previous_set_link = self.getNavigationLink( |
paul@57 | 179 | self.previous_set_start, self.previous_set_end |
paul@37 | 180 | ) |
paul@57 | 181 | previous_month_link = self.getNavigationLink( |
paul@57 | 182 | self.previous_month_start, self.previous_month_end |
paul@37 | 183 | ) |
paul@37 | 184 | |
paul@37 | 185 | # Links to the next set of months and to a calendar shifted |
paul@37 | 186 | # forward one month. |
paul@37 | 187 | |
paul@57 | 188 | next_set_link = self.getNavigationLink( |
paul@57 | 189 | self.next_set_start, self.next_set_end |
paul@37 | 190 | ) |
paul@57 | 191 | next_month_link = self.getNavigationLink( |
paul@57 | 192 | self.next_month_start, self.next_month_end |
paul@37 | 193 | ) |
paul@37 | 194 | |
paul@37 | 195 | # A link leading to this month being at the top of the calendar. |
paul@37 | 196 | |
paul@37 | 197 | full_month_label = "%s %s" % (month_label, year) |
paul@67 | 198 | end_month = year_month.month_update(self.number_of_months - 1) |
paul@37 | 199 | |
paul@67 | 200 | month_link = self.getNavigationLink(year_month, end_month) |
paul@37 | 201 | |
paul@37 | 202 | output.append(fmt.span(on=1, css_class="previous-month")) |
paul@37 | 203 | output.append(linkToPage(request, page, "<<", previous_set_link)) |
paul@37 | 204 | output.append(fmt.text(" ")) |
paul@37 | 205 | output.append(linkToPage(request, page, "<", previous_month_link)) |
paul@37 | 206 | output.append(fmt.span(on=0)) |
paul@37 | 207 | |
paul@37 | 208 | output.append(fmt.span(on=1, css_class="next-month")) |
paul@37 | 209 | output.append(linkToPage(request, page, ">", next_month_link)) |
paul@37 | 210 | output.append(fmt.text(" ")) |
paul@37 | 211 | output.append(linkToPage(request, page, ">>", next_set_link)) |
paul@37 | 212 | output.append(fmt.span(on=0)) |
paul@37 | 213 | |
paul@37 | 214 | output.append(linkToPage(request, page, full_month_label, month_link)) |
paul@37 | 215 | |
paul@37 | 216 | else: |
paul@37 | 217 | output.append(fmt.span(on=1)) |
paul@37 | 218 | output.append(fmt.text(month_label)) |
paul@37 | 219 | output.append(fmt.span(on=0)) |
paul@37 | 220 | output.append(fmt.text(" ")) |
paul@37 | 221 | output.append(fmt.span(on=1)) |
paul@37 | 222 | output.append(fmt.text(unicode(year))) |
paul@37 | 223 | output.append(fmt.span(on=0)) |
paul@37 | 224 | |
paul@37 | 225 | return "".join(output) |
paul@37 | 226 | |
paul@67 | 227 | def writeDayNumberLinked(self, date): |
paul@57 | 228 | page = self.page |
paul@57 | 229 | request = page.request |
paul@57 | 230 | fmt = page.formatter |
paul@57 | 231 | _ = request.getText |
paul@57 | 232 | |
paul@67 | 233 | year, month, day = date.as_tuple() |
paul@57 | 234 | output = [] |
paul@57 | 235 | |
paul@58 | 236 | # Prepare navigation details for the calendar shown with the new event |
paul@58 | 237 | # form. |
paul@58 | 238 | |
paul@58 | 239 | navigation_link = self.getNavigationLink( |
paul@58 | 240 | self.calendar_start, self.calendar_end, self.mode |
paul@58 | 241 | ) |
paul@58 | 242 | |
paul@58 | 243 | # Prepare the link to the new event form, incorporating the above |
paul@58 | 244 | # calendar parameters. |
paul@58 | 245 | |
paul@57 | 246 | new_event_link = "action=EventAggregatorNewEvent&start-day=%d&start-month=%d&start-year=%d" \ |
paul@58 | 247 | "&%s&template=%s&parent=%s&%s" % ( |
paul@58 | 248 | day, month, year, self.category_name_parameters, self.template_name, self.parent_name or "", |
paul@58 | 249 | navigation_link) |
paul@57 | 250 | |
paul@57 | 251 | output.append(fmt.div(on=1)) |
paul@57 | 252 | output.append(fmt.span(on=1, css_class="event-day-number")) |
paul@57 | 253 | output.append(linkToPage(request, page, unicode(day), new_event_link)) |
paul@57 | 254 | output.append(fmt.span(on=0)) |
paul@57 | 255 | output.append(fmt.div(on=0)) |
paul@57 | 256 | |
paul@57 | 257 | return "".join(output) |
paul@57 | 258 | |
paul@10 | 259 | # HTML-related functions. |
paul@2 | 260 | |
paul@2 | 261 | def getColour(s): |
paul@2 | 262 | colour = [0, 0, 0] |
paul@2 | 263 | digit = 0 |
paul@2 | 264 | for c in s: |
paul@2 | 265 | colour[digit] += ord(c) |
paul@2 | 266 | colour[digit] = colour[digit] % 256 |
paul@2 | 267 | digit += 1 |
paul@2 | 268 | digit = digit % 3 |
paul@2 | 269 | return tuple(colour) |
paul@2 | 270 | |
paul@2 | 271 | def getBlackOrWhite(colour): |
paul@2 | 272 | if sum(colour) / 3.0 > 127: |
paul@2 | 273 | return (0, 0, 0) |
paul@2 | 274 | else: |
paul@2 | 275 | return (255, 255, 255) |
paul@2 | 276 | |
paul@11 | 277 | # Macro functions. |
paul@11 | 278 | |
paul@0 | 279 | def execute(macro, args): |
paul@0 | 280 | |
paul@0 | 281 | """ |
paul@0 | 282 | Execute the 'macro' with the given 'args': an optional list of selected |
paul@0 | 283 | category names (categories whose pages are to be shown), together with |
paul@5 | 284 | optional named arguments of the following forms: |
paul@5 | 285 | |
paul@5 | 286 | start=YYYY-MM shows event details starting from the specified month |
paul@13 | 287 | start=current-N shows event details relative to the current month |
paul@5 | 288 | end=YYYY-MM shows event details ending at the specified month |
paul@13 | 289 | end=current+N shows event details relative to the current month |
paul@5 | 290 | |
paul@5 | 291 | mode=calendar shows a calendar view of events |
paul@5 | 292 | mode=list shows a list of events by month |
paul@9 | 293 | mode=ics provides iCalendar data for the events |
paul@5 | 294 | |
paul@5 | 295 | names=daily shows the name of an event on every day of that event |
paul@5 | 296 | names=weekly shows the name of an event once per week |
paul@11 | 297 | |
paul@11 | 298 | calendar=NAME uses the given NAME to provide request parameters which |
paul@11 | 299 | can be used to control the calendar view |
paul@55 | 300 | |
paul@55 | 301 | template=PAGE uses the given PAGE as the default template for new |
paul@55 | 302 | events (or the default template from the configuration |
paul@55 | 303 | if not specified) |
paul@55 | 304 | |
paul@55 | 305 | parent=PAGE uses the given PAGE as the parent of any new event page |
paul@0 | 306 | """ |
paul@0 | 307 | |
paul@0 | 308 | request = macro.request |
paul@0 | 309 | fmt = macro.formatter |
paul@0 | 310 | page = fmt.page |
paul@0 | 311 | _ = request.getText |
paul@0 | 312 | |
paul@0 | 313 | # Interpret the arguments. |
paul@0 | 314 | |
paul@0 | 315 | try: |
paul@0 | 316 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@0 | 317 | except AttributeError: |
paul@0 | 318 | parsed_args = args.split(",") |
paul@0 | 319 | |
paul@0 | 320 | parsed_args = [arg for arg in parsed_args if arg] |
paul@0 | 321 | |
paul@0 | 322 | # Get special arguments. |
paul@0 | 323 | |
paul@0 | 324 | category_names = [] |
paul@0 | 325 | calendar_start = None |
paul@0 | 326 | calendar_end = None |
paul@57 | 327 | mode = None |
paul@35 | 328 | name_usage = "weekly" |
paul@11 | 329 | calendar_name = None |
paul@55 | 330 | template_name = getattr(request.cfg, "event_aggregator_new_event_template", "EventTemplate") |
paul@55 | 331 | parent_name = None |
paul@0 | 332 | |
paul@0 | 333 | for arg in parsed_args: |
paul@0 | 334 | if arg.startswith("start="): |
paul@19 | 335 | calendar_start = EventAggregatorSupport.getParameterMonth(arg[6:]) |
paul@11 | 336 | |
paul@0 | 337 | elif arg.startswith("end="): |
paul@19 | 338 | calendar_end = EventAggregatorSupport.getParameterMonth(arg[4:]) |
paul@11 | 339 | |
paul@0 | 340 | elif arg.startswith("mode="): |
paul@0 | 341 | mode = arg[5:] |
paul@11 | 342 | |
paul@5 | 343 | elif arg.startswith("names="): |
paul@5 | 344 | name_usage = arg[6:] |
paul@11 | 345 | |
paul@11 | 346 | elif arg.startswith("calendar="): |
paul@11 | 347 | calendar_name = arg[9:] |
paul@11 | 348 | |
paul@55 | 349 | elif arg.startswith("template="): |
paul@55 | 350 | template_name = arg[9:] |
paul@55 | 351 | |
paul@55 | 352 | elif arg.startswith("parent="): |
paul@55 | 353 | parent_name = arg[7:] |
paul@55 | 354 | |
paul@0 | 355 | else: |
paul@0 | 356 | category_names.append(arg) |
paul@0 | 357 | |
paul@11 | 358 | # Find request parameters to override settings. |
paul@11 | 359 | |
paul@11 | 360 | if calendar_name is not None: |
paul@19 | 361 | calendar_start = EventAggregatorSupport.getFormMonth(request, calendar_name, "start") or calendar_start |
paul@19 | 362 | calendar_end = EventAggregatorSupport.getFormMonth(request, calendar_name, "end") or calendar_end |
paul@11 | 363 | |
paul@58 | 364 | mode = EventAggregatorSupport.getQualifiedParameter(request, calendar_name, "mode", mode or "calendar") |
paul@55 | 365 | |
paul@11 | 366 | # Get the events. |
paul@11 | 367 | |
paul@10 | 368 | events, shown_events, all_shown_events, earliest, latest = \ |
paul@10 | 369 | EventAggregatorSupport.getEvents(request, category_names, calendar_start, calendar_end) |
paul@2 | 370 | |
paul@13 | 371 | # Get a concrete period of time. |
paul@13 | 372 | |
paul@13 | 373 | first, last = EventAggregatorSupport.getConcretePeriod(calendar_start, calendar_end, earliest, latest) |
paul@13 | 374 | |
paul@37 | 375 | # Define a view of the calendar, retaining useful navigational information. |
paul@13 | 376 | |
paul@57 | 377 | view = View(page, calendar_name, calendar_start, calendar_end, first, last, category_names, template_name, parent_name, mode) |
paul@13 | 378 | |
paul@0 | 379 | # Make a calendar. |
paul@0 | 380 | |
paul@0 | 381 | output = [] |
paul@0 | 382 | |
paul@26 | 383 | # Output download controls. |
paul@26 | 384 | |
paul@26 | 385 | output.append(fmt.div(on=1, css_class="event-controls")) |
paul@57 | 386 | output.append(view.writeDownloadControls()) |
paul@26 | 387 | output.append(fmt.div(on=0)) |
paul@26 | 388 | |
paul@61 | 389 | # Output a table. |
paul@51 | 390 | |
paul@61 | 391 | if mode == "table": |
paul@0 | 392 | |
paul@61 | 393 | # Start of table view output. |
paul@51 | 394 | |
paul@51 | 395 | output.append(fmt.table(on=1, attrs={"tableclass" : "event-table"})) |
paul@51 | 396 | |
paul@51 | 397 | output.append(fmt.table_row(on=1)) |
paul@51 | 398 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@51 | 399 | output.append(fmt.text(_("Event dates"))) |
paul@51 | 400 | output.append(fmt.table_cell(on=0)) |
paul@51 | 401 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@51 | 402 | output.append(fmt.text(_("Event location"))) |
paul@51 | 403 | output.append(fmt.table_cell(on=0)) |
paul@51 | 404 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@51 | 405 | output.append(fmt.text(_("Event details"))) |
paul@51 | 406 | output.append(fmt.table_cell(on=0)) |
paul@51 | 407 | output.append(fmt.table_row(on=0)) |
paul@51 | 408 | |
paul@61 | 409 | # Get the events in order. |
paul@0 | 410 | |
paul@61 | 411 | ordered_events = EventAggregatorSupport.getOrderedEvents(all_shown_events) |
paul@0 | 412 | |
paul@61 | 413 | # Show the events in order. |
paul@2 | 414 | |
paul@69 | 415 | for event in ordered_events: |
paul@69 | 416 | event_page = event.getPage() |
paul@69 | 417 | event_summary = event.getSummary(parent_name) |
paul@69 | 418 | event_details = event.getDetails() |
paul@0 | 419 | |
paul@61 | 420 | # Prepare CSS classes with category-related styling. |
paul@11 | 421 | |
paul@61 | 422 | css_classes = ["event-table-details"] |
paul@61 | 423 | |
paul@61 | 424 | for topic in event_details.get("topics") or event_details.get("categories") or []: |
paul@11 | 425 | |
paul@61 | 426 | # Filter the category text to avoid illegal characters. |
paul@11 | 427 | |
paul@61 | 428 | css_classes.append("event-table-category-%s" % "".join(filter(lambda c: c.isalnum(), topic))) |
paul@0 | 429 | |
paul@61 | 430 | attrs = {"class" : " ".join(css_classes)} |
paul@0 | 431 | |
paul@2 | 432 | output.append(fmt.table_row(on=1)) |
paul@0 | 433 | |
paul@61 | 434 | # Start and end dates. |
paul@61 | 435 | |
paul@61 | 436 | output.append(fmt.table_cell(on=1, attrs=attrs)) |
paul@61 | 437 | output.append(fmt.span(on=1)) |
paul@67 | 438 | output.append(fmt.text(str(event_details["start"]))) |
paul@61 | 439 | output.append(fmt.span(on=0)) |
paul@61 | 440 | |
paul@61 | 441 | if event_details["start"] != event_details["end"]: |
paul@61 | 442 | output.append(fmt.text(" - ")) |
paul@61 | 443 | output.append(fmt.span(on=1)) |
paul@67 | 444 | output.append(fmt.text(str(event_details["end"]))) |
paul@61 | 445 | output.append(fmt.span(on=0)) |
paul@61 | 446 | |
paul@61 | 447 | output.append(fmt.table_cell(on=0)) |
paul@61 | 448 | |
paul@61 | 449 | # Location. |
paul@61 | 450 | |
paul@61 | 451 | output.append(fmt.table_cell(on=1, attrs=attrs)) |
paul@61 | 452 | |
paul@61 | 453 | if event_details.has_key("location"): |
paul@61 | 454 | output.append(fmt.text(event_details["location"])) |
paul@61 | 455 | |
paul@61 | 456 | output.append(fmt.table_cell(on=0)) |
paul@61 | 457 | |
paul@61 | 458 | # Link to the page using the summary. |
paul@61 | 459 | |
paul@61 | 460 | output.append(fmt.table_cell(on=1, attrs=attrs)) |
paul@67 | 461 | output.append(event_page.linkToPage(request, event_summary)) |
paul@61 | 462 | output.append(fmt.table_cell(on=0)) |
paul@2 | 463 | |
paul@2 | 464 | output.append(fmt.table_row(on=0)) |
paul@2 | 465 | |
paul@61 | 466 | # End of table view output. |
paul@61 | 467 | |
paul@61 | 468 | output.append(fmt.table(on=0)) |
paul@2 | 469 | |
paul@61 | 470 | # Output a list or calendar. |
paul@61 | 471 | |
paul@61 | 472 | elif mode in ("list", "calendar"): |
paul@0 | 473 | |
paul@61 | 474 | # Output top-level information. |
paul@61 | 475 | |
paul@61 | 476 | # Start of list view output. |
paul@2 | 477 | |
paul@61 | 478 | if mode == "list": |
paul@61 | 479 | output.append(fmt.bullet_list(on=1, attr={"class" : "event-listings"})) |
paul@61 | 480 | |
paul@61 | 481 | # Visit all months in the requested range, or across known events. |
paul@61 | 482 | |
paul@67 | 483 | for month in first.months_until(last): |
paul@2 | 484 | |
paul@61 | 485 | # Either output a calendar view... |
paul@61 | 486 | |
paul@61 | 487 | if mode == "calendar": |
paul@2 | 488 | |
paul@61 | 489 | # Output a month. |
paul@61 | 490 | |
paul@61 | 491 | output.append(fmt.table(on=1, attrs={"tableclass" : "event-month"})) |
paul@5 | 492 | |
paul@61 | 493 | output.append(fmt.table_row(on=1)) |
paul@61 | 494 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "21"})) |
paul@61 | 495 | |
paul@61 | 496 | # Either write a month heading or produce links for navigable |
paul@61 | 497 | # calendars. |
paul@5 | 498 | |
paul@67 | 499 | output.append(view.writeMonthHeading(month)) |
paul@5 | 500 | |
paul@61 | 501 | output.append(fmt.table_cell(on=0)) |
paul@61 | 502 | output.append(fmt.table_row(on=0)) |
paul@61 | 503 | |
paul@61 | 504 | # Weekday headings. |
paul@0 | 505 | |
paul@0 | 506 | output.append(fmt.table_row(on=1)) |
paul@0 | 507 | |
paul@0 | 508 | for weekday in range(0, 7): |
paul@61 | 509 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-weekday-heading", "colspan" : "3"})) |
paul@61 | 510 | output.append(fmt.text(_(EventAggregatorSupport.getDayLabel(weekday)))) |
paul@61 | 511 | output.append(fmt.table_cell(on=0)) |
paul@5 | 512 | |
paul@5 | 513 | output.append(fmt.table_row(on=0)) |
paul@5 | 514 | |
paul@61 | 515 | # Process the days of the month. |
paul@61 | 516 | |
paul@67 | 517 | start_weekday, number_of_days = month.month_properties() |
paul@61 | 518 | |
paul@61 | 519 | # The start weekday is the weekday of day number 1. |
paul@61 | 520 | # Find the first day of the week, counting from below zero, if |
paul@61 | 521 | # necessary, in order to land on the first day of the month as |
paul@61 | 522 | # day number 1. |
paul@61 | 523 | |
paul@61 | 524 | first_day = 1 - start_weekday |
paul@5 | 525 | |
paul@61 | 526 | while first_day <= number_of_days: |
paul@61 | 527 | |
paul@61 | 528 | # Find events in this week and determine how to mark them on the |
paul@61 | 529 | # calendar. |
paul@61 | 530 | |
paul@67 | 531 | week_start = month.as_date(max(first_day, 1)) |
paul@67 | 532 | week_end = month.as_date(min(first_day + 6, number_of_days)) |
paul@61 | 533 | |
paul@61 | 534 | week_coverage, week_events = EventAggregatorSupport.getCoverage( |
paul@67 | 535 | week_start, week_end, shown_events.get(month, [])) |
paul@61 | 536 | |
paul@61 | 537 | # Output a week, starting with the day numbers. |
paul@61 | 538 | |
paul@5 | 539 | output.append(fmt.table_row(on=1)) |
paul@5 | 540 | |
paul@5 | 541 | for weekday in range(0, 7): |
paul@5 | 542 | day = first_day + weekday |
paul@67 | 543 | date = month.as_date(day) |
paul@5 | 544 | |
paul@7 | 545 | # Output out-of-month days. |
paul@5 | 546 | |
paul@5 | 547 | if day < 1 or day > number_of_days: |
paul@35 | 548 | output.append(fmt.table_cell(on=1, |
paul@61 | 549 | attrs={"class" : "event-day-heading event-day-excluded", "colspan" : "3"})) |
paul@5 | 550 | output.append(fmt.table_cell(on=0)) |
paul@2 | 551 | |
paul@61 | 552 | # Output normal days. |
paul@5 | 553 | |
paul@5 | 554 | else: |
paul@61 | 555 | if date in week_coverage: |
paul@61 | 556 | output.append(fmt.table_cell(on=1, |
paul@61 | 557 | attrs={"class" : "event-day-heading event-day-busy", "colspan" : "3"})) |
paul@61 | 558 | else: |
paul@61 | 559 | output.append(fmt.table_cell(on=1, |
paul@61 | 560 | attrs={"class" : "event-day-heading event-day-empty", "colspan" : "3"})) |
paul@61 | 561 | |
paul@61 | 562 | # Output the day number, making a link to a new event |
paul@61 | 563 | # action. |
paul@61 | 564 | |
paul@67 | 565 | output.append(view.writeDayNumberLinked(date)) |
paul@61 | 566 | |
paul@61 | 567 | # End of day. |
paul@61 | 568 | |
paul@61 | 569 | output.append(fmt.table_cell(on=0)) |
paul@61 | 570 | |
paul@61 | 571 | # End of day numbers. |
paul@5 | 572 | |
paul@7 | 573 | output.append(fmt.table_row(on=0)) |
paul@7 | 574 | |
paul@61 | 575 | # Either generate empty days... |
paul@7 | 576 | |
paul@61 | 577 | if not week_events: |
paul@7 | 578 | output.append(fmt.table_row(on=1)) |
paul@5 | 579 | |
paul@7 | 580 | for weekday in range(0, 7): |
paul@7 | 581 | day = first_day + weekday |
paul@7 | 582 | |
paul@61 | 583 | # Output out-of-month days. |
paul@7 | 584 | |
paul@7 | 585 | if day < 1 or day > number_of_days: |
paul@35 | 586 | output.append(fmt.table_cell(on=1, |
paul@35 | 587 | attrs={"class" : "event-day-content event-day-excluded", "colspan" : "3"})) |
paul@7 | 588 | output.append(fmt.table_cell(on=0)) |
paul@2 | 589 | |
paul@61 | 590 | # Output empty days. |
paul@2 | 591 | |
paul@61 | 592 | else: |
paul@35 | 593 | output.append(fmt.table_cell(on=1, |
paul@35 | 594 | attrs={"class" : "event-day-content event-day-empty", "colspan" : "3"})) |
paul@2 | 595 | |
paul@61 | 596 | output.append(fmt.table_row(on=0)) |
paul@61 | 597 | |
paul@61 | 598 | # Or visit each set of scheduled events... |
paul@61 | 599 | |
paul@61 | 600 | else: |
paul@61 | 601 | for coverage, events in week_events: |
paul@61 | 602 | |
paul@61 | 603 | # Output each set. |
paul@61 | 604 | |
paul@61 | 605 | output.append(fmt.table_row(on=1)) |
paul@2 | 606 | |
paul@61 | 607 | # Then, output day details. |
paul@61 | 608 | |
paul@61 | 609 | for weekday in range(0, 7): |
paul@61 | 610 | day = first_day + weekday |
paul@67 | 611 | date = month.as_date(day) |
paul@61 | 612 | |
paul@61 | 613 | # Skip out-of-month days. |
paul@61 | 614 | |
paul@61 | 615 | if day < 1 or day > number_of_days: |
paul@61 | 616 | output.append(fmt.table_cell(on=1, |
paul@61 | 617 | attrs={"class" : "event-day-content event-day-excluded", "colspan" : "3"})) |
paul@61 | 618 | output.append(fmt.table_cell(on=0)) |
paul@7 | 619 | continue |
paul@7 | 620 | |
paul@61 | 621 | # Output the day. |
paul@35 | 622 | |
paul@61 | 623 | if date not in coverage: |
paul@61 | 624 | output.append(fmt.table_cell(on=1, |
paul@61 | 625 | attrs={"class" : "event-day-content event-day-empty", "colspan" : "3"})) |
paul@7 | 626 | |
paul@61 | 627 | # Get event details for the current day. |
paul@3 | 628 | |
paul@69 | 629 | for event in events: |
paul@69 | 630 | event_page = event.getPage() |
paul@69 | 631 | event_details = event.getDetails() |
paul@67 | 632 | |
paul@61 | 633 | if not (event_details["start"] <= date <= event_details["end"]): |
paul@61 | 634 | continue |
paul@7 | 635 | |
paul@61 | 636 | # Get basic properties of the event. |
paul@3 | 637 | |
paul@61 | 638 | starts_today = event_details["start"] == date |
paul@61 | 639 | ends_today = event_details["end"] == date |
paul@69 | 640 | event_summary = event.getSummary(parent_name) |
paul@2 | 641 | |
paul@61 | 642 | # Generate a colour for the event. |
paul@5 | 643 | |
paul@66 | 644 | bg = getColour(event_summary) |
paul@61 | 645 | fg = getBlackOrWhite(bg) |
paul@61 | 646 | style = ("background-color: rgb(%d, %d, %d); color: rgb(%d, %d, %d);" % (bg + fg)) |
paul@35 | 647 | |
paul@61 | 648 | # Determine if the event name should be shown. |
paul@35 | 649 | |
paul@61 | 650 | start_of_period = starts_today or weekday == 0 or day == 1 |
paul@35 | 651 | |
paul@61 | 652 | if name_usage == "daily" or start_of_period: |
paul@61 | 653 | hide_text = 0 |
paul@61 | 654 | else: |
paul@61 | 655 | hide_text = 1 |
paul@35 | 656 | |
paul@61 | 657 | # Output start of day gap and determine whether |
paul@61 | 658 | # any event content should be explicitly output |
paul@61 | 659 | # for this day. |
paul@35 | 660 | |
paul@61 | 661 | if starts_today: |
paul@35 | 662 | |
paul@61 | 663 | # Single day events... |
paul@35 | 664 | |
paul@61 | 665 | if ends_today: |
paul@61 | 666 | colspan = 3 |
paul@61 | 667 | event_day_type = "event-day-single" |
paul@35 | 668 | |
paul@61 | 669 | # Events starting today... |
paul@35 | 670 | |
paul@35 | 671 | else: |
paul@61 | 672 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-start-gap"})) |
paul@61 | 673 | output.append(fmt.table_cell(on=0)) |
paul@61 | 674 | |
paul@61 | 675 | # Calculate the span of this cell. |
paul@61 | 676 | # Events whose names appear on every day... |
paul@61 | 677 | |
paul@61 | 678 | if name_usage == "daily": |
paul@61 | 679 | colspan = 2 |
paul@61 | 680 | event_day_type = "event-day-starting" |
paul@61 | 681 | |
paul@61 | 682 | # Events whose names appear once per week... |
paul@61 | 683 | |
paul@35 | 684 | else: |
paul@61 | 685 | if event_details["end"] <= week_end: |
paul@67 | 686 | event_length = event_details["end"].day() - day + 1 |
paul@61 | 687 | colspan = (event_length - 2) * 3 + 4 |
paul@61 | 688 | else: |
paul@67 | 689 | event_length = week_end.day() - day + 1 |
paul@61 | 690 | colspan = (event_length - 1) * 3 + 2 |
paul@35 | 691 | |
paul@61 | 692 | event_day_type = "event-day-multiple" |
paul@61 | 693 | |
paul@61 | 694 | # Events continuing from a previous week... |
paul@35 | 695 | |
paul@61 | 696 | elif start_of_period: |
paul@35 | 697 | |
paul@61 | 698 | # End of continuing event... |
paul@35 | 699 | |
paul@61 | 700 | if ends_today: |
paul@61 | 701 | colspan = 2 |
paul@61 | 702 | event_day_type = "event-day-ending" |
paul@61 | 703 | |
paul@61 | 704 | # Events continuing for at least one more day... |
paul@61 | 705 | |
paul@61 | 706 | else: |
paul@35 | 707 | |
paul@61 | 708 | # Calculate the span of this cell. |
paul@61 | 709 | # Events whose names appear on every day... |
paul@61 | 710 | |
paul@61 | 711 | if name_usage == "daily": |
paul@61 | 712 | colspan = 3 |
paul@61 | 713 | event_day_type = "event-day-full" |
paul@61 | 714 | |
paul@61 | 715 | # Events whose names appear once per week... |
paul@35 | 716 | |
paul@61 | 717 | else: |
paul@61 | 718 | if event_details["end"] <= week_end: |
paul@67 | 719 | event_length = event_details["end"].day() - day + 1 |
paul@61 | 720 | colspan = (event_length - 1) * 3 + 2 |
paul@61 | 721 | else: |
paul@67 | 722 | event_length = week_end.day() - day + 1 |
paul@61 | 723 | colspan = event_length * 3 |
paul@35 | 724 | |
paul@61 | 725 | event_day_type = "event-day-multiple" |
paul@61 | 726 | |
paul@61 | 727 | # Continuing events whose names appear on every day... |
paul@35 | 728 | |
paul@61 | 729 | elif name_usage == "daily": |
paul@61 | 730 | if ends_today: |
paul@61 | 731 | colspan = 2 |
paul@61 | 732 | event_day_type = "event-day-ending" |
paul@61 | 733 | else: |
paul@35 | 734 | colspan = 3 |
paul@35 | 735 | event_day_type = "event-day-full" |
paul@35 | 736 | |
paul@61 | 737 | # Continuing events whose names appear once per week... |
paul@61 | 738 | |
paul@61 | 739 | else: |
paul@61 | 740 | colspan = None |
paul@31 | 741 | |
paul@61 | 742 | # Output the main content only if it is not |
paul@61 | 743 | # continuing from a previous day. |
paul@35 | 744 | |
paul@61 | 745 | if colspan is not None: |
paul@35 | 746 | |
paul@61 | 747 | # Colour the cell for continuing events. |
paul@35 | 748 | |
paul@61 | 749 | attrs={ |
paul@61 | 750 | "class" : "event-day-content event-day-busy %s" % event_day_type, |
paul@61 | 751 | "colspan" : str(colspan) |
paul@61 | 752 | } |
paul@35 | 753 | |
paul@61 | 754 | if not (starts_today and ends_today): |
paul@61 | 755 | attrs["style"] = style |
paul@35 | 756 | |
paul@61 | 757 | output.append(fmt.table_cell(on=1, attrs=attrs)) |
paul@35 | 758 | |
paul@61 | 759 | # Output the event. |
paul@35 | 760 | |
paul@61 | 761 | if starts_today and ends_today or not hide_text: |
paul@35 | 762 | |
paul@61 | 763 | output.append(fmt.div(on=1, css_class="event-summary-box")) |
paul@61 | 764 | output.append(fmt.div(on=1, css_class="event-summary", style=style)) |
paul@67 | 765 | output.append(event_page.linkToPage(request, event_summary)) |
paul@61 | 766 | output.append(fmt.div(on=0)) |
paul@35 | 767 | |
paul@61 | 768 | # Add a pop-up element for long summaries. |
paul@35 | 769 | |
paul@61 | 770 | output.append(fmt.div(on=1, css_class="event-summary-popup", style=style)) |
paul@67 | 771 | output.append(event_page.linkToPage(request, event_summary)) |
paul@61 | 772 | output.append(fmt.div(on=0)) |
paul@35 | 773 | |
paul@61 | 774 | output.append(fmt.div(on=0)) |
paul@35 | 775 | |
paul@61 | 776 | # Output end of day content. |
paul@35 | 777 | |
paul@35 | 778 | output.append(fmt.div(on=0)) |
paul@35 | 779 | |
paul@61 | 780 | # Output end of day gap. |
paul@31 | 781 | |
paul@61 | 782 | if ends_today and not starts_today: |
paul@61 | 783 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-end-gap"})) |
paul@61 | 784 | output.append(fmt.table_cell(on=0)) |
paul@2 | 785 | |
paul@61 | 786 | # End of day. |
paul@5 | 787 | |
paul@61 | 788 | output.append(fmt.table_cell(on=0)) |
paul@35 | 789 | |
paul@61 | 790 | # End of set. |
paul@35 | 791 | |
paul@61 | 792 | output.append(fmt.table_row(on=0)) |
paul@35 | 793 | |
paul@61 | 794 | # Add a spacer. |
paul@35 | 795 | |
paul@61 | 796 | output.append(fmt.table_row(on=1)) |
paul@35 | 797 | |
paul@61 | 798 | for weekday in range(0, 7): |
paul@61 | 799 | day = first_day + weekday |
paul@61 | 800 | css_classes = "event-day-spacer" |
paul@0 | 801 | |
paul@61 | 802 | # Skip out-of-month days. |
paul@0 | 803 | |
paul@61 | 804 | if day < 1 or day > number_of_days: |
paul@61 | 805 | css_classes += " event-day-excluded" |
paul@37 | 806 | |
paul@61 | 807 | output.append(fmt.table_cell(on=1, attrs={"class" : css_classes, "colspan" : "3"})) |
paul@61 | 808 | output.append(fmt.table_cell(on=0)) |
paul@37 | 809 | |
paul@61 | 810 | output.append(fmt.table_row(on=0)) |
paul@25 | 811 | |
paul@61 | 812 | # Process the next week... |
paul@25 | 813 | |
paul@61 | 814 | first_day += 7 |
paul@25 | 815 | |
paul@61 | 816 | # End of month. |
paul@0 | 817 | |
paul@61 | 818 | output.append(fmt.table(on=0)) |
paul@0 | 819 | |
paul@61 | 820 | # Or output a summary view... |
paul@25 | 821 | |
paul@61 | 822 | elif mode == "list": |
paul@0 | 823 | |
paul@61 | 824 | # Output a list. |
paul@0 | 825 | |
paul@61 | 826 | output.append(fmt.listitem(on=1, attr={"class" : "event-listings-month"})) |
paul@61 | 827 | output.append(fmt.div(on=1, attr={"class" : "event-listings-month-heading"})) |
paul@0 | 828 | |
paul@61 | 829 | # Either write a month heading or produce links for navigable |
paul@61 | 830 | # calendars. |
paul@25 | 831 | |
paul@67 | 832 | output.append(view.writeMonthHeading(month)) |
paul@0 | 833 | |
paul@61 | 834 | output.append(fmt.div(on=0)) |
paul@25 | 835 | |
paul@61 | 836 | output.append(fmt.bullet_list(on=1, attr={"class" : "event-month-listings"})) |
paul@25 | 837 | |
paul@61 | 838 | # Get the events in order. |
paul@0 | 839 | |
paul@67 | 840 | ordered_events = EventAggregatorSupport.getOrderedEvents(shown_events.get(month, [])) |
paul@51 | 841 | |
paul@61 | 842 | # Show the events in order. |
paul@51 | 843 | |
paul@69 | 844 | for event in ordered_events: |
paul@69 | 845 | event_page = event.getPage() |
paul@69 | 846 | event_details = event.getDetails() |
paul@69 | 847 | event_summary = event.getSummary(parent_name) |
paul@51 | 848 | |
paul@61 | 849 | output.append(fmt.listitem(on=1, attr={"class" : "event-listing"})) |
paul@51 | 850 | |
paul@61 | 851 | # Link to the page using the summary. |
paul@51 | 852 | |
paul@61 | 853 | output.append(fmt.paragraph(on=1)) |
paul@67 | 854 | output.append(event_page.linkToPage(request, event_summary)) |
paul@61 | 855 | output.append(fmt.paragraph(on=0)) |
paul@51 | 856 | |
paul@61 | 857 | # Start and end dates. |
paul@51 | 858 | |
paul@61 | 859 | output.append(fmt.paragraph(on=1)) |
paul@61 | 860 | output.append(fmt.span(on=1)) |
paul@67 | 861 | output.append(fmt.text(str(event_details["start"]))) |
paul@61 | 862 | output.append(fmt.span(on=0)) |
paul@52 | 863 | output.append(fmt.text(" - ")) |
paul@52 | 864 | output.append(fmt.span(on=1)) |
paul@67 | 865 | output.append(fmt.text(str(event_details["end"]))) |
paul@52 | 866 | output.append(fmt.span(on=0)) |
paul@61 | 867 | output.append(fmt.paragraph(on=0)) |
paul@51 | 868 | |
paul@61 | 869 | # Location. |
paul@51 | 870 | |
paul@61 | 871 | if event_details.has_key("location"): |
paul@61 | 872 | output.append(fmt.paragraph(on=1)) |
paul@61 | 873 | output.append(fmt.text(event_details["location"])) |
paul@61 | 874 | output.append(fmt.paragraph(on=1)) |
paul@51 | 875 | |
paul@61 | 876 | # Topics. |
paul@51 | 877 | |
paul@61 | 878 | if event_details.has_key("topics") or event_details.has_key("categories"): |
paul@61 | 879 | output.append(fmt.bullet_list(on=1, attr={"class" : "event-topics"})) |
paul@51 | 880 | |
paul@61 | 881 | for topic in event_details.get("topics") or event_details.get("categories") or []: |
paul@61 | 882 | output.append(fmt.listitem(on=1)) |
paul@61 | 883 | output.append(fmt.text(topic)) |
paul@61 | 884 | output.append(fmt.listitem(on=0)) |
paul@51 | 885 | |
paul@61 | 886 | output.append(fmt.bullet_list(on=0)) |
paul@51 | 887 | |
paul@61 | 888 | output.append(fmt.listitem(on=0)) |
paul@9 | 889 | |
paul@61 | 890 | output.append(fmt.bullet_list(on=0)) |
paul@9 | 891 | |
paul@61 | 892 | # Output top-level information. |
paul@0 | 893 | |
paul@61 | 894 | # End of list view output. |
paul@51 | 895 | |
paul@61 | 896 | if mode == "list": |
paul@61 | 897 | output.append(fmt.bullet_list(on=0)) |
paul@51 | 898 | |
paul@57 | 899 | # Output view controls. |
paul@57 | 900 | |
paul@57 | 901 | output.append(fmt.div(on=1, css_class="event-controls")) |
paul@57 | 902 | output.append(view.writeViewControls()) |
paul@57 | 903 | output.append(fmt.div(on=0)) |
paul@57 | 904 | |
paul@0 | 905 | return ''.join(output) |
paul@0 | 906 | |
paul@0 | 907 | # vim: tabstop=4 expandtab shiftwidth=4 |