paul@10 | 1 | # -*- coding: iso-8859-1 -*- |
paul@10 | 2 | """ |
paul@10 | 3 | MoinMoin - EventAggregator library |
paul@10 | 4 | |
paul@137 | 5 | @copyright: 2008, 2009, 2010, 2011 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@10 | 11 | from MoinMoin.Page import Page |
paul@10 | 12 | from MoinMoin import search, version |
paul@24 | 13 | from MoinMoin import wikiutil |
paul@10 | 14 | import calendar |
paul@11 | 15 | import datetime |
paul@24 | 16 | import time |
paul@10 | 17 | import re |
paul@10 | 18 | |
paul@69 | 19 | try: |
paul@69 | 20 | set |
paul@69 | 21 | except NameError: |
paul@69 | 22 | from sets import Set as set |
paul@69 | 23 | |
paul@100 | 24 | try: |
paul@100 | 25 | import pytz |
paul@100 | 26 | except ImportError: |
paul@100 | 27 | pytz = None |
paul@100 | 28 | |
paul@137 | 29 | escape = wikiutil.escape |
paul@137 | 30 | |
paul@137 | 31 | def escattr(s): |
paul@137 | 32 | return escape(s, 1) |
paul@137 | 33 | |
paul@148 | 34 | __version__ = "0.6.2" |
paul@10 | 35 | |
paul@22 | 36 | # Date labels. |
paul@22 | 37 | |
paul@22 | 38 | month_labels = ["January", "February", "March", "April", "May", "June", |
paul@22 | 39 | "July", "August", "September", "October", "November", "December"] |
paul@22 | 40 | weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
paul@22 | 41 | |
paul@10 | 42 | # Regular expressions where MoinMoin does not provide the required support. |
paul@10 | 43 | |
paul@10 | 44 | category_regexp = None |
paul@47 | 45 | |
paul@47 | 46 | # Page parsing. |
paul@47 | 47 | |
paul@84 | 48 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?):: )(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@47 | 49 | category_membership_regexp = re.compile(ur"^\s*((Category\S+)(\s+Category\S+)*)\s*$", re.MULTILINE | re.UNICODE) |
paul@47 | 50 | |
paul@47 | 51 | # Value parsing. |
paul@47 | 52 | |
paul@93 | 53 | country_code_regexp = re.compile(ur'(?:^|\W)(?P<code>[A-Z]{2})(?:$|\W+$)', re.UNICODE) |
paul@92 | 54 | |
paul@92 | 55 | month_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})' |
paul@92 | 56 | date_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})' |
paul@92 | 57 | time_regexp_str = ur'(?P<hour>[0-2][0-9]):(?P<minute>[0-5][0-9])(?::(?P<second>[0-6][0-9]))?' |
paul@98 | 58 | timezone_offset_str = ur'(?P<offset>(UTC)?(?:(?P<sign>[-+])(?P<hours>[0-9]{2})(?::?(?P<minutes>[0-9]{2}))?))' |
paul@98 | 59 | timezone_olson_str = ur'(?P<olson>[a-zA-Z]+(?:/[-_a-zA-Z]+){1,2})' |
paul@98 | 60 | timezone_utc_str = ur'UTC' |
paul@98 | 61 | timezone_regexp_str = ur'(?P<zone>' + timezone_offset_str + '|' + timezone_olson_str + '|' + timezone_utc_str + ')' |
paul@92 | 62 | datetime_regexp_str = date_regexp_str + ur'(?:\s+' + time_regexp_str + ur'(?:\s+' + timezone_regexp_str + ur')?)?' |
paul@92 | 63 | |
paul@92 | 64 | month_regexp = re.compile(month_regexp_str, re.UNICODE) |
paul@92 | 65 | date_regexp = re.compile(date_regexp_str, re.UNICODE) |
paul@92 | 66 | time_regexp = re.compile(time_regexp_str, re.UNICODE) |
paul@92 | 67 | datetime_regexp = re.compile(datetime_regexp_str, re.UNICODE) |
paul@100 | 68 | timezone_olson_regexp = re.compile(timezone_olson_str, re.UNICODE) |
paul@94 | 69 | timezone_offset_regexp = re.compile(timezone_offset_str, re.UNICODE) |
paul@92 | 70 | |
paul@19 | 71 | verbatim_regexp = re.compile(ur'(?:' |
paul@19 | 72 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@19 | 73 | ur'|' |
paul@19 | 74 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@19 | 75 | ur'|' |
paul@19 | 76 | ur'`(?P<monospace>.*?)`' |
paul@19 | 77 | ur'|' |
paul@19 | 78 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@19 | 79 | ur')', re.UNICODE) |
paul@10 | 80 | |
paul@10 | 81 | # Utility functions. |
paul@10 | 82 | |
paul@10 | 83 | def getCategoryPattern(request): |
paul@10 | 84 | global category_regexp |
paul@10 | 85 | |
paul@10 | 86 | try: |
paul@10 | 87 | return request.cfg.cache.page_category_regexact |
paul@10 | 88 | except AttributeError: |
paul@10 | 89 | |
paul@10 | 90 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@10 | 91 | |
paul@10 | 92 | if category_regexp is None: |
paul@10 | 93 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@10 | 94 | return category_regexp |
paul@10 | 95 | |
paul@92 | 96 | def int_or_none(x): |
paul@92 | 97 | if x is None: |
paul@92 | 98 | return x |
paul@92 | 99 | else: |
paul@92 | 100 | return int(x) |
paul@92 | 101 | |
paul@118 | 102 | def sort_none_first(x, y): |
paul@118 | 103 | if x is None: |
paul@118 | 104 | return -1 |
paul@118 | 105 | elif y is None: |
paul@118 | 106 | return 1 |
paul@118 | 107 | else: |
paul@118 | 108 | return cmp(x, y) |
paul@118 | 109 | |
paul@109 | 110 | # Utility classes and associated functions. |
paul@109 | 111 | |
paul@109 | 112 | class Form: |
paul@109 | 113 | |
paul@109 | 114 | """ |
paul@109 | 115 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@109 | 116 | environment. |
paul@109 | 117 | """ |
paul@109 | 118 | |
paul@109 | 119 | def __init__(self, form): |
paul@109 | 120 | self.form = form |
paul@109 | 121 | |
paul@110 | 122 | def get(self, name, default=None): |
paul@109 | 123 | values = self.form.getlist(name) |
paul@109 | 124 | if not values: |
paul@110 | 125 | return default |
paul@109 | 126 | else: |
paul@109 | 127 | return values |
paul@109 | 128 | |
paul@109 | 129 | def __getitem__(self, name): |
paul@109 | 130 | return self.form.getlist(name) |
paul@109 | 131 | |
paul@109 | 132 | class ActionSupport: |
paul@109 | 133 | |
paul@112 | 134 | """ |
paul@112 | 135 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@112 | 136 | convenience methods. |
paul@112 | 137 | """ |
paul@109 | 138 | |
paul@109 | 139 | def get_form(self): |
paul@109 | 140 | return get_form(self.request) |
paul@109 | 141 | |
paul@112 | 142 | def _get_selected(self, value, input_value): |
paul@112 | 143 | |
paul@112 | 144 | """ |
paul@112 | 145 | Return the HTML attribute text indicating selection of an option (or |
paul@112 | 146 | otherwise) if 'value' matches 'input_value'. |
paul@112 | 147 | """ |
paul@112 | 148 | |
paul@112 | 149 | return input_value is not None and value == input_value and 'selected="selected"' or '' |
paul@112 | 150 | |
paul@112 | 151 | def _get_selected_for_list(self, value, input_values): |
paul@112 | 152 | |
paul@112 | 153 | """ |
paul@112 | 154 | Return the HTML attribute text indicating selection of an option (or |
paul@112 | 155 | otherwise) if 'value' matches one of the 'input_values'. |
paul@112 | 156 | """ |
paul@112 | 157 | |
paul@112 | 158 | return value in input_values and 'selected="selected"' or '' |
paul@112 | 159 | |
paul@112 | 160 | def _get_input(self, form, name, default=None): |
paul@112 | 161 | |
paul@112 | 162 | """ |
paul@112 | 163 | Return the input from 'form' having the given 'name', returning either |
paul@112 | 164 | the input converted to an integer or the given 'default' (optional, None |
paul@112 | 165 | if not specified). |
paul@112 | 166 | """ |
paul@112 | 167 | |
paul@112 | 168 | value = form.get(name, [None])[0] |
paul@112 | 169 | if not value: # true if 0 obtained |
paul@112 | 170 | return default |
paul@112 | 171 | else: |
paul@112 | 172 | return int(value) |
paul@112 | 173 | |
paul@114 | 174 | def get_month_lists(self, default_as_current=0): |
paul@112 | 175 | |
paul@112 | 176 | """ |
paul@112 | 177 | Return two lists of HTML element definitions corresponding to the start |
paul@112 | 178 | and end month selection controls, with months selected according to any |
paul@112 | 179 | values that have been specified via request parameters. |
paul@112 | 180 | """ |
paul@112 | 181 | |
paul@112 | 182 | _ = self._ |
paul@112 | 183 | form = self.get_form() |
paul@112 | 184 | |
paul@112 | 185 | # Initialise month lists. |
paul@112 | 186 | |
paul@112 | 187 | start_month_list = [] |
paul@112 | 188 | end_month_list = [] |
paul@112 | 189 | |
paul@114 | 190 | start_month = self._get_input(form, "start-month", default_as_current and getCurrentMonth().month() or None) |
paul@112 | 191 | end_month = self._get_input(form, "end-month", start_month) |
paul@112 | 192 | |
paul@112 | 193 | # Prepare month lists, selecting specified months. |
paul@112 | 194 | |
paul@114 | 195 | if not default_as_current: |
paul@114 | 196 | start_month_list.append('<option value=""></option>') |
paul@114 | 197 | end_month_list.append('<option value=""></option>') |
paul@114 | 198 | |
paul@112 | 199 | for month in range(1, 13): |
paul@112 | 200 | month_label = _(getMonthLabel(month)) |
paul@112 | 201 | selected = self._get_selected(month, start_month) |
paul@112 | 202 | start_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@112 | 203 | selected = self._get_selected(month, end_month) |
paul@112 | 204 | end_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@112 | 205 | |
paul@112 | 206 | return start_month_list, end_month_list |
paul@112 | 207 | |
paul@114 | 208 | def get_year_defaults(self, default_as_current=0): |
paul@112 | 209 | |
paul@112 | 210 | "Return defaults for the start and end years." |
paul@112 | 211 | |
paul@112 | 212 | form = self.get_form() |
paul@112 | 213 | |
paul@114 | 214 | start_year_default = form.get("start-year", [default_as_current and getCurrentYear() or ""])[0] |
paul@114 | 215 | end_year_default = form.get("end-year", [default_as_current and start_year_default or ""])[0] |
paul@112 | 216 | |
paul@112 | 217 | return start_year_default, end_year_default |
paul@112 | 218 | |
paul@109 | 219 | def get_form(request): |
paul@109 | 220 | |
paul@109 | 221 | "Work around disruptive MoinMoin changes in 1.9." |
paul@109 | 222 | |
paul@109 | 223 | if hasattr(request, "values"): |
paul@109 | 224 | return Form(request.values) |
paul@109 | 225 | else: |
paul@109 | 226 | return request.form |
paul@109 | 227 | |
paul@110 | 228 | class send_headers: |
paul@110 | 229 | |
paul@110 | 230 | """ |
paul@110 | 231 | A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a |
paul@110 | 232 | 1.9.x environment. |
paul@110 | 233 | """ |
paul@110 | 234 | |
paul@110 | 235 | def __init__(self, request): |
paul@110 | 236 | self.request = request |
paul@110 | 237 | |
paul@110 | 238 | def __call__(self, headers): |
paul@110 | 239 | for header in headers: |
paul@110 | 240 | parts = header.split(":") |
paul@110 | 241 | self.request.headers.add(parts[0], ":".join(parts[1:])) |
paul@110 | 242 | |
paul@67 | 243 | # Textual representations. |
paul@67 | 244 | |
paul@67 | 245 | def getHTTPTimeString(tmtuple): |
paul@67 | 246 | return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( |
paul@67 | 247 | weekday_labels[tmtuple.tm_wday], |
paul@67 | 248 | tmtuple.tm_mday, |
paul@67 | 249 | month_labels[tmtuple.tm_mon -1], # zero-based labels |
paul@67 | 250 | tmtuple.tm_year, |
paul@67 | 251 | tmtuple.tm_hour, |
paul@67 | 252 | tmtuple.tm_min, |
paul@67 | 253 | tmtuple.tm_sec |
paul@67 | 254 | ) |
paul@67 | 255 | |
paul@67 | 256 | def getSimpleWikiText(text): |
paul@67 | 257 | |
paul@67 | 258 | """ |
paul@67 | 259 | Return the plain text representation of the given 'text' which may employ |
paul@67 | 260 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@67 | 261 | text. |
paul@67 | 262 | """ |
paul@67 | 263 | |
paul@67 | 264 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@67 | 265 | |
paul@67 | 266 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@67 | 267 | |
paul@67 | 268 | def getEncodedWikiText(text): |
paul@67 | 269 | |
paul@67 | 270 | "Encode the given 'text' in a verbatim representation." |
paul@67 | 271 | |
paul@67 | 272 | return "<<Verbatim(%s)>>" % text |
paul@67 | 273 | |
paul@67 | 274 | def getPrettyTitle(title): |
paul@67 | 275 | |
paul@67 | 276 | "Return a nicely formatted version of the given 'title'." |
paul@67 | 277 | |
paul@67 | 278 | return title.replace("_", " ").replace("/", u" ? ") |
paul@67 | 279 | |
paul@67 | 280 | def getMonthLabel(month): |
paul@67 | 281 | |
paul@67 | 282 | "Return an unlocalised label for the given 'month'." |
paul@67 | 283 | |
paul@67 | 284 | return month_labels[month - 1] # zero-based labels |
paul@67 | 285 | |
paul@67 | 286 | def getDayLabel(weekday): |
paul@67 | 287 | |
paul@67 | 288 | "Return an unlocalised label for the given 'weekday'." |
paul@67 | 289 | |
paul@67 | 290 | return weekday_labels[weekday] |
paul@67 | 291 | |
paul@19 | 292 | # Action support functions. |
paul@19 | 293 | |
paul@67 | 294 | def getPageRevision(page): |
paul@67 | 295 | |
paul@67 | 296 | "Return the revision details dictionary for the given 'page'." |
paul@67 | 297 | |
paul@67 | 298 | # From Page.edit_info... |
paul@67 | 299 | |
paul@67 | 300 | if hasattr(page, "editlog_entry"): |
paul@67 | 301 | line = page.editlog_entry() |
paul@67 | 302 | else: |
paul@67 | 303 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@67 | 304 | |
paul@89 | 305 | # Similar to Page.mtime_usecs behaviour... |
paul@89 | 306 | |
paul@89 | 307 | if line: |
paul@89 | 308 | timestamp = line.ed_time_usecs |
paul@89 | 309 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@89 | 310 | comment = line.comment |
paul@89 | 311 | else: |
paul@89 | 312 | mtime = 0 |
paul@89 | 313 | comment = "" |
paul@89 | 314 | |
paul@89 | 315 | return {"timestamp" : time.gmtime(mtime), "comment" : comment} |
paul@67 | 316 | |
paul@67 | 317 | # Category discovery and searching. |
paul@67 | 318 | |
paul@19 | 319 | def getCategories(request): |
paul@19 | 320 | |
paul@19 | 321 | """ |
paul@19 | 322 | From the AdvancedSearch macro, return a list of category page names using |
paul@19 | 323 | the given 'request'. |
paul@19 | 324 | """ |
paul@19 | 325 | |
paul@19 | 326 | # This will return all pages with "Category" in the title. |
paul@19 | 327 | |
paul@19 | 328 | cat_filter = getCategoryPattern(request).search |
paul@19 | 329 | return request.rootpage.getPageList(filter=cat_filter) |
paul@19 | 330 | |
paul@19 | 331 | def getCategoryMapping(category_pagenames, request): |
paul@19 | 332 | |
paul@19 | 333 | """ |
paul@19 | 334 | For the given 'category_pagenames' return a list of tuples of the form |
paul@19 | 335 | (category name, category page name) using the given 'request'. |
paul@19 | 336 | """ |
paul@19 | 337 | |
paul@19 | 338 | cat_pattern = getCategoryPattern(request) |
paul@19 | 339 | mapping = [] |
paul@19 | 340 | for pagename in category_pagenames: |
paul@19 | 341 | name = cat_pattern.match(pagename).group("key") |
paul@19 | 342 | if name != "Category": |
paul@19 | 343 | mapping.append((name, pagename)) |
paul@19 | 344 | mapping.sort() |
paul@19 | 345 | return mapping |
paul@19 | 346 | |
paul@67 | 347 | def getCategoryPages(pagename, request): |
paul@29 | 348 | |
paul@67 | 349 | """ |
paul@67 | 350 | Return the pages associated with the given category 'pagename' using the |
paul@67 | 351 | 'request'. |
paul@67 | 352 | """ |
paul@10 | 353 | |
paul@10 | 354 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@117 | 355 | results = search.searchPages(request, query, "page_name") |
paul@10 | 356 | |
paul@10 | 357 | cat_pattern = getCategoryPattern(request) |
paul@10 | 358 | pages = [] |
paul@10 | 359 | for page in results.hits: |
paul@10 | 360 | if not cat_pattern.match(page.page_name): |
paul@10 | 361 | pages.append(page) |
paul@10 | 362 | return pages |
paul@10 | 363 | |
paul@67 | 364 | # The main activity functions. |
paul@67 | 365 | |
paul@67 | 366 | class EventPage: |
paul@67 | 367 | |
paul@67 | 368 | "An event page." |
paul@67 | 369 | |
paul@67 | 370 | def __init__(self, page): |
paul@67 | 371 | self.page = page |
paul@69 | 372 | self.events = None |
paul@67 | 373 | self.body = None |
paul@67 | 374 | self.categories = None |
paul@67 | 375 | |
paul@67 | 376 | def copyPage(self, page): |
paul@67 | 377 | |
paul@67 | 378 | "Copy the body of the given 'page'." |
paul@67 | 379 | |
paul@67 | 380 | self.body = page.getBody() |
paul@67 | 381 | |
paul@67 | 382 | def getPageURL(self, request): |
paul@67 | 383 | |
paul@67 | 384 | "Using 'request', return the URL of this page." |
paul@24 | 385 | |
paul@117 | 386 | return request.getQualifiedURL(self.page.url(request, relative=0)) |
paul@67 | 387 | |
paul@67 | 388 | def getFormat(self): |
paul@67 | 389 | |
paul@67 | 390 | "Get the format used on this page." |
paul@24 | 391 | |
paul@117 | 392 | return self.page.pi["format"] |
paul@67 | 393 | |
paul@67 | 394 | def getRevisions(self): |
paul@67 | 395 | |
paul@67 | 396 | "Return a list of page revisions." |
paul@67 | 397 | |
paul@67 | 398 | return self.page.getRevList() |
paul@67 | 399 | |
paul@67 | 400 | def getPageRevision(self): |
paul@24 | 401 | |
paul@67 | 402 | "Return the revision details dictionary for this page." |
paul@67 | 403 | |
paul@67 | 404 | return getPageRevision(self.page) |
paul@67 | 405 | |
paul@67 | 406 | def getPageName(self): |
paul@67 | 407 | |
paul@67 | 408 | "Return the page name." |
paul@67 | 409 | |
paul@67 | 410 | return self.page.page_name |
paul@24 | 411 | |
paul@67 | 412 | def getPrettyPageName(self): |
paul@67 | 413 | |
paul@67 | 414 | "Return a nicely formatted title/name for this page." |
paul@67 | 415 | |
paul@67 | 416 | return getPrettyPageName(self.page) |
paul@67 | 417 | |
paul@67 | 418 | def getBody(self): |
paul@67 | 419 | |
paul@67 | 420 | "Get the current page body." |
paul@47 | 421 | |
paul@67 | 422 | if self.body is None: |
paul@67 | 423 | self.body = self.page.get_raw_body() |
paul@67 | 424 | return self.body |
paul@67 | 425 | |
paul@69 | 426 | def getEvents(self): |
paul@69 | 427 | |
paul@69 | 428 | "Return a list of events from this page." |
paul@67 | 429 | |
paul@69 | 430 | if self.events is None: |
paul@69 | 431 | details = {} |
paul@69 | 432 | self.events = [Event(self, details)] |
paul@47 | 433 | |
paul@67 | 434 | if self.getFormat() == "wiki": |
paul@67 | 435 | for match in definition_list_regexp.finditer(self.getBody()): |
paul@67 | 436 | |
paul@67 | 437 | # Skip commented-out items. |
paul@47 | 438 | |
paul@67 | 439 | if match.group("optcomment"): |
paul@67 | 440 | continue |
paul@67 | 441 | |
paul@67 | 442 | # Permit case-insensitive list terms. |
paul@67 | 443 | |
paul@67 | 444 | term = match.group("term").lower() |
paul@67 | 445 | desc = match.group("desc") |
paul@67 | 446 | |
paul@67 | 447 | # Special value type handling. |
paul@27 | 448 | |
paul@67 | 449 | # Dates. |
paul@67 | 450 | |
paul@67 | 451 | if term in ("start", "end"): |
paul@67 | 452 | desc = getDate(desc) |
paul@67 | 453 | |
paul@67 | 454 | # Lists (whose elements may be quoted). |
paul@67 | 455 | |
paul@67 | 456 | elif term in ("topics", "categories"): |
paul@84 | 457 | desc = [getSimpleWikiText(value.strip()) for value in desc.split(",") if value.strip()] |
paul@67 | 458 | |
paul@67 | 459 | # Labels which may well be quoted. |
paul@67 | 460 | |
paul@92 | 461 | elif term in ("title", "summary", "description", "location"): |
paul@67 | 462 | desc = getSimpleWikiText(desc) |
paul@67 | 463 | |
paul@67 | 464 | if desc is not None: |
paul@69 | 465 | |
paul@69 | 466 | # Handle apparent duplicates by creating a new set of |
paul@69 | 467 | # details. |
paul@69 | 468 | |
paul@69 | 469 | if details.has_key(term): |
paul@92 | 470 | |
paul@92 | 471 | # Make a new event. |
paul@92 | 472 | |
paul@69 | 473 | details = {} |
paul@69 | 474 | self.events.append(Event(self, details)) |
paul@67 | 475 | |
paul@69 | 476 | details[term] = desc |
paul@69 | 477 | |
paul@69 | 478 | return self.events |
paul@69 | 479 | |
paul@69 | 480 | def setEvents(self, events): |
paul@69 | 481 | |
paul@69 | 482 | "Set the given 'events' on this page." |
paul@69 | 483 | |
paul@69 | 484 | self.events = events |
paul@67 | 485 | |
paul@67 | 486 | def getCategoryMembership(self): |
paul@27 | 487 | |
paul@67 | 488 | "Get the category names from this page." |
paul@67 | 489 | |
paul@67 | 490 | if self.categories is None: |
paul@67 | 491 | body = self.getBody() |
paul@67 | 492 | match = category_membership_regexp.search(body) |
paul@67 | 493 | self.categories = match.findall().split() |
paul@67 | 494 | |
paul@67 | 495 | return self.categories |
paul@67 | 496 | |
paul@67 | 497 | def setCategoryMembership(self, category_names): |
paul@10 | 498 | |
paul@67 | 499 | """ |
paul@67 | 500 | Set the category membership for the page using the specified |
paul@67 | 501 | 'category_names'. |
paul@67 | 502 | """ |
paul@67 | 503 | |
paul@67 | 504 | self.categories = category_names |
paul@67 | 505 | |
paul@67 | 506 | def flushEventDetails(self): |
paul@67 | 507 | |
paul@67 | 508 | "Flush the current event details to this page's body text." |
paul@10 | 509 | |
paul@67 | 510 | new_body_parts = [] |
paul@67 | 511 | end_of_last_match = 0 |
paul@67 | 512 | body = self.getBody() |
paul@69 | 513 | |
paul@69 | 514 | events = iter(self.getEvents()) |
paul@69 | 515 | |
paul@69 | 516 | event = events.next() |
paul@69 | 517 | event_details = event.getDetails() |
paul@69 | 518 | replaced_terms = set() |
paul@67 | 519 | |
paul@67 | 520 | for match in definition_list_regexp.finditer(body): |
paul@47 | 521 | |
paul@10 | 522 | # Permit case-insensitive list terms. |
paul@10 | 523 | |
paul@10 | 524 | term = match.group("term").lower() |
paul@10 | 525 | desc = match.group("desc") |
paul@10 | 526 | |
paul@69 | 527 | # Check that the term has not already been substituted. If so, |
paul@69 | 528 | # get the next event. |
paul@69 | 529 | |
paul@69 | 530 | if term in replaced_terms: |
paul@69 | 531 | try: |
paul@69 | 532 | event = events.next() |
paul@69 | 533 | |
paul@69 | 534 | # No more events. |
paul@69 | 535 | |
paul@69 | 536 | except StopIteration: |
paul@69 | 537 | break |
paul@69 | 538 | |
paul@69 | 539 | event_details = event.getDetails() |
paul@69 | 540 | replaced_terms = set() |
paul@69 | 541 | |
paul@75 | 542 | # Add preceding text to the new body. |
paul@75 | 543 | |
paul@75 | 544 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@75 | 545 | |
paul@75 | 546 | # Get the matching regions, adding the term to the new body. |
paul@75 | 547 | |
paul@75 | 548 | new_body_parts.append(match.group("wholeterm")) |
paul@75 | 549 | |
paul@10 | 550 | # Special value type handling. |
paul@10 | 551 | |
paul@67 | 552 | if event_details.has_key(term): |
paul@19 | 553 | |
paul@67 | 554 | # Dates. |
paul@47 | 555 | |
paul@67 | 556 | if term in ("start", "end"): |
paul@67 | 557 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@47 | 558 | |
paul@67 | 559 | # Lists (whose elements may be quoted). |
paul@47 | 560 | |
paul@67 | 561 | elif term in ("topics", "categories"): |
paul@92 | 562 | desc = ", ".join([getEncodedWikiText(item) for item in event_details[term]]) |
paul@47 | 563 | |
paul@92 | 564 | # Labels which must be quoted. |
paul@47 | 565 | |
paul@67 | 566 | elif term in ("title", "summary"): |
paul@67 | 567 | desc = getEncodedWikiText(event_details[term]) |
paul@47 | 568 | |
paul@67 | 569 | # Text which need not be quoted, but it will be Wiki text. |
paul@55 | 570 | |
paul@92 | 571 | elif term in ("description", "link", "location"): |
paul@67 | 572 | desc = event_details[term] |
paul@55 | 573 | |
paul@69 | 574 | replaced_terms.add(term) |
paul@69 | 575 | |
paul@75 | 576 | # Add the replaced value. |
paul@75 | 577 | |
paul@67 | 578 | new_body_parts.append(desc) |
paul@10 | 579 | |
paul@69 | 580 | # Remember where in the page has been processed. |
paul@69 | 581 | |
paul@69 | 582 | end_of_last_match = match.end() |
paul@69 | 583 | |
paul@69 | 584 | # Write the rest of the page. |
paul@69 | 585 | |
paul@69 | 586 | new_body_parts.append(body[end_of_last_match:]) |
paul@10 | 587 | |
paul@67 | 588 | self.body = "".join(new_body_parts) |
paul@11 | 589 | |
paul@67 | 590 | def flushCategoryMembership(self): |
paul@17 | 591 | |
paul@67 | 592 | "Flush the category membership to the page body." |
paul@11 | 593 | |
paul@67 | 594 | body = self.getBody() |
paul@67 | 595 | category_names = self.getCategoryMembership() |
paul@67 | 596 | match = category_membership_regexp.search(body) |
paul@10 | 597 | |
paul@67 | 598 | if match: |
paul@67 | 599 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@10 | 600 | |
paul@67 | 601 | def saveChanges(self): |
paul@10 | 602 | |
paul@67 | 603 | "Save changes to the event." |
paul@10 | 604 | |
paul@67 | 605 | self.flushEventDetails() |
paul@67 | 606 | self.flushCategoryMembership() |
paul@67 | 607 | self.page.saveText(self.getBody(), 0) |
paul@10 | 608 | |
paul@67 | 609 | def linkToPage(self, request, text, query_string=None): |
paul@11 | 610 | |
paul@67 | 611 | """ |
paul@67 | 612 | Using 'request', return a link to this page with the given link 'text' |
paul@67 | 613 | and optional 'query_string'. |
paul@67 | 614 | """ |
paul@11 | 615 | |
paul@67 | 616 | return linkToPage(request, self.page, text, query_string) |
paul@13 | 617 | |
paul@69 | 618 | class Event: |
paul@69 | 619 | |
paul@69 | 620 | "A description of an event." |
paul@69 | 621 | |
paul@69 | 622 | def __init__(self, page, details): |
paul@69 | 623 | self.page = page |
paul@69 | 624 | self.details = details |
paul@69 | 625 | |
paul@69 | 626 | def __cmp__(self, other): |
paul@69 | 627 | |
paul@69 | 628 | """ |
paul@69 | 629 | Compare this object with 'other' using the event start and end details. |
paul@69 | 630 | """ |
paul@69 | 631 | |
paul@69 | 632 | details1 = self.details |
paul@69 | 633 | details2 = other.details |
paul@69 | 634 | return cmp( |
paul@69 | 635 | (details1["start"], details1["end"]), |
paul@69 | 636 | (details2["start"], details2["end"]) |
paul@69 | 637 | ) |
paul@69 | 638 | |
paul@69 | 639 | def getPage(self): |
paul@69 | 640 | |
paul@69 | 641 | "Return the page describing this event." |
paul@69 | 642 | |
paul@69 | 643 | return self.page |
paul@69 | 644 | |
paul@69 | 645 | def setPage(self, page): |
paul@69 | 646 | |
paul@69 | 647 | "Set the 'page' describing this event." |
paul@69 | 648 | |
paul@69 | 649 | self.page = page |
paul@69 | 650 | |
paul@69 | 651 | def getSummary(self, event_parent=None): |
paul@69 | 652 | |
paul@69 | 653 | """ |
paul@69 | 654 | Return either the given title or summary of the event according to the |
paul@69 | 655 | event details, or a summary made from using the pretty version of the |
paul@69 | 656 | page name. |
paul@69 | 657 | |
paul@69 | 658 | If the optional 'event_parent' is specified, any page beneath the given |
paul@69 | 659 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@69 | 660 | if its name is used as the summary. |
paul@69 | 661 | """ |
paul@69 | 662 | |
paul@69 | 663 | event_details = self.details |
paul@69 | 664 | |
paul@69 | 665 | if event_details.has_key("title"): |
paul@69 | 666 | return event_details["title"] |
paul@69 | 667 | elif event_details.has_key("summary"): |
paul@69 | 668 | return event_details["summary"] |
paul@69 | 669 | else: |
paul@69 | 670 | # If appropriate, remove the parent details and "/" character. |
paul@69 | 671 | |
paul@69 | 672 | title = self.page.getPageName() |
paul@69 | 673 | |
paul@74 | 674 | if event_parent and title.startswith(event_parent): |
paul@69 | 675 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@69 | 676 | |
paul@69 | 677 | return getPrettyTitle(title) |
paul@69 | 678 | |
paul@69 | 679 | def getDetails(self): |
paul@69 | 680 | |
paul@69 | 681 | "Return the details for this event." |
paul@69 | 682 | |
paul@69 | 683 | return self.details |
paul@69 | 684 | |
paul@69 | 685 | def setDetails(self, event_details): |
paul@69 | 686 | |
paul@69 | 687 | "Set the 'event_details' for this event." |
paul@69 | 688 | |
paul@69 | 689 | self.details = event_details |
paul@69 | 690 | |
paul@10 | 691 | def getEvents(request, category_names, calendar_start=None, calendar_end=None): |
paul@10 | 692 | |
paul@10 | 693 | """ |
paul@10 | 694 | Using the 'request', generate a list of events found on pages belonging to |
paul@10 | 695 | the specified 'category_names', using the optional 'calendar_start' and |
paul@10 | 696 | 'calendar_end' month tuples of the form (year, month) to indicate a window |
paul@10 | 697 | of interest. |
paul@10 | 698 | |
paul@10 | 699 | Return a list of events, a dictionary mapping months to event lists (within |
paul@10 | 700 | the window of interest), a list of all events within the window of interest, |
paul@10 | 701 | the earliest month of an event within the window of interest, and the latest |
paul@10 | 702 | month of an event within the window of interest. |
paul@10 | 703 | """ |
paul@10 | 704 | |
paul@12 | 705 | # Re-order the window, if appropriate. |
paul@12 | 706 | |
paul@12 | 707 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@12 | 708 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@12 | 709 | |
paul@10 | 710 | events = [] |
paul@10 | 711 | shown_events = {} |
paul@10 | 712 | all_shown_events = [] |
paul@17 | 713 | processed_pages = set() |
paul@10 | 714 | |
paul@10 | 715 | earliest = None |
paul@10 | 716 | latest = None |
paul@10 | 717 | |
paul@10 | 718 | for category_name in category_names: |
paul@10 | 719 | |
paul@10 | 720 | # Get the pages and page names in the category. |
paul@10 | 721 | |
paul@67 | 722 | pages_in_category = getCategoryPages(category_name, request) |
paul@10 | 723 | |
paul@10 | 724 | # Visit each page in the category. |
paul@10 | 725 | |
paul@10 | 726 | for page_in_category in pages_in_category: |
paul@10 | 727 | pagename = page_in_category.page_name |
paul@10 | 728 | |
paul@17 | 729 | # Only process each page once. |
paul@17 | 730 | |
paul@17 | 731 | if pagename in processed_pages: |
paul@17 | 732 | continue |
paul@17 | 733 | else: |
paul@17 | 734 | processed_pages.add(pagename) |
paul@17 | 735 | |
paul@10 | 736 | # Get a real page, not a result page. |
paul@10 | 737 | |
paul@67 | 738 | event_page = EventPage(Page(request, pagename)) |
paul@10 | 739 | |
paul@69 | 740 | # Get all events described in the page. |
paul@10 | 741 | |
paul@69 | 742 | for event in event_page.getEvents(): |
paul@69 | 743 | event_details = event.getDetails() |
paul@10 | 744 | |
paul@69 | 745 | # Remember the event. |
paul@69 | 746 | |
paul@69 | 747 | events.append(event) |
paul@10 | 748 | |
paul@69 | 749 | # Test for the suitability of the event. |
paul@69 | 750 | |
paul@69 | 751 | if event_details.has_key("start") and event_details.has_key("end"): |
paul@10 | 752 | |
paul@69 | 753 | start_month = event_details["start"].as_month() |
paul@69 | 754 | end_month = event_details["end"].as_month() |
paul@10 | 755 | |
paul@69 | 756 | # Compare the months of the dates to the requested calendar |
paul@69 | 757 | # window, if any. |
paul@10 | 758 | |
paul@69 | 759 | if (calendar_start is None or end_month >= calendar_start) and \ |
paul@69 | 760 | (calendar_end is None or start_month <= calendar_end): |
paul@10 | 761 | |
paul@69 | 762 | all_shown_events.append(event) |
paul@10 | 763 | |
paul@69 | 764 | if earliest is None or start_month < earliest: |
paul@69 | 765 | earliest = start_month |
paul@69 | 766 | if latest is None or end_month > latest: |
paul@69 | 767 | latest = end_month |
paul@10 | 768 | |
paul@69 | 769 | # Store the event in the month-specific dictionary. |
paul@10 | 770 | |
paul@69 | 771 | first = max(start_month, calendar_start or start_month) |
paul@69 | 772 | last = min(end_month, calendar_end or end_month) |
paul@10 | 773 | |
paul@69 | 774 | for event_month in first.months_until(last): |
paul@69 | 775 | if not shown_events.has_key(event_month): |
paul@69 | 776 | shown_events[event_month] = [] |
paul@69 | 777 | shown_events[event_month].append(event) |
paul@10 | 778 | |
paul@10 | 779 | return events, shown_events, all_shown_events, earliest, latest |
paul@10 | 780 | |
paul@29 | 781 | def setEventTimestamps(request, events): |
paul@29 | 782 | |
paul@29 | 783 | """ |
paul@29 | 784 | Using 'request', set timestamp details in the details dictionary of each of |
paul@67 | 785 | the 'events'. |
paul@29 | 786 | |
paul@111 | 787 | Return the latest timestamp found. |
paul@29 | 788 | """ |
paul@29 | 789 | |
paul@29 | 790 | latest = None |
paul@29 | 791 | |
paul@69 | 792 | for event in events: |
paul@69 | 793 | event_details = event.getDetails() |
paul@69 | 794 | event_page = event.getPage() |
paul@29 | 795 | |
paul@29 | 796 | # Get the initial revision of the page. |
paul@29 | 797 | |
paul@67 | 798 | revisions = event_page.getRevisions() |
paul@67 | 799 | event_page_initial = Page(request, event_page.getPageName(), rev=revisions[-1]) |
paul@29 | 800 | |
paul@29 | 801 | # Get the created and last modified times. |
paul@29 | 802 | |
paul@30 | 803 | initial_revision = getPageRevision(event_page_initial) |
paul@30 | 804 | event_details["created"] = initial_revision["timestamp"] |
paul@67 | 805 | latest_revision = event_page.getPageRevision() |
paul@30 | 806 | event_details["last-modified"] = latest_revision["timestamp"] |
paul@29 | 807 | event_details["sequence"] = len(revisions) - 1 |
paul@30 | 808 | event_details["last-comment"] = latest_revision["comment"] |
paul@29 | 809 | |
paul@29 | 810 | if latest is None or latest < event_details["last-modified"]: |
paul@29 | 811 | latest = event_details["last-modified"] |
paul@29 | 812 | |
paul@29 | 813 | return latest |
paul@29 | 814 | |
paul@26 | 815 | def getOrderedEvents(events): |
paul@26 | 816 | |
paul@26 | 817 | """ |
paul@26 | 818 | Return a list with the given 'events' ordered according to their start and |
paul@67 | 819 | end dates. |
paul@26 | 820 | """ |
paul@26 | 821 | |
paul@26 | 822 | ordered_events = events[:] |
paul@68 | 823 | ordered_events.sort() |
paul@26 | 824 | return ordered_events |
paul@26 | 825 | |
paul@13 | 826 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest): |
paul@13 | 827 | |
paul@13 | 828 | """ |
paul@13 | 829 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@13 | 830 | indicating that no restriction is imposed on the period for each of the |
paul@13 | 831 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@13 | 832 | specific period of interest. |
paul@13 | 833 | """ |
paul@13 | 834 | |
paul@13 | 835 | # Define the period as starting with any specified start month or the |
paul@13 | 836 | # earliest event known, ending with any specified end month or the latest |
paul@13 | 837 | # event known. |
paul@13 | 838 | |
paul@13 | 839 | first = calendar_start or earliest |
paul@13 | 840 | last = calendar_end or latest |
paul@13 | 841 | |
paul@13 | 842 | # If there is no range of months to show, perhaps because there are no |
paul@13 | 843 | # events in the requested period, and there was no start or end month |
paul@13 | 844 | # specified, show only the month indicated by the start or end of the |
paul@13 | 845 | # requested period. If all events were to be shown but none were found show |
paul@13 | 846 | # the current month. |
paul@13 | 847 | |
paul@13 | 848 | if first is None: |
paul@13 | 849 | first = last or getCurrentMonth() |
paul@13 | 850 | if last is None: |
paul@13 | 851 | last = first or getCurrentMonth() |
paul@13 | 852 | |
paul@13 | 853 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@13 | 854 | |
paul@13 | 855 | return min(first, last), last |
paul@13 | 856 | |
paul@119 | 857 | # NOTE: Support coverage using times within days. This will involve timespan |
paul@119 | 858 | # NOTE: objects which can be compared in such a way that set operations will be |
paul@119 | 859 | # NOTE: able to detect overlapping periods. |
paul@118 | 860 | |
paul@15 | 861 | def getCoverage(start, end, events): |
paul@15 | 862 | |
paul@15 | 863 | """ |
paul@15 | 864 | Within the period defined by the 'start' and 'end' dates, determine the |
paul@15 | 865 | coverage of the days in the period by the given 'events', returning a set of |
paul@15 | 866 | covered days, along with a list of slots, where each slot contains a tuple |
paul@15 | 867 | of the form (set of covered days, events). |
paul@15 | 868 | """ |
paul@15 | 869 | |
paul@118 | 870 | all_events = {} |
paul@15 | 871 | full_coverage = set() |
paul@15 | 872 | |
paul@15 | 873 | # Get event details. |
paul@15 | 874 | |
paul@69 | 875 | for event in events: |
paul@69 | 876 | event_details = event.getDetails() |
paul@15 | 877 | |
paul@15 | 878 | # Test for the event in the period. |
paul@15 | 879 | |
paul@15 | 880 | if event_details["start"] <= end and event_details["end"] >= start: |
paul@15 | 881 | |
paul@15 | 882 | # Find the coverage of this period for the event. |
paul@15 | 883 | |
paul@15 | 884 | event_start = max(event_details["start"], start) |
paul@15 | 885 | event_end = min(event_details["end"], end) |
paul@67 | 886 | event_coverage = set(event_start.days_until(event_end)) |
paul@118 | 887 | event_location = event_details.get("location") |
paul@15 | 888 | |
paul@15 | 889 | # Update the overall coverage. |
paul@15 | 890 | |
paul@15 | 891 | full_coverage.update(event_coverage) |
paul@15 | 892 | |
paul@118 | 893 | # Add a new events list for a new location. |
paul@118 | 894 | # Locations can be unspecified, thus None refers to all unlocalised |
paul@118 | 895 | # events. |
paul@15 | 896 | |
paul@118 | 897 | if not all_events.has_key(event_location): |
paul@118 | 898 | all_events[event_location] = [(event_coverage, [event])] |
paul@15 | 899 | |
paul@118 | 900 | # Try and fit the event into an events list. |
paul@15 | 901 | |
paul@15 | 902 | else: |
paul@118 | 903 | slot = all_events[event_location] |
paul@118 | 904 | |
paul@118 | 905 | for i, (coverage, covered_events) in enumerate(slot): |
paul@118 | 906 | |
paul@118 | 907 | # Where the event does not overlap with the current |
paul@118 | 908 | # element, add it alongside existing events. |
paul@118 | 909 | |
paul@118 | 910 | if not coverage.intersection(event_coverage): |
paul@118 | 911 | covered_events.append(event) |
paul@118 | 912 | slot[i] = coverage.union(event_coverage), covered_events |
paul@118 | 913 | break |
paul@118 | 914 | |
paul@118 | 915 | # Make a new element in the list if the event cannot be |
paul@118 | 916 | # marked alongside existing events. |
paul@118 | 917 | |
paul@118 | 918 | else: |
paul@118 | 919 | slot.append((event_coverage, [event])) |
paul@15 | 920 | |
paul@15 | 921 | return full_coverage, all_events |
paul@15 | 922 | |
paul@67 | 923 | # Date-related functions. |
paul@67 | 924 | |
paul@67 | 925 | class Period: |
paul@67 | 926 | |
paul@67 | 927 | "A simple period of time." |
paul@67 | 928 | |
paul@67 | 929 | def __init__(self, data): |
paul@67 | 930 | self.data = data |
paul@67 | 931 | |
paul@67 | 932 | def months(self): |
paul@67 | 933 | return self.data[0] * 12 + self.data[1] |
paul@67 | 934 | |
paul@92 | 935 | class Temporal: |
paul@67 | 936 | |
paul@92 | 937 | "A simple temporal representation, common to dates and times." |
paul@67 | 938 | |
paul@67 | 939 | def __init__(self, data): |
paul@92 | 940 | self.data = list(data) |
paul@67 | 941 | |
paul@67 | 942 | def __repr__(self): |
paul@67 | 943 | return "%s(%r)" % (self.__class__.__name__, self.data) |
paul@67 | 944 | |
paul@67 | 945 | def __hash__(self): |
paul@67 | 946 | return hash(self.as_tuple()) |
paul@67 | 947 | |
paul@67 | 948 | def as_tuple(self): |
paul@92 | 949 | return tuple(self.data) |
paul@92 | 950 | |
paul@92 | 951 | def __cmp__(self, other): |
paul@92 | 952 | data = self.as_tuple() |
paul@92 | 953 | other_data = other.as_tuple() |
paul@92 | 954 | length = min(len(data), len(other_data)) |
paul@92 | 955 | return cmp(self.data[:length], other.data[:length]) |
paul@92 | 956 | |
paul@103 | 957 | def until(self, start, end, nextfn, prevfn): |
paul@103 | 958 | |
paul@103 | 959 | """ |
paul@103 | 960 | Return a collection of units of time by starting from the given 'start' |
paul@103 | 961 | and stepping across intervening units until 'end' is reached, using the |
paul@103 | 962 | given 'nextfn' and 'prevfn' to step from one unit to the next. |
paul@103 | 963 | """ |
paul@103 | 964 | |
paul@103 | 965 | current = start |
paul@103 | 966 | units = [current] |
paul@103 | 967 | if current < end: |
paul@103 | 968 | while current < end: |
paul@103 | 969 | current = nextfn(current) |
paul@103 | 970 | units.append(current) |
paul@103 | 971 | elif current > end: |
paul@103 | 972 | while current > end: |
paul@103 | 973 | current = prevfn(current) |
paul@103 | 974 | units.append(current) |
paul@103 | 975 | return units |
paul@103 | 976 | |
paul@92 | 977 | class Month(Temporal): |
paul@92 | 978 | |
paul@92 | 979 | "A simple year-month representation." |
paul@92 | 980 | |
paul@92 | 981 | def __str__(self): |
paul@92 | 982 | return "%04d-%02d" % self.as_tuple()[:2] |
paul@67 | 983 | |
paul@93 | 984 | def as_datetime(self, day, hour, minute, second, zone): |
paul@93 | 985 | return DateTime(self.as_tuple() + (day, hour, minute, second, zone)) |
paul@93 | 986 | |
paul@67 | 987 | def as_date(self, day): |
paul@67 | 988 | return Date(self.as_tuple() + (day,)) |
paul@67 | 989 | |
paul@92 | 990 | def as_month(self): |
paul@92 | 991 | return self |
paul@92 | 992 | |
paul@67 | 993 | def year(self): |
paul@67 | 994 | return self.data[0] |
paul@67 | 995 | |
paul@67 | 996 | def month(self): |
paul@67 | 997 | return self.data[1] |
paul@67 | 998 | |
paul@67 | 999 | def month_properties(self): |
paul@67 | 1000 | |
paul@67 | 1001 | """ |
paul@67 | 1002 | Return the weekday of the 1st of the month, along with the number of |
paul@67 | 1003 | days, as a tuple. |
paul@67 | 1004 | """ |
paul@67 | 1005 | |
paul@92 | 1006 | year, month = self.as_tuple()[:2] |
paul@67 | 1007 | return calendar.monthrange(year, month) |
paul@67 | 1008 | |
paul@67 | 1009 | def month_update(self, n=1): |
paul@67 | 1010 | |
paul@67 | 1011 | "Return the month updated by 'n' months." |
paul@67 | 1012 | |
paul@92 | 1013 | year, month = self.as_tuple()[:2] |
paul@67 | 1014 | return Month((year + (month - 1 + n) / 12, (month - 1 + n) % 12 + 1)) |
paul@67 | 1015 | |
paul@67 | 1016 | def next_month(self): |
paul@67 | 1017 | |
paul@67 | 1018 | "Return the month following this one." |
paul@67 | 1019 | |
paul@67 | 1020 | return self.month_update(1) |
paul@67 | 1021 | |
paul@67 | 1022 | def previous_month(self): |
paul@67 | 1023 | |
paul@67 | 1024 | "Return the month preceding this one." |
paul@67 | 1025 | |
paul@67 | 1026 | return self.month_update(-1) |
paul@67 | 1027 | |
paul@67 | 1028 | def __sub__(self, start): |
paul@67 | 1029 | |
paul@67 | 1030 | """ |
paul@67 | 1031 | Return the difference in years and months between this month and the |
paul@67 | 1032 | 'start' month as a period. |
paul@67 | 1033 | """ |
paul@67 | 1034 | |
paul@67 | 1035 | return Period([(x - y) for x, y in zip(self.data, start.data)]) |
paul@67 | 1036 | |
paul@67 | 1037 | def months_until(self, end): |
paul@92 | 1038 | |
paul@92 | 1039 | "Return the collection of months from this month until 'end'." |
paul@92 | 1040 | |
paul@92 | 1041 | return self.until(self.as_month(), end.as_month(), Month.next_month, Month.previous_month) |
paul@67 | 1042 | |
paul@67 | 1043 | class Date(Month): |
paul@67 | 1044 | |
paul@67 | 1045 | "A simple year-month-day representation." |
paul@67 | 1046 | |
paul@96 | 1047 | def constrain(self): |
paul@96 | 1048 | year, month, day = self.as_tuple()[:3] |
paul@96 | 1049 | |
paul@96 | 1050 | month = max(min(month, 12), 1) |
paul@96 | 1051 | wd, last_day = calendar.monthrange(year, month) |
paul@96 | 1052 | day = max(min(day, last_day), 1) |
paul@96 | 1053 | |
paul@96 | 1054 | self.data[1:3] = month, day |
paul@96 | 1055 | |
paul@67 | 1056 | def __str__(self): |
paul@67 | 1057 | return "%04d-%02d-%02d" % self.as_tuple()[:3] |
paul@67 | 1058 | |
paul@93 | 1059 | def as_datetime(self, hour, minute, second, zone): |
paul@93 | 1060 | return DateTime(self.as_tuple() + (hour, minute, second, zone)) |
paul@93 | 1061 | |
paul@92 | 1062 | def as_date(self): |
paul@92 | 1063 | return self |
paul@92 | 1064 | |
paul@67 | 1065 | def as_month(self): |
paul@67 | 1066 | return Month(self.data[:2]) |
paul@67 | 1067 | |
paul@67 | 1068 | def day(self): |
paul@67 | 1069 | return self.data[2] |
paul@67 | 1070 | |
paul@67 | 1071 | def next_day(self): |
paul@67 | 1072 | |
paul@67 | 1073 | "Return the date following this one." |
paul@67 | 1074 | |
paul@92 | 1075 | year, month, day = self.as_tuple()[:3] |
paul@67 | 1076 | _wd, end_day = calendar.monthrange(year, month) |
paul@67 | 1077 | if day == end_day: |
paul@67 | 1078 | if month == 12: |
paul@67 | 1079 | return Date((year + 1, 1, 1)) |
paul@67 | 1080 | else: |
paul@67 | 1081 | return Date((year, month + 1, 1)) |
paul@67 | 1082 | else: |
paul@67 | 1083 | return Date((year, month, day + 1)) |
paul@67 | 1084 | |
paul@67 | 1085 | def previous_day(self): |
paul@67 | 1086 | |
paul@67 | 1087 | "Return the date preceding this one." |
paul@67 | 1088 | |
paul@92 | 1089 | year, month, day = self.as_tuple()[:3] |
paul@67 | 1090 | if day == 1: |
paul@67 | 1091 | if month == 1: |
paul@67 | 1092 | return Date((year - 1, 12, 31)) |
paul@67 | 1093 | else: |
paul@67 | 1094 | _wd, end_day = calendar.monthrange(year, month - 1) |
paul@67 | 1095 | return Date((year, month - 1, end_day)) |
paul@67 | 1096 | else: |
paul@67 | 1097 | return Date((year, month, day - 1)) |
paul@67 | 1098 | |
paul@67 | 1099 | def days_until(self, end): |
paul@92 | 1100 | |
paul@92 | 1101 | "Return the collection of days from this date until 'end'." |
paul@92 | 1102 | |
paul@92 | 1103 | return self.until(self.as_date(), end.as_date(), Date.next_day, Date.previous_day) |
paul@92 | 1104 | |
paul@92 | 1105 | class DateTime(Date): |
paul@92 | 1106 | |
paul@92 | 1107 | "A simple date plus time representation." |
paul@92 | 1108 | |
paul@96 | 1109 | def constrain(self): |
paul@96 | 1110 | Date.constrain(self) |
paul@96 | 1111 | |
paul@96 | 1112 | hour, minute, second = self.as_tuple()[3:6] |
paul@96 | 1113 | |
paul@96 | 1114 | if self.has_time(): |
paul@96 | 1115 | hour = max(min(hour, 23), 0) |
paul@96 | 1116 | minute = max(min(minute, 59), 0) |
paul@96 | 1117 | |
paul@96 | 1118 | if second is not None: |
paul@96 | 1119 | second = max(min(second, 60), 0) # support leap seconds |
paul@96 | 1120 | |
paul@96 | 1121 | self.data[3:6] = hour, minute, second |
paul@92 | 1122 | |
paul@92 | 1123 | def __str__(self): |
paul@92 | 1124 | if self.has_time(): |
paul@92 | 1125 | data = self.as_tuple() |
paul@92 | 1126 | time_str = " %02d:%02d" % data[3:5] |
paul@92 | 1127 | if data[5] is not None: |
paul@92 | 1128 | time_str += ":%02d" % data[5] |
paul@92 | 1129 | if data[6] is not None: |
paul@92 | 1130 | time_str += " %s" % data[6] |
paul@92 | 1131 | else: |
paul@92 | 1132 | time_str = "" |
paul@92 | 1133 | |
paul@92 | 1134 | return Date.__str__(self) + time_str |
paul@92 | 1135 | |
paul@93 | 1136 | def as_datetime(self): |
paul@93 | 1137 | return self |
paul@93 | 1138 | |
paul@92 | 1139 | def as_date(self): |
paul@92 | 1140 | return Date(self.data[:3]) |
paul@92 | 1141 | |
paul@92 | 1142 | def has_time(self): |
paul@92 | 1143 | return self.data[3] is not None and self.data[4] is not None |
paul@92 | 1144 | |
paul@92 | 1145 | def seconds(self): |
paul@92 | 1146 | return self.data[5] |
paul@92 | 1147 | |
paul@92 | 1148 | def time_zone(self): |
paul@92 | 1149 | return self.data[6] |
paul@92 | 1150 | |
paul@93 | 1151 | def set_time_zone(self, value): |
paul@92 | 1152 | self.data[6] = value |
paul@92 | 1153 | |
paul@92 | 1154 | def padded(self): |
paul@92 | 1155 | |
paul@92 | 1156 | "Return a datetime with missing fields defined as being zero." |
paul@92 | 1157 | |
paul@92 | 1158 | data = map(lambda x: x or 0, self.data[:6]) + self.data[6:] |
paul@92 | 1159 | return DateTime(data) |
paul@92 | 1160 | |
paul@93 | 1161 | def to_utc(self): |
paul@93 | 1162 | |
paul@93 | 1163 | """ |
paul@93 | 1164 | Return this object converted to UTC, or None if such a conversion is not |
paul@93 | 1165 | defined. |
paul@93 | 1166 | """ |
paul@93 | 1167 | |
paul@93 | 1168 | offset = self.utc_offset() |
paul@93 | 1169 | if offset: |
paul@93 | 1170 | hours, minutes = offset |
paul@93 | 1171 | |
paul@93 | 1172 | # Invert the offset to get the correction. |
paul@93 | 1173 | |
paul@93 | 1174 | hours, minutes = -hours, -minutes |
paul@93 | 1175 | |
paul@93 | 1176 | # Get the components. |
paul@93 | 1177 | |
paul@93 | 1178 | hour, minute, second, zone = self.as_tuple()[3:] |
paul@93 | 1179 | date = self.as_date() |
paul@93 | 1180 | |
paul@93 | 1181 | # Add the minutes and hours. |
paul@93 | 1182 | |
paul@93 | 1183 | minute += minutes |
paul@103 | 1184 | if minute < 0 or minute > 59: |
paul@103 | 1185 | hour += minute / 60 |
paul@103 | 1186 | minute = minute % 60 |
paul@103 | 1187 | |
paul@103 | 1188 | # NOTE: This makes various assumptions and probably would not work |
paul@103 | 1189 | # NOTE: for general arithmetic. |
paul@93 | 1190 | |
paul@93 | 1191 | hour += hours |
paul@93 | 1192 | if hour < 0: |
paul@93 | 1193 | date = date.previous_day() |
paul@93 | 1194 | hour += 24 |
paul@93 | 1195 | elif hour > 23: |
paul@93 | 1196 | date = date.next_day() |
paul@93 | 1197 | hour -= 24 |
paul@93 | 1198 | |
paul@93 | 1199 | return date.as_datetime(hour, minute, second, "UTC") |
paul@100 | 1200 | |
paul@100 | 1201 | # Cannot convert. |
paul@100 | 1202 | |
paul@93 | 1203 | else: |
paul@93 | 1204 | return None |
paul@93 | 1205 | |
paul@93 | 1206 | def utc_offset(self): |
paul@93 | 1207 | |
paul@93 | 1208 | "Return the UTC offset in hours and minutes." |
paul@93 | 1209 | |
paul@93 | 1210 | zone = self.time_zone() |
paul@95 | 1211 | if not zone: |
paul@95 | 1212 | return None |
paul@93 | 1213 | |
paul@96 | 1214 | # Support explicit UTC zones. |
paul@96 | 1215 | |
paul@96 | 1216 | if zone == "UTC": |
paul@96 | 1217 | return 0, 0 |
paul@96 | 1218 | |
paul@94 | 1219 | # Attempt to return a UTC offset where an explicit offset has been set. |
paul@93 | 1220 | |
paul@94 | 1221 | match = timezone_offset_regexp.match(zone) |
paul@94 | 1222 | if match: |
paul@94 | 1223 | if match.group("sign") == "-": |
paul@93 | 1224 | sign = -1 |
paul@93 | 1225 | else: |
paul@93 | 1226 | sign = 1 |
paul@93 | 1227 | |
paul@94 | 1228 | hours = int(match.group("hours")) * sign |
paul@94 | 1229 | minutes = int(match.group("minutes") or 0) * sign |
paul@94 | 1230 | return hours, minutes |
paul@94 | 1231 | |
paul@102 | 1232 | # Attempt to handle Olson time zone identifiers. |
paul@102 | 1233 | |
paul@102 | 1234 | dt = self.as_olson_datetime() |
paul@102 | 1235 | if dt: |
paul@102 | 1236 | seconds = dt.utcoffset().seconds |
paul@102 | 1237 | hours = seconds / 3600 |
paul@102 | 1238 | minutes = (seconds % 3600) / 60 |
paul@102 | 1239 | return hours, minutes |
paul@102 | 1240 | |
paul@102 | 1241 | # Otherwise return None. |
paul@102 | 1242 | |
paul@93 | 1243 | return None |
paul@93 | 1244 | |
paul@100 | 1245 | def olson_identifier(self): |
paul@100 | 1246 | |
paul@100 | 1247 | "Return the Olson identifier from any zone information." |
paul@100 | 1248 | |
paul@100 | 1249 | zone = self.time_zone() |
paul@100 | 1250 | if not zone: |
paul@100 | 1251 | return None |
paul@100 | 1252 | |
paul@100 | 1253 | # Attempt to match an identifier. |
paul@100 | 1254 | |
paul@100 | 1255 | match = timezone_olson_regexp.match(zone) |
paul@100 | 1256 | if match: |
paul@100 | 1257 | return match.group("olson") |
paul@100 | 1258 | else: |
paul@100 | 1259 | return None |
paul@100 | 1260 | |
paul@102 | 1261 | def _as_olson_datetime(self, hours=None): |
paul@102 | 1262 | |
paul@102 | 1263 | """ |
paul@102 | 1264 | Return a Python datetime object for this datetime interpreted using any |
paul@102 | 1265 | Olson time zone identifier and the given 'hours' offset, raising one of |
paul@102 | 1266 | the pytz exceptions in case of ambiguity. |
paul@102 | 1267 | """ |
paul@102 | 1268 | |
paul@102 | 1269 | olson = self.olson_identifier() |
paul@102 | 1270 | if olson and pytz: |
paul@102 | 1271 | tz = pytz.timezone(olson) |
paul@102 | 1272 | data = self.padded().as_tuple()[:6] |
paul@102 | 1273 | dt = datetime.datetime(*data) |
paul@102 | 1274 | |
paul@102 | 1275 | # With an hours offset, find a time probably in a previously |
paul@102 | 1276 | # applicable time zone. |
paul@102 | 1277 | |
paul@102 | 1278 | if hours is not None: |
paul@102 | 1279 | td = datetime.timedelta(0, hours * 3600) |
paul@102 | 1280 | dt += td |
paul@102 | 1281 | |
paul@102 | 1282 | ldt = tz.localize(dt, None) |
paul@102 | 1283 | |
paul@102 | 1284 | # With an hours offset, adjust the time to define it within the |
paul@102 | 1285 | # previously applicable time zone but at the presumably intended |
paul@102 | 1286 | # position. |
paul@102 | 1287 | |
paul@102 | 1288 | if hours is not None: |
paul@102 | 1289 | ldt -= td |
paul@102 | 1290 | |
paul@102 | 1291 | return ldt |
paul@102 | 1292 | else: |
paul@102 | 1293 | return None |
paul@102 | 1294 | |
paul@102 | 1295 | def as_olson_datetime(self): |
paul@102 | 1296 | |
paul@102 | 1297 | """ |
paul@102 | 1298 | Return a Python datetime object for this datetime interpreted using any |
paul@102 | 1299 | Olson time zone identifier, choosing the time from the zone before the |
paul@102 | 1300 | period of ambiguity. |
paul@102 | 1301 | """ |
paul@102 | 1302 | |
paul@102 | 1303 | try: |
paul@102 | 1304 | return self._as_olson_datetime() |
paul@102 | 1305 | except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError): |
paul@103 | 1306 | |
paul@103 | 1307 | # Try again, using an earlier local time and then stepping forward |
paul@103 | 1308 | # in the chosen zone. |
paul@103 | 1309 | # NOTE: Four hours earlier seems reasonable. |
paul@103 | 1310 | |
paul@103 | 1311 | return self._as_olson_datetime(-4) |
paul@102 | 1312 | |
paul@100 | 1313 | def ambiguous(self): |
paul@100 | 1314 | |
paul@100 | 1315 | "Return whether the time is local and ambiguous." |
paul@100 | 1316 | |
paul@102 | 1317 | try: |
paul@102 | 1318 | self._as_olson_datetime() |
paul@102 | 1319 | except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError): |
paul@102 | 1320 | return 1 |
paul@100 | 1321 | |
paul@100 | 1322 | return 0 |
paul@100 | 1323 | |
paul@93 | 1324 | def getCountry(s): |
paul@92 | 1325 | |
paul@93 | 1326 | "Find a country code in the given string 's'." |
paul@93 | 1327 | |
paul@93 | 1328 | match = country_code_regexp.search(s) |
paul@92 | 1329 | |
paul@93 | 1330 | if match: |
paul@93 | 1331 | return match.group("code") |
paul@93 | 1332 | else: |
paul@93 | 1333 | return None |
paul@67 | 1334 | |
paul@67 | 1335 | def getDate(s): |
paul@67 | 1336 | |
paul@92 | 1337 | "Parse the string 's', extracting and returning a datetime object." |
paul@67 | 1338 | |
paul@92 | 1339 | m = datetime_regexp.search(s) |
paul@67 | 1340 | if m: |
paul@92 | 1341 | groups = list(m.groups()) |
paul@92 | 1342 | |
paul@94 | 1343 | # Convert date and time data to integer or None. |
paul@92 | 1344 | |
paul@94 | 1345 | return DateTime(map(int_or_none, groups[:6]) + [m.group("zone")]) |
paul@67 | 1346 | else: |
paul@67 | 1347 | return None |
paul@67 | 1348 | |
paul@88 | 1349 | def getDateStrings(s): |
paul@88 | 1350 | |
paul@88 | 1351 | "Parse the string 's', extracting and returning all date strings." |
paul@88 | 1352 | |
paul@88 | 1353 | start = 0 |
paul@88 | 1354 | m = date_regexp.search(s, start) |
paul@88 | 1355 | l = [] |
paul@88 | 1356 | while m: |
paul@88 | 1357 | l.append("-".join(m.groups())) |
paul@88 | 1358 | m = date_regexp.search(s, m.end()) |
paul@88 | 1359 | return l |
paul@88 | 1360 | |
paul@67 | 1361 | def getMonth(s): |
paul@67 | 1362 | |
paul@88 | 1363 | "Parse the string 's', extracting and returning a month object." |
paul@67 | 1364 | |
paul@67 | 1365 | m = month_regexp.search(s) |
paul@67 | 1366 | if m: |
paul@67 | 1367 | return Month(map(int, m.groups())) |
paul@67 | 1368 | else: |
paul@67 | 1369 | return None |
paul@67 | 1370 | |
paul@67 | 1371 | def getCurrentMonth(): |
paul@67 | 1372 | |
paul@67 | 1373 | "Return the current month as a (year, month) tuple." |
paul@67 | 1374 | |
paul@67 | 1375 | today = datetime.date.today() |
paul@67 | 1376 | return Month((today.year, today.month)) |
paul@67 | 1377 | |
paul@67 | 1378 | def getCurrentYear(): |
paul@67 | 1379 | |
paul@67 | 1380 | "Return the current year." |
paul@67 | 1381 | |
paul@67 | 1382 | today = datetime.date.today() |
paul@67 | 1383 | return today.year |
paul@67 | 1384 | |
paul@19 | 1385 | # User interface functions. |
paul@19 | 1386 | |
paul@55 | 1387 | def getParameter(request, name, default=None): |
paul@92 | 1388 | |
paul@92 | 1389 | """ |
paul@92 | 1390 | Using the given 'request', return the value of the parameter with the given |
paul@92 | 1391 | 'name', returning the optional 'default' (or None) if no value was supplied |
paul@92 | 1392 | in the 'request'. |
paul@92 | 1393 | """ |
paul@92 | 1394 | |
paul@110 | 1395 | return get_form(request).get(name, [default])[0] |
paul@23 | 1396 | |
paul@58 | 1397 | def getQualifiedParameter(request, calendar_name, argname, default=None): |
paul@92 | 1398 | |
paul@92 | 1399 | """ |
paul@92 | 1400 | Using the given 'request', 'calendar_name' and 'argname', retrieve the |
paul@92 | 1401 | value of the qualified parameter, returning the optional 'default' (or None) |
paul@92 | 1402 | if no value was supplied in the 'request'. |
paul@92 | 1403 | """ |
paul@92 | 1404 | |
paul@58 | 1405 | argname = getQualifiedParameterName(calendar_name, argname) |
paul@58 | 1406 | return getParameter(request, argname, default) |
paul@58 | 1407 | |
paul@58 | 1408 | def getQualifiedParameterName(calendar_name, argname): |
paul@92 | 1409 | |
paul@92 | 1410 | """ |
paul@92 | 1411 | Return the qualified parameter name using the given 'calendar_name' and |
paul@92 | 1412 | 'argname'. |
paul@92 | 1413 | """ |
paul@92 | 1414 | |
paul@58 | 1415 | if calendar_name is None: |
paul@58 | 1416 | return argname |
paul@58 | 1417 | else: |
paul@58 | 1418 | return "%s-%s" % (calendar_name, argname) |
paul@58 | 1419 | |
paul@19 | 1420 | def getParameterMonth(arg): |
paul@67 | 1421 | |
paul@67 | 1422 | "Interpret 'arg', recognising keywords and simple arithmetic operations." |
paul@67 | 1423 | |
paul@19 | 1424 | n = None |
paul@19 | 1425 | |
paul@19 | 1426 | if arg.startswith("current"): |
paul@19 | 1427 | date = getCurrentMonth() |
paul@19 | 1428 | if len(arg) > 8: |
paul@19 | 1429 | n = int(arg[7:]) |
paul@19 | 1430 | |
paul@19 | 1431 | elif arg.startswith("yearstart"): |
paul@67 | 1432 | date = Month((getCurrentYear(), 1)) |
paul@19 | 1433 | if len(arg) > 10: |
paul@19 | 1434 | n = int(arg[9:]) |
paul@19 | 1435 | |
paul@19 | 1436 | elif arg.startswith("yearend"): |
paul@67 | 1437 | date = Month((getCurrentYear(), 12)) |
paul@19 | 1438 | if len(arg) > 8: |
paul@19 | 1439 | n = int(arg[7:]) |
paul@19 | 1440 | |
paul@19 | 1441 | else: |
paul@19 | 1442 | date = getMonth(arg) |
paul@19 | 1443 | |
paul@19 | 1444 | if n is not None: |
paul@67 | 1445 | date = date.month_update(n) |
paul@19 | 1446 | |
paul@19 | 1447 | return date |
paul@19 | 1448 | |
paul@19 | 1449 | def getFormMonth(request, calendar_name, argname): |
paul@67 | 1450 | |
paul@67 | 1451 | """ |
paul@67 | 1452 | Return the month from the 'request' for the calendar with the given |
paul@67 | 1453 | 'calendar_name' using the parameter having the given 'argname'. |
paul@67 | 1454 | """ |
paul@67 | 1455 | |
paul@58 | 1456 | arg = getQualifiedParameter(request, calendar_name, argname) |
paul@19 | 1457 | if arg is not None: |
paul@19 | 1458 | return getParameterMonth(arg) |
paul@19 | 1459 | else: |
paul@19 | 1460 | return None |
paul@19 | 1461 | |
paul@23 | 1462 | def getFormMonthPair(request, yeararg, montharg): |
paul@67 | 1463 | |
paul@67 | 1464 | """ |
paul@67 | 1465 | Return the month from the 'request' for the calendar with the given |
paul@67 | 1466 | 'calendar_name' using the parameters having the given 'yeararg' and |
paul@67 | 1467 | 'montharg' names. |
paul@67 | 1468 | """ |
paul@67 | 1469 | |
paul@23 | 1470 | year = getParameter(request, yeararg) |
paul@23 | 1471 | month = getParameter(request, montharg) |
paul@23 | 1472 | if year and month: |
paul@67 | 1473 | return Month((int(year), int(month))) |
paul@23 | 1474 | else: |
paul@23 | 1475 | return None |
paul@23 | 1476 | |
paul@114 | 1477 | def getFullMonthLabel(request, year_month): |
paul@114 | 1478 | |
paul@114 | 1479 | """ |
paul@114 | 1480 | Return the full month plus year label using the given 'request' and |
paul@114 | 1481 | 'year_month'. |
paul@114 | 1482 | """ |
paul@114 | 1483 | |
paul@114 | 1484 | _ = request.getText |
paul@114 | 1485 | year, month = year_month.as_tuple() |
paul@114 | 1486 | month_label = _(getMonthLabel(month)) |
paul@114 | 1487 | return "%s %s" % (month_label, year) |
paul@114 | 1488 | |
paul@67 | 1489 | # Page-related functions. |
paul@67 | 1490 | |
paul@19 | 1491 | def getPrettyPageName(page): |
paul@19 | 1492 | |
paul@19 | 1493 | "Return a nicely formatted title/name for the given 'page'." |
paul@19 | 1494 | |
paul@117 | 1495 | title = page.split_title(force=1) |
paul@55 | 1496 | return getPrettyTitle(title) |
paul@55 | 1497 | |
paul@27 | 1498 | def linkToPage(request, page, text, query_string=None): |
paul@27 | 1499 | |
paul@27 | 1500 | """ |
paul@27 | 1501 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@27 | 1502 | optional 'query_string'. |
paul@27 | 1503 | """ |
paul@27 | 1504 | |
paul@27 | 1505 | text = wikiutil.escape(text) |
paul@117 | 1506 | return page.link_to_raw(request, text, query_string) |
paul@27 | 1507 | |
paul@88 | 1508 | def getFullPageName(parent, title): |
paul@88 | 1509 | |
paul@88 | 1510 | """ |
paul@88 | 1511 | Return a full page name from the given 'parent' page (can be empty or None) |
paul@88 | 1512 | and 'title' (a simple page name). |
paul@88 | 1513 | """ |
paul@88 | 1514 | |
paul@88 | 1515 | if parent: |
paul@88 | 1516 | return "%s/%s" % (parent.rstrip("/"), title) |
paul@88 | 1517 | else: |
paul@88 | 1518 | return title |
paul@88 | 1519 | |
paul@88 | 1520 | def fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames): |
paul@88 | 1521 | |
paul@88 | 1522 | """ |
paul@88 | 1523 | Using the given 'template_page', complete the 'new_page' by copying the |
paul@88 | 1524 | template and adding the given 'event_details' (a dictionary of event |
paul@88 | 1525 | fields), setting also the 'category_pagenames' to define category |
paul@88 | 1526 | membership. |
paul@88 | 1527 | """ |
paul@88 | 1528 | |
paul@88 | 1529 | event_page = EventPage(template_page) |
paul@88 | 1530 | new_event_page = EventPage(new_page) |
paul@88 | 1531 | new_event_page.copyPage(event_page) |
paul@88 | 1532 | |
paul@88 | 1533 | if new_event_page.getFormat() == "wiki": |
paul@88 | 1534 | new_event = Event(new_event_page, event_details) |
paul@88 | 1535 | new_event_page.setEvents([new_event]) |
paul@88 | 1536 | new_event_page.setCategoryMembership(category_pagenames) |
paul@88 | 1537 | new_event_page.saveChanges() |
paul@88 | 1538 | |
paul@10 | 1539 | # vim: tabstop=4 expandtab shiftwidth=4 |