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