paul@10 | 1 | # -*- coding: iso-8859-1 -*- |
paul@10 | 2 | """ |
paul@10 | 3 | MoinMoin - EventAggregator library |
paul@10 | 4 | |
paul@278 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@10 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@10 | 7 | 2005-2008 MoinMoin:ThomasWaldmann. |
paul@10 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@10 | 9 | """ |
paul@10 | 10 | |
paul@323 | 11 | from GeneralSupport import * |
paul@277 | 12 | from LocationSupport import * |
paul@277 | 13 | from MoinDateSupport import * |
paul@312 | 14 | from MoinRemoteSupport import * |
paul@277 | 15 | from MoinSupport import * |
paul@315 | 16 | from ViewSupport import * |
paul@277 | 17 | |
paul@10 | 18 | from MoinMoin.Page import Page |
paul@312 | 19 | from MoinMoin.action import AttachFile |
paul@24 | 20 | from MoinMoin import wikiutil |
paul@277 | 21 | |
paul@217 | 22 | import codecs |
paul@10 | 23 | import re |
paul@312 | 24 | import urllib |
paul@10 | 25 | |
paul@69 | 26 | try: |
paul@225 | 27 | from cStringIO import StringIO |
paul@225 | 28 | except ImportError: |
paul@225 | 29 | from StringIO import StringIO |
paul@10 | 30 | |
paul@69 | 31 | try: |
paul@69 | 32 | set |
paul@69 | 33 | except NameError: |
paul@69 | 34 | from sets import Set as set |
paul@69 | 35 | |
paul@100 | 36 | try: |
paul@217 | 37 | import vCalendar |
paul@217 | 38 | except ImportError: |
paul@217 | 39 | vCalendar = None |
paul@217 | 40 | |
paul@137 | 41 | escape = wikiutil.escape |
paul@137 | 42 | |
paul@277 | 43 | __version__ = "0.9" |
paul@22 | 44 | |
paul@47 | 45 | # Page parsing. |
paul@47 | 46 | |
paul@84 | 47 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?):: )(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@158 | 48 | category_membership_regexp = re.compile(ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$", re.MULTILINE | re.UNICODE) |
paul@47 | 49 | |
paul@47 | 50 | # Value parsing. |
paul@47 | 51 | |
paul@93 | 52 | country_code_regexp = re.compile(ur'(?:^|\W)(?P<code>[A-Z]{2})(?:$|\W+$)', re.UNICODE) |
paul@219 | 53 | |
paul@10 | 54 | # Utility functions. |
paul@10 | 55 | |
paul@303 | 56 | def getLocationPosition(location, locations): |
paul@303 | 57 | |
paul@303 | 58 | """ |
paul@303 | 59 | Attempt to return the position of the given 'location' using the 'locations' |
paul@303 | 60 | dictionary provided. If no position can be found, return a latitude of None |
paul@303 | 61 | and a longitude of None. |
paul@303 | 62 | """ |
paul@303 | 63 | |
paul@303 | 64 | latitude, longitude = None, None |
paul@303 | 65 | |
paul@303 | 66 | if location is not None: |
paul@303 | 67 | try: |
paul@303 | 68 | latitude, longitude = map(getMapReference, locations[location].split()) |
paul@303 | 69 | except (KeyError, ValueError): |
paul@303 | 70 | pass |
paul@303 | 71 | |
paul@303 | 72 | return latitude, longitude |
paul@303 | 73 | |
paul@109 | 74 | # Utility classes and associated functions. |
paul@109 | 75 | |
paul@277 | 76 | class ActionSupport(ActionSupport): |
paul@109 | 77 | |
paul@277 | 78 | "Extend the generic action support." |
paul@112 | 79 | |
paul@114 | 80 | def get_month_lists(self, default_as_current=0): |
paul@112 | 81 | |
paul@112 | 82 | """ |
paul@112 | 83 | Return two lists of HTML element definitions corresponding to the start |
paul@112 | 84 | and end month selection controls, with months selected according to any |
paul@112 | 85 | values that have been specified via request parameters. |
paul@112 | 86 | """ |
paul@112 | 87 | |
paul@112 | 88 | _ = self._ |
paul@112 | 89 | form = self.get_form() |
paul@112 | 90 | |
paul@112 | 91 | # Initialise month lists. |
paul@112 | 92 | |
paul@112 | 93 | start_month_list = [] |
paul@112 | 94 | end_month_list = [] |
paul@112 | 95 | |
paul@114 | 96 | start_month = self._get_input(form, "start-month", default_as_current and getCurrentMonth().month() or None) |
paul@112 | 97 | end_month = self._get_input(form, "end-month", start_month) |
paul@112 | 98 | |
paul@112 | 99 | # Prepare month lists, selecting specified months. |
paul@112 | 100 | |
paul@114 | 101 | if not default_as_current: |
paul@114 | 102 | start_month_list.append('<option value=""></option>') |
paul@114 | 103 | end_month_list.append('<option value=""></option>') |
paul@114 | 104 | |
paul@112 | 105 | for month in range(1, 13): |
paul@136 | 106 | month_label = escape(_(getMonthLabel(month))) |
paul@112 | 107 | selected = self._get_selected(month, start_month) |
paul@112 | 108 | start_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@112 | 109 | selected = self._get_selected(month, end_month) |
paul@112 | 110 | end_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@112 | 111 | |
paul@112 | 112 | return start_month_list, end_month_list |
paul@112 | 113 | |
paul@114 | 114 | def get_year_defaults(self, default_as_current=0): |
paul@112 | 115 | |
paul@112 | 116 | "Return defaults for the start and end years." |
paul@112 | 117 | |
paul@112 | 118 | form = self.get_form() |
paul@112 | 119 | |
paul@114 | 120 | start_year_default = form.get("start-year", [default_as_current and getCurrentYear() or ""])[0] |
paul@114 | 121 | end_year_default = form.get("end-year", [default_as_current and start_year_default or ""])[0] |
paul@112 | 122 | |
paul@112 | 123 | return start_year_default, end_year_default |
paul@112 | 124 | |
paul@127 | 125 | def get_day_defaults(self, default_as_current=0): |
paul@127 | 126 | |
paul@127 | 127 | "Return defaults for the start and end days." |
paul@127 | 128 | |
paul@127 | 129 | form = self.get_form() |
paul@127 | 130 | |
paul@127 | 131 | start_day_default = form.get("start-day", [default_as_current and getCurrentDate().day() or ""])[0] |
paul@127 | 132 | end_day_default = form.get("end-day", [default_as_current and start_day_default or ""])[0] |
paul@127 | 133 | |
paul@127 | 134 | return start_day_default, end_day_default |
paul@127 | 135 | |
paul@307 | 136 | # Event parsing from page texts. |
paul@307 | 137 | |
paul@323 | 138 | def parseEvents(text, event_page, fragment=None): |
paul@307 | 139 | |
paul@307 | 140 | """ |
paul@307 | 141 | Parse events in the given 'text', returning a list of event objects for the |
paul@323 | 142 | given 'event_page'. An optional 'fragment' can be specified to indicate a |
paul@323 | 143 | specific region of the event page. |
paul@307 | 144 | """ |
paul@307 | 145 | |
paul@323 | 146 | template_details = {} |
paul@323 | 147 | if fragment: |
paul@323 | 148 | template_details["fragment"] = fragment |
paul@323 | 149 | |
paul@307 | 150 | details = {} |
paul@323 | 151 | details.update(template_details) |
paul@310 | 152 | events = [Event(event_page, details)] |
paul@307 | 153 | |
paul@307 | 154 | for match in definition_list_regexp.finditer(text): |
paul@307 | 155 | |
paul@307 | 156 | # Skip commented-out items. |
paul@307 | 157 | |
paul@307 | 158 | if match.group("optcomment"): |
paul@307 | 159 | continue |
paul@307 | 160 | |
paul@307 | 161 | # Permit case-insensitive list terms. |
paul@307 | 162 | |
paul@307 | 163 | term = match.group("term").lower() |
paul@307 | 164 | desc = match.group("desc") |
paul@307 | 165 | |
paul@307 | 166 | # Special value type handling. |
paul@307 | 167 | |
paul@307 | 168 | # Dates. |
paul@307 | 169 | |
paul@323 | 170 | if term in Event.date_terms: |
paul@307 | 171 | desc = getDateTime(desc) |
paul@307 | 172 | |
paul@307 | 173 | # Lists (whose elements may be quoted). |
paul@307 | 174 | |
paul@323 | 175 | elif term in Event.list_terms: |
paul@307 | 176 | desc = map(getSimpleWikiText, to_list(desc, ",")) |
paul@307 | 177 | |
paul@307 | 178 | # Position details. |
paul@307 | 179 | |
paul@307 | 180 | elif term == "geo": |
paul@307 | 181 | try: |
paul@307 | 182 | desc = map(getMapReference, to_list(desc, None)) |
paul@307 | 183 | if len(desc) != 2: |
paul@307 | 184 | continue |
paul@307 | 185 | except (KeyError, ValueError): |
paul@307 | 186 | continue |
paul@307 | 187 | |
paul@307 | 188 | # Labels which may well be quoted. |
paul@307 | 189 | |
paul@323 | 190 | elif term in Event.title_terms: |
paul@307 | 191 | desc = getSimpleWikiText(desc.strip()) |
paul@307 | 192 | |
paul@323 | 193 | # Plain Wiki text terms. |
paul@323 | 194 | |
paul@323 | 195 | elif term in Event.other_terms: |
paul@323 | 196 | desc = desc.strip() |
paul@323 | 197 | |
paul@307 | 198 | if desc is not None: |
paul@307 | 199 | |
paul@307 | 200 | # Handle apparent duplicates by creating a new set of |
paul@307 | 201 | # details. |
paul@307 | 202 | |
paul@307 | 203 | if details.has_key(term): |
paul@307 | 204 | |
paul@307 | 205 | # Make a new event. |
paul@307 | 206 | |
paul@307 | 207 | details = {} |
paul@323 | 208 | details.update(template_details) |
paul@310 | 209 | events.append(Event(event_page, details)) |
paul@307 | 210 | |
paul@307 | 211 | details[term] = desc |
paul@307 | 212 | |
paul@307 | 213 | return events |
paul@307 | 214 | |
paul@217 | 215 | # Event resources providing collections of events. |
paul@217 | 216 | |
paul@217 | 217 | class EventResource: |
paul@217 | 218 | |
paul@217 | 219 | "A resource providing event information." |
paul@217 | 220 | |
paul@217 | 221 | def __init__(self, url): |
paul@217 | 222 | self.url = url |
paul@217 | 223 | |
paul@223 | 224 | def getPageURL(self): |
paul@223 | 225 | |
paul@223 | 226 | "Return the URL of this page." |
paul@217 | 227 | |
paul@217 | 228 | return self.url |
paul@217 | 229 | |
paul@217 | 230 | def getFormat(self): |
paul@217 | 231 | |
paul@219 | 232 | "Get the format used by this resource." |
paul@217 | 233 | |
paul@217 | 234 | return "plain" |
paul@217 | 235 | |
paul@219 | 236 | def getMetadata(self): |
paul@219 | 237 | |
paul@219 | 238 | """ |
paul@219 | 239 | Return a dictionary containing items describing the page's "created" |
paul@219 | 240 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@219 | 241 | "last-comment" made about the last edit. |
paul@219 | 242 | """ |
paul@219 | 243 | |
paul@219 | 244 | return {} |
paul@219 | 245 | |
paul@217 | 246 | def getEvents(self): |
paul@217 | 247 | |
paul@217 | 248 | "Return a list of events from this resource." |
paul@217 | 249 | |
paul@217 | 250 | return [] |
paul@217 | 251 | |
paul@323 | 252 | def linkToPage(self, request, text, query_string=None, anchor=None): |
paul@217 | 253 | |
paul@217 | 254 | """ |
paul@217 | 255 | Using 'request', return a link to this page with the given link 'text' |
paul@323 | 256 | and optional 'query_string' and 'anchor'. |
paul@217 | 257 | """ |
paul@217 | 258 | |
paul@323 | 259 | return linkToResource(self.url, request, text, query_string, anchor) |
paul@217 | 260 | |
paul@217 | 261 | # Formatting-related functions. |
paul@217 | 262 | |
paul@284 | 263 | def formatText(self, text, fmt): |
paul@217 | 264 | |
paul@217 | 265 | """ |
paul@284 | 266 | Format the given 'text' using the specified formatter 'fmt'. |
paul@217 | 267 | """ |
paul@217 | 268 | |
paul@217 | 269 | # Assume plain text which is then formatted appropriately. |
paul@217 | 270 | |
paul@217 | 271 | return fmt.text(text) |
paul@217 | 272 | |
paul@217 | 273 | class EventCalendar(EventResource): |
paul@217 | 274 | |
paul@217 | 275 | "An iCalendar resource." |
paul@217 | 276 | |
paul@217 | 277 | def __init__(self, url, calendar): |
paul@217 | 278 | EventResource.__init__(self, url) |
paul@217 | 279 | self.calendar = calendar |
paul@217 | 280 | self.events = None |
paul@217 | 281 | |
paul@217 | 282 | def getEvents(self): |
paul@217 | 283 | |
paul@217 | 284 | "Return a list of events from this resource." |
paul@217 | 285 | |
paul@217 | 286 | if self.events is None: |
paul@217 | 287 | self.events = [] |
paul@217 | 288 | |
paul@217 | 289 | _calendar, _empty, calendar = self.calendar |
paul@217 | 290 | |
paul@217 | 291 | for objtype, attrs, obj in calendar: |
paul@217 | 292 | |
paul@217 | 293 | # Read events. |
paul@217 | 294 | |
paul@217 | 295 | if objtype == "VEVENT": |
paul@217 | 296 | details = {} |
paul@217 | 297 | |
paul@217 | 298 | for property, attrs, value in obj: |
paul@217 | 299 | |
paul@217 | 300 | # Convert dates. |
paul@217 | 301 | |
paul@219 | 302 | if property in ("DTSTART", "DTEND", "CREATED", "DTSTAMP", "LAST-MODIFIED"): |
paul@219 | 303 | if property in ("DTSTART", "DTEND"): |
paul@219 | 304 | property = property[2:] |
paul@217 | 305 | if attrs.get("VALUE") == "DATE": |
paul@217 | 306 | value = getDateFromCalendar(value) |
paul@287 | 307 | if value and property == "END": |
paul@287 | 308 | value = value.previous_day() |
paul@217 | 309 | else: |
paul@217 | 310 | value = getDateTimeFromCalendar(value) |
paul@217 | 311 | |
paul@219 | 312 | # Convert numeric data. |
paul@219 | 313 | |
paul@219 | 314 | elif property == "SEQUENCE": |
paul@219 | 315 | value = int(value) |
paul@219 | 316 | |
paul@219 | 317 | # Convert lists. |
paul@219 | 318 | |
paul@219 | 319 | elif property == "CATEGORIES": |
paul@229 | 320 | value = to_list(value, ",") |
paul@229 | 321 | |
paul@229 | 322 | # Convert positions (using decimal values). |
paul@229 | 323 | |
paul@229 | 324 | elif property == "GEO": |
paul@237 | 325 | try: |
paul@237 | 326 | value = map(getMapReferenceFromDecimal, to_list(value, ";")) |
paul@246 | 327 | if len(value) != 2: |
paul@246 | 328 | continue |
paul@237 | 329 | except (KeyError, ValueError): |
paul@237 | 330 | continue |
paul@219 | 331 | |
paul@217 | 332 | # Accept other textual data as it is. |
paul@217 | 333 | |
paul@219 | 334 | elif property in ("LOCATION", "SUMMARY", "URL"): |
paul@257 | 335 | value = value or None |
paul@219 | 336 | |
paul@219 | 337 | # Ignore other properties. |
paul@217 | 338 | |
paul@217 | 339 | else: |
paul@217 | 340 | continue |
paul@217 | 341 | |
paul@219 | 342 | property = property.lower() |
paul@217 | 343 | details[property] = value |
paul@217 | 344 | |
paul@219 | 345 | self.events.append(CalendarEvent(self, details)) |
paul@217 | 346 | |
paul@217 | 347 | return self.events |
paul@67 | 348 | |
paul@67 | 349 | class EventPage: |
paul@67 | 350 | |
paul@229 | 351 | "An event page acting as an event resource." |
paul@67 | 352 | |
paul@67 | 353 | def __init__(self, page): |
paul@67 | 354 | self.page = page |
paul@69 | 355 | self.events = None |
paul@67 | 356 | self.body = None |
paul@67 | 357 | self.categories = None |
paul@219 | 358 | self.metadata = None |
paul@67 | 359 | |
paul@67 | 360 | def copyPage(self, page): |
paul@67 | 361 | |
paul@67 | 362 | "Copy the body of the given 'page'." |
paul@67 | 363 | |
paul@67 | 364 | self.body = page.getBody() |
paul@67 | 365 | |
paul@223 | 366 | def getPageURL(self): |
paul@223 | 367 | |
paul@223 | 368 | "Return the URL of this page." |
paul@223 | 369 | |
paul@280 | 370 | return getPageURL(self.page) |
paul@67 | 371 | |
paul@67 | 372 | def getFormat(self): |
paul@67 | 373 | |
paul@67 | 374 | "Get the format used on this page." |
paul@24 | 375 | |
paul@280 | 376 | return getFormat(self.page) |
paul@67 | 377 | |
paul@219 | 378 | def getMetadata(self): |
paul@219 | 379 | |
paul@219 | 380 | """ |
paul@219 | 381 | Return a dictionary containing items describing the page's "created" |
paul@219 | 382 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@219 | 383 | "last-comment" made about the last edit. |
paul@219 | 384 | """ |
paul@219 | 385 | |
paul@219 | 386 | if self.metadata is None: |
paul@280 | 387 | self.metadata = getMetadata(self.page) |
paul@219 | 388 | return self.metadata |
paul@219 | 389 | |
paul@67 | 390 | def getRevisions(self): |
paul@67 | 391 | |
paul@67 | 392 | "Return a list of page revisions." |
paul@67 | 393 | |
paul@67 | 394 | return self.page.getRevList() |
paul@67 | 395 | |
paul@67 | 396 | def getPageRevision(self): |
paul@24 | 397 | |
paul@67 | 398 | "Return the revision details dictionary for this page." |
paul@67 | 399 | |
paul@67 | 400 | return getPageRevision(self.page) |
paul@67 | 401 | |
paul@67 | 402 | def getPageName(self): |
paul@67 | 403 | |
paul@67 | 404 | "Return the page name." |
paul@67 | 405 | |
paul@67 | 406 | return self.page.page_name |
paul@24 | 407 | |
paul@67 | 408 | def getPrettyPageName(self): |
paul@67 | 409 | |
paul@67 | 410 | "Return a nicely formatted title/name for this page." |
paul@67 | 411 | |
paul@67 | 412 | return getPrettyPageName(self.page) |
paul@67 | 413 | |
paul@67 | 414 | def getBody(self): |
paul@67 | 415 | |
paul@67 | 416 | "Get the current page body." |
paul@47 | 417 | |
paul@67 | 418 | if self.body is None: |
paul@67 | 419 | self.body = self.page.get_raw_body() |
paul@67 | 420 | return self.body |
paul@67 | 421 | |
paul@69 | 422 | def getEvents(self): |
paul@69 | 423 | |
paul@69 | 424 | "Return a list of events from this page." |
paul@67 | 425 | |
paul@69 | 426 | if self.events is None: |
paul@323 | 427 | self.events = [] |
paul@67 | 428 | if self.getFormat() == "wiki": |
paul@323 | 429 | for format, attributes, region in getFragments(self.getBody(), True): |
paul@323 | 430 | self.events += parseEvents(region, self, attributes.get("fragment")) |
paul@69 | 431 | |
paul@69 | 432 | return self.events |
paul@69 | 433 | |
paul@69 | 434 | def setEvents(self, events): |
paul@69 | 435 | |
paul@69 | 436 | "Set the given 'events' on this page." |
paul@69 | 437 | |
paul@69 | 438 | self.events = events |
paul@67 | 439 | |
paul@67 | 440 | def getCategoryMembership(self): |
paul@27 | 441 | |
paul@67 | 442 | "Get the category names from this page." |
paul@67 | 443 | |
paul@67 | 444 | if self.categories is None: |
paul@67 | 445 | body = self.getBody() |
paul@67 | 446 | match = category_membership_regexp.search(body) |
paul@158 | 447 | self.categories = match and [x for x in match.groups() if x] or [] |
paul@67 | 448 | |
paul@67 | 449 | return self.categories |
paul@67 | 450 | |
paul@67 | 451 | def setCategoryMembership(self, category_names): |
paul@10 | 452 | |
paul@67 | 453 | """ |
paul@67 | 454 | Set the category membership for the page using the specified |
paul@67 | 455 | 'category_names'. |
paul@67 | 456 | """ |
paul@67 | 457 | |
paul@67 | 458 | self.categories = category_names |
paul@67 | 459 | |
paul@67 | 460 | def flushEventDetails(self): |
paul@67 | 461 | |
paul@67 | 462 | "Flush the current event details to this page's body text." |
paul@10 | 463 | |
paul@67 | 464 | new_body_parts = [] |
paul@67 | 465 | end_of_last_match = 0 |
paul@67 | 466 | body = self.getBody() |
paul@69 | 467 | |
paul@69 | 468 | events = iter(self.getEvents()) |
paul@69 | 469 | |
paul@69 | 470 | event = events.next() |
paul@69 | 471 | event_details = event.getDetails() |
paul@69 | 472 | replaced_terms = set() |
paul@67 | 473 | |
paul@67 | 474 | for match in definition_list_regexp.finditer(body): |
paul@47 | 475 | |
paul@10 | 476 | # Permit case-insensitive list terms. |
paul@10 | 477 | |
paul@10 | 478 | term = match.group("term").lower() |
paul@10 | 479 | desc = match.group("desc") |
paul@10 | 480 | |
paul@69 | 481 | # Check that the term has not already been substituted. If so, |
paul@69 | 482 | # get the next event. |
paul@69 | 483 | |
paul@69 | 484 | if term in replaced_terms: |
paul@69 | 485 | try: |
paul@69 | 486 | event = events.next() |
paul@69 | 487 | |
paul@69 | 488 | # No more events. |
paul@69 | 489 | |
paul@69 | 490 | except StopIteration: |
paul@69 | 491 | break |
paul@69 | 492 | |
paul@69 | 493 | event_details = event.getDetails() |
paul@69 | 494 | replaced_terms = set() |
paul@69 | 495 | |
paul@75 | 496 | # Add preceding text to the new body. |
paul@75 | 497 | |
paul@75 | 498 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@75 | 499 | |
paul@75 | 500 | # Get the matching regions, adding the term to the new body. |
paul@75 | 501 | |
paul@75 | 502 | new_body_parts.append(match.group("wholeterm")) |
paul@75 | 503 | |
paul@10 | 504 | # Special value type handling. |
paul@10 | 505 | |
paul@67 | 506 | if event_details.has_key(term): |
paul@19 | 507 | |
paul@67 | 508 | # Dates. |
paul@47 | 509 | |
paul@323 | 510 | if term in event.date_terms: |
paul@67 | 511 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@47 | 512 | |
paul@67 | 513 | # Lists (whose elements may be quoted). |
paul@47 | 514 | |
paul@323 | 515 | elif term in event.list_terms: |
paul@92 | 516 | desc = ", ".join([getEncodedWikiText(item) for item in event_details[term]]) |
paul@47 | 517 | |
paul@92 | 518 | # Labels which must be quoted. |
paul@47 | 519 | |
paul@323 | 520 | elif term in event.title_terms: |
paul@67 | 521 | desc = getEncodedWikiText(event_details[term]) |
paul@47 | 522 | |
paul@230 | 523 | # Position details. |
paul@230 | 524 | |
paul@230 | 525 | elif term == "geo": |
paul@230 | 526 | desc = " ".join(map(str, event_details[term])) |
paul@230 | 527 | |
paul@67 | 528 | # Text which need not be quoted, but it will be Wiki text. |
paul@55 | 529 | |
paul@323 | 530 | elif term in event.other_terms: |
paul@67 | 531 | desc = event_details[term] |
paul@55 | 532 | |
paul@69 | 533 | replaced_terms.add(term) |
paul@69 | 534 | |
paul@75 | 535 | # Add the replaced value. |
paul@75 | 536 | |
paul@67 | 537 | new_body_parts.append(desc) |
paul@10 | 538 | |
paul@69 | 539 | # Remember where in the page has been processed. |
paul@69 | 540 | |
paul@69 | 541 | end_of_last_match = match.end() |
paul@69 | 542 | |
paul@69 | 543 | # Write the rest of the page. |
paul@69 | 544 | |
paul@69 | 545 | new_body_parts.append(body[end_of_last_match:]) |
paul@10 | 546 | |
paul@67 | 547 | self.body = "".join(new_body_parts) |
paul@11 | 548 | |
paul@67 | 549 | def flushCategoryMembership(self): |
paul@17 | 550 | |
paul@67 | 551 | "Flush the category membership to the page body." |
paul@11 | 552 | |
paul@67 | 553 | body = self.getBody() |
paul@67 | 554 | category_names = self.getCategoryMembership() |
paul@67 | 555 | match = category_membership_regexp.search(body) |
paul@10 | 556 | |
paul@67 | 557 | if match: |
paul@67 | 558 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@10 | 559 | |
paul@67 | 560 | def saveChanges(self): |
paul@10 | 561 | |
paul@67 | 562 | "Save changes to the event." |
paul@10 | 563 | |
paul@67 | 564 | self.flushEventDetails() |
paul@67 | 565 | self.flushCategoryMembership() |
paul@67 | 566 | self.page.saveText(self.getBody(), 0) |
paul@10 | 567 | |
paul@323 | 568 | def linkToPage(self, request, text, query_string=None, anchor=None): |
paul@11 | 569 | |
paul@67 | 570 | """ |
paul@67 | 571 | Using 'request', return a link to this page with the given link 'text' |
paul@323 | 572 | and optional 'query_string' and 'anchor'. |
paul@67 | 573 | """ |
paul@11 | 574 | |
paul@323 | 575 | return linkToPage(request, self.page, text, query_string, anchor) |
paul@13 | 576 | |
paul@217 | 577 | # Formatting-related functions. |
paul@217 | 578 | |
paul@284 | 579 | def getParserClass(self, format): |
paul@217 | 580 | |
paul@217 | 581 | """ |
paul@284 | 582 | Return a parser class for the given 'format', returning a plain text |
paul@284 | 583 | parser if no parser can be found for the specified 'format'. |
paul@217 | 584 | """ |
paul@217 | 585 | |
paul@305 | 586 | return getParserClass(self.page.request, format) |
paul@284 | 587 | |
paul@284 | 588 | def formatText(self, text, fmt): |
paul@217 | 589 | |
paul@217 | 590 | """ |
paul@284 | 591 | Format the given 'text' using the specified formatter 'fmt'. |
paul@217 | 592 | """ |
paul@217 | 593 | |
paul@284 | 594 | fmt.page = page = self.page |
paul@284 | 595 | request = page.request |
paul@219 | 596 | |
paul@284 | 597 | parser_cls = self.getParserClass(self.getFormat()) |
paul@309 | 598 | return formatText(text, request, fmt, parser_cls) |
paul@217 | 599 | |
paul@217 | 600 | # Event details. |
paul@217 | 601 | |
paul@130 | 602 | class Event(ActsAsTimespan): |
paul@69 | 603 | |
paul@69 | 604 | "A description of an event." |
paul@69 | 605 | |
paul@323 | 606 | title_terms = "title", "summary" |
paul@323 | 607 | date_terms = "start", "end" |
paul@323 | 608 | list_terms = "topics", "categories" |
paul@323 | 609 | other_terms = "description", "location", "link" |
paul@323 | 610 | geo_terms = "geo", |
paul@323 | 611 | all_terms = title_terms + date_terms + list_terms + other_terms + geo_terms |
paul@323 | 612 | |
paul@69 | 613 | def __init__(self, page, details): |
paul@69 | 614 | self.page = page |
paul@69 | 615 | self.details = details |
paul@69 | 616 | |
paul@217 | 617 | # Permit omission of the end of the event by duplicating the start. |
paul@217 | 618 | |
paul@240 | 619 | if self.details.has_key("start") and not self.details.get("end"): |
paul@218 | 620 | end = self.details["start"] |
paul@218 | 621 | |
paul@218 | 622 | # Make any end time refer to the day instead. |
paul@218 | 623 | |
paul@218 | 624 | if isinstance(end, DateTime): |
paul@218 | 625 | end = end.as_date() |
paul@218 | 626 | |
paul@218 | 627 | self.details["end"] = end |
paul@217 | 628 | |
paul@170 | 629 | def __repr__(self): |
paul@170 | 630 | return "<Event %r %r>" % (self.getSummary(), self.as_limits()) |
paul@170 | 631 | |
paul@128 | 632 | def __hash__(self): |
paul@223 | 633 | |
paul@223 | 634 | """ |
paul@223 | 635 | Return a dictionary hash, avoiding mistaken equality of events in some |
paul@223 | 636 | situations (notably membership tests) by including the URL as well as |
paul@223 | 637 | the summary. |
paul@223 | 638 | """ |
paul@223 | 639 | |
paul@223 | 640 | return hash(self.getSummary() + self.getEventURL()) |
paul@69 | 641 | |
paul@69 | 642 | def getPage(self): |
paul@69 | 643 | |
paul@69 | 644 | "Return the page describing this event." |
paul@69 | 645 | |
paul@69 | 646 | return self.page |
paul@69 | 647 | |
paul@69 | 648 | def setPage(self, page): |
paul@69 | 649 | |
paul@69 | 650 | "Set the 'page' describing this event." |
paul@69 | 651 | |
paul@69 | 652 | self.page = page |
paul@69 | 653 | |
paul@223 | 654 | def getEventURL(self): |
paul@223 | 655 | |
paul@223 | 656 | "Return the URL of this event." |
paul@223 | 657 | |
paul@323 | 658 | fragment = self.details.get("fragment") |
paul@323 | 659 | return self.page.getPageURL() + (fragment and "#" + fragment or "") |
paul@219 | 660 | |
paul@219 | 661 | def linkToEvent(self, request, text, query_string=None): |
paul@219 | 662 | |
paul@219 | 663 | """ |
paul@219 | 664 | Using 'request', return a link to this event with the given link 'text' |
paul@219 | 665 | and optional 'query_string'. |
paul@219 | 666 | """ |
paul@219 | 667 | |
paul@323 | 668 | return self.page.linkToPage(request, text, query_string, self.details.get("fragment")) |
paul@219 | 669 | |
paul@219 | 670 | def getMetadata(self): |
paul@219 | 671 | |
paul@219 | 672 | """ |
paul@219 | 673 | Return a dictionary containing items describing the event's "created" |
paul@219 | 674 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@219 | 675 | "last-comment" made about the last edit. |
paul@219 | 676 | """ |
paul@219 | 677 | |
paul@219 | 678 | # Delegate this to the page. |
paul@219 | 679 | |
paul@219 | 680 | return self.page.getMetadata() |
paul@219 | 681 | |
paul@69 | 682 | def getSummary(self, event_parent=None): |
paul@69 | 683 | |
paul@69 | 684 | """ |
paul@69 | 685 | Return either the given title or summary of the event according to the |
paul@69 | 686 | event details, or a summary made from using the pretty version of the |
paul@69 | 687 | page name. |
paul@69 | 688 | |
paul@69 | 689 | If the optional 'event_parent' is specified, any page beneath the given |
paul@69 | 690 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@69 | 691 | if its name is used as the summary. |
paul@69 | 692 | """ |
paul@69 | 693 | |
paul@69 | 694 | event_details = self.details |
paul@69 | 695 | |
paul@69 | 696 | if event_details.has_key("title"): |
paul@69 | 697 | return event_details["title"] |
paul@69 | 698 | elif event_details.has_key("summary"): |
paul@69 | 699 | return event_details["summary"] |
paul@69 | 700 | else: |
paul@69 | 701 | # If appropriate, remove the parent details and "/" character. |
paul@69 | 702 | |
paul@69 | 703 | title = self.page.getPageName() |
paul@69 | 704 | |
paul@74 | 705 | if event_parent and title.startswith(event_parent): |
paul@69 | 706 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@69 | 707 | |
paul@69 | 708 | return getPrettyTitle(title) |
paul@69 | 709 | |
paul@69 | 710 | def getDetails(self): |
paul@69 | 711 | |
paul@69 | 712 | "Return the details for this event." |
paul@69 | 713 | |
paul@69 | 714 | return self.details |
paul@69 | 715 | |
paul@69 | 716 | def setDetails(self, event_details): |
paul@69 | 717 | |
paul@69 | 718 | "Set the 'event_details' for this event." |
paul@69 | 719 | |
paul@69 | 720 | self.details = event_details |
paul@69 | 721 | |
paul@123 | 722 | # Timespan-related methods. |
paul@123 | 723 | |
paul@123 | 724 | def __contains__(self, other): |
paul@123 | 725 | return self == other |
paul@123 | 726 | |
paul@224 | 727 | def __eq__(self, other): |
paul@224 | 728 | if isinstance(other, Event): |
paul@224 | 729 | return self.getSummary() == other.getSummary() and self.getEventURL() == other.getEventURL() and self._cmp(other) |
paul@224 | 730 | else: |
paul@224 | 731 | return self._cmp(other) == 0 |
paul@224 | 732 | |
paul@224 | 733 | def __ne__(self, other): |
paul@224 | 734 | return not self.__eq__(other) |
paul@224 | 735 | |
paul@224 | 736 | def __lt__(self, other): |
paul@224 | 737 | return self._cmp(other) == -1 |
paul@224 | 738 | |
paul@224 | 739 | def __le__(self, other): |
paul@224 | 740 | return self._cmp(other) in (-1, 0) |
paul@224 | 741 | |
paul@224 | 742 | def __gt__(self, other): |
paul@224 | 743 | return self._cmp(other) == 1 |
paul@224 | 744 | |
paul@224 | 745 | def __ge__(self, other): |
paul@224 | 746 | return self._cmp(other) in (0, 1) |
paul@224 | 747 | |
paul@224 | 748 | def _cmp(self, other): |
paul@223 | 749 | |
paul@223 | 750 | "Compare this event to an 'other' event purely by their timespans." |
paul@223 | 751 | |
paul@123 | 752 | if isinstance(other, Event): |
paul@123 | 753 | return cmp(self.as_timespan(), other.as_timespan()) |
paul@123 | 754 | else: |
paul@123 | 755 | return cmp(self.as_timespan(), other) |
paul@123 | 756 | |
paul@123 | 757 | def as_timespan(self): |
paul@123 | 758 | details = self.details |
paul@123 | 759 | if details.has_key("start") and details.has_key("end"): |
paul@123 | 760 | return Timespan(details["start"], details["end"]) |
paul@123 | 761 | else: |
paul@123 | 762 | return None |
paul@123 | 763 | |
paul@128 | 764 | def as_limits(self): |
paul@170 | 765 | ts = self.as_timespan() |
paul@170 | 766 | return ts and ts.as_limits() |
paul@123 | 767 | |
paul@219 | 768 | class CalendarEvent(Event): |
paul@219 | 769 | |
paul@219 | 770 | "An event from a remote calendar." |
paul@219 | 771 | |
paul@223 | 772 | def getEventURL(self): |
paul@223 | 773 | |
paul@223 | 774 | "Return the URL of this event." |
paul@223 | 775 | |
paul@223 | 776 | return self.details.get("url") or self.page.getPageURL() |
paul@219 | 777 | |
paul@323 | 778 | def linkToEvent(self, request, text, query_string=None, anchor=None): |
paul@219 | 779 | |
paul@219 | 780 | """ |
paul@219 | 781 | Using 'request', return a link to this event with the given link 'text' |
paul@323 | 782 | and optional 'query_string' and 'anchor'. |
paul@219 | 783 | """ |
paul@219 | 784 | |
paul@323 | 785 | return linkToResource(self.getEventURL(), request, text, query_string, anchor) |
paul@219 | 786 | |
paul@219 | 787 | def getMetadata(self): |
paul@219 | 788 | |
paul@219 | 789 | """ |
paul@219 | 790 | Return a dictionary containing items describing the event's "created" |
paul@219 | 791 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@219 | 792 | "last-comment" made about the last edit. |
paul@219 | 793 | """ |
paul@219 | 794 | |
paul@219 | 795 | return { |
paul@219 | 796 | "created" : self.details.get("created") or self.details["dtstamp"], |
paul@219 | 797 | "last-modified" : self.details.get("last-modified") or self.details["dtstamp"], |
paul@219 | 798 | "sequence" : self.details.get("sequence") or 0, |
paul@219 | 799 | "last-comment" : "" |
paul@219 | 800 | } |
paul@219 | 801 | |
paul@217 | 802 | # Obtaining event containers and events from such containers. |
paul@217 | 803 | |
paul@217 | 804 | def getEventPages(pages): |
paul@168 | 805 | |
paul@168 | 806 | "Return a list of events found on the given 'pages'." |
paul@12 | 807 | |
paul@217 | 808 | # Get real pages instead of result pages. |
paul@217 | 809 | |
paul@217 | 810 | return map(EventPage, pages) |
paul@217 | 811 | |
paul@219 | 812 | def getAllEventSources(request): |
paul@219 | 813 | |
paul@219 | 814 | "Return all event sources defined in the Wiki using the 'request'." |
paul@219 | 815 | |
paul@219 | 816 | sources_page = getattr(request.cfg, "event_aggregator_sources_page", "EventSourcesDict") |
paul@219 | 817 | |
paul@219 | 818 | # Remote sources are accessed via dictionary page definitions. |
paul@219 | 819 | |
paul@237 | 820 | return getWikiDict(sources_page, request) |
paul@219 | 821 | |
paul@217 | 822 | def getEventResources(sources, calendar_start, calendar_end, request): |
paul@217 | 823 | |
paul@217 | 824 | """ |
paul@217 | 825 | Return resource objects for the given 'sources' using the given |
paul@217 | 826 | 'calendar_start' and 'calendar_end' to parameterise requests to the sources, |
paul@217 | 827 | and the 'request' to access configuration settings in the Wiki. |
paul@217 | 828 | """ |
paul@217 | 829 | |
paul@219 | 830 | sources_dict = getAllEventSources(request) |
paul@219 | 831 | if not sources_dict: |
paul@217 | 832 | return [] |
paul@217 | 833 | |
paul@217 | 834 | # Use dates for the calendar limits. |
paul@217 | 835 | |
paul@218 | 836 | if isinstance(calendar_start, Date): |
paul@218 | 837 | pass |
paul@218 | 838 | elif isinstance(calendar_start, Month): |
paul@217 | 839 | calendar_start = calendar_start.as_date(1) |
paul@217 | 840 | |
paul@218 | 841 | if isinstance(calendar_end, Date): |
paul@218 | 842 | pass |
paul@218 | 843 | elif isinstance(calendar_end, Month): |
paul@217 | 844 | calendar_end = calendar_end.as_date(-1) |
paul@217 | 845 | |
paul@217 | 846 | resources = [] |
paul@217 | 847 | |
paul@217 | 848 | for source in sources: |
paul@217 | 849 | try: |
paul@265 | 850 | details = sources_dict[source].split() |
paul@265 | 851 | url = details[0] |
paul@265 | 852 | format = (details[1:] or ["ical"])[0] |
paul@237 | 853 | except (KeyError, ValueError): |
paul@237 | 854 | pass |
paul@237 | 855 | else: |
paul@217 | 856 | # Prevent local file access. |
paul@217 | 857 | |
paul@217 | 858 | if url.startswith("file:"): |
paul@217 | 859 | continue |
paul@217 | 860 | |
paul@217 | 861 | # Parameterise the URL. |
paul@221 | 862 | # Where other parameters are used, care must be taken to encode them |
paul@221 | 863 | # properly. |
paul@221 | 864 | |
paul@221 | 865 | url = url.replace("{start}", urllib.quote_plus(calendar_start and str(calendar_start) or "")) |
paul@221 | 866 | url = url.replace("{end}", urllib.quote_plus(calendar_end and str(calendar_end) or "")) |
paul@217 | 867 | |
paul@217 | 868 | # Get a parser. |
paul@265 | 869 | # NOTE: This could be done reactively by choosing a parser based on |
paul@265 | 870 | # NOTE: the content type provided by the URL. |
paul@217 | 871 | |
paul@217 | 872 | if format == "ical" and vCalendar is not None: |
paul@217 | 873 | parser = vCalendar.parse |
paul@217 | 874 | resource_cls = EventCalendar |
paul@265 | 875 | required_content_type = "text/calendar" |
paul@217 | 876 | else: |
paul@217 | 877 | continue |
paul@217 | 878 | |
paul@312 | 879 | # Obtain the resource, using a cached version if appropriate. |
paul@312 | 880 | |
paul@226 | 881 | max_cache_age = int(getattr(request.cfg, "event_aggregator_max_cache_age", "300")) |
paul@312 | 882 | data = getCachedResource(request, url, "EventAggregator", "wiki", max_cache_age) |
paul@313 | 883 | if not data: |
paul@313 | 884 | continue |
paul@225 | 885 | |
paul@225 | 886 | # Process the entry, parsing the content. |
paul@225 | 887 | |
paul@225 | 888 | f = StringIO(data) |
paul@225 | 889 | try: |
paul@225 | 890 | url = f.readline() |
paul@265 | 891 | |
paul@265 | 892 | # Get the content type and encoding, making sure that the data |
paul@265 | 893 | # can be parsed. |
paul@265 | 894 | |
paul@265 | 895 | content_type, encoding = getContentTypeAndEncoding(f.readline()) |
paul@265 | 896 | if content_type != required_content_type: |
paul@265 | 897 | continue |
paul@265 | 898 | |
paul@265 | 899 | # Send the data to the parser. |
paul@265 | 900 | |
paul@225 | 901 | uf = codecs.getreader(encoding or "utf-8")(f) |
paul@225 | 902 | try: |
paul@225 | 903 | resources.append(resource_cls(url, parser(uf))) |
paul@225 | 904 | finally: |
paul@225 | 905 | uf.close() |
paul@217 | 906 | finally: |
paul@217 | 907 | f.close() |
paul@217 | 908 | |
paul@217 | 909 | return resources |
paul@217 | 910 | |
paul@217 | 911 | def getEventsFromResources(resources): |
paul@217 | 912 | |
paul@217 | 913 | "Return a list of events supplied by the given event 'resources'." |
paul@217 | 914 | |
paul@10 | 915 | events = [] |
paul@168 | 916 | |
paul@217 | 917 | for resource in resources: |
paul@217 | 918 | |
paul@217 | 919 | # Get all events described by the resource. |
paul@217 | 920 | |
paul@217 | 921 | for event in resource.getEvents(): |
paul@168 | 922 | |
paul@168 | 923 | # Remember the event. |
paul@168 | 924 | |
paul@168 | 925 | events.append(event) |
paul@168 | 926 | |
paul@168 | 927 | return events |
paul@168 | 928 | |
paul@217 | 929 | # Event filtering and limits. |
paul@217 | 930 | |
paul@171 | 931 | def getEventsInPeriod(events, calendar_period): |
paul@168 | 932 | |
paul@168 | 933 | """ |
paul@168 | 934 | Return a collection containing those of the given 'events' which occur |
paul@171 | 935 | within the given 'calendar_period'. |
paul@168 | 936 | """ |
paul@168 | 937 | |
paul@171 | 938 | all_shown_events = [] |
paul@168 | 939 | |
paul@168 | 940 | for event in events: |
paul@168 | 941 | |
paul@168 | 942 | # Test for the suitability of the event. |
paul@168 | 943 | |
paul@168 | 944 | if event.as_timespan() is not None: |
paul@168 | 945 | |
paul@168 | 946 | # Compare the dates to the requested calendar window, if any. |
paul@168 | 947 | |
paul@168 | 948 | if event in calendar_period: |
paul@171 | 949 | all_shown_events.append(event) |
paul@168 | 950 | |
paul@168 | 951 | return all_shown_events |
paul@10 | 952 | |
paul@171 | 953 | def getEventLimits(events): |
paul@171 | 954 | |
paul@171 | 955 | "Return the earliest and latest of the given 'events'." |
paul@171 | 956 | |
paul@171 | 957 | earliest = None |
paul@171 | 958 | latest = None |
paul@171 | 959 | |
paul@171 | 960 | for event in events: |
paul@171 | 961 | |
paul@171 | 962 | # Test for the suitability of the event. |
paul@171 | 963 | |
paul@171 | 964 | if event.as_timespan() is not None: |
paul@171 | 965 | ts = event.as_timespan() |
paul@171 | 966 | if earliest is None or ts.start < earliest: |
paul@171 | 967 | earliest = ts.start |
paul@171 | 968 | if latest is None or ts.end > latest: |
paul@171 | 969 | latest = ts.end |
paul@171 | 970 | |
paul@171 | 971 | return earliest, latest |
paul@171 | 972 | |
paul@310 | 973 | def getLatestEventTimestamp(events): |
paul@29 | 974 | |
paul@29 | 975 | """ |
paul@310 | 976 | Return the latest timestamp found from the given 'events'. |
paul@29 | 977 | """ |
paul@29 | 978 | |
paul@29 | 979 | latest = None |
paul@29 | 980 | |
paul@69 | 981 | for event in events: |
paul@310 | 982 | metadata = event.getMetadata() |
paul@310 | 983 | |
paul@310 | 984 | if latest is None or latest < metadata["last-modified"]: |
paul@310 | 985 | latest = metadata["last-modified"] |
paul@29 | 986 | |
paul@29 | 987 | return latest |
paul@29 | 988 | |
paul@26 | 989 | def getOrderedEvents(events): |
paul@26 | 990 | |
paul@26 | 991 | """ |
paul@26 | 992 | Return a list with the given 'events' ordered according to their start and |
paul@67 | 993 | end dates. |
paul@26 | 994 | """ |
paul@26 | 995 | |
paul@26 | 996 | ordered_events = events[:] |
paul@68 | 997 | ordered_events.sort() |
paul@26 | 998 | return ordered_events |
paul@26 | 999 | |
paul@171 | 1000 | def getCalendarPeriod(calendar_start, calendar_end): |
paul@168 | 1001 | |
paul@168 | 1002 | """ |
paul@171 | 1003 | Return a calendar period for the given 'calendar_start' and 'calendar_end'. |
paul@171 | 1004 | These parameters can be given as None. |
paul@168 | 1005 | """ |
paul@168 | 1006 | |
paul@168 | 1007 | # Re-order the window, if appropriate. |
paul@168 | 1008 | |
paul@168 | 1009 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@168 | 1010 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@168 | 1011 | |
paul@171 | 1012 | return Timespan(calendar_start, calendar_end) |
paul@168 | 1013 | |
paul@187 | 1014 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest, resolution): |
paul@13 | 1015 | |
paul@13 | 1016 | """ |
paul@13 | 1017 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@13 | 1018 | indicating that no restriction is imposed on the period for each of the |
paul@13 | 1019 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@13 | 1020 | specific period of interest. |
paul@13 | 1021 | """ |
paul@13 | 1022 | |
paul@13 | 1023 | # Define the period as starting with any specified start month or the |
paul@13 | 1024 | # earliest event known, ending with any specified end month or the latest |
paul@13 | 1025 | # event known. |
paul@13 | 1026 | |
paul@13 | 1027 | first = calendar_start or earliest |
paul@13 | 1028 | last = calendar_end or latest |
paul@13 | 1029 | |
paul@13 | 1030 | # If there is no range of months to show, perhaps because there are no |
paul@13 | 1031 | # events in the requested period, and there was no start or end month |
paul@13 | 1032 | # specified, show only the month indicated by the start or end of the |
paul@13 | 1033 | # requested period. If all events were to be shown but none were found show |
paul@13 | 1034 | # the current month. |
paul@13 | 1035 | |
paul@187 | 1036 | if resolution == "date": |
paul@122 | 1037 | get_current = getCurrentDate |
paul@122 | 1038 | else: |
paul@122 | 1039 | get_current = getCurrentMonth |
paul@122 | 1040 | |
paul@13 | 1041 | if first is None: |
paul@122 | 1042 | first = last or get_current() |
paul@13 | 1043 | if last is None: |
paul@122 | 1044 | last = first or get_current() |
paul@13 | 1045 | |
paul@187 | 1046 | if resolution == "month": |
paul@187 | 1047 | first = first.as_month() |
paul@187 | 1048 | last = last.as_month() |
paul@187 | 1049 | |
paul@13 | 1050 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@13 | 1051 | |
paul@13 | 1052 | return min(first, last), last |
paul@13 | 1053 | |
paul@168 | 1054 | def getCoverage(events, resolution="date"): |
paul@15 | 1055 | |
paul@15 | 1056 | """ |
paul@168 | 1057 | Determine the coverage of the given 'events', returning a collection of |
paul@168 | 1058 | timespans, along with a dictionary mapping locations to collections of |
paul@168 | 1059 | slots, where each slot contains a tuple of the form (timespans, events). |
paul@15 | 1060 | """ |
paul@15 | 1061 | |
paul@118 | 1062 | all_events = {} |
paul@123 | 1063 | full_coverage = TimespanCollection(resolution) |
paul@15 | 1064 | |
paul@15 | 1065 | # Get event details. |
paul@15 | 1066 | |
paul@69 | 1067 | for event in events: |
paul@69 | 1068 | event_details = event.getDetails() |
paul@15 | 1069 | |
paul@168 | 1070 | # Find the coverage of this period for the event. |
paul@168 | 1071 | |
paul@168 | 1072 | # For day views, each location has its own slot, but for month |
paul@168 | 1073 | # views, all locations are pooled together since having separate |
paul@168 | 1074 | # slots for each location can lead to poor usage of vertical space. |
paul@168 | 1075 | |
paul@168 | 1076 | if resolution == "datetime": |
paul@168 | 1077 | event_location = event_details.get("location") |
paul@168 | 1078 | else: |
paul@168 | 1079 | event_location = None |
paul@168 | 1080 | |
paul@168 | 1081 | # Update the overall coverage. |
paul@168 | 1082 | |
paul@168 | 1083 | full_coverage.insert_in_order(event) |
paul@168 | 1084 | |
paul@168 | 1085 | # Add a new events list for a new location. |
paul@168 | 1086 | # Locations can be unspecified, thus None refers to all unlocalised |
paul@168 | 1087 | # events. |
paul@168 | 1088 | |
paul@168 | 1089 | if not all_events.has_key(event_location): |
paul@168 | 1090 | all_events[event_location] = [TimespanCollection(resolution, [event])] |
paul@168 | 1091 | |
paul@168 | 1092 | # Try and fit the event into an events list. |
paul@168 | 1093 | |
paul@168 | 1094 | else: |
paul@168 | 1095 | slot = all_events[event_location] |
paul@168 | 1096 | |
paul@168 | 1097 | for slot_events in slot: |
paul@168 | 1098 | |
paul@168 | 1099 | # Where the event does not overlap with the events in the |
paul@168 | 1100 | # current collection, add it alongside these events. |
paul@168 | 1101 | |
paul@168 | 1102 | if not event in slot_events: |
paul@168 | 1103 | slot_events.insert_in_order(event) |
paul@168 | 1104 | break |
paul@168 | 1105 | |
paul@168 | 1106 | # Make a new element in the list if the event cannot be |
paul@168 | 1107 | # marked alongside existing events. |
paul@15 | 1108 | |
paul@15 | 1109 | else: |
paul@168 | 1110 | slot.append(TimespanCollection(resolution, [event])) |
paul@15 | 1111 | |
paul@15 | 1112 | return full_coverage, all_events |
paul@15 | 1113 | |
paul@122 | 1114 | def getCoverageScale(coverage): |
paul@126 | 1115 | |
paul@126 | 1116 | """ |
paul@126 | 1117 | Return a scale for the given coverage so that the times involved are |
paul@126 | 1118 | exposed. The scale consists of a list of non-overlapping timespans forming |
paul@126 | 1119 | a contiguous period of time. |
paul@126 | 1120 | """ |
paul@126 | 1121 | |
paul@122 | 1122 | times = set() |
paul@122 | 1123 | for timespan in coverage: |
paul@128 | 1124 | start, end = timespan.as_limits() |
paul@128 | 1125 | |
paul@128 | 1126 | # Add either genuine times or dates converted to times. |
paul@128 | 1127 | |
paul@128 | 1128 | if isinstance(start, DateTime): |
paul@128 | 1129 | times.add(start) |
paul@140 | 1130 | else: |
paul@218 | 1131 | times.add(start.as_start_of_day()) |
paul@128 | 1132 | |
paul@128 | 1133 | if isinstance(end, DateTime): |
paul@140 | 1134 | times.add(end) |
paul@140 | 1135 | else: |
paul@140 | 1136 | times.add(end.as_date().next_day()) |
paul@128 | 1137 | |
paul@122 | 1138 | times = list(times) |
paul@135 | 1139 | times.sort(cmp_dates_as_day_start) |
paul@122 | 1140 | |
paul@122 | 1141 | scale = [] |
paul@122 | 1142 | first = 1 |
paul@122 | 1143 | start = None |
paul@122 | 1144 | for time in times: |
paul@122 | 1145 | if not first: |
paul@123 | 1146 | scale.append(Timespan(start, time)) |
paul@122 | 1147 | else: |
paul@122 | 1148 | first = 0 |
paul@122 | 1149 | start = time |
paul@126 | 1150 | |
paul@122 | 1151 | return scale |
paul@122 | 1152 | |
paul@323 | 1153 | # Event sorting. |
paul@323 | 1154 | |
paul@323 | 1155 | def sort_start_first(x, y): |
paul@323 | 1156 | x_ts = x.as_limits() |
paul@323 | 1157 | if x_ts is not None: |
paul@323 | 1158 | x_start, x_end = x_ts |
paul@323 | 1159 | y_ts = y.as_limits() |
paul@323 | 1160 | if y_ts is not None: |
paul@323 | 1161 | y_start, y_end = y_ts |
paul@323 | 1162 | start_order = cmp(x_start, y_start) |
paul@323 | 1163 | if start_order == 0: |
paul@323 | 1164 | return cmp(x_end, y_end) |
paul@323 | 1165 | else: |
paul@323 | 1166 | return start_order |
paul@323 | 1167 | return 0 |
paul@323 | 1168 | |
paul@323 | 1169 | # Country code parsing. |
paul@323 | 1170 | |
paul@93 | 1171 | def getCountry(s): |
paul@92 | 1172 | |
paul@93 | 1173 | "Find a country code in the given string 's'." |
paul@93 | 1174 | |
paul@93 | 1175 | match = country_code_regexp.search(s) |
paul@92 | 1176 | |
paul@93 | 1177 | if match: |
paul@93 | 1178 | return match.group("code") |
paul@93 | 1179 | else: |
paul@93 | 1180 | return None |
paul@67 | 1181 | |
paul@67 | 1182 | # Page-related functions. |
paul@67 | 1183 | |
paul@88 | 1184 | def fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames): |
paul@88 | 1185 | |
paul@88 | 1186 | """ |
paul@88 | 1187 | Using the given 'template_page', complete the 'new_page' by copying the |
paul@88 | 1188 | template and adding the given 'event_details' (a dictionary of event |
paul@88 | 1189 | fields), setting also the 'category_pagenames' to define category |
paul@88 | 1190 | membership. |
paul@88 | 1191 | """ |
paul@88 | 1192 | |
paul@88 | 1193 | event_page = EventPage(template_page) |
paul@88 | 1194 | new_event_page = EventPage(new_page) |
paul@88 | 1195 | new_event_page.copyPage(event_page) |
paul@88 | 1196 | |
paul@88 | 1197 | if new_event_page.getFormat() == "wiki": |
paul@88 | 1198 | new_event = Event(new_event_page, event_details) |
paul@88 | 1199 | new_event_page.setEvents([new_event]) |
paul@88 | 1200 | new_event_page.setCategoryMembership(category_pagenames) |
paul@255 | 1201 | new_event_page.flushEventDetails() |
paul@255 | 1202 | |
paul@255 | 1203 | return new_event_page.getBody() |
paul@88 | 1204 | |
paul@294 | 1205 | def getMapsPage(request): |
paul@294 | 1206 | return getattr(request.cfg, "event_aggregator_maps_page", "EventMapsDict") |
paul@294 | 1207 | |
paul@294 | 1208 | def getLocationsPage(request): |
paul@294 | 1209 | return getattr(request.cfg, "event_aggregator_locations_page", "EventLocationsDict") |
paul@294 | 1210 | |
paul@294 | 1211 | class Location: |
paul@294 | 1212 | |
paul@294 | 1213 | """ |
paul@294 | 1214 | A representation of a location acquired from the locations dictionary. |
paul@294 | 1215 | |
paul@294 | 1216 | The locations dictionary is a mapping from location to a string containing |
paul@294 | 1217 | white-space-separated values describing... |
paul@294 | 1218 | |
paul@294 | 1219 | * The latitude and longitude of the location. |
paul@294 | 1220 | * Optionally, the time regime used by the location. |
paul@294 | 1221 | """ |
paul@294 | 1222 | |
paul@294 | 1223 | def __init__(self, location, locations): |
paul@294 | 1224 | |
paul@294 | 1225 | """ |
paul@294 | 1226 | Initialise the given 'location' using the 'locations' dictionary |
paul@294 | 1227 | provided. |
paul@294 | 1228 | """ |
paul@294 | 1229 | |
paul@294 | 1230 | self.location = location |
paul@294 | 1231 | |
paul@294 | 1232 | try: |
paul@294 | 1233 | self.data = locations[location].split() |
paul@294 | 1234 | except KeyError: |
paul@294 | 1235 | self.data = [] |
paul@294 | 1236 | |
paul@294 | 1237 | def getPosition(self): |
paul@294 | 1238 | |
paul@294 | 1239 | """ |
paul@294 | 1240 | Attempt to return the position of this location. If no position can be |
paul@294 | 1241 | found, return a latitude of None and a longitude of None. |
paul@294 | 1242 | """ |
paul@294 | 1243 | |
paul@294 | 1244 | try: |
paul@294 | 1245 | latitude, longitude = map(getMapReference, self.data[:2]) |
paul@294 | 1246 | return latitude, longitude |
paul@294 | 1247 | except ValueError: |
paul@294 | 1248 | return None, None |
paul@294 | 1249 | |
paul@294 | 1250 | def getTimeRegime(self): |
paul@294 | 1251 | |
paul@294 | 1252 | """ |
paul@294 | 1253 | Attempt to return the time regime employed at this location. If no |
paul@294 | 1254 | regime has been specified, return None. |
paul@294 | 1255 | """ |
paul@294 | 1256 | |
paul@294 | 1257 | try: |
paul@294 | 1258 | return self.data[2] |
paul@294 | 1259 | except IndexError: |
paul@294 | 1260 | return None |
paul@294 | 1261 | |
paul@281 | 1262 | # User interface abstractions. |
paul@281 | 1263 | |
paul@281 | 1264 | class View: |
paul@281 | 1265 | |
paul@281 | 1266 | "A view of the event calendar." |
paul@281 | 1267 | |
paul@281 | 1268 | def __init__(self, page, calendar_name, raw_calendar_start, raw_calendar_end, |
paul@281 | 1269 | original_calendar_start, original_calendar_end, calendar_start, calendar_end, |
paul@281 | 1270 | first, last, category_names, remote_sources, template_name, parent_name, mode, |
paul@281 | 1271 | resolution, name_usage, map_name): |
paul@281 | 1272 | |
paul@281 | 1273 | """ |
paul@281 | 1274 | Initialise the view with the current 'page', a 'calendar_name' (which |
paul@281 | 1275 | may be None), the 'raw_calendar_start' and 'raw_calendar_end' (which |
paul@281 | 1276 | are the actual start and end values provided by the request), the |
paul@281 | 1277 | calculated 'original_calendar_start' and 'original_calendar_end' (which |
paul@281 | 1278 | are the result of calculating the calendar's limits from the raw start |
paul@281 | 1279 | and end values), and the requested, calculated 'calendar_start' and |
paul@281 | 1280 | 'calendar_end' (which may involve different start and end values due to |
paul@281 | 1281 | navigation in the user interface), along with the 'first' and 'last' |
paul@281 | 1282 | months of event coverage. |
paul@281 | 1283 | |
paul@281 | 1284 | The additional 'category_names', 'remote_sources', 'template_name', |
paul@281 | 1285 | 'parent_name' and 'mode' parameters are used to configure the links |
paul@281 | 1286 | employed by the view. |
paul@281 | 1287 | |
paul@281 | 1288 | The 'resolution' affects the view for certain modes and is also used to |
paul@281 | 1289 | parameterise links. |
paul@281 | 1290 | |
paul@281 | 1291 | The 'name_usage' parameter controls how names are shown on calendar mode |
paul@281 | 1292 | events, such as how often labels are repeated. |
paul@281 | 1293 | |
paul@281 | 1294 | The 'map_name' parameter provides the name of a map to be used in the |
paul@281 | 1295 | map mode. |
paul@281 | 1296 | """ |
paul@281 | 1297 | |
paul@281 | 1298 | self.page = page |
paul@281 | 1299 | self.calendar_name = calendar_name |
paul@281 | 1300 | self.raw_calendar_start = raw_calendar_start |
paul@281 | 1301 | self.raw_calendar_end = raw_calendar_end |
paul@281 | 1302 | self.original_calendar_start = original_calendar_start |
paul@281 | 1303 | self.original_calendar_end = original_calendar_end |
paul@281 | 1304 | self.calendar_start = calendar_start |
paul@281 | 1305 | self.calendar_end = calendar_end |
paul@281 | 1306 | self.template_name = template_name |
paul@281 | 1307 | self.parent_name = parent_name |
paul@281 | 1308 | self.mode = mode |
paul@281 | 1309 | self.resolution = resolution |
paul@281 | 1310 | self.name_usage = name_usage |
paul@281 | 1311 | self.map_name = map_name |
paul@281 | 1312 | |
paul@281 | 1313 | self.category_name_parameters = "&".join([("category=%s" % name) for name in category_names]) |
paul@281 | 1314 | self.remote_source_parameters = "&".join([("source=%s" % source) for source in remote_sources]) |
paul@281 | 1315 | |
paul@281 | 1316 | # Calculate the duration in terms of the highest common unit of time. |
paul@281 | 1317 | |
paul@281 | 1318 | self.first = first |
paul@281 | 1319 | self.last = last |
paul@297 | 1320 | self.duration = abs(last - first) + 1 |
paul@281 | 1321 | |
paul@292 | 1322 | if self.calendar_name: |
paul@281 | 1323 | |
paul@281 | 1324 | # Store the view parameters. |
paul@281 | 1325 | |
paul@281 | 1326 | self.previous_start = first.previous() |
paul@281 | 1327 | self.next_start = first.next() |
paul@281 | 1328 | self.previous_end = last.previous() |
paul@281 | 1329 | self.next_end = last.next() |
paul@281 | 1330 | |
paul@281 | 1331 | self.previous_set_start = first.update(-self.duration) |
paul@281 | 1332 | self.next_set_start = first.update(self.duration) |
paul@281 | 1333 | self.previous_set_end = last.update(-self.duration) |
paul@281 | 1334 | self.next_set_end = last.update(self.duration) |
paul@281 | 1335 | |
paul@281 | 1336 | def getIdentifier(self): |
paul@281 | 1337 | |
paul@281 | 1338 | "Return a unique identifier to be used to refer to this view." |
paul@281 | 1339 | |
paul@281 | 1340 | # NOTE: Nasty hack to get a unique identifier if no name is given. |
paul@281 | 1341 | |
paul@281 | 1342 | return self.calendar_name or str(id(self)) |
paul@281 | 1343 | |
paul@281 | 1344 | def getQualifiedParameterName(self, argname): |
paul@281 | 1345 | |
paul@281 | 1346 | "Return the 'argname' qualified using the calendar name." |
paul@281 | 1347 | |
paul@281 | 1348 | return getQualifiedParameterName(self.calendar_name, argname) |
paul@281 | 1349 | |
paul@281 | 1350 | def getDateQueryString(self, argname, date, prefix=1): |
paul@281 | 1351 | |
paul@281 | 1352 | """ |
paul@281 | 1353 | Return a query string fragment for the given 'argname', referring to the |
paul@281 | 1354 | month given by the specified 'year_month' object, appropriate for this |
paul@281 | 1355 | calendar. |
paul@281 | 1356 | |
paul@281 | 1357 | If 'prefix' is specified and set to a false value, the parameters in the |
paul@281 | 1358 | query string will not be calendar-specific, but could be used with the |
paul@281 | 1359 | summary action. |
paul@281 | 1360 | """ |
paul@281 | 1361 | |
paul@281 | 1362 | suffixes = ["year", "month", "day"] |
paul@281 | 1363 | |
paul@281 | 1364 | if date is not None: |
paul@281 | 1365 | args = [] |
paul@281 | 1366 | for suffix, value in zip(suffixes, date.as_tuple()): |
paul@281 | 1367 | suffixed_argname = "%s-%s" % (argname, suffix) |
paul@281 | 1368 | if prefix: |
paul@281 | 1369 | suffixed_argname = self.getQualifiedParameterName(suffixed_argname) |
paul@281 | 1370 | args.append("%s=%s" % (suffixed_argname, value)) |
paul@281 | 1371 | return "&".join(args) |
paul@281 | 1372 | else: |
paul@281 | 1373 | return "" |
paul@281 | 1374 | |
paul@281 | 1375 | def getRawDateQueryString(self, argname, date, prefix=1): |
paul@281 | 1376 | |
paul@281 | 1377 | """ |
paul@281 | 1378 | Return a query string fragment for the given 'argname', referring to the |
paul@281 | 1379 | date given by the specified 'date' value, appropriate for this |
paul@281 | 1380 | calendar. |
paul@281 | 1381 | |
paul@281 | 1382 | If 'prefix' is specified and set to a false value, the parameters in the |
paul@281 | 1383 | query string will not be calendar-specific, but could be used with the |
paul@281 | 1384 | summary action. |
paul@281 | 1385 | """ |
paul@281 | 1386 | |
paul@281 | 1387 | if date is not None: |
paul@281 | 1388 | if prefix: |
paul@281 | 1389 | argname = self.getQualifiedParameterName(argname) |
paul@281 | 1390 | return "%s=%s" % (argname, wikiutil.url_quote_plus(date)) |
paul@281 | 1391 | else: |
paul@281 | 1392 | return "" |
paul@281 | 1393 | |
paul@281 | 1394 | def getNavigationLink(self, start, end, mode=None, resolution=None): |
paul@281 | 1395 | |
paul@281 | 1396 | """ |
paul@281 | 1397 | Return a query string fragment for navigation to a view showing months |
paul@281 | 1398 | from 'start' to 'end' inclusive, with the optional 'mode' indicating the |
paul@281 | 1399 | view style and the optional 'resolution' indicating the resolution of a |
paul@281 | 1400 | view, if configurable. |
paul@281 | 1401 | """ |
paul@281 | 1402 | |
paul@281 | 1403 | return "%s&%s&%s=%s&%s=%s" % ( |
paul@281 | 1404 | self.getRawDateQueryString("start", start), |
paul@281 | 1405 | self.getRawDateQueryString("end", end), |
paul@281 | 1406 | self.getQualifiedParameterName("mode"), mode or self.mode, |
paul@281 | 1407 | self.getQualifiedParameterName("resolution"), resolution or self.resolution |
paul@281 | 1408 | ) |
paul@281 | 1409 | |
paul@281 | 1410 | def getUpdateLink(self, start, end, mode=None, resolution=None): |
paul@281 | 1411 | |
paul@281 | 1412 | """ |
paul@281 | 1413 | Return a query string fragment for navigation to a view showing months |
paul@281 | 1414 | from 'start' to 'end' inclusive, with the optional 'mode' indicating the |
paul@281 | 1415 | view style and the optional 'resolution' indicating the resolution of a |
paul@281 | 1416 | view, if configurable. This link differs from the conventional |
paul@281 | 1417 | navigation link in that it is sufficient to activate the update action |
paul@281 | 1418 | and produce an updated region of the page without needing to locate and |
paul@281 | 1419 | process the page or any macro invocation. |
paul@281 | 1420 | """ |
paul@281 | 1421 | |
paul@282 | 1422 | parameters = [ |
paul@282 | 1423 | self.getRawDateQueryString("start", start, 0), |
paul@282 | 1424 | self.getRawDateQueryString("end", end, 0), |
paul@282 | 1425 | self.category_name_parameters, |
paul@282 | 1426 | self.remote_source_parameters, |
paul@282 | 1427 | ] |
paul@282 | 1428 | |
paul@282 | 1429 | pairs = [ |
paul@282 | 1430 | ("calendar", self.calendar_name or ""), |
paul@282 | 1431 | ("calendarstart", self.raw_calendar_start or ""), |
paul@282 | 1432 | ("calendarend", self.raw_calendar_end or ""), |
paul@282 | 1433 | ("mode", mode or self.mode), |
paul@282 | 1434 | ("resolution", resolution or self.resolution), |
paul@282 | 1435 | ("parent", self.parent_name or ""), |
paul@282 | 1436 | ("template", self.template_name or ""), |
paul@282 | 1437 | ("names", self.name_usage), |
paul@282 | 1438 | ("map", self.map_name or ""), |
paul@282 | 1439 | ] |
paul@282 | 1440 | |
paul@282 | 1441 | url = self.page.url(self.page.request, |
paul@282 | 1442 | "action=EventAggregatorUpdate&%s" % ( |
paul@282 | 1443 | "&".join([("%s=%s" % pair) for pair in pairs] + parameters) |
paul@282 | 1444 | ), relative=True) |
paul@282 | 1445 | |
paul@282 | 1446 | return "return replaceCalendar('EventAggregator-%s', '%s')" % (self.getIdentifier(), url) |
paul@281 | 1447 | |
paul@281 | 1448 | def getNewEventLink(self, start): |
paul@281 | 1449 | |
paul@281 | 1450 | """ |
paul@281 | 1451 | Return a query string activating the new event form, incorporating the |
paul@281 | 1452 | calendar parameters, specialising the form for the given 'start' date or |
paul@281 | 1453 | month. |
paul@281 | 1454 | """ |
paul@281 | 1455 | |
paul@281 | 1456 | if start is not None: |
paul@281 | 1457 | details = start.as_tuple() |
paul@281 | 1458 | pairs = zip(["start-year=%d", "start-month=%d", "start-day=%d"], details) |
paul@281 | 1459 | args = [(param % value) for (param, value) in pairs] |
paul@281 | 1460 | args = "&".join(args) |
paul@281 | 1461 | else: |
paul@281 | 1462 | args = "" |
paul@281 | 1463 | |
paul@281 | 1464 | # Prepare navigation details for the calendar shown with the new event |
paul@281 | 1465 | # form. |
paul@281 | 1466 | |
paul@281 | 1467 | navigation_link = self.getNavigationLink( |
paul@281 | 1468 | self.calendar_start, self.calendar_end |
paul@281 | 1469 | ) |
paul@281 | 1470 | |
paul@307 | 1471 | return "action=EventAggregatorNewEvent%s%s&template=%s&parent=%s&%s" % ( |
paul@307 | 1472 | args and "&%s" % args, |
paul@307 | 1473 | self.category_name_parameters and "&%s" % self.category_name_parameters, |
paul@307 | 1474 | self.template_name, self.parent_name or "", |
paul@281 | 1475 | navigation_link) |
paul@281 | 1476 | |
paul@281 | 1477 | def getFullDateLabel(self, date): |
paul@281 | 1478 | page = self.page |
paul@281 | 1479 | request = page.request |
paul@281 | 1480 | return getFullDateLabel(request, date) |
paul@281 | 1481 | |
paul@281 | 1482 | def getFullMonthLabel(self, year_month): |
paul@281 | 1483 | page = self.page |
paul@281 | 1484 | request = page.request |
paul@281 | 1485 | return getFullMonthLabel(request, year_month) |
paul@281 | 1486 | |
paul@281 | 1487 | def getFullLabel(self, arg): |
paul@281 | 1488 | return self.resolution == "date" and self.getFullDateLabel(arg) or self.getFullMonthLabel(arg) |
paul@281 | 1489 | |
paul@281 | 1490 | def _getCalendarPeriod(self, start_label, end_label, default_label): |
paul@281 | 1491 | output = [] |
paul@308 | 1492 | append = output.append |
paul@308 | 1493 | |
paul@281 | 1494 | if start_label: |
paul@308 | 1495 | append(start_label) |
paul@281 | 1496 | if end_label and start_label != end_label: |
paul@281 | 1497 | if output: |
paul@308 | 1498 | append(" - ") |
paul@308 | 1499 | append(end_label) |
paul@281 | 1500 | return "".join(output) or default_label |
paul@281 | 1501 | |
paul@281 | 1502 | def getCalendarPeriod(self): |
paul@281 | 1503 | _ = self.page.request.getText |
paul@281 | 1504 | return self._getCalendarPeriod( |
paul@281 | 1505 | self.calendar_start and self.getFullLabel(self.calendar_start), |
paul@281 | 1506 | self.calendar_end and self.getFullLabel(self.calendar_end), |
paul@281 | 1507 | _("All events") |
paul@281 | 1508 | ) |
paul@281 | 1509 | |
paul@281 | 1510 | def getOriginalCalendarPeriod(self): |
paul@281 | 1511 | _ = self.page.request.getText |
paul@281 | 1512 | return self._getCalendarPeriod( |
paul@281 | 1513 | self.original_calendar_start and self.getFullLabel(self.original_calendar_start), |
paul@281 | 1514 | self.original_calendar_end and self.getFullLabel(self.original_calendar_end), |
paul@281 | 1515 | _("All events") |
paul@281 | 1516 | ) |
paul@281 | 1517 | |
paul@281 | 1518 | def getRawCalendarPeriod(self): |
paul@281 | 1519 | _ = self.page.request.getText |
paul@281 | 1520 | return self._getCalendarPeriod( |
paul@281 | 1521 | self.raw_calendar_start, |
paul@281 | 1522 | self.raw_calendar_end, |
paul@281 | 1523 | _("No period specified") |
paul@281 | 1524 | ) |
paul@281 | 1525 | |
paul@281 | 1526 | def writeDownloadControls(self): |
paul@281 | 1527 | |
paul@281 | 1528 | """ |
paul@281 | 1529 | Return a representation of the download controls, featuring links for |
paul@281 | 1530 | view, calendar and customised downloads and subscriptions. |
paul@281 | 1531 | """ |
paul@281 | 1532 | |
paul@281 | 1533 | page = self.page |
paul@281 | 1534 | request = page.request |
paul@282 | 1535 | fmt = request.formatter |
paul@281 | 1536 | _ = request.getText |
paul@281 | 1537 | |
paul@281 | 1538 | output = [] |
paul@308 | 1539 | append = output.append |
paul@281 | 1540 | |
paul@281 | 1541 | # The full URL is needed for webcal links. |
paul@281 | 1542 | |
paul@281 | 1543 | full_url = "%s%s" % (request.getBaseURL(), getPathInfo(request)) |
paul@281 | 1544 | |
paul@281 | 1545 | # Generate the links. |
paul@281 | 1546 | |
paul@307 | 1547 | download_dialogue_link = "action=EventAggregatorSummary&parent=%s&resolution=%s%s%s" % ( |
paul@281 | 1548 | self.parent_name or "", |
paul@281 | 1549 | self.resolution, |
paul@307 | 1550 | self.category_name_parameters and "&%s" % self.category_name_parameters, |
paul@307 | 1551 | self.remote_source_parameters and "&%s" % self.remote_source_parameters |
paul@281 | 1552 | ) |
paul@281 | 1553 | download_all_link = download_dialogue_link + "&doit=1" |
paul@281 | 1554 | download_link = download_all_link + ("&%s&%s" % ( |
paul@281 | 1555 | self.getDateQueryString("start", self.calendar_start, prefix=0), |
paul@281 | 1556 | self.getDateQueryString("end", self.calendar_end, prefix=0) |
paul@281 | 1557 | )) |
paul@281 | 1558 | |
paul@281 | 1559 | # Subscription links just explicitly select the RSS format. |
paul@281 | 1560 | |
paul@281 | 1561 | subscribe_dialogue_link = download_dialogue_link + "&format=RSS" |
paul@281 | 1562 | subscribe_all_link = download_all_link + "&format=RSS" |
paul@281 | 1563 | subscribe_link = download_link + "&format=RSS" |
paul@281 | 1564 | |
paul@281 | 1565 | # Adjust the "download all" and "subscribe all" links if the calendar |
paul@281 | 1566 | # has an inherent period associated with it. |
paul@281 | 1567 | |
paul@281 | 1568 | period_limits = [] |
paul@281 | 1569 | |
paul@281 | 1570 | if self.raw_calendar_start: |
paul@281 | 1571 | period_limits.append("&%s" % |
paul@281 | 1572 | self.getRawDateQueryString("start", self.raw_calendar_start, prefix=0) |
paul@281 | 1573 | ) |
paul@281 | 1574 | if self.raw_calendar_end: |
paul@281 | 1575 | period_limits.append("&%s" % |
paul@281 | 1576 | self.getRawDateQueryString("end", self.raw_calendar_end, prefix=0) |
paul@281 | 1577 | ) |
paul@281 | 1578 | |
paul@281 | 1579 | period_limits = "".join(period_limits) |
paul@281 | 1580 | |
paul@281 | 1581 | download_dialogue_link += period_limits |
paul@281 | 1582 | download_all_link += period_limits |
paul@281 | 1583 | subscribe_dialogue_link += period_limits |
paul@281 | 1584 | subscribe_all_link += period_limits |
paul@281 | 1585 | |
paul@281 | 1586 | # Pop-up descriptions of the downloadable calendars. |
paul@281 | 1587 | |
paul@281 | 1588 | calendar_period = self.getCalendarPeriod() |
paul@281 | 1589 | original_calendar_period = self.getOriginalCalendarPeriod() |
paul@281 | 1590 | raw_calendar_period = self.getRawCalendarPeriod() |
paul@281 | 1591 | |
paul@281 | 1592 | # Write the controls. |
paul@281 | 1593 | |
paul@281 | 1594 | # Download controls. |
paul@281 | 1595 | |
paul@308 | 1596 | append(fmt.div(on=1, css_class="event-download-controls")) |
paul@308 | 1597 | |
paul@308 | 1598 | append(fmt.span(on=1, css_class="event-download")) |
paul@308 | 1599 | append(fmt.text(_("Download..."))) |
paul@308 | 1600 | append(fmt.div(on=1, css_class="event-download-popup")) |
paul@308 | 1601 | |
paul@308 | 1602 | append(fmt.div(on=1, css_class="event-download-item")) |
paul@308 | 1603 | append(fmt.span(on=1, css_class="event-download-types")) |
paul@308 | 1604 | append(fmt.span(on=1, css_class="event-download-webcal")) |
paul@308 | 1605 | append(linkToResource(full_url.replace("http", "webcal", 1), request, _("webcal"), download_link)) |
paul@308 | 1606 | append(fmt.span(on=0)) |
paul@308 | 1607 | append(fmt.span(on=1, css_class="event-download-http")) |
paul@308 | 1608 | append(linkToPage(request, page, _("http"), download_link)) |
paul@308 | 1609 | append(fmt.span(on=0)) |
paul@308 | 1610 | append(fmt.span(on=0)) # end types |
paul@308 | 1611 | append(fmt.span(on=1, css_class="event-download-label")) |
paul@308 | 1612 | append(fmt.text(_("Download this view"))) |
paul@308 | 1613 | append(fmt.span(on=0)) # end label |
paul@308 | 1614 | append(fmt.span(on=1, css_class="event-download-period")) |
paul@308 | 1615 | append(fmt.text(calendar_period)) |
paul@308 | 1616 | append(fmt.span(on=0)) |
paul@308 | 1617 | append(fmt.div(on=0)) |
paul@308 | 1618 | |
paul@308 | 1619 | append(fmt.div(on=1, css_class="event-download-item")) |
paul@308 | 1620 | append(fmt.span(on=1, css_class="event-download-types")) |
paul@308 | 1621 | append(fmt.span(on=1, css_class="event-download-webcal")) |
paul@308 | 1622 | append(linkToResource(full_url.replace("http", "webcal", 1), request, _("webcal"), download_all_link)) |
paul@308 | 1623 | append(fmt.span(on=0)) |
paul@308 | 1624 | append(fmt.span(on=1, css_class="event-download-http")) |
paul@308 | 1625 | append(linkToPage(request, page, _("http"), download_all_link)) |
paul@308 | 1626 | append(fmt.span(on=0)) |
paul@308 | 1627 | append(fmt.span(on=0)) # end types |
paul@308 | 1628 | append(fmt.span(on=1, css_class="event-download-label")) |
paul@308 | 1629 | append(fmt.text(_("Download this calendar"))) |
paul@308 | 1630 | append(fmt.span(on=0)) # end label |
paul@308 | 1631 | append(fmt.span(on=1, css_class="event-download-period")) |
paul@308 | 1632 | append(fmt.text(original_calendar_period)) |
paul@308 | 1633 | append(fmt.span(on=0)) |
paul@308 | 1634 | append(fmt.span(on=1, css_class="event-download-period-raw")) |
paul@308 | 1635 | append(fmt.text(raw_calendar_period)) |
paul@308 | 1636 | append(fmt.span(on=0)) |
paul@308 | 1637 | append(fmt.div(on=0)) |
paul@308 | 1638 | |
paul@308 | 1639 | append(fmt.div(on=1, css_class="event-download-item")) |
paul@308 | 1640 | append(fmt.span(on=1, css_class="event-download-link")) |
paul@308 | 1641 | append(linkToPage(request, page, _("Edit download options..."), download_dialogue_link)) |
paul@308 | 1642 | append(fmt.span(on=0)) # end label |
paul@308 | 1643 | append(fmt.div(on=0)) |
paul@308 | 1644 | |
paul@308 | 1645 | append(fmt.div(on=0)) # end of pop-up |
paul@308 | 1646 | append(fmt.span(on=0)) # end of download |
paul@281 | 1647 | |
paul@281 | 1648 | # Subscription controls. |
paul@281 | 1649 | |
paul@308 | 1650 | append(fmt.span(on=1, css_class="event-download")) |
paul@308 | 1651 | append(fmt.text(_("Subscribe..."))) |
paul@308 | 1652 | append(fmt.div(on=1, css_class="event-download-popup")) |
paul@308 | 1653 | |
paul@308 | 1654 | append(fmt.div(on=1, css_class="event-download-item")) |
paul@308 | 1655 | append(fmt.span(on=1, css_class="event-download-label")) |
paul@308 | 1656 | append(linkToPage(request, page, _("Subscribe to this view"), subscribe_link)) |
paul@308 | 1657 | append(fmt.span(on=0)) # end label |
paul@308 | 1658 | append(fmt.span(on=1, css_class="event-download-period")) |
paul@308 | 1659 | append(fmt.text(calendar_period)) |
paul@308 | 1660 | append(fmt.span(on=0)) |
paul@308 | 1661 | append(fmt.div(on=0)) |
paul@308 | 1662 | |
paul@308 | 1663 | append(fmt.div(on=1, css_class="event-download-item")) |
paul@308 | 1664 | append(fmt.span(on=1, css_class="event-download-label")) |
paul@308 | 1665 | append(linkToPage(request, page, _("Subscribe to this calendar"), subscribe_all_link)) |
paul@308 | 1666 | append(fmt.span(on=0)) # end label |
paul@308 | 1667 | append(fmt.span(on=1, css_class="event-download-period")) |
paul@308 | 1668 | append(fmt.text(original_calendar_period)) |
paul@308 | 1669 | append(fmt.span(on=0)) |
paul@308 | 1670 | append(fmt.span(on=1, css_class="event-download-period-raw")) |
paul@308 | 1671 | append(fmt.text(raw_calendar_period)) |
paul@308 | 1672 | append(fmt.span(on=0)) |
paul@308 | 1673 | append(fmt.div(on=0)) |
paul@308 | 1674 | |
paul@308 | 1675 | append(fmt.div(on=1, css_class="event-download-item")) |
paul@308 | 1676 | append(fmt.span(on=1, css_class="event-download-link")) |
paul@308 | 1677 | append(linkToPage(request, page, _("Edit subscription options..."), subscribe_dialogue_link)) |
paul@308 | 1678 | append(fmt.span(on=0)) # end label |
paul@308 | 1679 | append(fmt.div(on=0)) |
paul@308 | 1680 | |
paul@308 | 1681 | append(fmt.div(on=0)) # end of pop-up |
paul@308 | 1682 | append(fmt.span(on=0)) # end of download |
paul@308 | 1683 | |
paul@308 | 1684 | append(fmt.div(on=0)) # end of controls |
paul@281 | 1685 | |
paul@281 | 1686 | return "".join(output) |
paul@281 | 1687 | |
paul@281 | 1688 | def writeViewControls(self): |
paul@281 | 1689 | |
paul@281 | 1690 | """ |
paul@281 | 1691 | Return a representation of the view mode controls, permitting viewing of |
paul@281 | 1692 | aggregated events in calendar, list or table form. |
paul@281 | 1693 | """ |
paul@281 | 1694 | |
paul@281 | 1695 | page = self.page |
paul@281 | 1696 | request = page.request |
paul@282 | 1697 | fmt = request.formatter |
paul@281 | 1698 | _ = request.getText |
paul@281 | 1699 | |
paul@281 | 1700 | output = [] |
paul@308 | 1701 | append = output.append |
paul@281 | 1702 | |
paul@281 | 1703 | start = self.calendar_start |
paul@281 | 1704 | end = self.calendar_end |
paul@281 | 1705 | |
paul@281 | 1706 | help_page = Page(request, "HelpOnEventAggregator") |
paul@281 | 1707 | calendar_link = self.getNavigationLink(start and start.as_month(), end and end.as_month(), "calendar", "month") |
paul@283 | 1708 | calendar_update_link = self.getUpdateLink(start and start.as_month(), end and end.as_month(), "calendar", "month") |
paul@281 | 1709 | list_link = self.getNavigationLink(start, end, "list") |
paul@283 | 1710 | list_update_link = self.getUpdateLink(start, end, "list") |
paul@281 | 1711 | table_link = self.getNavigationLink(start, end, "table") |
paul@283 | 1712 | table_update_link = self.getUpdateLink(start, end, "table") |
paul@281 | 1713 | map_link = self.getNavigationLink(start, end, "map") |
paul@283 | 1714 | map_update_link = self.getUpdateLink(start, end, "map") |
paul@281 | 1715 | new_event_link = self.getNewEventLink(start) |
paul@281 | 1716 | |
paul@281 | 1717 | # Write the controls. |
paul@281 | 1718 | |
paul@308 | 1719 | append(fmt.div(on=1, css_class="event-view-controls")) |
paul@308 | 1720 | |
paul@308 | 1721 | append(fmt.span(on=1, css_class="event-view")) |
paul@308 | 1722 | append(linkToPage(request, help_page, _("Help"))) |
paul@308 | 1723 | append(fmt.span(on=0)) |
paul@308 | 1724 | |
paul@308 | 1725 | append(fmt.span(on=1, css_class="event-view")) |
paul@308 | 1726 | append(linkToPage(request, page, _("New event"), new_event_link)) |
paul@308 | 1727 | append(fmt.span(on=0)) |
paul@281 | 1728 | |
paul@281 | 1729 | if self.mode != "calendar": |
paul@308 | 1730 | append(fmt.span(on=1, css_class="event-view")) |
paul@308 | 1731 | append(linkToPage(request, page, _("View as calendar"), calendar_link, onclick=calendar_update_link)) |
paul@308 | 1732 | append(fmt.span(on=0)) |
paul@281 | 1733 | |
paul@281 | 1734 | if self.mode != "list": |
paul@308 | 1735 | append(fmt.span(on=1, css_class="event-view")) |
paul@308 | 1736 | append(linkToPage(request, page, _("View as list"), list_link, onclick=list_update_link)) |
paul@308 | 1737 | append(fmt.span(on=0)) |
paul@281 | 1738 | |
paul@281 | 1739 | if self.mode != "table": |
paul@308 | 1740 | append(fmt.span(on=1, css_class="event-view")) |
paul@308 | 1741 | append(linkToPage(request, page, _("View as table"), table_link, onclick=table_update_link)) |
paul@308 | 1742 | append(fmt.span(on=0)) |
paul@281 | 1743 | |
paul@286 | 1744 | if self.mode != "map" and self.map_name: |
paul@308 | 1745 | append(fmt.span(on=1, css_class="event-view")) |
paul@308 | 1746 | append(linkToPage(request, page, _("View as map"), map_link, onclick=map_update_link)) |
paul@308 | 1747 | append(fmt.span(on=0)) |
paul@308 | 1748 | |
paul@308 | 1749 | append(fmt.div(on=0)) |
paul@281 | 1750 | |
paul@281 | 1751 | return "".join(output) |
paul@281 | 1752 | |
paul@281 | 1753 | def writeMapHeading(self): |
paul@281 | 1754 | |
paul@281 | 1755 | """ |
paul@281 | 1756 | Return the calendar heading for the current calendar, providing links |
paul@281 | 1757 | permitting navigation to other periods. |
paul@281 | 1758 | """ |
paul@281 | 1759 | |
paul@281 | 1760 | label = self.getCalendarPeriod() |
paul@281 | 1761 | |
paul@281 | 1762 | if self.raw_calendar_start is None or self.raw_calendar_end is None: |
paul@282 | 1763 | fmt = self.page.request.formatter |
paul@281 | 1764 | output = [] |
paul@308 | 1765 | append = output.append |
paul@308 | 1766 | append(fmt.span(on=1)) |
paul@308 | 1767 | append(fmt.text(label)) |
paul@308 | 1768 | append(fmt.span(on=0)) |
paul@281 | 1769 | return "".join(output) |
paul@281 | 1770 | else: |
paul@281 | 1771 | return self._writeCalendarHeading(label, self.calendar_start, self.calendar_end) |
paul@281 | 1772 | |
paul@281 | 1773 | def writeDateHeading(self, date): |
paul@281 | 1774 | if isinstance(date, Date): |
paul@281 | 1775 | return self.writeDayHeading(date) |
paul@281 | 1776 | else: |
paul@281 | 1777 | return self.writeMonthHeading(date) |
paul@281 | 1778 | |
paul@281 | 1779 | def writeMonthHeading(self, year_month): |
paul@281 | 1780 | |
paul@281 | 1781 | """ |
paul@281 | 1782 | Return the calendar heading for the given 'year_month' (a Month object) |
paul@281 | 1783 | providing links permitting navigation to other months. |
paul@281 | 1784 | """ |
paul@281 | 1785 | |
paul@281 | 1786 | full_month_label = self.getFullMonthLabel(year_month) |
paul@281 | 1787 | end_month = year_month.update(self.duration - 1) |
paul@281 | 1788 | return self._writeCalendarHeading(full_month_label, year_month, end_month) |
paul@281 | 1789 | |
paul@281 | 1790 | def writeDayHeading(self, date): |
paul@281 | 1791 | |
paul@281 | 1792 | """ |
paul@281 | 1793 | Return the calendar heading for the given 'date' (a Date object) |
paul@281 | 1794 | providing links permitting navigation to other dates. |
paul@281 | 1795 | """ |
paul@281 | 1796 | |
paul@281 | 1797 | full_date_label = self.getFullDateLabel(date) |
paul@281 | 1798 | end_date = date.update(self.duration - 1) |
paul@281 | 1799 | return self._writeCalendarHeading(full_date_label, date, end_date) |
paul@281 | 1800 | |
paul@281 | 1801 | def _writeCalendarHeading(self, label, start, end): |
paul@281 | 1802 | |
paul@281 | 1803 | """ |
paul@281 | 1804 | Write a calendar heading providing links permitting navigation to other |
paul@281 | 1805 | periods, using the given 'label' along with the 'start' and 'end' dates |
paul@281 | 1806 | to provide a link to a particular period. |
paul@281 | 1807 | """ |
paul@281 | 1808 | |
paul@281 | 1809 | page = self.page |
paul@281 | 1810 | request = page.request |
paul@282 | 1811 | fmt = request.formatter |
paul@281 | 1812 | _ = request.getText |
paul@281 | 1813 | |
paul@281 | 1814 | output = [] |
paul@308 | 1815 | append = output.append |
paul@281 | 1816 | |
paul@281 | 1817 | # Prepare navigation links. |
paul@281 | 1818 | |
paul@292 | 1819 | if self.calendar_name: |
paul@281 | 1820 | calendar_name = self.calendar_name |
paul@281 | 1821 | |
paul@281 | 1822 | # Links to the previous set of months and to a calendar shifted |
paul@281 | 1823 | # back one month. |
paul@281 | 1824 | |
paul@281 | 1825 | previous_set_link = self.getNavigationLink( |
paul@281 | 1826 | self.previous_set_start, self.previous_set_end |
paul@281 | 1827 | ) |
paul@281 | 1828 | previous_link = self.getNavigationLink( |
paul@281 | 1829 | self.previous_start, self.previous_end |
paul@281 | 1830 | ) |
paul@282 | 1831 | previous_set_update_link = self.getUpdateLink( |
paul@282 | 1832 | self.previous_set_start, self.previous_set_end |
paul@282 | 1833 | ) |
paul@282 | 1834 | previous_update_link = self.getUpdateLink( |
paul@282 | 1835 | self.previous_start, self.previous_end |
paul@282 | 1836 | ) |
paul@281 | 1837 | |
paul@281 | 1838 | # Links to the next set of months and to a calendar shifted |
paul@281 | 1839 | # forward one month. |
paul@281 | 1840 | |
paul@281 | 1841 | next_set_link = self.getNavigationLink( |
paul@281 | 1842 | self.next_set_start, self.next_set_end |
paul@281 | 1843 | ) |
paul@281 | 1844 | next_link = self.getNavigationLink( |
paul@281 | 1845 | self.next_start, self.next_end |
paul@281 | 1846 | ) |
paul@282 | 1847 | next_set_update_link = self.getUpdateLink( |
paul@282 | 1848 | self.next_set_start, self.next_set_end |
paul@282 | 1849 | ) |
paul@282 | 1850 | next_update_link = self.getUpdateLink( |
paul@282 | 1851 | self.next_start, self.next_end |
paul@282 | 1852 | ) |
paul@281 | 1853 | |
paul@281 | 1854 | # A link leading to this date being at the top of the calendar. |
paul@281 | 1855 | |
paul@281 | 1856 | date_link = self.getNavigationLink(start, end) |
paul@282 | 1857 | date_update_link = self.getUpdateLink(start, end) |
paul@281 | 1858 | |
paul@308 | 1859 | append(fmt.span(on=1, css_class="previous")) |
paul@308 | 1860 | append(linkToPage(request, page, "<<", previous_set_link, onclick=previous_set_update_link)) |
paul@308 | 1861 | append(fmt.text(" ")) |
paul@308 | 1862 | append(linkToPage(request, page, "<", previous_link, onclick=previous_update_link)) |
paul@308 | 1863 | append(fmt.span(on=0)) |
paul@308 | 1864 | |
paul@308 | 1865 | append(fmt.span(on=1, css_class="next")) |
paul@308 | 1866 | append(linkToPage(request, page, ">", next_link, onclick=next_update_link)) |
paul@308 | 1867 | append(fmt.text(" ")) |
paul@308 | 1868 | append(linkToPage(request, page, ">>", next_set_link, onclick=next_set_update_link)) |
paul@308 | 1869 | append(fmt.span(on=0)) |
paul@308 | 1870 | |
paul@308 | 1871 | append(linkToPage(request, page, label, date_link, onclick=date_update_link)) |
paul@281 | 1872 | |
paul@281 | 1873 | else: |
paul@308 | 1874 | append(fmt.span(on=1)) |
paul@308 | 1875 | append(fmt.text(label)) |
paul@308 | 1876 | append(fmt.span(on=0)) |
paul@281 | 1877 | |
paul@281 | 1878 | return "".join(output) |
paul@281 | 1879 | |
paul@281 | 1880 | def writeDayNumberHeading(self, date, busy): |
paul@281 | 1881 | |
paul@281 | 1882 | """ |
paul@281 | 1883 | Return a link for the given 'date' which will activate the new event |
paul@281 | 1884 | action for the given day. If 'busy' is given as a true value, the |
paul@281 | 1885 | heading will be marked as busy. |
paul@281 | 1886 | """ |
paul@281 | 1887 | |
paul@281 | 1888 | page = self.page |
paul@281 | 1889 | request = page.request |
paul@282 | 1890 | fmt = request.formatter |
paul@281 | 1891 | _ = request.getText |
paul@281 | 1892 | |
paul@281 | 1893 | output = [] |
paul@308 | 1894 | append = output.append |
paul@281 | 1895 | |
paul@281 | 1896 | year, month, day = date.as_tuple() |
paul@281 | 1897 | new_event_link = self.getNewEventLink(date) |
paul@281 | 1898 | |
paul@281 | 1899 | # Prepare a link to the day view for this day. |
paul@281 | 1900 | |
paul@281 | 1901 | day_view_link = self.getNavigationLink(date, date, "day", "date") |
paul@283 | 1902 | day_view_update_link = self.getUpdateLink(date, date, "day", "date") |
paul@281 | 1903 | |
paul@281 | 1904 | # Output the heading class. |
paul@281 | 1905 | |
paul@308 | 1906 | append( |
paul@281 | 1907 | fmt.table_cell(on=1, attrs={ |
paul@281 | 1908 | "class" : "event-day-heading event-day-%s" % (busy and "busy" or "empty"), |
paul@281 | 1909 | "colspan" : "3" |
paul@281 | 1910 | })) |
paul@281 | 1911 | |
paul@281 | 1912 | # Output the number and pop-up menu. |
paul@281 | 1913 | |
paul@308 | 1914 | append(fmt.div(on=1, css_class="event-day-box")) |
paul@308 | 1915 | |
paul@308 | 1916 | append(fmt.span(on=1, css_class="event-day-number-popup")) |
paul@308 | 1917 | append(fmt.span(on=1, css_class="event-day-number-link")) |
paul@308 | 1918 | append(linkToPage(request, page, _("View day"), day_view_link, onclick=day_view_update_link)) |
paul@308 | 1919 | append(fmt.span(on=0)) |
paul@308 | 1920 | append(fmt.span(on=1, css_class="event-day-number-link")) |
paul@308 | 1921 | append(linkToPage(request, page, _("New event"), new_event_link)) |
paul@308 | 1922 | append(fmt.span(on=0)) |
paul@308 | 1923 | append(fmt.span(on=0)) |
paul@308 | 1924 | |
paul@308 | 1925 | append(fmt.span(on=1, css_class="event-day-number")) |
paul@308 | 1926 | append(fmt.text(unicode(day))) |
paul@308 | 1927 | append(fmt.span(on=0)) |
paul@308 | 1928 | |
paul@308 | 1929 | append(fmt.div(on=0)) |
paul@281 | 1930 | |
paul@281 | 1931 | # End of heading. |
paul@281 | 1932 | |
paul@308 | 1933 | append(fmt.table_cell(on=0)) |
paul@281 | 1934 | |
paul@281 | 1935 | return "".join(output) |
paul@281 | 1936 | |
paul@281 | 1937 | # Common layout methods. |
paul@281 | 1938 | |
paul@281 | 1939 | def getEventStyle(self, colour_seed): |
paul@281 | 1940 | |
paul@281 | 1941 | "Generate colour style information using the given 'colour_seed'." |
paul@281 | 1942 | |
paul@281 | 1943 | bg = getColour(colour_seed) |
paul@281 | 1944 | fg = getBlackOrWhite(bg) |
paul@281 | 1945 | return "background-color: rgb(%d, %d, %d); color: rgb(%d, %d, %d);" % (bg + fg) |
paul@281 | 1946 | |
paul@281 | 1947 | def writeEventSummaryBox(self, event): |
paul@281 | 1948 | |
paul@281 | 1949 | "Return an event summary box linking to the given 'event'." |
paul@281 | 1950 | |
paul@281 | 1951 | page = self.page |
paul@281 | 1952 | request = page.request |
paul@282 | 1953 | fmt = request.formatter |
paul@281 | 1954 | |
paul@281 | 1955 | output = [] |
paul@308 | 1956 | append = output.append |
paul@281 | 1957 | |
paul@281 | 1958 | event_details = event.getDetails() |
paul@281 | 1959 | event_summary = event.getSummary(self.parent_name) |
paul@281 | 1960 | |
paul@281 | 1961 | is_ambiguous = event.as_timespan().ambiguous() |
paul@281 | 1962 | style = self.getEventStyle(event_summary) |
paul@281 | 1963 | |
paul@281 | 1964 | # The event box contains the summary, alongside |
paul@281 | 1965 | # other elements. |
paul@281 | 1966 | |
paul@308 | 1967 | append(fmt.div(on=1, css_class="event-summary-box")) |
paul@308 | 1968 | append(fmt.div(on=1, css_class="event-summary", style=style)) |
paul@281 | 1969 | |
paul@281 | 1970 | if is_ambiguous: |
paul@308 | 1971 | append(fmt.icon("/!\\")) |
paul@308 | 1972 | |
paul@308 | 1973 | append(event.linkToEvent(request, event_summary)) |
paul@308 | 1974 | append(fmt.div(on=0)) |
paul@281 | 1975 | |
paul@281 | 1976 | # Add a pop-up element for long summaries. |
paul@281 | 1977 | |
paul@308 | 1978 | append(fmt.div(on=1, css_class="event-summary-popup", style=style)) |
paul@281 | 1979 | |
paul@281 | 1980 | if is_ambiguous: |
paul@308 | 1981 | append(fmt.icon("/!\\")) |
paul@308 | 1982 | |
paul@308 | 1983 | append(event.linkToEvent(request, event_summary)) |
paul@308 | 1984 | append(fmt.div(on=0)) |
paul@308 | 1985 | |
paul@308 | 1986 | append(fmt.div(on=0)) |
paul@281 | 1987 | |
paul@281 | 1988 | return "".join(output) |
paul@281 | 1989 | |
paul@281 | 1990 | # Calendar layout methods. |
paul@281 | 1991 | |
paul@281 | 1992 | def writeMonthTableHeading(self, year_month): |
paul@281 | 1993 | page = self.page |
paul@282 | 1994 | fmt = page.request.formatter |
paul@281 | 1995 | |
paul@281 | 1996 | output = [] |
paul@308 | 1997 | append = output.append |
paul@308 | 1998 | |
paul@308 | 1999 | append(fmt.table_row(on=1)) |
paul@308 | 2000 | append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "21"})) |
paul@308 | 2001 | |
paul@308 | 2002 | append(self.writeMonthHeading(year_month)) |
paul@308 | 2003 | |
paul@308 | 2004 | append(fmt.table_cell(on=0)) |
paul@308 | 2005 | append(fmt.table_row(on=0)) |
paul@281 | 2006 | |
paul@281 | 2007 | return "".join(output) |
paul@281 | 2008 | |
paul@281 | 2009 | def writeWeekdayHeadings(self): |
paul@281 | 2010 | page = self.page |
paul@281 | 2011 | request = page.request |
paul@282 | 2012 | fmt = request.formatter |
paul@281 | 2013 | _ = request.getText |
paul@281 | 2014 | |
paul@281 | 2015 | output = [] |
paul@308 | 2016 | append = output.append |
paul@308 | 2017 | |
paul@308 | 2018 | append(fmt.table_row(on=1)) |
paul@281 | 2019 | |
paul@281 | 2020 | for weekday in range(0, 7): |
paul@308 | 2021 | append(fmt.table_cell(on=1, attrs={"class" : "event-weekday-heading", "colspan" : "3"})) |
paul@308 | 2022 | append(fmt.text(_(getDayLabel(weekday)))) |
paul@308 | 2023 | append(fmt.table_cell(on=0)) |
paul@308 | 2024 | |
paul@308 | 2025 | append(fmt.table_row(on=0)) |
paul@281 | 2026 | return "".join(output) |
paul@281 | 2027 | |
paul@281 | 2028 | def writeDayNumbers(self, first_day, number_of_days, month, coverage): |
paul@281 | 2029 | page = self.page |
paul@282 | 2030 | fmt = page.request.formatter |
paul@281 | 2031 | |
paul@281 | 2032 | output = [] |
paul@308 | 2033 | append = output.append |
paul@308 | 2034 | |
paul@308 | 2035 | append(fmt.table_row(on=1)) |
paul@281 | 2036 | |
paul@281 | 2037 | for weekday in range(0, 7): |
paul@281 | 2038 | day = first_day + weekday |
paul@281 | 2039 | date = month.as_date(day) |
paul@281 | 2040 | |
paul@281 | 2041 | # Output out-of-month days. |
paul@281 | 2042 | |
paul@281 | 2043 | if day < 1 or day > number_of_days: |
paul@308 | 2044 | append(fmt.table_cell(on=1, |
paul@281 | 2045 | attrs={"class" : "event-day-heading event-day-excluded", "colspan" : "3"})) |
paul@308 | 2046 | append(fmt.table_cell(on=0)) |
paul@281 | 2047 | |
paul@281 | 2048 | # Output normal days. |
paul@281 | 2049 | |
paul@281 | 2050 | else: |
paul@281 | 2051 | # Output the day heading, making a link to a new event |
paul@281 | 2052 | # action. |
paul@281 | 2053 | |
paul@308 | 2054 | append(self.writeDayNumberHeading(date, date in coverage)) |
paul@281 | 2055 | |
paul@281 | 2056 | # End of day numbers. |
paul@281 | 2057 | |
paul@308 | 2058 | append(fmt.table_row(on=0)) |
paul@281 | 2059 | return "".join(output) |
paul@281 | 2060 | |
paul@281 | 2061 | def writeEmptyWeek(self, first_day, number_of_days): |
paul@281 | 2062 | page = self.page |
paul@282 | 2063 | fmt = page.request.formatter |
paul@281 | 2064 | |
paul@281 | 2065 | output = [] |
paul@308 | 2066 | append = output.append |
paul@308 | 2067 | |
paul@308 | 2068 | append(fmt.table_row(on=1)) |
paul@281 | 2069 | |
paul@281 | 2070 | for weekday in range(0, 7): |
paul@281 | 2071 | day = first_day + weekday |
paul@281 | 2072 | |
paul@281 | 2073 | # Output out-of-month days. |
paul@281 | 2074 | |
paul@281 | 2075 | if day < 1 or day > number_of_days: |
paul@308 | 2076 | append(fmt.table_cell(on=1, |
paul@281 | 2077 | attrs={"class" : "event-day-content event-day-excluded", "colspan" : "3"})) |
paul@308 | 2078 | append(fmt.table_cell(on=0)) |
paul@281 | 2079 | |
paul@281 | 2080 | # Output empty days. |
paul@281 | 2081 | |
paul@281 | 2082 | else: |
paul@308 | 2083 | append(fmt.table_cell(on=1, |
paul@281 | 2084 | attrs={"class" : "event-day-content event-day-empty", "colspan" : "3"})) |
paul@281 | 2085 | |
paul@308 | 2086 | append(fmt.table_row(on=0)) |
paul@281 | 2087 | return "".join(output) |
paul@281 | 2088 | |
paul@281 | 2089 | def writeWeekSlots(self, first_day, number_of_days, month, week_end, week_slots): |
paul@281 | 2090 | output = [] |
paul@308 | 2091 | append = output.append |
paul@281 | 2092 | |
paul@281 | 2093 | locations = week_slots.keys() |
paul@281 | 2094 | locations.sort(sort_none_first) |
paul@281 | 2095 | |
paul@281 | 2096 | # Visit each slot corresponding to a location (or no location). |
paul@281 | 2097 | |
paul@281 | 2098 | for location in locations: |
paul@281 | 2099 | |
paul@281 | 2100 | # Visit each coverage span, presenting the events in the span. |
paul@281 | 2101 | |
paul@281 | 2102 | for events in week_slots[location]: |
paul@281 | 2103 | |
paul@281 | 2104 | # Output each set. |
paul@281 | 2105 | |
paul@308 | 2106 | append(self.writeWeekSlot(first_day, number_of_days, month, week_end, events)) |
paul@281 | 2107 | |
paul@281 | 2108 | # Add a spacer. |
paul@281 | 2109 | |
paul@308 | 2110 | append(self.writeWeekSpacer(first_day, number_of_days)) |
paul@281 | 2111 | |
paul@281 | 2112 | return "".join(output) |
paul@281 | 2113 | |
paul@281 | 2114 | def writeWeekSlot(self, first_day, number_of_days, month, week_end, events): |
paul@281 | 2115 | page = self.page |
paul@281 | 2116 | request = page.request |
paul@282 | 2117 | fmt = request.formatter |
paul@281 | 2118 | |
paul@281 | 2119 | output = [] |
paul@308 | 2120 | append = output.append |
paul@308 | 2121 | |
paul@308 | 2122 | append(fmt.table_row(on=1)) |
paul@281 | 2123 | |
paul@281 | 2124 | # Then, output day details. |
paul@281 | 2125 | |
paul@281 | 2126 | for weekday in range(0, 7): |
paul@281 | 2127 | day = first_day + weekday |
paul@281 | 2128 | date = month.as_date(day) |
paul@281 | 2129 | |
paul@281 | 2130 | # Skip out-of-month days. |
paul@281 | 2131 | |
paul@281 | 2132 | if day < 1 or day > number_of_days: |
paul@308 | 2133 | append(fmt.table_cell(on=1, |
paul@281 | 2134 | attrs={"class" : "event-day-content event-day-excluded", "colspan" : "3"})) |
paul@308 | 2135 | append(fmt.table_cell(on=0)) |
paul@281 | 2136 | continue |
paul@281 | 2137 | |
paul@281 | 2138 | # Output the day. |
paul@281 | 2139 | |
paul@281 | 2140 | if date not in events: |
paul@308 | 2141 | append(fmt.table_cell(on=1, |
paul@281 | 2142 | attrs={"class" : "event-day-content event-day-empty", "colspan" : "3"})) |
paul@281 | 2143 | |
paul@281 | 2144 | # Get event details for the current day. |
paul@281 | 2145 | |
paul@281 | 2146 | for event in events: |
paul@281 | 2147 | event_details = event.getDetails() |
paul@281 | 2148 | |
paul@281 | 2149 | if date not in event: |
paul@281 | 2150 | continue |
paul@281 | 2151 | |
paul@281 | 2152 | # Get basic properties of the event. |
paul@281 | 2153 | |
paul@281 | 2154 | starts_today = event_details["start"] == date |
paul@281 | 2155 | ends_today = event_details["end"] == date |
paul@281 | 2156 | event_summary = event.getSummary(self.parent_name) |
paul@281 | 2157 | |
paul@281 | 2158 | style = self.getEventStyle(event_summary) |
paul@281 | 2159 | |
paul@281 | 2160 | # Determine if the event name should be shown. |
paul@281 | 2161 | |
paul@281 | 2162 | start_of_period = starts_today or weekday == 0 or day == 1 |
paul@281 | 2163 | |
paul@281 | 2164 | if self.name_usage == "daily" or start_of_period: |
paul@281 | 2165 | hide_text = 0 |
paul@281 | 2166 | else: |
paul@281 | 2167 | hide_text = 1 |
paul@281 | 2168 | |
paul@281 | 2169 | # Output start of day gap and determine whether |
paul@281 | 2170 | # any event content should be explicitly output |
paul@281 | 2171 | # for this day. |
paul@281 | 2172 | |
paul@281 | 2173 | if starts_today: |
paul@281 | 2174 | |
paul@281 | 2175 | # Single day events... |
paul@281 | 2176 | |
paul@281 | 2177 | if ends_today: |
paul@281 | 2178 | colspan = 3 |
paul@281 | 2179 | event_day_type = "event-day-single" |
paul@281 | 2180 | |
paul@281 | 2181 | # Events starting today... |
paul@281 | 2182 | |
paul@281 | 2183 | else: |
paul@308 | 2184 | append(fmt.table_cell(on=1, attrs={"class" : "event-day-start-gap"})) |
paul@308 | 2185 | append(fmt.table_cell(on=0)) |
paul@281 | 2186 | |
paul@281 | 2187 | # Calculate the span of this cell. |
paul@281 | 2188 | # Events whose names appear on every day... |
paul@281 | 2189 | |
paul@281 | 2190 | if self.name_usage == "daily": |
paul@281 | 2191 | colspan = 2 |
paul@281 | 2192 | event_day_type = "event-day-starting" |
paul@281 | 2193 | |
paul@281 | 2194 | # Events whose names appear once per week... |
paul@281 | 2195 | |
paul@281 | 2196 | else: |
paul@281 | 2197 | if event_details["end"] <= week_end: |
paul@281 | 2198 | event_length = event_details["end"].day() - day + 1 |
paul@281 | 2199 | colspan = (event_length - 2) * 3 + 4 |
paul@281 | 2200 | else: |
paul@281 | 2201 | event_length = week_end.day() - day + 1 |
paul@281 | 2202 | colspan = (event_length - 1) * 3 + 2 |
paul@281 | 2203 | |
paul@281 | 2204 | event_day_type = "event-day-multiple" |
paul@281 | 2205 | |
paul@281 | 2206 | # Events continuing from a previous week... |
paul@281 | 2207 | |
paul@281 | 2208 | elif start_of_period: |
paul@281 | 2209 | |
paul@281 | 2210 | # End of continuing event... |
paul@281 | 2211 | |
paul@281 | 2212 | if ends_today: |
paul@281 | 2213 | colspan = 2 |
paul@281 | 2214 | event_day_type = "event-day-ending" |
paul@281 | 2215 | |
paul@281 | 2216 | # Events continuing for at least one more day... |
paul@281 | 2217 | |
paul@281 | 2218 | else: |
paul@281 | 2219 | |
paul@281 | 2220 | # Calculate the span of this cell. |
paul@281 | 2221 | # Events whose names appear on every day... |
paul@281 | 2222 | |
paul@281 | 2223 | if self.name_usage == "daily": |
paul@281 | 2224 | colspan = 3 |
paul@281 | 2225 | event_day_type = "event-day-full" |
paul@281 | 2226 | |
paul@281 | 2227 | # Events whose names appear once per week... |
paul@281 | 2228 | |
paul@281 | 2229 | else: |
paul@281 | 2230 | if event_details["end"] <= week_end: |
paul@281 | 2231 | event_length = event_details["end"].day() - day + 1 |
paul@281 | 2232 | colspan = (event_length - 1) * 3 + 2 |
paul@281 | 2233 | else: |
paul@281 | 2234 | event_length = week_end.day() - day + 1 |
paul@281 | 2235 | colspan = event_length * 3 |
paul@281 | 2236 | |
paul@281 | 2237 | event_day_type = "event-day-multiple" |
paul@281 | 2238 | |
paul@281 | 2239 | # Continuing events whose names appear on every day... |
paul@281 | 2240 | |
paul@281 | 2241 | elif self.name_usage == "daily": |
paul@281 | 2242 | if ends_today: |
paul@281 | 2243 | colspan = 2 |
paul@281 | 2244 | event_day_type = "event-day-ending" |
paul@281 | 2245 | else: |
paul@281 | 2246 | colspan = 3 |
paul@281 | 2247 | event_day_type = "event-day-full" |
paul@281 | 2248 | |
paul@281 | 2249 | # Continuing events whose names appear once per week... |
paul@281 | 2250 | |
paul@281 | 2251 | else: |
paul@281 | 2252 | colspan = None |
paul@281 | 2253 | |
paul@281 | 2254 | # Output the main content only if it is not |
paul@281 | 2255 | # continuing from a previous day. |
paul@281 | 2256 | |
paul@281 | 2257 | if colspan is not None: |
paul@281 | 2258 | |
paul@281 | 2259 | # Colour the cell for continuing events. |
paul@281 | 2260 | |
paul@281 | 2261 | attrs={ |
paul@281 | 2262 | "class" : "event-day-content event-day-busy %s" % event_day_type, |
paul@281 | 2263 | "colspan" : str(colspan) |
paul@281 | 2264 | } |
paul@281 | 2265 | |
paul@281 | 2266 | if not (starts_today and ends_today): |
paul@281 | 2267 | attrs["style"] = style |
paul@281 | 2268 | |
paul@308 | 2269 | append(fmt.table_cell(on=1, attrs=attrs)) |
paul@281 | 2270 | |
paul@281 | 2271 | # Output the event. |
paul@281 | 2272 | |
paul@281 | 2273 | if starts_today and ends_today or not hide_text: |
paul@308 | 2274 | append(self.writeEventSummaryBox(event)) |
paul@308 | 2275 | |
paul@308 | 2276 | append(fmt.table_cell(on=0)) |
paul@281 | 2277 | |
paul@281 | 2278 | # Output end of day gap. |
paul@281 | 2279 | |
paul@281 | 2280 | if ends_today and not starts_today: |
paul@308 | 2281 | append(fmt.table_cell(on=1, attrs={"class" : "event-day-end-gap"})) |
paul@308 | 2282 | append(fmt.table_cell(on=0)) |
paul@281 | 2283 | |
paul@281 | 2284 | # End of set. |
paul@281 | 2285 | |
paul@308 | 2286 | append(fmt.table_row(on=0)) |
paul@281 | 2287 | return "".join(output) |
paul@281 | 2288 | |
paul@281 | 2289 | def writeWeekSpacer(self, first_day, number_of_days): |
paul@281 | 2290 | page = self.page |
paul@282 | 2291 | fmt = page.request.formatter |
paul@281 | 2292 | |
paul@281 | 2293 | output = [] |
paul@308 | 2294 | append = output.append |
paul@308 | 2295 | |
paul@308 | 2296 | append(fmt.table_row(on=1)) |
paul@281 | 2297 | |
paul@281 | 2298 | for weekday in range(0, 7): |
paul@281 | 2299 | day = first_day + weekday |
paul@281 | 2300 | css_classes = "event-day-spacer" |
paul@281 | 2301 | |
paul@281 | 2302 | # Skip out-of-month days. |
paul@281 | 2303 | |
paul@281 | 2304 | if day < 1 or day > number_of_days: |
paul@281 | 2305 | css_classes += " event-day-excluded" |
paul@281 | 2306 | |
paul@308 | 2307 | append(fmt.table_cell(on=1, attrs={"class" : css_classes, "colspan" : "3"})) |
paul@308 | 2308 | append(fmt.table_cell(on=0)) |
paul@308 | 2309 | |
paul@308 | 2310 | append(fmt.table_row(on=0)) |
paul@281 | 2311 | return "".join(output) |
paul@281 | 2312 | |
paul@281 | 2313 | # Day layout methods. |
paul@281 | 2314 | |
paul@281 | 2315 | def writeDayTableHeading(self, date, colspan=1): |
paul@281 | 2316 | page = self.page |
paul@282 | 2317 | fmt = page.request.formatter |
paul@281 | 2318 | |
paul@281 | 2319 | output = [] |
paul@308 | 2320 | append = output.append |
paul@308 | 2321 | |
paul@308 | 2322 | append(fmt.table_row(on=1)) |
paul@308 | 2323 | |
paul@308 | 2324 | append(fmt.table_cell(on=1, attrs={"class" : "event-full-day-heading", "colspan" : str(colspan)})) |
paul@308 | 2325 | append(self.writeDayHeading(date)) |
paul@308 | 2326 | append(fmt.table_cell(on=0)) |
paul@308 | 2327 | |
paul@308 | 2328 | append(fmt.table_row(on=0)) |
paul@281 | 2329 | return "".join(output) |
paul@281 | 2330 | |
paul@281 | 2331 | def writeEmptyDay(self, date): |
paul@281 | 2332 | page = self.page |
paul@282 | 2333 | fmt = page.request.formatter |
paul@281 | 2334 | |
paul@281 | 2335 | output = [] |
paul@308 | 2336 | append = output.append |
paul@308 | 2337 | |
paul@308 | 2338 | append(fmt.table_row(on=1)) |
paul@308 | 2339 | |
paul@308 | 2340 | append(fmt.table_cell(on=1, |
paul@281 | 2341 | attrs={"class" : "event-day-content event-day-empty"})) |
paul@281 | 2342 | |
paul@308 | 2343 | append(fmt.table_row(on=0)) |
paul@281 | 2344 | return "".join(output) |
paul@281 | 2345 | |
paul@281 | 2346 | def writeDaySlots(self, date, full_coverage, day_slots): |
paul@281 | 2347 | |
paul@281 | 2348 | """ |
paul@281 | 2349 | Given a 'date', non-empty 'full_coverage' for the day concerned, and a |
paul@281 | 2350 | non-empty mapping of 'day_slots' (from locations to event collections), |
paul@281 | 2351 | output the day slots for the day. |
paul@281 | 2352 | """ |
paul@281 | 2353 | |
paul@281 | 2354 | page = self.page |
paul@282 | 2355 | fmt = page.request.formatter |
paul@281 | 2356 | |
paul@281 | 2357 | output = [] |
paul@308 | 2358 | append = output.append |
paul@281 | 2359 | |
paul@281 | 2360 | locations = day_slots.keys() |
paul@281 | 2361 | locations.sort(sort_none_first) |
paul@281 | 2362 | |
paul@281 | 2363 | # Traverse the time scale of the full coverage, visiting each slot to |
paul@281 | 2364 | # determine whether it provides content for each period. |
paul@281 | 2365 | |
paul@281 | 2366 | scale = getCoverageScale(full_coverage) |
paul@281 | 2367 | |
paul@281 | 2368 | # Define a mapping of events to rowspans. |
paul@281 | 2369 | |
paul@281 | 2370 | rowspans = {} |
paul@281 | 2371 | |
paul@281 | 2372 | # Populate each period with event details, recording how many periods |
paul@281 | 2373 | # each event populates. |
paul@281 | 2374 | |
paul@281 | 2375 | day_rows = [] |
paul@281 | 2376 | |
paul@281 | 2377 | for period in scale: |
paul@281 | 2378 | |
paul@281 | 2379 | # Ignore timespans before this day. |
paul@281 | 2380 | |
paul@281 | 2381 | if period != date: |
paul@281 | 2382 | continue |
paul@281 | 2383 | |
paul@281 | 2384 | # Visit each slot corresponding to a location (or no location). |
paul@281 | 2385 | |
paul@281 | 2386 | day_row = [] |
paul@281 | 2387 | |
paul@281 | 2388 | for location in locations: |
paul@281 | 2389 | |
paul@281 | 2390 | # Visit each coverage span, presenting the events in the span. |
paul@281 | 2391 | |
paul@281 | 2392 | for events in day_slots[location]: |
paul@281 | 2393 | event = self.getActiveEvent(period, events) |
paul@281 | 2394 | if event is not None: |
paul@281 | 2395 | if not rowspans.has_key(event): |
paul@281 | 2396 | rowspans[event] = 1 |
paul@281 | 2397 | else: |
paul@281 | 2398 | rowspans[event] += 1 |
paul@281 | 2399 | day_row.append((location, event)) |
paul@281 | 2400 | |
paul@281 | 2401 | day_rows.append((period, day_row)) |
paul@281 | 2402 | |
paul@281 | 2403 | # Output the locations. |
paul@281 | 2404 | |
paul@308 | 2405 | append(fmt.table_row(on=1)) |
paul@281 | 2406 | |
paul@281 | 2407 | # Add a spacer. |
paul@281 | 2408 | |
paul@308 | 2409 | append(self.writeDaySpacer(colspan=2, cls="location")) |
paul@281 | 2410 | |
paul@281 | 2411 | for location in locations: |
paul@281 | 2412 | |
paul@281 | 2413 | # Add spacers to the column spans. |
paul@281 | 2414 | |
paul@281 | 2415 | columns = len(day_slots[location]) * 2 - 1 |
paul@308 | 2416 | append(fmt.table_cell(on=1, attrs={"class" : "event-location-heading", "colspan" : str(columns)})) |
paul@308 | 2417 | append(fmt.text(location or "")) |
paul@308 | 2418 | append(fmt.table_cell(on=0)) |
paul@281 | 2419 | |
paul@281 | 2420 | # Add a trailing spacer. |
paul@281 | 2421 | |
paul@308 | 2422 | append(self.writeDaySpacer(cls="location")) |
paul@308 | 2423 | |
paul@308 | 2424 | append(fmt.table_row(on=0)) |
paul@281 | 2425 | |
paul@281 | 2426 | # Output the periods with event details. |
paul@281 | 2427 | |
paul@281 | 2428 | period = None |
paul@281 | 2429 | events_written = set() |
paul@281 | 2430 | |
paul@281 | 2431 | for period, day_row in day_rows: |
paul@281 | 2432 | |
paul@281 | 2433 | # Write an empty heading for the start of the day where the first |
paul@281 | 2434 | # applicable timespan starts before this day. |
paul@281 | 2435 | |
paul@281 | 2436 | if period.start < date: |
paul@308 | 2437 | append(fmt.table_row(on=1)) |
paul@308 | 2438 | append(self.writeDayScaleHeading("")) |
paul@281 | 2439 | |
paul@281 | 2440 | # Otherwise, write a heading describing the time. |
paul@281 | 2441 | |
paul@281 | 2442 | else: |
paul@308 | 2443 | append(fmt.table_row(on=1)) |
paul@308 | 2444 | append(self.writeDayScaleHeading(period.start.time_string())) |
paul@308 | 2445 | |
paul@308 | 2446 | append(self.writeDaySpacer()) |
paul@281 | 2447 | |
paul@281 | 2448 | # Visit each slot corresponding to a location (or no location). |
paul@281 | 2449 | |
paul@281 | 2450 | for location, event in day_row: |
paul@281 | 2451 | |
paul@281 | 2452 | # Output each location slot's contribution. |
paul@281 | 2453 | |
paul@281 | 2454 | if event is None or event not in events_written: |
paul@308 | 2455 | append(self.writeDaySlot(period, event, event is None and 1 or rowspans[event])) |
paul@281 | 2456 | if event is not None: |
paul@281 | 2457 | events_written.add(event) |
paul@281 | 2458 | |
paul@281 | 2459 | # Add a trailing spacer. |
paul@281 | 2460 | |
paul@308 | 2461 | append(self.writeDaySpacer()) |
paul@308 | 2462 | |
paul@308 | 2463 | append(fmt.table_row(on=0)) |
paul@281 | 2464 | |
paul@281 | 2465 | # Write a final time heading if the last period ends in the current day. |
paul@281 | 2466 | |
paul@281 | 2467 | if period is not None: |
paul@281 | 2468 | if period.end == date: |
paul@308 | 2469 | append(fmt.table_row(on=1)) |
paul@308 | 2470 | append(self.writeDayScaleHeading(period.end.time_string())) |
paul@281 | 2471 | |
paul@281 | 2472 | for slot in day_row: |
paul@308 | 2473 | append(self.writeDaySpacer()) |
paul@308 | 2474 | append(self.writeEmptyDaySlot()) |
paul@308 | 2475 | |
paul@308 | 2476 | append(fmt.table_row(on=0)) |
paul@281 | 2477 | |
paul@281 | 2478 | return "".join(output) |
paul@281 | 2479 | |
paul@281 | 2480 | def writeDayScaleHeading(self, heading): |
paul@281 | 2481 | page = self.page |
paul@282 | 2482 | fmt = page.request.formatter |
paul@281 | 2483 | |
paul@281 | 2484 | output = [] |
paul@308 | 2485 | append = output.append |
paul@308 | 2486 | |
paul@308 | 2487 | append(fmt.table_cell(on=1, attrs={"class" : "event-scale-heading"})) |
paul@308 | 2488 | append(fmt.text(heading)) |
paul@308 | 2489 | append(fmt.table_cell(on=0)) |
paul@281 | 2490 | |
paul@281 | 2491 | return "".join(output) |
paul@281 | 2492 | |
paul@281 | 2493 | def getActiveEvent(self, period, events): |
paul@281 | 2494 | for event in events: |
paul@281 | 2495 | if period not in event: |
paul@281 | 2496 | continue |
paul@281 | 2497 | return event |
paul@281 | 2498 | else: |
paul@281 | 2499 | return None |
paul@281 | 2500 | |
paul@281 | 2501 | def writeDaySlot(self, period, event, rowspan): |
paul@281 | 2502 | page = self.page |
paul@282 | 2503 | fmt = page.request.formatter |
paul@281 | 2504 | |
paul@281 | 2505 | output = [] |
paul@308 | 2506 | append = output.append |
paul@281 | 2507 | |
paul@281 | 2508 | if event is not None: |
paul@281 | 2509 | event_summary = event.getSummary(self.parent_name) |
paul@281 | 2510 | style = self.getEventStyle(event_summary) |
paul@281 | 2511 | |
paul@308 | 2512 | append(fmt.table_cell(on=1, attrs={ |
paul@281 | 2513 | "class" : "event-timespan-content event-timespan-busy", |
paul@281 | 2514 | "style" : style, |
paul@281 | 2515 | "rowspan" : str(rowspan) |
paul@281 | 2516 | })) |
paul@308 | 2517 | append(self.writeEventSummaryBox(event)) |
paul@308 | 2518 | append(fmt.table_cell(on=0)) |
paul@281 | 2519 | else: |
paul@308 | 2520 | append(self.writeEmptyDaySlot()) |
paul@281 | 2521 | |
paul@281 | 2522 | return "".join(output) |
paul@281 | 2523 | |
paul@281 | 2524 | def writeEmptyDaySlot(self): |
paul@281 | 2525 | page = self.page |
paul@282 | 2526 | fmt = page.request.formatter |
paul@281 | 2527 | |
paul@281 | 2528 | output = [] |
paul@308 | 2529 | append = output.append |
paul@308 | 2530 | |
paul@308 | 2531 | append(fmt.table_cell(on=1, |
paul@281 | 2532 | attrs={"class" : "event-timespan-content event-timespan-empty"})) |
paul@308 | 2533 | append(fmt.table_cell(on=0)) |
paul@281 | 2534 | |
paul@281 | 2535 | return "".join(output) |
paul@281 | 2536 | |
paul@281 | 2537 | def writeDaySpacer(self, colspan=1, cls="timespan"): |
paul@281 | 2538 | page = self.page |
paul@282 | 2539 | fmt = page.request.formatter |
paul@281 | 2540 | |
paul@281 | 2541 | output = [] |
paul@308 | 2542 | append = output.append |
paul@308 | 2543 | |
paul@308 | 2544 | append(fmt.table_cell(on=1, attrs={ |
paul@281 | 2545 | "class" : "event-%s-spacer" % cls, |
paul@281 | 2546 | "colspan" : str(colspan)})) |
paul@308 | 2547 | append(fmt.table_cell(on=0)) |
paul@281 | 2548 | return "".join(output) |
paul@281 | 2549 | |
paul@281 | 2550 | # Map layout methods. |
paul@281 | 2551 | |
paul@281 | 2552 | def writeMapTableHeading(self): |
paul@281 | 2553 | page = self.page |
paul@282 | 2554 | fmt = page.request.formatter |
paul@281 | 2555 | |
paul@281 | 2556 | output = [] |
paul@308 | 2557 | append = output.append |
paul@308 | 2558 | |
paul@308 | 2559 | append(fmt.table_cell(on=1, attrs={"class" : "event-map-heading"})) |
paul@308 | 2560 | append(self.writeMapHeading()) |
paul@308 | 2561 | append(fmt.table_cell(on=0)) |
paul@281 | 2562 | |
paul@281 | 2563 | return "".join(output) |
paul@281 | 2564 | |
paul@281 | 2565 | def showDictError(self, text, pagename): |
paul@281 | 2566 | page = self.page |
paul@281 | 2567 | request = page.request |
paul@282 | 2568 | fmt = request.formatter |
paul@281 | 2569 | |
paul@281 | 2570 | output = [] |
paul@308 | 2571 | append = output.append |
paul@308 | 2572 | |
paul@308 | 2573 | append(fmt.div(on=1, attrs={"class" : "event-aggregator-error"})) |
paul@308 | 2574 | append(fmt.paragraph(on=1)) |
paul@308 | 2575 | append(fmt.text(text)) |
paul@308 | 2576 | append(fmt.paragraph(on=0)) |
paul@308 | 2577 | append(fmt.paragraph(on=1)) |
paul@308 | 2578 | append(linkToPage(request, Page(request, pagename), pagename)) |
paul@308 | 2579 | append(fmt.paragraph(on=0)) |
paul@281 | 2580 | |
paul@281 | 2581 | return "".join(output) |
paul@281 | 2582 | |
paul@281 | 2583 | def writeMapEventSummaries(self, events): |
paul@281 | 2584 | page = self.page |
paul@281 | 2585 | request = page.request |
paul@282 | 2586 | fmt = request.formatter |
paul@281 | 2587 | |
paul@281 | 2588 | # Sort the events by date. |
paul@281 | 2589 | |
paul@281 | 2590 | events.sort(sort_start_first) |
paul@281 | 2591 | |
paul@281 | 2592 | # Write out a self-contained list of events. |
paul@281 | 2593 | |
paul@281 | 2594 | output = [] |
paul@308 | 2595 | append = output.append |
paul@308 | 2596 | |
paul@308 | 2597 | append(fmt.bullet_list(on=1, attr={"class" : "event-map-location-events"})) |
paul@281 | 2598 | |
paul@281 | 2599 | for event in events: |
paul@281 | 2600 | |
paul@281 | 2601 | # Get the event details. |
paul@281 | 2602 | |
paul@281 | 2603 | event_summary = event.getSummary(self.parent_name) |
paul@281 | 2604 | start, end = event.as_limits() |
paul@281 | 2605 | event_period = self._getCalendarPeriod( |
paul@281 | 2606 | start and self.getFullDateLabel(start), |
paul@281 | 2607 | end and self.getFullDateLabel(end), |
paul@281 | 2608 | "") |
paul@281 | 2609 | |
paul@308 | 2610 | append(fmt.listitem(on=1)) |
paul@281 | 2611 | |
paul@281 | 2612 | # Link to the page using the summary. |
paul@281 | 2613 | |
paul@308 | 2614 | append(event.linkToEvent(request, event_summary)) |
paul@281 | 2615 | |
paul@281 | 2616 | # Add the event period. |
paul@281 | 2617 | |
paul@308 | 2618 | append(fmt.text(" ")) |
paul@308 | 2619 | append(fmt.span(on=1, css_class="event-map-period")) |
paul@308 | 2620 | append(fmt.text(event_period)) |
paul@308 | 2621 | append(fmt.span(on=0)) |
paul@308 | 2622 | |
paul@308 | 2623 | append(fmt.listitem(on=0)) |
paul@308 | 2624 | |
paul@308 | 2625 | append(fmt.bullet_list(on=0)) |
paul@281 | 2626 | |
paul@281 | 2627 | return "".join(output) |
paul@281 | 2628 | |
paul@281 | 2629 | def render(self, all_shown_events): |
paul@281 | 2630 | |
paul@281 | 2631 | """ |
paul@281 | 2632 | Render the view, returning the rendered representation as a string. |
paul@281 | 2633 | The view will show a list of 'all_shown_events'. |
paul@281 | 2634 | """ |
paul@281 | 2635 | |
paul@281 | 2636 | page = self.page |
paul@281 | 2637 | request = page.request |
paul@282 | 2638 | fmt = request.formatter |
paul@281 | 2639 | _ = request.getText |
paul@281 | 2640 | |
paul@281 | 2641 | # Make a calendar. |
paul@281 | 2642 | |
paul@281 | 2643 | output = [] |
paul@308 | 2644 | append = output.append |
paul@308 | 2645 | |
paul@308 | 2646 | append(fmt.div(on=1, css_class="event-calendar", id=("EventAggregator-%s" % self.getIdentifier()))) |
paul@281 | 2647 | |
paul@281 | 2648 | # Output download controls. |
paul@281 | 2649 | |
paul@308 | 2650 | append(fmt.div(on=1, css_class="event-controls")) |
paul@308 | 2651 | append(self.writeDownloadControls()) |
paul@308 | 2652 | append(fmt.div(on=0)) |
paul@281 | 2653 | |
paul@281 | 2654 | # Output a table. |
paul@281 | 2655 | |
paul@281 | 2656 | if self.mode == "table": |
paul@281 | 2657 | |
paul@281 | 2658 | # Start of table view output. |
paul@281 | 2659 | |
paul@308 | 2660 | append(fmt.table(on=1, attrs={"tableclass" : "event-table"})) |
paul@308 | 2661 | |
paul@308 | 2662 | append(fmt.table_row(on=1)) |
paul@308 | 2663 | append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@308 | 2664 | append(fmt.text(_("Event dates"))) |
paul@308 | 2665 | append(fmt.table_cell(on=0)) |
paul@308 | 2666 | append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@308 | 2667 | append(fmt.text(_("Event location"))) |
paul@308 | 2668 | append(fmt.table_cell(on=0)) |
paul@308 | 2669 | append(fmt.table_cell(on=1, attrs={"class" : "event-table-heading"})) |
paul@308 | 2670 | append(fmt.text(_("Event details"))) |
paul@308 | 2671 | append(fmt.table_cell(on=0)) |
paul@308 | 2672 | append(fmt.table_row(on=0)) |
paul@281 | 2673 | |
paul@281 | 2674 | # Show the events in order. |
paul@281 | 2675 | |
paul@281 | 2676 | all_shown_events.sort(sort_start_first) |
paul@281 | 2677 | |
paul@281 | 2678 | for event in all_shown_events: |
paul@281 | 2679 | event_page = event.getPage() |
paul@281 | 2680 | event_summary = event.getSummary(self.parent_name) |
paul@281 | 2681 | event_details = event.getDetails() |
paul@281 | 2682 | |
paul@281 | 2683 | # Prepare CSS classes with category-related styling. |
paul@281 | 2684 | |
paul@281 | 2685 | css_classes = ["event-table-details"] |
paul@281 | 2686 | |
paul@281 | 2687 | for topic in event_details.get("topics") or event_details.get("categories") or []: |
paul@281 | 2688 | |
paul@281 | 2689 | # Filter the category text to avoid illegal characters. |
paul@281 | 2690 | |
paul@281 | 2691 | css_classes.append("event-table-category-%s" % "".join(filter(lambda c: c.isalnum(), topic))) |
paul@281 | 2692 | |
paul@281 | 2693 | attrs = {"class" : " ".join(css_classes)} |
paul@281 | 2694 | |
paul@308 | 2695 | append(fmt.table_row(on=1)) |
paul@281 | 2696 | |
paul@281 | 2697 | # Start and end dates. |
paul@281 | 2698 | |
paul@308 | 2699 | append(fmt.table_cell(on=1, attrs=attrs)) |
paul@308 | 2700 | append(fmt.span(on=1)) |
paul@308 | 2701 | append(fmt.text(str(event_details["start"]))) |
paul@308 | 2702 | append(fmt.span(on=0)) |
paul@281 | 2703 | |
paul@281 | 2704 | if event_details["start"] != event_details["end"]: |
paul@308 | 2705 | append(fmt.text(" - ")) |
paul@308 | 2706 | append(fmt.span(on=1)) |
paul@308 | 2707 | append(fmt.text(str(event_details["end"]))) |
paul@308 | 2708 | append(fmt.span(on=0)) |
paul@308 | 2709 | |
paul@308 | 2710 | append(fmt.table_cell(on=0)) |
paul@281 | 2711 | |
paul@281 | 2712 | # Location. |
paul@281 | 2713 | |
paul@308 | 2714 | append(fmt.table_cell(on=1, attrs=attrs)) |
paul@281 | 2715 | |
paul@281 | 2716 | if event_details.has_key("location"): |
paul@308 | 2717 | append(event_page.formatText(event_details["location"], fmt)) |
paul@308 | 2718 | |
paul@308 | 2719 | append(fmt.table_cell(on=0)) |
paul@281 | 2720 | |
paul@281 | 2721 | # Link to the page using the summary. |
paul@281 | 2722 | |
paul@308 | 2723 | append(fmt.table_cell(on=1, attrs=attrs)) |
paul@308 | 2724 | append(event.linkToEvent(request, event_summary)) |
paul@308 | 2725 | append(fmt.table_cell(on=0)) |
paul@308 | 2726 | |
paul@308 | 2727 | append(fmt.table_row(on=0)) |
paul@281 | 2728 | |
paul@281 | 2729 | # End of table view output. |
paul@281 | 2730 | |
paul@308 | 2731 | append(fmt.table(on=0)) |
paul@281 | 2732 | |
paul@281 | 2733 | # Output a map view. |
paul@281 | 2734 | |
paul@281 | 2735 | elif self.mode == "map": |
paul@281 | 2736 | |
paul@281 | 2737 | # Special dictionary pages. |
paul@281 | 2738 | |
paul@294 | 2739 | maps_page = getMapsPage(request) |
paul@294 | 2740 | locations_page = getLocationsPage(request) |
paul@281 | 2741 | |
paul@281 | 2742 | map_image = None |
paul@281 | 2743 | |
paul@281 | 2744 | # Get the maps and locations. |
paul@281 | 2745 | |
paul@281 | 2746 | maps = getWikiDict(maps_page, request) |
paul@281 | 2747 | locations = getWikiDict(locations_page, request) |
paul@281 | 2748 | |
paul@281 | 2749 | # Get the map image definition. |
paul@281 | 2750 | |
paul@286 | 2751 | if maps is not None and self.map_name: |
paul@281 | 2752 | try: |
paul@281 | 2753 | map_details = maps[self.map_name].split() |
paul@281 | 2754 | |
paul@281 | 2755 | map_bottom_left_latitude, map_bottom_left_longitude, map_top_right_latitude, map_top_right_longitude = \ |
paul@281 | 2756 | map(getMapReference, map_details[:4]) |
paul@281 | 2757 | map_width, map_height = map(int, map_details[4:6]) |
paul@281 | 2758 | map_image = map_details[6] |
paul@281 | 2759 | |
paul@281 | 2760 | map_x_scale = map_width / (map_top_right_longitude - map_bottom_left_longitude).to_degrees() |
paul@281 | 2761 | map_y_scale = map_height / (map_top_right_latitude - map_bottom_left_latitude).to_degrees() |
paul@281 | 2762 | |
paul@281 | 2763 | except (KeyError, ValueError): |
paul@281 | 2764 | pass |
paul@281 | 2765 | |
paul@281 | 2766 | # Report errors. |
paul@281 | 2767 | |
paul@281 | 2768 | if maps is None: |
paul@308 | 2769 | append(self.showDictError( |
paul@281 | 2770 | _("You do not have read access to the maps page:"), |
paul@281 | 2771 | maps_page)) |
paul@281 | 2772 | |
paul@286 | 2773 | elif not self.map_name: |
paul@308 | 2774 | append(self.showDictError( |
paul@281 | 2775 | _("Please specify a valid map name corresponding to an entry on the following page:"), |
paul@281 | 2776 | maps_page)) |
paul@281 | 2777 | |
paul@281 | 2778 | elif map_image is None: |
paul@308 | 2779 | append(self.showDictError( |
paul@281 | 2780 | _("Please specify a valid entry for %s on the following page:") % self.map_name, |
paul@281 | 2781 | maps_page)) |
paul@281 | 2782 | |
paul@281 | 2783 | elif locations is None: |
paul@308 | 2784 | append(self.showDictError( |
paul@281 | 2785 | _("You do not have read access to the locations page:"), |
paul@281 | 2786 | locations_page)) |
paul@281 | 2787 | |
paul@281 | 2788 | # Attempt to show the map. |
paul@281 | 2789 | |
paul@281 | 2790 | else: |
paul@281 | 2791 | |
paul@281 | 2792 | # Get events by position. |
paul@281 | 2793 | |
paul@281 | 2794 | events_by_location = {} |
paul@281 | 2795 | event_locations = {} |
paul@281 | 2796 | |
paul@281 | 2797 | for event in all_shown_events: |
paul@281 | 2798 | event_details = event.getDetails() |
paul@281 | 2799 | |
paul@281 | 2800 | location = event_details.get("location") |
paul@281 | 2801 | |
paul@281 | 2802 | if location is not None and not event_locations.has_key(location): |
paul@281 | 2803 | |
paul@281 | 2804 | # Get any explicit position of an event. |
paul@281 | 2805 | |
paul@281 | 2806 | if event_details.has_key("geo"): |
paul@281 | 2807 | latitude, longitude = event_details["geo"] |
paul@281 | 2808 | |
paul@281 | 2809 | # Or look up the position of a location using the locations |
paul@281 | 2810 | # page. |
paul@281 | 2811 | |
paul@281 | 2812 | else: |
paul@294 | 2813 | latitude, longitude = Location(location, locations).getPosition() |
paul@281 | 2814 | |
paul@281 | 2815 | # Use a normalised location if necessary. |
paul@281 | 2816 | |
paul@281 | 2817 | if latitude is None and longitude is None: |
paul@281 | 2818 | normalised_location = getNormalisedLocation(location) |
paul@281 | 2819 | if normalised_location is not None: |
paul@281 | 2820 | latitude, longitude = getLocationPosition(normalised_location, locations) |
paul@281 | 2821 | if latitude is not None and longitude is not None: |
paul@281 | 2822 | location = normalised_location |
paul@281 | 2823 | |
paul@281 | 2824 | # Only remember positioned locations. |
paul@281 | 2825 | |
paul@281 | 2826 | if latitude is not None and longitude is not None: |
paul@281 | 2827 | event_locations[location] = latitude, longitude |
paul@281 | 2828 | |
paul@281 | 2829 | # Record events according to location. |
paul@281 | 2830 | |
paul@281 | 2831 | if not events_by_location.has_key(location): |
paul@281 | 2832 | events_by_location[location] = [] |
paul@281 | 2833 | |
paul@281 | 2834 | events_by_location[location].append(event) |
paul@281 | 2835 | |
paul@281 | 2836 | # Get the map image URL. |
paul@281 | 2837 | |
paul@281 | 2838 | map_image_url = AttachFile.getAttachUrl(maps_page, map_image, request) |
paul@281 | 2839 | |
paul@281 | 2840 | # Start of map view output. |
paul@281 | 2841 | |
paul@281 | 2842 | map_identifier = "map-%s" % self.getIdentifier() |
paul@308 | 2843 | append(fmt.div(on=1, css_class="event-map", id=map_identifier)) |
paul@308 | 2844 | |
paul@308 | 2845 | append(fmt.table(on=1)) |
paul@308 | 2846 | |
paul@308 | 2847 | append(fmt.table_row(on=1)) |
paul@308 | 2848 | append(self.writeMapTableHeading()) |
paul@308 | 2849 | append(fmt.table_row(on=0)) |
paul@308 | 2850 | |
paul@308 | 2851 | append(fmt.table_row(on=1)) |
paul@308 | 2852 | append(fmt.table_cell(on=1)) |
paul@308 | 2853 | |
paul@308 | 2854 | append(fmt.div(on=1, css_class="event-map-container")) |
paul@308 | 2855 | append(fmt.image(map_image_url)) |
paul@308 | 2856 | append(fmt.number_list(on=1)) |
paul@281 | 2857 | |
paul@281 | 2858 | # Events with no location are unpositioned. |
paul@281 | 2859 | |
paul@281 | 2860 | if events_by_location.has_key(None): |
paul@281 | 2861 | unpositioned_events = events_by_location[None] |
paul@281 | 2862 | del events_by_location[None] |
paul@281 | 2863 | else: |
paul@281 | 2864 | unpositioned_events = [] |
paul@281 | 2865 | |
paul@281 | 2866 | # Events whose location is unpositioned are themselves considered |
paul@281 | 2867 | # unpositioned. |
paul@281 | 2868 | |
paul@281 | 2869 | for location in set(events_by_location.keys()).difference(event_locations.keys()): |
paul@281 | 2870 | unpositioned_events += events_by_location[location] |
paul@281 | 2871 | |
paul@281 | 2872 | # Sort the locations before traversing them. |
paul@281 | 2873 | |
paul@281 | 2874 | event_locations = event_locations.items() |
paul@281 | 2875 | event_locations.sort() |
paul@281 | 2876 | |
paul@281 | 2877 | # Show the events in the map. |
paul@281 | 2878 | |
paul@281 | 2879 | for location, (latitude, longitude) in event_locations: |
paul@281 | 2880 | events = events_by_location[location] |
paul@281 | 2881 | |
paul@281 | 2882 | # Skip unpositioned locations and locations outside the map. |
paul@281 | 2883 | |
paul@281 | 2884 | if latitude is None or longitude is None or \ |
paul@281 | 2885 | latitude < map_bottom_left_latitude or \ |
paul@281 | 2886 | longitude < map_bottom_left_longitude or \ |
paul@281 | 2887 | latitude > map_top_right_latitude or \ |
paul@281 | 2888 | longitude > map_top_right_longitude: |
paul@281 | 2889 | |
paul@281 | 2890 | unpositioned_events += events |
paul@281 | 2891 | continue |
paul@281 | 2892 | |
paul@281 | 2893 | # Get the position and dimensions of the map marker. |
paul@281 | 2894 | # NOTE: Use one degree as the marker size. |
paul@281 | 2895 | |
paul@281 | 2896 | marker_x, marker_y = getPositionForCentrePoint( |
paul@281 | 2897 | getPositionForReference(map_top_right_latitude, longitude, latitude, map_bottom_left_longitude, |
paul@281 | 2898 | map_x_scale, map_y_scale), |
paul@281 | 2899 | map_x_scale, map_y_scale) |
paul@281 | 2900 | |
paul@281 | 2901 | # Put a marker on the map. |
paul@281 | 2902 | |
paul@308 | 2903 | append(fmt.listitem(on=1, css_class="event-map-label")) |
paul@281 | 2904 | |
paul@281 | 2905 | # Have a positioned marker for the print mode. |
paul@281 | 2906 | |
paul@308 | 2907 | append(fmt.div(on=1, css_class="event-map-label-only", |
paul@281 | 2908 | style="left:%dpx; top:%dpx; min-width:%dpx; min-height:%dpx") % ( |
paul@281 | 2909 | marker_x, marker_y, map_x_scale, map_y_scale)) |
paul@308 | 2910 | append(fmt.div(on=0)) |
paul@281 | 2911 | |
paul@281 | 2912 | # Have a marker containing a pop-up when using the screen mode, |
paul@281 | 2913 | # providing a normal block when using the print mode. |
paul@281 | 2914 | |
paul@308 | 2915 | append(fmt.div(on=1, css_class="event-map-label", |
paul@281 | 2916 | style="left:%dpx; top:%dpx; min-width:%dpx; min-height:%dpx") % ( |
paul@281 | 2917 | marker_x, marker_y, map_x_scale, map_y_scale)) |
paul@308 | 2918 | append(fmt.div(on=1, css_class="event-map-details")) |
paul@308 | 2919 | append(fmt.div(on=1, css_class="event-map-shadow")) |
paul@308 | 2920 | append(fmt.div(on=1, css_class="event-map-location")) |
paul@308 | 2921 | |
paul@308 | 2922 | append(fmt.heading(on=1, depth=2)) |
paul@308 | 2923 | append(fmt.text(location)) |
paul@308 | 2924 | append(fmt.heading(on=0, depth=2)) |
paul@308 | 2925 | |
paul@308 | 2926 | append(self.writeMapEventSummaries(events)) |
paul@308 | 2927 | |
paul@308 | 2928 | append(fmt.div(on=0)) |
paul@308 | 2929 | append(fmt.div(on=0)) |
paul@308 | 2930 | append(fmt.div(on=0)) |
paul@308 | 2931 | append(fmt.div(on=0)) |
paul@308 | 2932 | append(fmt.listitem(on=0)) |
paul@308 | 2933 | |
paul@308 | 2934 | append(fmt.number_list(on=0)) |
paul@308 | 2935 | append(fmt.div(on=0)) |
paul@308 | 2936 | append(fmt.table_cell(on=0)) |
paul@308 | 2937 | append(fmt.table_row(on=0)) |
paul@281 | 2938 | |
paul@281 | 2939 | # Write unpositioned events. |
paul@281 | 2940 | |
paul@281 | 2941 | if unpositioned_events: |
paul@281 | 2942 | unpositioned_identifier = "unpositioned-%s" % self.getIdentifier() |
paul@281 | 2943 | |
paul@308 | 2944 | append(fmt.table_row(on=1, css_class="event-map-unpositioned", |
paul@281 | 2945 | id=unpositioned_identifier)) |
paul@308 | 2946 | append(fmt.table_cell(on=1)) |
paul@308 | 2947 | |
paul@308 | 2948 | append(fmt.heading(on=1, depth=2)) |
paul@308 | 2949 | append(fmt.text(_("Events not shown on the map"))) |
paul@308 | 2950 | append(fmt.heading(on=0, depth=2)) |
paul@281 | 2951 | |
paul@281 | 2952 | # Show and hide controls. |
paul@281 | 2953 | |
paul@308 | 2954 | append(fmt.div(on=1, css_class="event-map-show-control")) |
paul@308 | 2955 | append(fmt.anchorlink(on=1, name=unpositioned_identifier)) |
paul@308 | 2956 | append(fmt.text(_("Show unpositioned events"))) |
paul@308 | 2957 | append(fmt.anchorlink(on=0)) |
paul@308 | 2958 | append(fmt.div(on=0)) |
paul@308 | 2959 | |
paul@308 | 2960 | append(fmt.div(on=1, css_class="event-map-hide-control")) |
paul@308 | 2961 | append(fmt.anchorlink(on=1, name=map_identifier)) |
paul@308 | 2962 | append(fmt.text(_("Hide unpositioned events"))) |
paul@308 | 2963 | append(fmt.anchorlink(on=0)) |
paul@308 | 2964 | append(fmt.div(on=0)) |
paul@308 | 2965 | |
paul@308 | 2966 | append(self.writeMapEventSummaries(unpositioned_events)) |
paul@281 | 2967 | |
paul@281 | 2968 | # End of map view output. |
paul@281 | 2969 | |
paul@308 | 2970 | append(fmt.table_cell(on=0)) |
paul@308 | 2971 | append(fmt.table_row(on=0)) |
paul@308 | 2972 | append(fmt.table(on=0)) |
paul@308 | 2973 | append(fmt.div(on=0)) |
paul@281 | 2974 | |
paul@281 | 2975 | # Output a list. |
paul@281 | 2976 | |
paul@281 | 2977 | elif self.mode == "list": |
paul@281 | 2978 | |
paul@281 | 2979 | # Start of list view output. |
paul@281 | 2980 | |
paul@308 | 2981 | append(fmt.bullet_list(on=1, attr={"class" : "event-listings"})) |
paul@281 | 2982 | |
paul@281 | 2983 | # Output a list. |
paul@281 | 2984 | |
paul@281 | 2985 | for period in self.first.until(self.last): |
paul@281 | 2986 | |
paul@308 | 2987 | append(fmt.listitem(on=1, attr={"class" : "event-listings-period"})) |
paul@308 | 2988 | append(fmt.div(on=1, attr={"class" : "event-listings-heading"})) |
paul@281 | 2989 | |
paul@281 | 2990 | # Either write a date heading or produce links for navigable |
paul@281 | 2991 | # calendars. |
paul@281 | 2992 | |
paul@308 | 2993 | append(self.writeDateHeading(period)) |
paul@308 | 2994 | |
paul@308 | 2995 | append(fmt.div(on=0)) |
paul@308 | 2996 | |
paul@308 | 2997 | append(fmt.bullet_list(on=1, attr={"class" : "event-period-listings"})) |
paul@281 | 2998 | |
paul@281 | 2999 | # Show the events in order. |
paul@281 | 3000 | |
paul@281 | 3001 | events_in_period = getEventsInPeriod(all_shown_events, getCalendarPeriod(period, period)) |
paul@281 | 3002 | events_in_period.sort(sort_start_first) |
paul@281 | 3003 | |
paul@281 | 3004 | for event in events_in_period: |
paul@281 | 3005 | event_page = event.getPage() |
paul@281 | 3006 | event_details = event.getDetails() |
paul@281 | 3007 | event_summary = event.getSummary(self.parent_name) |
paul@281 | 3008 | |
paul@308 | 3009 | append(fmt.listitem(on=1, attr={"class" : "event-listing"})) |
paul@281 | 3010 | |
paul@281 | 3011 | # Link to the page using the summary. |
paul@281 | 3012 | |
paul@308 | 3013 | append(fmt.paragraph(on=1)) |
paul@308 | 3014 | append(event.linkToEvent(request, event_summary)) |
paul@308 | 3015 | append(fmt.paragraph(on=0)) |
paul@281 | 3016 | |
paul@281 | 3017 | # Start and end dates. |
paul@281 | 3018 | |
paul@308 | 3019 | append(fmt.paragraph(on=1)) |
paul@308 | 3020 | append(fmt.span(on=1)) |
paul@308 | 3021 | append(fmt.text(str(event_details["start"]))) |
paul@308 | 3022 | append(fmt.span(on=0)) |
paul@308 | 3023 | append(fmt.text(" - ")) |
paul@308 | 3024 | append(fmt.span(on=1)) |
paul@308 | 3025 | append(fmt.text(str(event_details["end"]))) |
paul@308 | 3026 | append(fmt.span(on=0)) |
paul@308 | 3027 | append(fmt.paragraph(on=0)) |
paul@281 | 3028 | |
paul@281 | 3029 | # Location. |
paul@281 | 3030 | |
paul@281 | 3031 | if event_details.has_key("location"): |
paul@308 | 3032 | append(fmt.paragraph(on=1)) |
paul@308 | 3033 | append(event_page.formatText(event_details["location"], fmt)) |
paul@308 | 3034 | append(fmt.paragraph(on=1)) |
paul@281 | 3035 | |
paul@281 | 3036 | # Topics. |
paul@281 | 3037 | |
paul@281 | 3038 | if event_details.has_key("topics") or event_details.has_key("categories"): |
paul@308 | 3039 | append(fmt.bullet_list(on=1, attr={"class" : "event-topics"})) |
paul@281 | 3040 | |
paul@281 | 3041 | for topic in event_details.get("topics") or event_details.get("categories") or []: |
paul@308 | 3042 | append(fmt.listitem(on=1)) |
paul@308 | 3043 | append(event_page.formatText(topic, fmt)) |
paul@308 | 3044 | append(fmt.listitem(on=0)) |
paul@308 | 3045 | |
paul@308 | 3046 | append(fmt.bullet_list(on=0)) |
paul@308 | 3047 | |
paul@308 | 3048 | append(fmt.listitem(on=0)) |
paul@308 | 3049 | |
paul@308 | 3050 | append(fmt.bullet_list(on=0)) |
paul@281 | 3051 | |
paul@281 | 3052 | # End of list view output. |
paul@281 | 3053 | |
paul@308 | 3054 | append(fmt.bullet_list(on=0)) |
paul@281 | 3055 | |
paul@281 | 3056 | # Output a month calendar. This shows month-by-month data. |
paul@281 | 3057 | |
paul@281 | 3058 | elif self.mode == "calendar": |
paul@281 | 3059 | |
paul@281 | 3060 | # Visit all months in the requested range, or across known events. |
paul@281 | 3061 | |
paul@281 | 3062 | for month in self.first.months_until(self.last): |
paul@281 | 3063 | |
paul@281 | 3064 | # Output a month. |
paul@281 | 3065 | |
paul@308 | 3066 | append(fmt.table(on=1, attrs={"tableclass" : "event-month"})) |
paul@281 | 3067 | |
paul@281 | 3068 | # Either write a month heading or produce links for navigable |
paul@281 | 3069 | # calendars. |
paul@281 | 3070 | |
paul@308 | 3071 | append(self.writeMonthTableHeading(month)) |
paul@281 | 3072 | |
paul@281 | 3073 | # Weekday headings. |
paul@281 | 3074 | |
paul@308 | 3075 | append(self.writeWeekdayHeadings()) |
paul@281 | 3076 | |
paul@281 | 3077 | # Process the days of the month. |
paul@281 | 3078 | |
paul@281 | 3079 | start_weekday, number_of_days = month.month_properties() |
paul@281 | 3080 | |
paul@281 | 3081 | # The start weekday is the weekday of day number 1. |
paul@281 | 3082 | # Find the first day of the week, counting from below zero, if |
paul@281 | 3083 | # necessary, in order to land on the first day of the month as |
paul@281 | 3084 | # day number 1. |
paul@281 | 3085 | |
paul@281 | 3086 | first_day = 1 - start_weekday |
paul@281 | 3087 | |
paul@281 | 3088 | while first_day <= number_of_days: |
paul@281 | 3089 | |
paul@281 | 3090 | # Find events in this week and determine how to mark them on the |
paul@281 | 3091 | # calendar. |
paul@281 | 3092 | |
paul@281 | 3093 | week_start = month.as_date(max(first_day, 1)) |
paul@281 | 3094 | week_end = month.as_date(min(first_day + 6, number_of_days)) |
paul@281 | 3095 | |
paul@281 | 3096 | full_coverage, week_slots = getCoverage( |
paul@281 | 3097 | getEventsInPeriod(all_shown_events, getCalendarPeriod(week_start, week_end))) |
paul@281 | 3098 | |
paul@281 | 3099 | # Output a week, starting with the day numbers. |
paul@281 | 3100 | |
paul@308 | 3101 | append(self.writeDayNumbers(first_day, number_of_days, month, full_coverage)) |
paul@281 | 3102 | |
paul@281 | 3103 | # Either generate empty days... |
paul@281 | 3104 | |
paul@281 | 3105 | if not week_slots: |
paul@308 | 3106 | append(self.writeEmptyWeek(first_day, number_of_days)) |
paul@281 | 3107 | |
paul@281 | 3108 | # Or generate each set of scheduled events... |
paul@281 | 3109 | |
paul@281 | 3110 | else: |
paul@308 | 3111 | append(self.writeWeekSlots(first_day, number_of_days, month, week_end, week_slots)) |
paul@281 | 3112 | |
paul@281 | 3113 | # Process the next week... |
paul@281 | 3114 | |
paul@281 | 3115 | first_day += 7 |
paul@281 | 3116 | |
paul@281 | 3117 | # End of month. |
paul@281 | 3118 | |
paul@308 | 3119 | append(fmt.table(on=0)) |
paul@281 | 3120 | |
paul@281 | 3121 | # Output a day view. |
paul@281 | 3122 | |
paul@281 | 3123 | elif self.mode == "day": |
paul@281 | 3124 | |
paul@281 | 3125 | # Visit all days in the requested range, or across known events. |
paul@281 | 3126 | |
paul@281 | 3127 | for date in self.first.days_until(self.last): |
paul@281 | 3128 | |
paul@308 | 3129 | append(fmt.table(on=1, attrs={"tableclass" : "event-calendar-day"})) |
paul@281 | 3130 | |
paul@281 | 3131 | full_coverage, day_slots = getCoverage( |
paul@281 | 3132 | getEventsInPeriod(all_shown_events, getCalendarPeriod(date, date)), "datetime") |
paul@281 | 3133 | |
paul@281 | 3134 | # Work out how many columns the day title will need. |
paul@281 | 3135 | # Include spacers after the scale and each event column. |
paul@281 | 3136 | |
paul@281 | 3137 | colspan = sum(map(len, day_slots.values())) * 2 + 2 |
paul@281 | 3138 | |
paul@308 | 3139 | append(self.writeDayTableHeading(date, colspan)) |
paul@281 | 3140 | |
paul@281 | 3141 | # Either generate empty days... |
paul@281 | 3142 | |
paul@281 | 3143 | if not day_slots: |
paul@308 | 3144 | append(self.writeEmptyDay(date)) |
paul@281 | 3145 | |
paul@281 | 3146 | # Or generate each set of scheduled events... |
paul@281 | 3147 | |
paul@281 | 3148 | else: |
paul@308 | 3149 | append(self.writeDaySlots(date, full_coverage, day_slots)) |
paul@281 | 3150 | |
paul@281 | 3151 | # End of day. |
paul@281 | 3152 | |
paul@308 | 3153 | append(fmt.table(on=0)) |
paul@281 | 3154 | |
paul@281 | 3155 | # Output view controls. |
paul@281 | 3156 | |
paul@308 | 3157 | append(fmt.div(on=1, css_class="event-controls")) |
paul@308 | 3158 | append(self.writeViewControls()) |
paul@308 | 3159 | append(fmt.div(on=0)) |
paul@281 | 3160 | |
paul@281 | 3161 | # Close the calendar region. |
paul@281 | 3162 | |
paul@308 | 3163 | append(fmt.div(on=0)) |
paul@281 | 3164 | |
paul@282 | 3165 | # Add any scripts. |
paul@282 | 3166 | |
paul@282 | 3167 | if isinstance(fmt, request.html_formatter.__class__): |
paul@308 | 3168 | append(self.update_script) |
paul@282 | 3169 | |
paul@281 | 3170 | return ''.join(output) |
paul@281 | 3171 | |
paul@282 | 3172 | update_script = """\ |
paul@282 | 3173 | <script type="text/javascript"> |
paul@282 | 3174 | function replaceCalendar(name, url) { |
paul@282 | 3175 | var calendar = document.getElementById(name); |
paul@282 | 3176 | |
paul@282 | 3177 | if (calendar == null) { |
paul@282 | 3178 | return true; |
paul@282 | 3179 | } |
paul@282 | 3180 | |
paul@282 | 3181 | var xmlhttp = new XMLHttpRequest(); |
paul@282 | 3182 | xmlhttp.open("GET", url, false); |
paul@282 | 3183 | xmlhttp.send(null); |
paul@282 | 3184 | |
paul@282 | 3185 | var newCalendar = xmlhttp.responseText; |
paul@282 | 3186 | |
paul@282 | 3187 | if (newCalendar != null) { |
paul@285 | 3188 | calendar.innerHTML = newCalendar; |
paul@282 | 3189 | return false; |
paul@282 | 3190 | } |
paul@282 | 3191 | |
paul@282 | 3192 | return true; |
paul@282 | 3193 | } |
paul@282 | 3194 | </script> |
paul@282 | 3195 | """ |
paul@282 | 3196 | |
paul@307 | 3197 | # Event-only formatting. |
paul@307 | 3198 | |
paul@310 | 3199 | def formatEvent(event, request, fmt, write=None): |
paul@310 | 3200 | |
paul@310 | 3201 | """ |
paul@310 | 3202 | Format the given 'event' using the 'request' and formatter 'fmt'. If the |
paul@310 | 3203 | 'write' parameter is specified, use it to write output. |
paul@310 | 3204 | """ |
paul@309 | 3205 | |
paul@309 | 3206 | event_details = event.getDetails() |
paul@310 | 3207 | write = write or request.write |
paul@309 | 3208 | |
paul@323 | 3209 | if event_details.has_key("fragment"): |
paul@323 | 3210 | write(fmt.anchordef(event_details["fragment"])) |
paul@323 | 3211 | |
paul@309 | 3212 | write(fmt.definition_list(on=1)) |
paul@309 | 3213 | |
paul@323 | 3214 | for term in event.all_terms: |
paul@309 | 3215 | if event_details.has_key(term): |
paul@309 | 3216 | value = event_details[term] |
paul@309 | 3217 | if value: |
paul@309 | 3218 | write(fmt.definition_term(on=1)) |
paul@309 | 3219 | write(fmt.text(term)) |
paul@309 | 3220 | write(fmt.definition_term(on=0)) |
paul@309 | 3221 | write(fmt.definition_desc(on=1)) |
paul@323 | 3222 | if term in event.list_terms: |
paul@323 | 3223 | write(", ".join([formatText(str(v), request, fmt) for v in value])) |
paul@323 | 3224 | else: |
paul@323 | 3225 | write(formatText(str(value), request, fmt)) |
paul@309 | 3226 | write(fmt.definition_desc(on=0)) |
paul@309 | 3227 | |
paul@309 | 3228 | write(fmt.definition_list(on=0)) |
paul@309 | 3229 | |
paul@310 | 3230 | def formatEventsForOutputType(events, request, mimetype, parent=None, descriptions=None, latest_timestamp=None, write=None): |
paul@307 | 3231 | |
paul@307 | 3232 | """ |
paul@307 | 3233 | Format the given 'events' using the 'request' for the given 'mimetype'. |
paul@307 | 3234 | |
paul@307 | 3235 | The optional 'parent' indicates the "natural" parent page of the events. Any |
paul@307 | 3236 | event pages residing beneath the parent page will have their names |
paul@307 | 3237 | reproduced as relative to the parent page. |
paul@307 | 3238 | |
paul@307 | 3239 | The optional 'descriptions' indicates the nature of any description given |
paul@307 | 3240 | for events in the output resource. |
paul@307 | 3241 | |
paul@307 | 3242 | The optional 'latest_timestamp' indicates the timestamp of the latest edit |
paul@307 | 3243 | of the page or event collection. |
paul@310 | 3244 | |
paul@310 | 3245 | If the 'write' parameter is specified, use it to write output. |
paul@307 | 3246 | """ |
paul@307 | 3247 | |
paul@310 | 3248 | write = write or request.write |
paul@308 | 3249 | |
paul@307 | 3250 | # Start the collection. |
paul@307 | 3251 | |
paul@307 | 3252 | if mimetype == "text/calendar": |
paul@308 | 3253 | write("BEGIN:VCALENDAR\r\n") |
paul@308 | 3254 | write("PRODID:-//MoinMoin//EventAggregatorSummary\r\n") |
paul@308 | 3255 | write("VERSION:2.0\r\n") |
paul@307 | 3256 | |
paul@307 | 3257 | elif mimetype == "application/rss+xml": |
paul@307 | 3258 | |
paul@307 | 3259 | # Using the page name and the page URL in the title, link and |
paul@307 | 3260 | # description. |
paul@307 | 3261 | |
paul@307 | 3262 | path_info = getPathInfo(request) |
paul@307 | 3263 | |
paul@308 | 3264 | write('<rss version="2.0">\r\n') |
paul@308 | 3265 | write('<channel>\r\n') |
paul@308 | 3266 | write('<title>%s</title>\r\n' % path_info[1:]) |
paul@308 | 3267 | write('<link>%s%s</link>\r\n' % (request.getBaseURL(), path_info)) |
paul@308 | 3268 | write('<description>Events published on %s%s</description>\r\n' % (request.getBaseURL(), path_info)) |
paul@307 | 3269 | |
paul@307 | 3270 | if latest_timestamp is not None: |
paul@308 | 3271 | write('<lastBuildDate>%s</lastBuildDate>\r\n' % latest_timestamp.as_HTTP_datetime_string()) |
paul@307 | 3272 | |
paul@307 | 3273 | # Sort the events by start date, reversed. |
paul@307 | 3274 | |
paul@307 | 3275 | ordered_events = getOrderedEvents(events) |
paul@307 | 3276 | ordered_events.reverse() |
paul@307 | 3277 | events = ordered_events |
paul@307 | 3278 | |
paul@311 | 3279 | elif mimetype == "text/html": |
paul@311 | 3280 | write('<html>') |
paul@311 | 3281 | write('<body>') |
paul@311 | 3282 | |
paul@307 | 3283 | # Output the collection one by one. |
paul@307 | 3284 | |
paul@307 | 3285 | for event in events: |
paul@307 | 3286 | formatEventForOutputType(event, request, mimetype, parent, descriptions) |
paul@307 | 3287 | |
paul@307 | 3288 | # End the collection. |
paul@307 | 3289 | |
paul@307 | 3290 | if mimetype == "text/calendar": |
paul@308 | 3291 | write("END:VCALENDAR\r\n") |
paul@307 | 3292 | |
paul@307 | 3293 | elif mimetype == "application/rss+xml": |
paul@308 | 3294 | write('</channel>\r\n') |
paul@308 | 3295 | write('</rss>\r\n') |
paul@307 | 3296 | |
paul@311 | 3297 | elif mimetype == "text/html": |
paul@311 | 3298 | write('</body>') |
paul@311 | 3299 | write('</html>') |
paul@311 | 3300 | |
paul@310 | 3301 | def formatEventForOutputType(event, request, mimetype, parent=None, descriptions=None, write=None): |
paul@307 | 3302 | |
paul@307 | 3303 | """ |
paul@307 | 3304 | Format the given 'event' using the 'request' for the given 'mimetype'. |
paul@307 | 3305 | |
paul@307 | 3306 | The optional 'parent' indicates the "natural" parent page of the events. Any |
paul@307 | 3307 | event pages residing beneath the parent page will have their names |
paul@307 | 3308 | reproduced as relative to the parent page. |
paul@307 | 3309 | |
paul@307 | 3310 | The optional 'descriptions' indicates the nature of any description given |
paul@307 | 3311 | for events in the output resource. |
paul@310 | 3312 | |
paul@310 | 3313 | If the 'write' parameter is specified, use it to write output. |
paul@307 | 3314 | """ |
paul@307 | 3315 | |
paul@310 | 3316 | write = write or request.write |
paul@307 | 3317 | event_details = event.getDetails() |
paul@310 | 3318 | event_metadata = event.getMetadata() |
paul@307 | 3319 | |
paul@307 | 3320 | if mimetype == "text/calendar": |
paul@307 | 3321 | |
paul@307 | 3322 | # NOTE: A custom formatter making attributes for links and plain |
paul@307 | 3323 | # NOTE: text for values could be employed here. |
paul@307 | 3324 | |
paul@307 | 3325 | # Get the summary details. |
paul@307 | 3326 | |
paul@307 | 3327 | event_summary = event.getSummary(parent) |
paul@307 | 3328 | link = event.getEventURL() |
paul@307 | 3329 | |
paul@307 | 3330 | # Output the event details. |
paul@307 | 3331 | |
paul@308 | 3332 | write("BEGIN:VEVENT\r\n") |
paul@308 | 3333 | write("UID:%s\r\n" % link) |
paul@308 | 3334 | write("URL:%s\r\n" % link) |
paul@310 | 3335 | write("DTSTAMP:%04d%02d%02dT%02d%02d%02dZ\r\n" % event_metadata["created"].as_tuple()[:6]) |
paul@310 | 3336 | write("LAST-MODIFIED:%04d%02d%02dT%02d%02d%02dZ\r\n" % event_metadata["last-modified"].as_tuple()[:6]) |
paul@310 | 3337 | write("SEQUENCE:%d\r\n" % event_metadata["sequence"]) |
paul@307 | 3338 | |
paul@307 | 3339 | start = event_details["start"] |
paul@307 | 3340 | end = event_details["end"] |
paul@307 | 3341 | |
paul@307 | 3342 | if isinstance(start, DateTime): |
paul@308 | 3343 | write("DTSTART") |
paul@307 | 3344 | write_calendar_datetime(request, start) |
paul@307 | 3345 | else: |
paul@308 | 3346 | write("DTSTART;VALUE=DATE:%04d%02d%02d\r\n" % start.as_date().as_tuple()) |
paul@307 | 3347 | |
paul@307 | 3348 | if isinstance(end, DateTime): |
paul@308 | 3349 | write("DTEND") |
paul@307 | 3350 | write_calendar_datetime(request, end) |
paul@307 | 3351 | else: |
paul@308 | 3352 | write("DTEND;VALUE=DATE:%04d%02d%02d\r\n" % end.next_day().as_date().as_tuple()) |
paul@308 | 3353 | |
paul@308 | 3354 | write("SUMMARY:%s\r\n" % getQuotedText(event_summary)) |
paul@307 | 3355 | |
paul@307 | 3356 | # Optional details. |
paul@307 | 3357 | |
paul@307 | 3358 | if event_details.get("topics") or event_details.get("categories"): |
paul@308 | 3359 | write("CATEGORIES:%s\r\n" % ",".join( |
paul@307 | 3360 | [getQuotedText(topic) |
paul@307 | 3361 | for topic in event_details.get("topics") or event_details.get("categories")] |
paul@307 | 3362 | )) |
paul@307 | 3363 | if event_details.has_key("location"): |
paul@308 | 3364 | write("LOCATION:%s\r\n" % getQuotedText(event_details["location"])) |
paul@307 | 3365 | if event_details.has_key("geo"): |
paul@308 | 3366 | write("GEO:%s\r\n" % getQuotedText(";".join([str(ref.to_degrees()) for ref in event_details["geo"]]))) |
paul@308 | 3367 | |
paul@308 | 3368 | write("END:VEVENT\r\n") |
paul@307 | 3369 | |
paul@307 | 3370 | elif mimetype == "application/rss+xml": |
paul@307 | 3371 | |
paul@307 | 3372 | event_page = event.getPage() |
paul@307 | 3373 | event_details = event.getDetails() |
paul@307 | 3374 | |
paul@307 | 3375 | # Get a parser and formatter for the formatting of some attributes. |
paul@307 | 3376 | |
paul@307 | 3377 | fmt = request.html_formatter |
paul@307 | 3378 | |
paul@307 | 3379 | # Get the summary details. |
paul@307 | 3380 | |
paul@307 | 3381 | event_summary = event.getSummary(parent) |
paul@307 | 3382 | link = event.getEventURL() |
paul@307 | 3383 | |
paul@308 | 3384 | write('<item>\r\n') |
paul@323 | 3385 | write('<title>%s</title>\r\n' % escape(event_summary)) |
paul@308 | 3386 | write('<link>%s</link>\r\n' % link) |
paul@307 | 3387 | |
paul@307 | 3388 | # Write a description according to the preferred source of |
paul@307 | 3389 | # descriptions. |
paul@307 | 3390 | |
paul@307 | 3391 | if descriptions == "page": |
paul@307 | 3392 | description = event_details.get("description", "") |
paul@307 | 3393 | else: |
paul@310 | 3394 | description = event_metadata["last-comment"] |
paul@307 | 3395 | |
paul@308 | 3396 | write('<description>%s</description>\r\n' % |
paul@307 | 3397 | fmt.text(event_page.formatText(description, fmt))) |
paul@307 | 3398 | |
paul@307 | 3399 | for topic in event_details.get("topics") or event_details.get("categories") or []: |
paul@308 | 3400 | write('<category>%s</category>\r\n' % |
paul@307 | 3401 | fmt.text(event_page.formatText(topic, fmt))) |
paul@307 | 3402 | |
paul@310 | 3403 | write('<pubDate>%s</pubDate>\r\n' % event_metadata["created"].as_HTTP_datetime_string()) |
paul@310 | 3404 | write('<guid>%s#%s</guid>\r\n' % (link, event_metadata["sequence"])) |
paul@308 | 3405 | write('</item>\r\n') |
paul@307 | 3406 | |
paul@310 | 3407 | elif mimetype == "text/html": |
paul@310 | 3408 | fmt = request.html_formatter |
paul@310 | 3409 | fmt.setPage(request.page) |
paul@310 | 3410 | formatEvent(event, request, fmt, write=write) |
paul@310 | 3411 | |
paul@307 | 3412 | # iCalendar format helper functions. |
paul@307 | 3413 | |
paul@307 | 3414 | def write_calendar_datetime(request, datetime): |
paul@307 | 3415 | |
paul@307 | 3416 | """ |
paul@307 | 3417 | Write to the given 'request' the 'datetime' using appropriate time zone |
paul@307 | 3418 | information. |
paul@307 | 3419 | """ |
paul@307 | 3420 | |
paul@307 | 3421 | utc_datetime = datetime.to_utc() |
paul@307 | 3422 | if utc_datetime: |
paul@307 | 3423 | request.write(";VALUE=DATE-TIME:%04d%02d%02dT%02d%02d%02dZ\r\n" % utc_datetime.padded().as_tuple()[:-1]) |
paul@307 | 3424 | else: |
paul@307 | 3425 | zone = datetime.time_zone() |
paul@307 | 3426 | if zone: |
paul@307 | 3427 | request.write(";TZID=/%s" % zone) |
paul@307 | 3428 | request.write(";VALUE=DATE-TIME:%04d%02d%02dT%02d%02d%02d\r\n" % datetime.padded().as_tuple()[:-1]) |
paul@307 | 3429 | |
paul@307 | 3430 | def getQuotedText(text): |
paul@307 | 3431 | |
paul@307 | 3432 | "Return the 'text' quoted for iCalendar purposes." |
paul@307 | 3433 | |
paul@319 | 3434 | return text.replace(";", r"\;").replace(",", r"\,").replace("\n", "\\n") |
paul@307 | 3435 | |
paul@10 | 3436 | # vim: tabstop=4 expandtab shiftwidth=4 |