paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - EventAggregator Macro |
paul@0 | 4 | |
paul@126 | 5 | @copyright: 2008, 2009, 2010, 2011 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@0 | 17 | Dependencies = ['pages'] |
paul@0 | 18 | |
paul@37 | 19 | # Abstractions. |
paul@37 | 20 | |
paul@37 | 21 | class View: |
paul@37 | 22 | |
paul@37 | 23 | "A view of the event calendar." |
paul@37 | 24 | |
paul@76 | 25 | def __init__(self, page, calendar_name, raw_calendar_start, raw_calendar_end, |
paul@76 | 26 | calendar_start, calendar_end, first, last, category_names, template_name, |
paul@120 | 27 | parent_name, mode, name_usage): |
paul@37 | 28 | |
paul@37 | 29 | """ |
paul@37 | 30 | Initialise the view with the current 'page', a 'calendar_name' (which |
paul@76 | 31 | may be None), the 'raw_calendar_start' and 'raw_calendar_end' (which |
paul@76 | 32 | are the actual start and end values provided by the request), the |
paul@76 | 33 | requested, calculated 'calendar_start' and 'calendar_end', and the |
paul@57 | 34 | 'first' and 'last' months of event coverage. |
paul@57 | 35 | |
paul@57 | 36 | The additional 'category_names', 'template_name', 'parent_name' and |
paul@57 | 37 | 'mode' parameters are used to configure the links employed by the view. |
paul@120 | 38 | |
paul@120 | 39 | The 'name_usage' parameter controls how names are shown on calendar mode |
paul@126 | 40 | events, such as how often labels are repeated. |
paul@37 | 41 | """ |
paul@37 | 42 | |
paul@37 | 43 | self.page = page |
paul@37 | 44 | self.calendar_name = calendar_name |
paul@76 | 45 | self.raw_calendar_start = raw_calendar_start |
paul@76 | 46 | self.raw_calendar_end = raw_calendar_end |
paul@57 | 47 | self.calendar_start = calendar_start |
paul@57 | 48 | self.calendar_end = calendar_end |
paul@57 | 49 | self.template_name = template_name |
paul@57 | 50 | self.parent_name = parent_name |
paul@57 | 51 | self.mode = mode |
paul@120 | 52 | self.name_usage = name_usage |
paul@57 | 53 | |
paul@57 | 54 | self.category_name_parameters = "&".join([("category=%s" % name) for name in category_names]) |
paul@37 | 55 | |
paul@37 | 56 | if self.calendar_name is not None: |
paul@37 | 57 | |
paul@37 | 58 | # Store the view parameters. |
paul@37 | 59 | |
paul@67 | 60 | self.number_of_months = (last - first).months() + 1 |
paul@37 | 61 | |
paul@67 | 62 | self.previous_month_start = first.previous_month() |
paul@67 | 63 | self.next_month_start = first.next_month() |
paul@67 | 64 | self.previous_month_end = last.previous_month() |
paul@67 | 65 | self.next_month_end = last.next_month() |
paul@37 | 66 | |
paul@67 | 67 | self.previous_set_start = first.month_update(-self.number_of_months) |
paul@67 | 68 | self.next_set_start = first.month_update(self.number_of_months) |
paul@67 | 69 | self.previous_set_end = last.month_update(-self.number_of_months) |
paul@67 | 70 | self.next_set_end = last.month_update(self.number_of_months) |
paul@37 | 71 | |
paul@58 | 72 | def getQualifiedParameterName(self, argname): |
paul@119 | 73 | |
paul@119 | 74 | "Return the 'argname' qualified using the calendar name." |
paul@119 | 75 | |
paul@58 | 76 | return EventAggregatorSupport.getQualifiedParameterName(self.calendar_name, argname) |
paul@58 | 77 | |
paul@123 | 78 | def getDateQueryString(self, argname, date, prefix=1): |
paul@119 | 79 | |
paul@119 | 80 | """ |
paul@119 | 81 | Return a query string fragment for the given 'argname', referring to the |
paul@119 | 82 | month given by the specified 'year_month' object, appropriate for this |
paul@119 | 83 | calendar. |
paul@119 | 84 | |
paul@119 | 85 | If 'prefix' is specified and set to a false value, the parameters in the |
paul@119 | 86 | query string will not be calendar-specific, but could be used with the |
paul@119 | 87 | summary action. |
paul@119 | 88 | """ |
paul@119 | 89 | |
paul@123 | 90 | suffixes = ["year", "month", "day"] |
paul@123 | 91 | |
paul@123 | 92 | if date is not None: |
paul@123 | 93 | args = [] |
paul@123 | 94 | for suffix, value in zip(suffixes, date.as_tuple()): |
paul@123 | 95 | suffixed_argname = "%s-%s" % (argname, suffix) |
paul@123 | 96 | if prefix: |
paul@123 | 97 | suffixed_argname = self.getQualifiedParameterName(suffixed_argname) |
paul@123 | 98 | args.append("%s=%s" % (suffixed_argname, value)) |
paul@123 | 99 | return "&".join(args) |
paul@114 | 100 | else: |
paul@114 | 101 | return "" |
paul@114 | 102 | |
paul@123 | 103 | def getRawDateQueryString(self, argname, date, prefix=1): |
paul@119 | 104 | |
paul@119 | 105 | """ |
paul@119 | 106 | Return a query string fragment for the given 'argname', referring to the |
paul@123 | 107 | date given by the specified 'date' value, appropriate for this |
paul@119 | 108 | calendar. |
paul@119 | 109 | |
paul@119 | 110 | If 'prefix' is specified and set to a false value, the parameters in the |
paul@119 | 111 | query string will not be calendar-specific, but could be used with the |
paul@119 | 112 | summary action. |
paul@119 | 113 | """ |
paul@119 | 114 | |
paul@123 | 115 | if date is not None: |
paul@57 | 116 | if prefix: |
paul@58 | 117 | argname = self.getQualifiedParameterName(argname) |
paul@123 | 118 | return "%s=%s" % (argname, date) |
paul@37 | 119 | else: |
paul@37 | 120 | return "" |
paul@37 | 121 | |
paul@57 | 122 | def getNavigationLink(self, start, end, mode=None): |
paul@119 | 123 | |
paul@119 | 124 | """ |
paul@119 | 125 | Return a query string fragment for navigation to a view showing months |
paul@119 | 126 | from 'start' to 'end' inclusive, with the optional 'mode' indicating the |
paul@119 | 127 | view style. |
paul@119 | 128 | """ |
paul@119 | 129 | |
paul@58 | 130 | return "%s&%s&%s=%s" % ( |
paul@123 | 131 | self.getRawDateQueryString("start", start), |
paul@123 | 132 | self.getRawDateQueryString("end", end), |
paul@58 | 133 | self.getQualifiedParameterName("mode"), mode or self.mode |
paul@57 | 134 | ) |
paul@57 | 135 | |
paul@122 | 136 | def getFullDateLabel(self, date): |
paul@122 | 137 | page = self.page |
paul@122 | 138 | request = page.request |
paul@122 | 139 | return EventAggregatorSupport.getFullDateLabel(request, date) |
paul@122 | 140 | |
paul@114 | 141 | def getFullMonthLabel(self, year_month): |
paul@114 | 142 | page = self.page |
paul@114 | 143 | request = page.request |
paul@114 | 144 | return EventAggregatorSupport.getFullMonthLabel(request, year_month) |
paul@114 | 145 | |
paul@57 | 146 | def writeDownloadControls(self): |
paul@57 | 147 | page = self.page |
paul@57 | 148 | request = page.request |
paul@57 | 149 | fmt = page.formatter |
paul@57 | 150 | _ = request.getText |
paul@57 | 151 | |
paul@57 | 152 | output = [] |
paul@57 | 153 | |
paul@57 | 154 | # Generate the links. |
paul@57 | 155 | |
paul@112 | 156 | download_dialogue_link = "action=EventAggregatorSummary&parent=%s&%s" % ( |
paul@74 | 157 | self.parent_name or "", self.category_name_parameters |
paul@74 | 158 | ) |
paul@112 | 159 | download_all_link = download_dialogue_link + "&doit=1" |
paul@57 | 160 | download_link = download_all_link + ("&%s&%s" % ( |
paul@123 | 161 | self.getDateQueryString("start", self.calendar_start, prefix=0), |
paul@123 | 162 | self.getDateQueryString("end", self.calendar_end, prefix=0) |
paul@57 | 163 | )) |
paul@112 | 164 | |
paul@112 | 165 | # Subscription links just explicitly select the RSS format. |
paul@112 | 166 | |
paul@112 | 167 | subscribe_dialogue_link = download_dialogue_link + "&format=RSS" |
paul@57 | 168 | subscribe_all_link = download_all_link + "&format=RSS" |
paul@57 | 169 | subscribe_link = download_link + "&format=RSS" |
paul@57 | 170 | |
paul@76 | 171 | # Adjust the "download all" and "subscribe all" links if the calendar |
paul@76 | 172 | # has an inherent period associated with it. |
paul@76 | 173 | |
paul@76 | 174 | period_limits = [] |
paul@76 | 175 | |
paul@76 | 176 | if self.raw_calendar_start: |
paul@76 | 177 | period_limits.append("&%s" % |
paul@123 | 178 | self.getRawDateQueryString("start", self.raw_calendar_start, prefix=0) |
paul@76 | 179 | ) |
paul@76 | 180 | if self.raw_calendar_end: |
paul@76 | 181 | period_limits.append("&%s" % |
paul@123 | 182 | self.getRawDateQueryString("end", self.raw_calendar_end, prefix=0) |
paul@76 | 183 | ) |
paul@76 | 184 | |
paul@76 | 185 | period_limits = "".join(period_limits) |
paul@76 | 186 | |
paul@112 | 187 | download_dialogue_link += period_limits |
paul@76 | 188 | download_all_link += period_limits |
paul@112 | 189 | subscribe_dialogue_link += period_limits |
paul@76 | 190 | subscribe_all_link += period_limits |
paul@76 | 191 | |
paul@112 | 192 | # Pop-up descriptions of the downloadable calendars. |
paul@112 | 193 | |
paul@126 | 194 | get_label = self.mode == "day" and self.getFullDateLabel or self.getFullMonthLabel |
paul@126 | 195 | |
paul@114 | 196 | calendar_period = "%s - %s" % ( |
paul@126 | 197 | get_label(self.calendar_start), |
paul@126 | 198 | get_label(self.calendar_end) |
paul@114 | 199 | ) |
paul@112 | 200 | raw_calendar_period = "%s - %s" % (self.raw_calendar_start, self.raw_calendar_end) |
paul@112 | 201 | |
paul@57 | 202 | # Write the controls. |
paul@57 | 203 | |
paul@114 | 204 | # Download controls. |
paul@114 | 205 | |
paul@57 | 206 | output.append(fmt.div(on=1, css_class="event-download-controls")) |
paul@57 | 207 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@57 | 208 | output.append(linkToPage(request, page, _("Download this view"), download_link)) |
paul@112 | 209 | output.append(fmt.span(on=1, css_class="event-download-popup")) |
paul@112 | 210 | output.append(fmt.text(calendar_period)) |
paul@112 | 211 | output.append(fmt.span(on=0)) |
paul@57 | 212 | output.append(fmt.span(on=0)) |
paul@114 | 213 | |
paul@57 | 214 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@57 | 215 | output.append(linkToPage(request, page, _("Download this calendar"), download_all_link)) |
paul@112 | 216 | output.append(fmt.span(on=1, css_class="event-download-popup")) |
paul@114 | 217 | output.append(fmt.span(on=1, css_class="event-download-period")) |
paul@114 | 218 | output.append(fmt.text(calendar_period)) |
paul@114 | 219 | output.append(fmt.span(on=0)) |
paul@114 | 220 | output.append(fmt.span(on=1, css_class="event-download-period-raw")) |
paul@112 | 221 | output.append(fmt.text(raw_calendar_period)) |
paul@112 | 222 | output.append(fmt.span(on=0)) |
paul@112 | 223 | output.append(fmt.span(on=0)) |
paul@114 | 224 | output.append(fmt.span(on=0)) |
paul@114 | 225 | |
paul@112 | 226 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@112 | 227 | output.append(linkToPage(request, page, _("Download..."), download_dialogue_link)) |
paul@112 | 228 | output.append(fmt.span(on=1, css_class="event-download-popup")) |
paul@112 | 229 | output.append(fmt.text(_("Edit download options"))) |
paul@112 | 230 | output.append(fmt.span(on=0)) |
paul@57 | 231 | output.append(fmt.span(on=0)) |
paul@114 | 232 | |
paul@114 | 233 | # Subscription controls. |
paul@114 | 234 | |
paul@57 | 235 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@57 | 236 | output.append(linkToPage(request, page, _("Subscribe to this view"), subscribe_link)) |
paul@112 | 237 | output.append(fmt.span(on=1, css_class="event-download-popup")) |
paul@112 | 238 | output.append(fmt.text(calendar_period)) |
paul@112 | 239 | output.append(fmt.span(on=0)) |
paul@57 | 240 | output.append(fmt.span(on=0)) |
paul@114 | 241 | |
paul@57 | 242 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@57 | 243 | output.append(linkToPage(request, page, _("Subscribe to this calendar"), subscribe_all_link)) |
paul@112 | 244 | output.append(fmt.span(on=1, css_class="event-download-popup")) |
paul@114 | 245 | output.append(fmt.span(on=1, css_class="event-download-period")) |
paul@114 | 246 | output.append(fmt.text(calendar_period)) |
paul@114 | 247 | output.append(fmt.span(on=0)) |
paul@114 | 248 | output.append(fmt.span(on=1, css_class="event-download-period-raw")) |
paul@112 | 249 | output.append(fmt.text(raw_calendar_period)) |
paul@112 | 250 | output.append(fmt.span(on=0)) |
paul@112 | 251 | output.append(fmt.span(on=0)) |
paul@114 | 252 | output.append(fmt.span(on=0)) |
paul@114 | 253 | |
paul@112 | 254 | output.append(fmt.span(on=1, css_class="event-download")) |
paul@112 | 255 | output.append(linkToPage(request, page, _("Subscribe..."), subscribe_dialogue_link)) |
paul@112 | 256 | output.append(fmt.span(on=1, css_class="event-download-popup")) |
paul@112 | 257 | output.append(fmt.text(_("Edit subscription options"))) |
paul@112 | 258 | output.append(fmt.span(on=0)) |
paul@57 | 259 | output.append(fmt.span(on=0)) |
paul@57 | 260 | output.append(fmt.div(on=0)) |
paul@57 | 261 | |
paul@57 | 262 | return "".join(output) |
paul@57 | 263 | |
paul@57 | 264 | def writeViewControls(self): |
paul@57 | 265 | page = self.page |
paul@57 | 266 | request = page.request |
paul@57 | 267 | fmt = page.formatter |
paul@57 | 268 | _ = request.getText |
paul@57 | 269 | |
paul@57 | 270 | output = [] |
paul@57 | 271 | |
paul@57 | 272 | calendar_link = self.getNavigationLink( |
paul@57 | 273 | self.calendar_start, self.calendar_end, "calendar" |
paul@57 | 274 | ) |
paul@57 | 275 | list_link = self.getNavigationLink( |
paul@57 | 276 | self.calendar_start, self.calendar_end, "list" |
paul@57 | 277 | ) |
paul@57 | 278 | table_link = self.getNavigationLink( |
paul@57 | 279 | self.calendar_start, self.calendar_end, "table" |
paul@57 | 280 | ) |
paul@57 | 281 | |
paul@57 | 282 | # Write the controls. |
paul@57 | 283 | |
paul@57 | 284 | output.append(fmt.div(on=1, css_class="event-view-controls")) |
paul@57 | 285 | output.append(fmt.span(on=1, css_class="event-view")) |
paul@57 | 286 | output.append(linkToPage(request, page, _("View as calendar"), calendar_link)) |
paul@57 | 287 | output.append(fmt.span(on=0)) |
paul@57 | 288 | output.append(fmt.span(on=1, css_class="event-view")) |
paul@57 | 289 | output.append(linkToPage(request, page, _("View as list"), list_link)) |
paul@57 | 290 | output.append(fmt.span(on=0)) |
paul@57 | 291 | output.append(fmt.span(on=1, css_class="event-view")) |
paul@57 | 292 | output.append(linkToPage(request, page, _("View as table"), table_link)) |
paul@57 | 293 | output.append(fmt.span(on=0)) |
paul@57 | 294 | output.append(fmt.div(on=0)) |
paul@57 | 295 | |
paul@57 | 296 | return "".join(output) |
paul@57 | 297 | |
paul@67 | 298 | def writeMonthHeading(self, year_month): |
paul@37 | 299 | page = self.page |
paul@37 | 300 | request = page.request |
paul@37 | 301 | fmt = page.formatter |
paul@37 | 302 | _ = request.getText |
paul@116 | 303 | full_month_label = self.getFullMonthLabel(year_month) |
paul@37 | 304 | |
paul@37 | 305 | output = [] |
paul@37 | 306 | |
paul@37 | 307 | # Prepare navigation links. |
paul@37 | 308 | |
paul@37 | 309 | if self.calendar_name is not None: |
paul@37 | 310 | calendar_name = self.calendar_name |
paul@37 | 311 | |
paul@37 | 312 | # Links to the previous set of months and to a calendar shifted |
paul@37 | 313 | # back one month. |
paul@37 | 314 | |
paul@57 | 315 | previous_set_link = self.getNavigationLink( |
paul@57 | 316 | self.previous_set_start, self.previous_set_end |
paul@37 | 317 | ) |
paul@57 | 318 | previous_month_link = self.getNavigationLink( |
paul@57 | 319 | self.previous_month_start, self.previous_month_end |
paul@37 | 320 | ) |
paul@37 | 321 | |
paul@37 | 322 | # Links to the next set of months and to a calendar shifted |
paul@37 | 323 | # forward one month. |
paul@37 | 324 | |
paul@57 | 325 | next_set_link = self.getNavigationLink( |
paul@57 | 326 | self.next_set_start, self.next_set_end |
paul@37 | 327 | ) |
paul@57 | 328 | next_month_link = self.getNavigationLink( |
paul@57 | 329 | self.next_month_start, self.next_month_end |
paul@37 | 330 | ) |
paul@37 | 331 | |
paul@37 | 332 | # A link leading to this month being at the top of the calendar. |
paul@37 | 333 | |
paul@67 | 334 | end_month = year_month.month_update(self.number_of_months - 1) |
paul@37 | 335 | |
paul@67 | 336 | month_link = self.getNavigationLink(year_month, end_month) |
paul@37 | 337 | |
paul@37 | 338 | output.append(fmt.span(on=1, css_class="previous-month")) |
paul@37 | 339 | output.append(linkToPage(request, page, "<<", previous_set_link)) |
paul@37 | 340 | output.append(fmt.text(" ")) |
paul@37 | 341 | output.append(linkToPage(request, page, "<", previous_month_link)) |
paul@37 | 342 | output.append(fmt.span(on=0)) |
paul@37 | 343 | |
paul@37 | 344 | output.append(fmt.span(on=1, css_class="next-month")) |
paul@37 | 345 | output.append(linkToPage(request, page, ">", next_month_link)) |
paul@37 | 346 | output.append(fmt.text(" ")) |
paul@37 | 347 | output.append(linkToPage(request, page, ">>", next_set_link)) |
paul@37 | 348 | output.append(fmt.span(on=0)) |
paul@37 | 349 | |
paul@37 | 350 | output.append(linkToPage(request, page, full_month_label, month_link)) |
paul@37 | 351 | |
paul@37 | 352 | else: |
paul@37 | 353 | output.append(fmt.span(on=1)) |
paul@114 | 354 | output.append(fmt.text(full_month_label)) |
paul@37 | 355 | output.append(fmt.span(on=0)) |
paul@37 | 356 | |
paul@37 | 357 | return "".join(output) |
paul@37 | 358 | |
paul@67 | 359 | def writeDayNumberLinked(self, date): |
paul@57 | 360 | page = self.page |
paul@57 | 361 | request = page.request |
paul@57 | 362 | fmt = page.formatter |
paul@57 | 363 | _ = request.getText |
paul@57 | 364 | |
paul@67 | 365 | year, month, day = date.as_tuple() |
paul@57 | 366 | output = [] |
paul@57 | 367 | |
paul@58 | 368 | # Prepare navigation details for the calendar shown with the new event |
paul@58 | 369 | # form. |
paul@58 | 370 | |
paul@58 | 371 | navigation_link = self.getNavigationLink( |
paul@58 | 372 | self.calendar_start, self.calendar_end, self.mode |
paul@58 | 373 | ) |
paul@58 | 374 | |
paul@58 | 375 | # Prepare the link to the new event form, incorporating the above |
paul@58 | 376 | # calendar parameters. |
paul@58 | 377 | |
paul@57 | 378 | new_event_link = "action=EventAggregatorNewEvent&start-day=%d&start-month=%d&start-year=%d" \ |
paul@58 | 379 | "&%s&template=%s&parent=%s&%s" % ( |
paul@58 | 380 | day, month, year, self.category_name_parameters, self.template_name, self.parent_name or "", |
paul@58 | 381 | navigation_link) |
paul@57 | 382 | |
paul@57 | 383 | output.append(fmt.div(on=1)) |
paul@57 | 384 | output.append(fmt.span(on=1, css_class="event-day-number")) |
paul@57 | 385 | output.append(linkToPage(request, page, unicode(day), new_event_link)) |
paul@57 | 386 | output.append(fmt.span(on=0)) |
paul@57 | 387 | output.append(fmt.div(on=0)) |
paul@57 | 388 | |
paul@57 | 389 | return "".join(output) |
paul@57 | 390 | |
paul@126 | 391 | # Common layout methods. |
paul@126 | 392 | |
paul@126 | 393 | def getEventStyle(self, colour_seed): |
paul@126 | 394 | |
paul@126 | 395 | "Generate colour style information using the given 'colour_seed'." |
paul@126 | 396 | |
paul@126 | 397 | bg = getColour(colour_seed) |
paul@126 | 398 | fg = getBlackOrWhite(bg) |
paul@126 | 399 | return "background-color: rgb(%d, %d, %d); color: rgb(%d, %d, %d);" % (bg + fg) |
paul@126 | 400 | |
paul@126 | 401 | def writeEventSummaryBox(self, event): |
paul@126 | 402 | |
paul@126 | 403 | "Return an event summary box linking to the given 'event'." |
paul@126 | 404 | |
paul@126 | 405 | page = self.page |
paul@126 | 406 | request = page.request |
paul@126 | 407 | fmt = page.formatter |
paul@126 | 408 | |
paul@126 | 409 | output = [] |
paul@126 | 410 | |
paul@126 | 411 | event_page = event.getPage() |
paul@126 | 412 | event_details = event.getDetails() |
paul@126 | 413 | event_summary = event.getSummary(self.parent_name) |
paul@126 | 414 | |
paul@126 | 415 | is_ambiguous = event_details["start"].ambiguous() or event_details["end"].ambiguous() |
paul@126 | 416 | style = self.getEventStyle(event_summary) |
paul@126 | 417 | |
paul@126 | 418 | # The event box contains the summary, alongside |
paul@126 | 419 | # other elements. |
paul@126 | 420 | |
paul@126 | 421 | output.append(fmt.div(on=1, css_class="event-summary-box")) |
paul@126 | 422 | output.append(fmt.div(on=1, css_class="event-summary", style=style)) |
paul@126 | 423 | |
paul@126 | 424 | if is_ambiguous: |
paul@126 | 425 | output.append(fmt.icon("/!\\")) |
paul@126 | 426 | |
paul@126 | 427 | output.append(event_page.linkToPage(request, event_summary)) |
paul@126 | 428 | output.append(fmt.div(on=0)) |
paul@126 | 429 | |
paul@126 | 430 | # Add a pop-up element for long summaries. |
paul@126 | 431 | |
paul@126 | 432 | output.append(fmt.div(on=1, css_class="event-summary-popup", style=style)) |
paul@126 | 433 | |
paul@126 | 434 | if is_ambiguous: |
paul@126 | 435 | output.append(fmt.icon("/!\\")) |
paul@126 | 436 | |
paul@126 | 437 | output.append(event_page.linkToPage(request, event_summary)) |
paul@126 | 438 | output.append(fmt.div(on=0)) |
paul@126 | 439 | |
paul@126 | 440 | output.append(fmt.div(on=0)) |
paul@126 | 441 | |
paul@126 | 442 | return "".join(output) |
paul@126 | 443 | |
paul@120 | 444 | # Calendar layout methods. |
paul@120 | 445 | |
paul@122 | 446 | def writeMonthTableHeading(self, year_month): |
paul@122 | 447 | page = self.page |
paul@122 | 448 | fmt = page.formatter |
paul@122 | 449 | |
paul@122 | 450 | output = [] |
paul@122 | 451 | output.append(fmt.table_row(on=1)) |
paul@122 | 452 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "21"})) |
paul@122 | 453 | |
paul@122 | 454 | output.append(self.writeMonthHeading(year_month)) |
paul@122 | 455 | |
paul@122 | 456 | output.append(fmt.table_cell(on=0)) |
paul@122 | 457 | output.append(fmt.table_row(on=0)) |
paul@122 | 458 | |
paul@122 | 459 | return "".join(output) |
paul@122 | 460 | |
paul@122 | 461 | def writeWeekdayHeadings(self): |
paul@122 | 462 | page = self.page |
paul@122 | 463 | request = page.request |
paul@122 | 464 | fmt = page.formatter |
paul@122 | 465 | _ = request.getText |
paul@122 | 466 | |
paul@122 | 467 | output = [] |
paul@122 | 468 | output.append(fmt.table_row(on=1)) |
paul@122 | 469 | |
paul@122 | 470 | for weekday in range(0, 7): |
paul@122 | 471 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-weekday-heading", "colspan" : "3"})) |
paul@122 | 472 | output.append(fmt.text(_(EventAggregatorSupport.getDayLabel(weekday)))) |
paul@122 | 473 | output.append(fmt.table_cell(on=0)) |
paul@122 | 474 | |
paul@122 | 475 | output.append(fmt.table_row(on=0)) |
paul@122 | 476 | return "".join(output) |
paul@122 | 477 | |
paul@122 | 478 | def writeDayNumbers(self, first_day, number_of_days, month, coverage): |
paul@120 | 479 | page = self.page |
paul@120 | 480 | fmt = page.formatter |
paul@120 | 481 | |
paul@120 | 482 | output = [] |
paul@120 | 483 | output.append(fmt.table_row(on=1)) |
paul@120 | 484 | |
paul@120 | 485 | for weekday in range(0, 7): |
paul@120 | 486 | day = first_day + weekday |
paul@120 | 487 | date = month.as_date(day) |
paul@120 | 488 | |
paul@120 | 489 | # Output out-of-month days. |
paul@120 | 490 | |
paul@120 | 491 | if day < 1 or day > number_of_days: |
paul@120 | 492 | output.append(fmt.table_cell(on=1, |
paul@120 | 493 | attrs={"class" : "event-day-heading event-day-excluded", "colspan" : "3"})) |
paul@120 | 494 | output.append(fmt.table_cell(on=0)) |
paul@120 | 495 | |
paul@120 | 496 | # Output normal days. |
paul@120 | 497 | |
paul@120 | 498 | else: |
paul@122 | 499 | if date in coverage: |
paul@120 | 500 | output.append(fmt.table_cell(on=1, |
paul@120 | 501 | attrs={"class" : "event-day-heading event-day-busy", "colspan" : "3"})) |
paul@120 | 502 | else: |
paul@120 | 503 | output.append(fmt.table_cell(on=1, |
paul@120 | 504 | attrs={"class" : "event-day-heading event-day-empty", "colspan" : "3"})) |
paul@120 | 505 | |
paul@120 | 506 | # Output the day number, making a link to a new event |
paul@120 | 507 | # action. |
paul@120 | 508 | |
paul@120 | 509 | output.append(self.writeDayNumberLinked(date)) |
paul@120 | 510 | |
paul@120 | 511 | # End of day. |
paul@120 | 512 | |
paul@120 | 513 | output.append(fmt.table_cell(on=0)) |
paul@120 | 514 | |
paul@120 | 515 | # End of day numbers. |
paul@120 | 516 | |
paul@120 | 517 | output.append(fmt.table_row(on=0)) |
paul@120 | 518 | return "".join(output) |
paul@120 | 519 | |
paul@120 | 520 | def writeEmptyWeek(self, first_day, number_of_days): |
paul@120 | 521 | page = self.page |
paul@120 | 522 | fmt = page.formatter |
paul@120 | 523 | |
paul@120 | 524 | output = [] |
paul@120 | 525 | output.append(fmt.table_row(on=1)) |
paul@120 | 526 | |
paul@120 | 527 | for weekday in range(0, 7): |
paul@120 | 528 | day = first_day + weekday |
paul@120 | 529 | |
paul@120 | 530 | # Output out-of-month days. |
paul@120 | 531 | |
paul@120 | 532 | if day < 1 or day > number_of_days: |
paul@120 | 533 | output.append(fmt.table_cell(on=1, |
paul@120 | 534 | attrs={"class" : "event-day-content event-day-excluded", "colspan" : "3"})) |
paul@120 | 535 | output.append(fmt.table_cell(on=0)) |
paul@120 | 536 | |
paul@120 | 537 | # Output empty days. |
paul@120 | 538 | |
paul@120 | 539 | else: |
paul@120 | 540 | output.append(fmt.table_cell(on=1, |
paul@120 | 541 | attrs={"class" : "event-day-content event-day-empty", "colspan" : "3"})) |
paul@120 | 542 | |
paul@120 | 543 | output.append(fmt.table_row(on=0)) |
paul@120 | 544 | return "".join(output) |
paul@120 | 545 | |
paul@120 | 546 | def writeWeekSlots(self, first_day, number_of_days, month, week_end, week_slots): |
paul@120 | 547 | output = [] |
paul@120 | 548 | |
paul@120 | 549 | locations = week_slots.keys() |
paul@120 | 550 | locations.sort(EventAggregatorSupport.sort_none_first) |
paul@120 | 551 | |
paul@120 | 552 | # Visit each slot corresponding to a location (or no location). |
paul@120 | 553 | |
paul@120 | 554 | for location in locations: |
paul@120 | 555 | |
paul@120 | 556 | # Visit each coverage span, presenting the events in the span. |
paul@120 | 557 | |
paul@123 | 558 | for events in week_slots[location]: |
paul@120 | 559 | |
paul@120 | 560 | # Output each set. |
paul@120 | 561 | |
paul@123 | 562 | output.append(self.writeWeekSlot(first_day, number_of_days, month, week_end, events)) |
paul@120 | 563 | |
paul@120 | 564 | # Add a spacer. |
paul@120 | 565 | |
paul@123 | 566 | output.append(self.writeWeekSpacer(first_day, number_of_days)) |
paul@120 | 567 | |
paul@120 | 568 | return "".join(output) |
paul@120 | 569 | |
paul@123 | 570 | def writeWeekSlot(self, first_day, number_of_days, month, week_end, events): |
paul@120 | 571 | page = self.page |
paul@120 | 572 | request = page.request |
paul@120 | 573 | fmt = page.formatter |
paul@120 | 574 | |
paul@120 | 575 | output = [] |
paul@120 | 576 | output.append(fmt.table_row(on=1)) |
paul@120 | 577 | |
paul@120 | 578 | # Then, output day details. |
paul@120 | 579 | |
paul@120 | 580 | for weekday in range(0, 7): |
paul@120 | 581 | day = first_day + weekday |
paul@120 | 582 | date = month.as_date(day) |
paul@120 | 583 | |
paul@120 | 584 | # Skip out-of-month days. |
paul@120 | 585 | |
paul@120 | 586 | if day < 1 or day > number_of_days: |
paul@120 | 587 | output.append(fmt.table_cell(on=1, |
paul@120 | 588 | attrs={"class" : "event-day-content event-day-excluded", "colspan" : "3"})) |
paul@120 | 589 | output.append(fmt.table_cell(on=0)) |
paul@120 | 590 | continue |
paul@120 | 591 | |
paul@120 | 592 | # Output the day. |
paul@120 | 593 | |
paul@123 | 594 | if date not in events: |
paul@120 | 595 | output.append(fmt.table_cell(on=1, |
paul@120 | 596 | attrs={"class" : "event-day-content event-day-empty", "colspan" : "3"})) |
paul@120 | 597 | |
paul@120 | 598 | # Get event details for the current day. |
paul@120 | 599 | |
paul@120 | 600 | for event in events: |
paul@120 | 601 | event_page = event.getPage() |
paul@120 | 602 | event_details = event.getDetails() |
paul@120 | 603 | |
paul@123 | 604 | if date not in event: |
paul@120 | 605 | continue |
paul@120 | 606 | |
paul@120 | 607 | # Get basic properties of the event. |
paul@120 | 608 | |
paul@120 | 609 | starts_today = event_details["start"] == date |
paul@120 | 610 | ends_today = event_details["end"] == date |
paul@120 | 611 | event_summary = event.getSummary(self.parent_name) |
paul@120 | 612 | |
paul@126 | 613 | style = self.getEventStyle(event_summary) |
paul@120 | 614 | |
paul@120 | 615 | # Determine if the event name should be shown. |
paul@120 | 616 | |
paul@120 | 617 | start_of_period = starts_today or weekday == 0 or day == 1 |
paul@120 | 618 | |
paul@120 | 619 | if self.name_usage == "daily" or start_of_period: |
paul@120 | 620 | hide_text = 0 |
paul@120 | 621 | else: |
paul@120 | 622 | hide_text = 1 |
paul@120 | 623 | |
paul@120 | 624 | # Output start of day gap and determine whether |
paul@120 | 625 | # any event content should be explicitly output |
paul@120 | 626 | # for this day. |
paul@120 | 627 | |
paul@120 | 628 | if starts_today: |
paul@120 | 629 | |
paul@120 | 630 | # Single day events... |
paul@120 | 631 | |
paul@120 | 632 | if ends_today: |
paul@120 | 633 | colspan = 3 |
paul@120 | 634 | event_day_type = "event-day-single" |
paul@120 | 635 | |
paul@120 | 636 | # Events starting today... |
paul@120 | 637 | |
paul@120 | 638 | else: |
paul@120 | 639 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-start-gap"})) |
paul@120 | 640 | output.append(fmt.table_cell(on=0)) |
paul@120 | 641 | |
paul@120 | 642 | # Calculate the span of this cell. |
paul@120 | 643 | # Events whose names appear on every day... |
paul@120 | 644 | |
paul@120 | 645 | if self.name_usage == "daily": |
paul@120 | 646 | colspan = 2 |
paul@120 | 647 | event_day_type = "event-day-starting" |
paul@120 | 648 | |
paul@120 | 649 | # Events whose names appear once per week... |
paul@120 | 650 | |
paul@120 | 651 | else: |
paul@120 | 652 | if event_details["end"] <= week_end: |
paul@120 | 653 | event_length = event_details["end"].day() - day + 1 |
paul@120 | 654 | colspan = (event_length - 2) * 3 + 4 |
paul@120 | 655 | else: |
paul@120 | 656 | event_length = week_end.day() - day + 1 |
paul@120 | 657 | colspan = (event_length - 1) * 3 + 2 |
paul@120 | 658 | |
paul@120 | 659 | event_day_type = "event-day-multiple" |
paul@120 | 660 | |
paul@120 | 661 | # Events continuing from a previous week... |
paul@120 | 662 | |
paul@120 | 663 | elif start_of_period: |
paul@120 | 664 | |
paul@120 | 665 | # End of continuing event... |
paul@120 | 666 | |
paul@120 | 667 | if ends_today: |
paul@120 | 668 | colspan = 2 |
paul@120 | 669 | event_day_type = "event-day-ending" |
paul@120 | 670 | |
paul@120 | 671 | # Events continuing for at least one more day... |
paul@120 | 672 | |
paul@120 | 673 | else: |
paul@120 | 674 | |
paul@120 | 675 | # Calculate the span of this cell. |
paul@120 | 676 | # Events whose names appear on every day... |
paul@120 | 677 | |
paul@120 | 678 | if self.name_usage == "daily": |
paul@120 | 679 | colspan = 3 |
paul@120 | 680 | event_day_type = "event-day-full" |
paul@120 | 681 | |
paul@120 | 682 | # Events whose names appear once per week... |
paul@120 | 683 | |
paul@120 | 684 | else: |
paul@120 | 685 | if event_details["end"] <= week_end: |
paul@120 | 686 | event_length = event_details["end"].day() - day + 1 |
paul@120 | 687 | colspan = (event_length - 1) * 3 + 2 |
paul@120 | 688 | else: |
paul@120 | 689 | event_length = week_end.day() - day + 1 |
paul@120 | 690 | colspan = event_length * 3 |
paul@120 | 691 | |
paul@120 | 692 | event_day_type = "event-day-multiple" |
paul@120 | 693 | |
paul@120 | 694 | # Continuing events whose names appear on every day... |
paul@120 | 695 | |
paul@120 | 696 | elif self.name_usage == "daily": |
paul@120 | 697 | if ends_today: |
paul@120 | 698 | colspan = 2 |
paul@120 | 699 | event_day_type = "event-day-ending" |
paul@120 | 700 | else: |
paul@120 | 701 | colspan = 3 |
paul@120 | 702 | event_day_type = "event-day-full" |
paul@120 | 703 | |
paul@120 | 704 | # Continuing events whose names appear once per week... |
paul@120 | 705 | |
paul@120 | 706 | else: |
paul@120 | 707 | colspan = None |
paul@120 | 708 | |
paul@120 | 709 | # Output the main content only if it is not |
paul@120 | 710 | # continuing from a previous day. |
paul@120 | 711 | |
paul@120 | 712 | if colspan is not None: |
paul@120 | 713 | |
paul@120 | 714 | # Colour the cell for continuing events. |
paul@120 | 715 | |
paul@120 | 716 | attrs={ |
paul@120 | 717 | "class" : "event-day-content event-day-busy %s" % event_day_type, |
paul@120 | 718 | "colspan" : str(colspan) |
paul@120 | 719 | } |
paul@120 | 720 | |
paul@120 | 721 | if not (starts_today and ends_today): |
paul@120 | 722 | attrs["style"] = style |
paul@120 | 723 | |
paul@120 | 724 | output.append(fmt.table_cell(on=1, attrs=attrs)) |
paul@120 | 725 | |
paul@120 | 726 | # Output the event. |
paul@120 | 727 | |
paul@120 | 728 | if starts_today and ends_today or not hide_text: |
paul@126 | 729 | output.append(self.writeEventSummaryBox(event)) |
paul@120 | 730 | |
paul@126 | 731 | output.append(fmt.table_cell(on=0)) |
paul@120 | 732 | |
paul@120 | 733 | # Output end of day gap. |
paul@120 | 734 | |
paul@120 | 735 | if ends_today and not starts_today: |
paul@120 | 736 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-end-gap"})) |
paul@120 | 737 | output.append(fmt.table_cell(on=0)) |
paul@120 | 738 | |
paul@120 | 739 | # End of set. |
paul@120 | 740 | |
paul@120 | 741 | output.append(fmt.table_row(on=0)) |
paul@120 | 742 | return "".join(output) |
paul@120 | 743 | |
paul@123 | 744 | def writeWeekSpacer(self, first_day, number_of_days): |
paul@120 | 745 | page = self.page |
paul@120 | 746 | fmt = page.formatter |
paul@120 | 747 | |
paul@120 | 748 | output = [] |
paul@120 | 749 | output.append(fmt.table_row(on=1)) |
paul@120 | 750 | |
paul@120 | 751 | for weekday in range(0, 7): |
paul@120 | 752 | day = first_day + weekday |
paul@120 | 753 | css_classes = "event-day-spacer" |
paul@120 | 754 | |
paul@120 | 755 | # Skip out-of-month days. |
paul@120 | 756 | |
paul@120 | 757 | if day < 1 or day > number_of_days: |
paul@120 | 758 | css_classes += " event-day-excluded" |
paul@120 | 759 | |
paul@120 | 760 | output.append(fmt.table_cell(on=1, attrs={"class" : css_classes, "colspan" : "3"})) |
paul@120 | 761 | output.append(fmt.table_cell(on=0)) |
paul@120 | 762 | |
paul@120 | 763 | output.append(fmt.table_row(on=0)) |
paul@120 | 764 | return "".join(output) |
paul@120 | 765 | |
paul@122 | 766 | # Day layout methods. |
paul@122 | 767 | |
paul@126 | 768 | def writeDayHeading(self, date, colspan=1): |
paul@122 | 769 | page = self.page |
paul@122 | 770 | request = page.request |
paul@122 | 771 | fmt = page.formatter |
paul@122 | 772 | _ = request.getText |
paul@122 | 773 | full_date_label = self.getFullDateLabel(date) |
paul@122 | 774 | |
paul@122 | 775 | output = [] |
paul@122 | 776 | output.append(fmt.table_row(on=1)) |
paul@122 | 777 | |
paul@126 | 778 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-weekday-heading", "colspan" : str(colspan)})) |
paul@122 | 779 | output.append(fmt.text(full_date_label)) |
paul@122 | 780 | output.append(fmt.table_cell(on=0)) |
paul@122 | 781 | |
paul@122 | 782 | output.append(fmt.table_row(on=0)) |
paul@122 | 783 | return "".join(output) |
paul@122 | 784 | |
paul@123 | 785 | def writeEmptyDay(self, date): |
paul@123 | 786 | page = self.page |
paul@123 | 787 | fmt = page.formatter |
paul@123 | 788 | |
paul@123 | 789 | output = [] |
paul@123 | 790 | output.append(fmt.table_row(on=1)) |
paul@123 | 791 | |
paul@123 | 792 | output.append(fmt.table_cell(on=1, |
paul@123 | 793 | attrs={"class" : "event-day-content event-day-empty"})) |
paul@123 | 794 | |
paul@123 | 795 | output.append(fmt.table_row(on=0)) |
paul@123 | 796 | return "".join(output) |
paul@123 | 797 | |
paul@123 | 798 | def writeDaySlots(self, date, full_coverage, day_slots): |
paul@123 | 799 | page = self.page |
paul@123 | 800 | fmt = page.formatter |
paul@123 | 801 | |
paul@123 | 802 | output = [] |
paul@123 | 803 | |
paul@123 | 804 | locations = day_slots.keys() |
paul@123 | 805 | locations.sort(EventAggregatorSupport.sort_none_first) |
paul@123 | 806 | |
paul@123 | 807 | # Traverse the time scale of the full coverage, visiting each slot to |
paul@123 | 808 | # determine whether it provides content for each period. |
paul@123 | 809 | |
paul@123 | 810 | scale = EventAggregatorSupport.getCoverageScale(full_coverage) |
paul@126 | 811 | period = None |
paul@123 | 812 | |
paul@123 | 813 | for period in scale: |
paul@126 | 814 | |
paul@126 | 815 | # Ignore timespans before this day. |
paul@126 | 816 | |
paul@126 | 817 | if not date in period: |
paul@126 | 818 | continue |
paul@126 | 819 | |
paul@126 | 820 | # Write an empty heading for the start of the day where the first |
paul@126 | 821 | # applicable timespan starts before this day. |
paul@123 | 822 | |
paul@126 | 823 | if period.start < date: |
paul@126 | 824 | output.append(fmt.table_row(on=1)) |
paul@126 | 825 | output.append(self.writeDayScaleHeading("")) |
paul@126 | 826 | |
paul@126 | 827 | # Otherwise, write a heading describing the time. |
paul@126 | 828 | |
paul@126 | 829 | else: |
paul@126 | 830 | output.append(fmt.table_row(on=1)) |
paul@126 | 831 | output.append(self.writeDayScaleHeading(str(period.start))) |
paul@123 | 832 | |
paul@123 | 833 | # Visit each slot corresponding to a location (or no location). |
paul@123 | 834 | |
paul@126 | 835 | for location in locations: |
paul@123 | 836 | |
paul@126 | 837 | # Visit each coverage span, presenting the events in the span. |
paul@126 | 838 | |
paul@126 | 839 | for events in day_slots[location]: |
paul@123 | 840 | |
paul@126 | 841 | # Add a spacer. |
paul@126 | 842 | |
paul@126 | 843 | output.append(self.writeDaySpacer()) |
paul@123 | 844 | |
paul@126 | 845 | # Output each set's contribution to this period. |
paul@123 | 846 | |
paul@126 | 847 | output.append(self.writeDaySlot(period, events)) |
paul@123 | 848 | |
paul@123 | 849 | output.append(fmt.table_row(on=0)) |
paul@123 | 850 | |
paul@126 | 851 | # Write a final time heading if the last period ends in the current day. |
paul@126 | 852 | |
paul@126 | 853 | if period is not None: |
paul@126 | 854 | if period.end == date: |
paul@126 | 855 | output.append(fmt.table_row(on=1)) |
paul@126 | 856 | output.append(self.writeDayScaleHeading(str(period.end))) |
paul@126 | 857 | |
paul@126 | 858 | for location in locations: |
paul@126 | 859 | for events in day_slots[location]: |
paul@126 | 860 | output.append(self.writeDaySpacer()) |
paul@126 | 861 | output.append(self.writeEmptyDaySlot()) |
paul@126 | 862 | |
paul@126 | 863 | output.append(fmt.table_row(on=0)) |
paul@126 | 864 | |
paul@126 | 865 | return "".join(output) |
paul@126 | 866 | |
paul@126 | 867 | def writeDayScaleHeading(self, heading): |
paul@126 | 868 | page = self.page |
paul@126 | 869 | fmt = page.formatter |
paul@126 | 870 | |
paul@126 | 871 | output = [] |
paul@126 | 872 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-scale-heading"})) |
paul@126 | 873 | output.append(fmt.text(heading)) |
paul@126 | 874 | output.append(fmt.table_cell(on=0)) |
paul@126 | 875 | |
paul@123 | 876 | return "".join(output) |
paul@123 | 877 | |
paul@126 | 878 | def writeDaySlot(self, period, events): |
paul@126 | 879 | page = self.page |
paul@126 | 880 | fmt = page.formatter |
paul@126 | 881 | |
paul@126 | 882 | output = [] |
paul@126 | 883 | |
paul@126 | 884 | for event in events: |
paul@126 | 885 | if period not in event: |
paul@126 | 886 | continue |
paul@126 | 887 | |
paul@126 | 888 | event_summary = event.getSummary(self.parent_name) |
paul@126 | 889 | style = self.getEventStyle(event_summary) |
paul@126 | 890 | |
paul@126 | 891 | output.append(fmt.table_cell(on=1, attrs={ |
paul@126 | 892 | "class" : "event-timespan-content event-timespan-busy", |
paul@126 | 893 | "style" : style})) |
paul@126 | 894 | output.append(self.writeEventSummaryBox(event)) |
paul@126 | 895 | output.append(fmt.table_cell(on=0)) |
paul@126 | 896 | break |
paul@126 | 897 | |
paul@126 | 898 | else: |
paul@126 | 899 | output.append(self.writeEmptyDaySlot()) |
paul@126 | 900 | |
paul@126 | 901 | return "".join(output) |
paul@126 | 902 | |
paul@126 | 903 | def writeEmptyDaySlot(self): |
paul@126 | 904 | page = self.page |
paul@126 | 905 | fmt = page.formatter |
paul@126 | 906 | |
paul@126 | 907 | output = [] |
paul@126 | 908 | |
paul@126 | 909 | output.append(fmt.table_cell(on=1, |
paul@126 | 910 | attrs={"class" : "event-timespan-content event-timespan-empty"})) |
paul@126 | 911 | output.append(fmt.table_cell(on=0)) |
paul@126 | 912 | |
paul@126 | 913 | return "".join(output) |
paul@126 | 914 | |
paul@126 | 915 | def writeDaySpacer(self): |
paul@126 | 916 | page = self.page |
paul@126 | 917 | fmt = page.formatter |
paul@126 | 918 | |
paul@126 | 919 | output = [] |
paul@126 | 920 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-timespan-spacer"})) |
paul@126 | 921 | output.append(fmt.table_cell(on=0)) |
paul@126 | 922 | return "".join(output) |
paul@123 | 923 | |
paul@10 | 924 | # HTML-related functions. |
paul@2 | 925 | |
paul@2 | 926 | def getColour(s): |
paul@2 | 927 | colour = [0, 0, 0] |
paul@2 | 928 | digit = 0 |
paul@2 | 929 | for c in s: |
paul@2 | 930 | colour[digit] += ord(c) |
paul@2 | 931 | colour[digit] = colour[digit] % 256 |
paul@2 | 932 | digit += 1 |
paul@2 | 933 | digit = digit % 3 |
paul@2 | 934 | return tuple(colour) |
paul@2 | 935 | |
paul@2 | 936 | def getBlackOrWhite(colour): |
paul@2 | 937 | if sum(colour) / 3.0 > 127: |
paul@2 | 938 | return (0, 0, 0) |
paul@2 | 939 | else: |
paul@2 | 940 | return (255, 255, 255) |
paul@2 | 941 | |
paul@11 | 942 | # Macro functions. |
paul@11 | 943 | |
paul@0 | 944 | def execute(macro, args): |
paul@0 | 945 | |
paul@0 | 946 | """ |
paul@0 | 947 | Execute the 'macro' with the given 'args': an optional list of selected |
paul@0 | 948 | category names (categories whose pages are to be shown), together with |
paul@5 | 949 | optional named arguments of the following forms: |
paul@5 | 950 | |
paul@5 | 951 | start=YYYY-MM shows event details starting from the specified month |
paul@122 | 952 | start=YYYY-MM-DD shows event details starting from the specified day |
paul@13 | 953 | start=current-N shows event details relative to the current month |
paul@122 | 954 | (or relative to the current day in "day" mode) |
paul@5 | 955 | end=YYYY-MM shows event details ending at the specified month |
paul@122 | 956 | end=YYYY-MM-DD shows event details ending on the specified day |
paul@13 | 957 | end=current+N shows event details relative to the current month |
paul@122 | 958 | (or relative to the current day in "day" mode) |
paul@5 | 959 | |
paul@5 | 960 | mode=calendar shows a calendar view of events |
paul@122 | 961 | mode=day shows a calendar day view of events |
paul@5 | 962 | mode=list shows a list of events by month |
paul@121 | 963 | mode=table shows a table of events |
paul@5 | 964 | |
paul@5 | 965 | names=daily shows the name of an event on every day of that event |
paul@5 | 966 | names=weekly shows the name of an event once per week |
paul@11 | 967 | |
paul@11 | 968 | calendar=NAME uses the given NAME to provide request parameters which |
paul@11 | 969 | can be used to control the calendar view |
paul@55 | 970 | |
paul@55 | 971 | template=PAGE uses the given PAGE as the default template for new |
paul@55 | 972 | events (or the default template from the configuration |
paul@55 | 973 | if not specified) |
paul@55 | 974 | |
paul@55 | 975 | parent=PAGE uses the given PAGE as the parent of any new event page |
paul@0 | 976 | """ |
paul@0 | 977 | |
paul@0 | 978 | request = macro.request |
paul@0 | 979 | fmt = macro.formatter |
paul@0 | 980 | page = fmt.page |
paul@0 | 981 | _ = request.getText |
paul@0 | 982 | |
paul@0 | 983 | # Interpret the arguments. |
paul@0 | 984 | |
paul@0 | 985 | try: |
paul@0 | 986 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@0 | 987 | except AttributeError: |
paul@0 | 988 | parsed_args = args.split(",") |
paul@0 | 989 | |
paul@0 | 990 | parsed_args = [arg for arg in parsed_args if arg] |
paul@0 | 991 | |
paul@0 | 992 | # Get special arguments. |
paul@0 | 993 | |
paul@0 | 994 | category_names = [] |
paul@76 | 995 | raw_calendar_start = None |
paul@76 | 996 | raw_calendar_end = None |
paul@0 | 997 | calendar_start = None |
paul@0 | 998 | calendar_end = None |
paul@57 | 999 | mode = None |
paul@35 | 1000 | name_usage = "weekly" |
paul@11 | 1001 | calendar_name = None |
paul@55 | 1002 | template_name = getattr(request.cfg, "event_aggregator_new_event_template", "EventTemplate") |
paul@55 | 1003 | parent_name = None |
paul@0 | 1004 | |
paul@0 | 1005 | for arg in parsed_args: |
paul@0 | 1006 | if arg.startswith("start="): |
paul@76 | 1007 | raw_calendar_start = arg[6:] |
paul@11 | 1008 | |
paul@0 | 1009 | elif arg.startswith("end="): |
paul@76 | 1010 | raw_calendar_end = arg[4:] |
paul@11 | 1011 | |
paul@0 | 1012 | elif arg.startswith("mode="): |
paul@0 | 1013 | mode = arg[5:] |
paul@11 | 1014 | |
paul@5 | 1015 | elif arg.startswith("names="): |
paul@5 | 1016 | name_usage = arg[6:] |
paul@11 | 1017 | |
paul@11 | 1018 | elif arg.startswith("calendar="): |
paul@11 | 1019 | calendar_name = arg[9:] |
paul@11 | 1020 | |
paul@55 | 1021 | elif arg.startswith("template="): |
paul@55 | 1022 | template_name = arg[9:] |
paul@55 | 1023 | |
paul@55 | 1024 | elif arg.startswith("parent="): |
paul@55 | 1025 | parent_name = arg[7:] |
paul@55 | 1026 | |
paul@0 | 1027 | else: |
paul@0 | 1028 | category_names.append(arg) |
paul@0 | 1029 | |
paul@11 | 1030 | # Find request parameters to override settings. |
paul@11 | 1031 | |
paul@58 | 1032 | mode = EventAggregatorSupport.getQualifiedParameter(request, calendar_name, "mode", mode or "calendar") |
paul@55 | 1033 | |
paul@126 | 1034 | # Different modes require different levels of precision. |
paul@126 | 1035 | |
paul@122 | 1036 | if mode == "day": |
paul@122 | 1037 | get_date = EventAggregatorSupport.getParameterDate |
paul@122 | 1038 | get_form_date = EventAggregatorSupport.getFormDate |
paul@122 | 1039 | else: |
paul@122 | 1040 | get_date = EventAggregatorSupport.getParameterMonth |
paul@122 | 1041 | get_form_date = EventAggregatorSupport.getFormMonth |
paul@122 | 1042 | |
paul@122 | 1043 | # Determine the limits of the calendar. |
paul@122 | 1044 | |
paul@122 | 1045 | calendar_start = get_date(raw_calendar_start) |
paul@122 | 1046 | calendar_end = get_date(raw_calendar_end) |
paul@122 | 1047 | |
paul@122 | 1048 | if calendar_name is not None: |
paul@122 | 1049 | calendar_start = get_form_date(request, calendar_name, "start") or calendar_start |
paul@122 | 1050 | calendar_end = get_form_date(request, calendar_name, "end") or calendar_end |
paul@122 | 1051 | |
paul@122 | 1052 | # Get the events according to the resolution of the calendar. |
paul@11 | 1053 | |
paul@10 | 1054 | events, shown_events, all_shown_events, earliest, latest = \ |
paul@122 | 1055 | EventAggregatorSupport.getEvents(request, category_names, calendar_start, calendar_end, |
paul@122 | 1056 | mode == "day" and "date" or "month") |
paul@2 | 1057 | |
paul@13 | 1058 | # Get a concrete period of time. |
paul@13 | 1059 | |
paul@13 | 1060 | first, last = EventAggregatorSupport.getConcretePeriod(calendar_start, calendar_end, earliest, latest) |
paul@13 | 1061 | |
paul@37 | 1062 | # Define a view of the calendar, retaining useful navigational information. |
paul@13 | 1063 | |
paul@76 | 1064 | view = View(page, calendar_name, raw_calendar_start, raw_calendar_end, calendar_start, calendar_end, |
paul@120 | 1065 | first, last, category_names, template_name, parent_name, mode, name_usage) |
paul@13 | 1066 | |
paul@0 | 1067 | # Make a calendar. |
paul@0 | 1068 | |
paul@0 | 1069 | output = [] |
paul@0 | 1070 | |
paul@26 | 1071 | # Output download controls. |
paul@26 | 1072 | |
paul@26 | 1073 | output.append(fmt.div(on=1, css_class="event-controls")) |
paul@57 | 1074 | output.append(view.writeDownloadControls()) |
paul@26 | 1075 | output.append(fmt.div(on=0)) |
paul@26 | 1076 | |
paul@61 | 1077 | # Output a table. |
paul@51 | 1078 | |
paul@61 | 1079 | if mode == "table": |
paul@0 | 1080 | |
paul@61 | 1081 | # Start of table view output. |
paul@51 | 1082 | |
paul@51 | 1083 | output.append(fmt.table(on=1, attrs={"tableclass" : "event-table"})) |
paul@51 | 1084 | |
paul@51 | 1085 | output.append(fmt.table_row(on=1)) |
paul@51 | 1086 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@51 | 1087 | output.append(fmt.text(_("Event dates"))) |
paul@51 | 1088 | output.append(fmt.table_cell(on=0)) |
paul@51 | 1089 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@51 | 1090 | output.append(fmt.text(_("Event location"))) |
paul@51 | 1091 | output.append(fmt.table_cell(on=0)) |
paul@51 | 1092 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@51 | 1093 | output.append(fmt.text(_("Event details"))) |
paul@51 | 1094 | output.append(fmt.table_cell(on=0)) |
paul@51 | 1095 | output.append(fmt.table_row(on=0)) |
paul@51 | 1096 | |
paul@61 | 1097 | # Get the events in order. |
paul@0 | 1098 | |
paul@61 | 1099 | ordered_events = EventAggregatorSupport.getOrderedEvents(all_shown_events) |
paul@0 | 1100 | |
paul@61 | 1101 | # Show the events in order. |
paul@2 | 1102 | |
paul@69 | 1103 | for event in ordered_events: |
paul@69 | 1104 | event_page = event.getPage() |
paul@69 | 1105 | event_summary = event.getSummary(parent_name) |
paul@69 | 1106 | event_details = event.getDetails() |
paul@0 | 1107 | |
paul@61 | 1108 | # Prepare CSS classes with category-related styling. |
paul@11 | 1109 | |
paul@61 | 1110 | css_classes = ["event-table-details"] |
paul@61 | 1111 | |
paul@61 | 1112 | for topic in event_details.get("topics") or event_details.get("categories") or []: |
paul@11 | 1113 | |
paul@61 | 1114 | # Filter the category text to avoid illegal characters. |
paul@11 | 1115 | |
paul@61 | 1116 | css_classes.append("event-table-category-%s" % "".join(filter(lambda c: c.isalnum(), topic))) |
paul@0 | 1117 | |
paul@61 | 1118 | attrs = {"class" : " ".join(css_classes)} |
paul@0 | 1119 | |
paul@2 | 1120 | output.append(fmt.table_row(on=1)) |
paul@0 | 1121 | |
paul@61 | 1122 | # Start and end dates. |
paul@61 | 1123 | |
paul@61 | 1124 | output.append(fmt.table_cell(on=1, attrs=attrs)) |
paul@61 | 1125 | output.append(fmt.span(on=1)) |
paul@67 | 1126 | output.append(fmt.text(str(event_details["start"]))) |
paul@61 | 1127 | output.append(fmt.span(on=0)) |
paul@61 | 1128 | |
paul@61 | 1129 | if event_details["start"] != event_details["end"]: |
paul@61 | 1130 | output.append(fmt.text(" - ")) |
paul@61 | 1131 | output.append(fmt.span(on=1)) |
paul@67 | 1132 | output.append(fmt.text(str(event_details["end"]))) |
paul@61 | 1133 | output.append(fmt.span(on=0)) |
paul@61 | 1134 | |
paul@61 | 1135 | output.append(fmt.table_cell(on=0)) |
paul@61 | 1136 | |
paul@61 | 1137 | # Location. |
paul@61 | 1138 | |
paul@61 | 1139 | output.append(fmt.table_cell(on=1, attrs=attrs)) |
paul@61 | 1140 | |
paul@61 | 1141 | if event_details.has_key("location"): |
paul@61 | 1142 | output.append(fmt.text(event_details["location"])) |
paul@61 | 1143 | |
paul@61 | 1144 | output.append(fmt.table_cell(on=0)) |
paul@61 | 1145 | |
paul@61 | 1146 | # Link to the page using the summary. |
paul@61 | 1147 | |
paul@61 | 1148 | output.append(fmt.table_cell(on=1, attrs=attrs)) |
paul@67 | 1149 | output.append(event_page.linkToPage(request, event_summary)) |
paul@61 | 1150 | output.append(fmt.table_cell(on=0)) |
paul@2 | 1151 | |
paul@2 | 1152 | output.append(fmt.table_row(on=0)) |
paul@2 | 1153 | |
paul@61 | 1154 | # End of table view output. |
paul@61 | 1155 | |
paul@61 | 1156 | output.append(fmt.table(on=0)) |
paul@2 | 1157 | |
paul@122 | 1158 | # Output a list or month calendar. |
paul@61 | 1159 | |
paul@61 | 1160 | elif mode in ("list", "calendar"): |
paul@0 | 1161 | |
paul@61 | 1162 | # Output top-level information. |
paul@61 | 1163 | |
paul@61 | 1164 | # Start of list view output. |
paul@2 | 1165 | |
paul@61 | 1166 | if mode == "list": |
paul@61 | 1167 | output.append(fmt.bullet_list(on=1, attr={"class" : "event-listings"})) |
paul@61 | 1168 | |
paul@61 | 1169 | # Visit all months in the requested range, or across known events. |
paul@61 | 1170 | |
paul@67 | 1171 | for month in first.months_until(last): |
paul@2 | 1172 | |
paul@61 | 1173 | # Either output a calendar view... |
paul@61 | 1174 | |
paul@61 | 1175 | if mode == "calendar": |
paul@2 | 1176 | |
paul@61 | 1177 | # Output a month. |
paul@61 | 1178 | |
paul@61 | 1179 | output.append(fmt.table(on=1, attrs={"tableclass" : "event-month"})) |
paul@5 | 1180 | |
paul@61 | 1181 | # Either write a month heading or produce links for navigable |
paul@61 | 1182 | # calendars. |
paul@5 | 1183 | |
paul@122 | 1184 | output.append(view.writeMonthTableHeading(month)) |
paul@61 | 1185 | |
paul@61 | 1186 | # Weekday headings. |
paul@0 | 1187 | |
paul@122 | 1188 | output.append(view.writeWeekdayHeadings()) |
paul@5 | 1189 | |
paul@61 | 1190 | # Process the days of the month. |
paul@61 | 1191 | |
paul@67 | 1192 | start_weekday, number_of_days = month.month_properties() |
paul@61 | 1193 | |
paul@61 | 1194 | # The start weekday is the weekday of day number 1. |
paul@61 | 1195 | # Find the first day of the week, counting from below zero, if |
paul@61 | 1196 | # necessary, in order to land on the first day of the month as |
paul@61 | 1197 | # day number 1. |
paul@61 | 1198 | |
paul@61 | 1199 | first_day = 1 - start_weekday |
paul@5 | 1200 | |
paul@61 | 1201 | while first_day <= number_of_days: |
paul@61 | 1202 | |
paul@61 | 1203 | # Find events in this week and determine how to mark them on the |
paul@61 | 1204 | # calendar. |
paul@61 | 1205 | |
paul@67 | 1206 | week_start = month.as_date(max(first_day, 1)) |
paul@67 | 1207 | week_end = month.as_date(min(first_day + 6, number_of_days)) |
paul@61 | 1208 | |
paul@118 | 1209 | full_coverage, week_slots = EventAggregatorSupport.getCoverage( |
paul@67 | 1210 | week_start, week_end, shown_events.get(month, [])) |
paul@61 | 1211 | |
paul@61 | 1212 | # Output a week, starting with the day numbers. |
paul@61 | 1213 | |
paul@120 | 1214 | output.append(view.writeDayNumbers(first_day, number_of_days, month, full_coverage)) |
paul@7 | 1215 | |
paul@61 | 1216 | # Either generate empty days... |
paul@7 | 1217 | |
paul@118 | 1218 | if not week_slots: |
paul@120 | 1219 | output.append(view.writeEmptyWeek(first_day, number_of_days)) |
paul@7 | 1220 | |
paul@120 | 1221 | # Or generate each set of scheduled events... |
paul@61 | 1222 | |
paul@61 | 1223 | else: |
paul@120 | 1224 | output.append(view.writeWeekSlots(first_day, number_of_days, month, week_end, week_slots)) |
paul@25 | 1225 | |
paul@61 | 1226 | # Process the next week... |
paul@25 | 1227 | |
paul@61 | 1228 | first_day += 7 |
paul@25 | 1229 | |
paul@61 | 1230 | # End of month. |
paul@0 | 1231 | |
paul@61 | 1232 | output.append(fmt.table(on=0)) |
paul@0 | 1233 | |
paul@61 | 1234 | # Or output a summary view... |
paul@25 | 1235 | |
paul@61 | 1236 | elif mode == "list": |
paul@0 | 1237 | |
paul@61 | 1238 | # Output a list. |
paul@0 | 1239 | |
paul@61 | 1240 | output.append(fmt.listitem(on=1, attr={"class" : "event-listings-month"})) |
paul@61 | 1241 | output.append(fmt.div(on=1, attr={"class" : "event-listings-month-heading"})) |
paul@0 | 1242 | |
paul@61 | 1243 | # Either write a month heading or produce links for navigable |
paul@61 | 1244 | # calendars. |
paul@25 | 1245 | |
paul@67 | 1246 | output.append(view.writeMonthHeading(month)) |
paul@0 | 1247 | |
paul@61 | 1248 | output.append(fmt.div(on=0)) |
paul@25 | 1249 | |
paul@61 | 1250 | output.append(fmt.bullet_list(on=1, attr={"class" : "event-month-listings"})) |
paul@25 | 1251 | |
paul@61 | 1252 | # Get the events in order. |
paul@0 | 1253 | |
paul@67 | 1254 | ordered_events = EventAggregatorSupport.getOrderedEvents(shown_events.get(month, [])) |
paul@51 | 1255 | |
paul@61 | 1256 | # Show the events in order. |
paul@51 | 1257 | |
paul@69 | 1258 | for event in ordered_events: |
paul@69 | 1259 | event_page = event.getPage() |
paul@69 | 1260 | event_details = event.getDetails() |
paul@69 | 1261 | event_summary = event.getSummary(parent_name) |
paul@51 | 1262 | |
paul@61 | 1263 | output.append(fmt.listitem(on=1, attr={"class" : "event-listing"})) |
paul@51 | 1264 | |
paul@61 | 1265 | # Link to the page using the summary. |
paul@51 | 1266 | |
paul@61 | 1267 | output.append(fmt.paragraph(on=1)) |
paul@67 | 1268 | output.append(event_page.linkToPage(request, event_summary)) |
paul@61 | 1269 | output.append(fmt.paragraph(on=0)) |
paul@51 | 1270 | |
paul@61 | 1271 | # Start and end dates. |
paul@51 | 1272 | |
paul@61 | 1273 | output.append(fmt.paragraph(on=1)) |
paul@61 | 1274 | output.append(fmt.span(on=1)) |
paul@67 | 1275 | output.append(fmt.text(str(event_details["start"]))) |
paul@61 | 1276 | output.append(fmt.span(on=0)) |
paul@52 | 1277 | output.append(fmt.text(" - ")) |
paul@52 | 1278 | output.append(fmt.span(on=1)) |
paul@67 | 1279 | output.append(fmt.text(str(event_details["end"]))) |
paul@52 | 1280 | output.append(fmt.span(on=0)) |
paul@61 | 1281 | output.append(fmt.paragraph(on=0)) |
paul@51 | 1282 | |
paul@61 | 1283 | # Location. |
paul@51 | 1284 | |
paul@61 | 1285 | if event_details.has_key("location"): |
paul@61 | 1286 | output.append(fmt.paragraph(on=1)) |
paul@61 | 1287 | output.append(fmt.text(event_details["location"])) |
paul@61 | 1288 | output.append(fmt.paragraph(on=1)) |
paul@51 | 1289 | |
paul@61 | 1290 | # Topics. |
paul@51 | 1291 | |
paul@61 | 1292 | if event_details.has_key("topics") or event_details.has_key("categories"): |
paul@61 | 1293 | output.append(fmt.bullet_list(on=1, attr={"class" : "event-topics"})) |
paul@51 | 1294 | |
paul@61 | 1295 | for topic in event_details.get("topics") or event_details.get("categories") or []: |
paul@61 | 1296 | output.append(fmt.listitem(on=1)) |
paul@61 | 1297 | output.append(fmt.text(topic)) |
paul@61 | 1298 | output.append(fmt.listitem(on=0)) |
paul@51 | 1299 | |
paul@61 | 1300 | output.append(fmt.bullet_list(on=0)) |
paul@51 | 1301 | |
paul@61 | 1302 | output.append(fmt.listitem(on=0)) |
paul@9 | 1303 | |
paul@61 | 1304 | output.append(fmt.bullet_list(on=0)) |
paul@9 | 1305 | |
paul@61 | 1306 | # Output top-level information. |
paul@0 | 1307 | |
paul@61 | 1308 | # End of list view output. |
paul@51 | 1309 | |
paul@61 | 1310 | if mode == "list": |
paul@61 | 1311 | output.append(fmt.bullet_list(on=0)) |
paul@51 | 1312 | |
paul@122 | 1313 | # Output a day view. |
paul@122 | 1314 | |
paul@122 | 1315 | elif mode == "day": |
paul@122 | 1316 | |
paul@122 | 1317 | # Visit all days in the requested range, or across known events. |
paul@122 | 1318 | |
paul@122 | 1319 | for date in first.days_until(last): |
paul@122 | 1320 | |
paul@122 | 1321 | output.append(fmt.table(on=1, attrs={"tableclass" : "event-calendar-day"})) |
paul@122 | 1322 | |
paul@122 | 1323 | full_coverage, day_slots = EventAggregatorSupport.getCoverage( |
paul@122 | 1324 | date, date, shown_events.get(date, [])) |
paul@122 | 1325 | |
paul@126 | 1326 | # Work out how many columns the day title will need. |
paul@126 | 1327 | # Includer spacers before each event column. |
paul@126 | 1328 | |
paul@126 | 1329 | colspan = sum(map(len, day_slots.values())) * 2 + 1 |
paul@126 | 1330 | |
paul@126 | 1331 | output.append(view.writeDayHeading(date, colspan)) |
paul@123 | 1332 | |
paul@123 | 1333 | # Either generate empty days... |
paul@123 | 1334 | |
paul@123 | 1335 | if not day_slots: |
paul@123 | 1336 | output.append(view.writeEmptyDay(date)) |
paul@123 | 1337 | |
paul@123 | 1338 | # Or generate each set of scheduled events... |
paul@123 | 1339 | |
paul@123 | 1340 | else: |
paul@123 | 1341 | output.append(view.writeDaySlots(date, full_coverage, day_slots)) |
paul@122 | 1342 | |
paul@122 | 1343 | # End of day. |
paul@122 | 1344 | |
paul@122 | 1345 | output.append(fmt.table(on=0)) |
paul@122 | 1346 | |
paul@57 | 1347 | # Output view controls. |
paul@57 | 1348 | |
paul@57 | 1349 | output.append(fmt.div(on=1, css_class="event-controls")) |
paul@57 | 1350 | output.append(view.writeViewControls()) |
paul@57 | 1351 | output.append(fmt.div(on=0)) |
paul@57 | 1352 | |
paul@0 | 1353 | return ''.join(output) |
paul@0 | 1354 | |
paul@0 | 1355 | # vim: tabstop=4 expandtab shiftwidth=4 |