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