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