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