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@88 | 24 | __version__ = "0.6" |
paul@10 | 25 | |
paul@22 | 26 | # Date labels. |
paul@22 | 27 | |
paul@22 | 28 | month_labels = ["January", "February", "March", "April", "May", "June", |
paul@22 | 29 | "July", "August", "September", "October", "November", "December"] |
paul@22 | 30 | weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
paul@22 | 31 | |
paul@10 | 32 | # Regular expressions where MoinMoin does not provide the required support. |
paul@10 | 33 | |
paul@10 | 34 | category_regexp = None |
paul@47 | 35 | |
paul@47 | 36 | # Page parsing. |
paul@47 | 37 | |
paul@84 | 38 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?):: )(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@47 | 39 | category_membership_regexp = re.compile(ur"^\s*((Category\S+)(\s+Category\S+)*)\s*$", re.MULTILINE | re.UNICODE) |
paul@47 | 40 | |
paul@47 | 41 | # Value parsing. |
paul@47 | 42 | |
paul@10 | 43 | date_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})', re.UNICODE) |
paul@10 | 44 | month_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})', re.UNICODE) |
paul@19 | 45 | verbatim_regexp = re.compile(ur'(?:' |
paul@19 | 46 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@19 | 47 | ur'|' |
paul@19 | 48 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@19 | 49 | ur'|' |
paul@19 | 50 | ur'`(?P<monospace>.*?)`' |
paul@19 | 51 | ur'|' |
paul@19 | 52 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@19 | 53 | ur')', re.UNICODE) |
paul@10 | 54 | |
paul@10 | 55 | # Utility functions. |
paul@10 | 56 | |
paul@10 | 57 | def isMoin15(): |
paul@10 | 58 | return version.release.startswith("1.5.") |
paul@10 | 59 | |
paul@10 | 60 | def getCategoryPattern(request): |
paul@10 | 61 | global category_regexp |
paul@10 | 62 | |
paul@10 | 63 | try: |
paul@10 | 64 | return request.cfg.cache.page_category_regexact |
paul@10 | 65 | except AttributeError: |
paul@10 | 66 | |
paul@10 | 67 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@10 | 68 | |
paul@10 | 69 | if category_regexp is None: |
paul@10 | 70 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@10 | 71 | return category_regexp |
paul@10 | 72 | |
paul@67 | 73 | # Textual representations. |
paul@67 | 74 | |
paul@67 | 75 | def getHTTPTimeString(tmtuple): |
paul@67 | 76 | return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( |
paul@67 | 77 | weekday_labels[tmtuple.tm_wday], |
paul@67 | 78 | tmtuple.tm_mday, |
paul@67 | 79 | month_labels[tmtuple.tm_mon -1], # zero-based labels |
paul@67 | 80 | tmtuple.tm_year, |
paul@67 | 81 | tmtuple.tm_hour, |
paul@67 | 82 | tmtuple.tm_min, |
paul@67 | 83 | tmtuple.tm_sec |
paul@67 | 84 | ) |
paul@67 | 85 | |
paul@67 | 86 | def getSimpleWikiText(text): |
paul@67 | 87 | |
paul@67 | 88 | """ |
paul@67 | 89 | Return the plain text representation of the given 'text' which may employ |
paul@67 | 90 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@67 | 91 | text. |
paul@67 | 92 | """ |
paul@67 | 93 | |
paul@67 | 94 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@67 | 95 | |
paul@67 | 96 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@67 | 97 | |
paul@67 | 98 | def getEncodedWikiText(text): |
paul@67 | 99 | |
paul@67 | 100 | "Encode the given 'text' in a verbatim representation." |
paul@67 | 101 | |
paul@67 | 102 | return "<<Verbatim(%s)>>" % text |
paul@67 | 103 | |
paul@67 | 104 | def getPrettyTitle(title): |
paul@67 | 105 | |
paul@67 | 106 | "Return a nicely formatted version of the given 'title'." |
paul@67 | 107 | |
paul@67 | 108 | return title.replace("_", " ").replace("/", u" ? ") |
paul@67 | 109 | |
paul@67 | 110 | def getMonthLabel(month): |
paul@67 | 111 | |
paul@67 | 112 | "Return an unlocalised label for the given 'month'." |
paul@67 | 113 | |
paul@67 | 114 | return month_labels[month - 1] # zero-based labels |
paul@67 | 115 | |
paul@67 | 116 | def getDayLabel(weekday): |
paul@67 | 117 | |
paul@67 | 118 | "Return an unlocalised label for the given 'weekday'." |
paul@67 | 119 | |
paul@67 | 120 | return weekday_labels[weekday] |
paul@67 | 121 | |
paul@19 | 122 | # Action support functions. |
paul@19 | 123 | |
paul@67 | 124 | def getPageRevision(page): |
paul@67 | 125 | |
paul@67 | 126 | "Return the revision details dictionary for the given 'page'." |
paul@67 | 127 | |
paul@67 | 128 | # From Page.edit_info... |
paul@67 | 129 | |
paul@67 | 130 | if hasattr(page, "editlog_entry"): |
paul@67 | 131 | line = page.editlog_entry() |
paul@67 | 132 | else: |
paul@67 | 133 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@67 | 134 | |
paul@89 | 135 | # Similar to Page.mtime_usecs behaviour... |
paul@89 | 136 | |
paul@89 | 137 | if line: |
paul@89 | 138 | timestamp = line.ed_time_usecs |
paul@89 | 139 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@89 | 140 | comment = line.comment |
paul@89 | 141 | else: |
paul@89 | 142 | mtime = 0 |
paul@89 | 143 | comment = "" |
paul@89 | 144 | |
paul@89 | 145 | return {"timestamp" : time.gmtime(mtime), "comment" : comment} |
paul@67 | 146 | |
paul@67 | 147 | # Category discovery and searching. |
paul@67 | 148 | |
paul@19 | 149 | def getCategories(request): |
paul@19 | 150 | |
paul@19 | 151 | """ |
paul@19 | 152 | From the AdvancedSearch macro, return a list of category page names using |
paul@19 | 153 | the given 'request'. |
paul@19 | 154 | """ |
paul@19 | 155 | |
paul@19 | 156 | # This will return all pages with "Category" in the title. |
paul@19 | 157 | |
paul@19 | 158 | cat_filter = getCategoryPattern(request).search |
paul@19 | 159 | return request.rootpage.getPageList(filter=cat_filter) |
paul@19 | 160 | |
paul@19 | 161 | def getCategoryMapping(category_pagenames, request): |
paul@19 | 162 | |
paul@19 | 163 | """ |
paul@19 | 164 | For the given 'category_pagenames' return a list of tuples of the form |
paul@19 | 165 | (category name, category page name) using the given 'request'. |
paul@19 | 166 | """ |
paul@19 | 167 | |
paul@19 | 168 | cat_pattern = getCategoryPattern(request) |
paul@19 | 169 | mapping = [] |
paul@19 | 170 | for pagename in category_pagenames: |
paul@19 | 171 | name = cat_pattern.match(pagename).group("key") |
paul@19 | 172 | if name != "Category": |
paul@19 | 173 | mapping.append((name, pagename)) |
paul@19 | 174 | mapping.sort() |
paul@19 | 175 | return mapping |
paul@19 | 176 | |
paul@67 | 177 | def getCategoryPages(pagename, request): |
paul@29 | 178 | |
paul@67 | 179 | """ |
paul@67 | 180 | Return the pages associated with the given category 'pagename' using the |
paul@67 | 181 | 'request'. |
paul@67 | 182 | """ |
paul@10 | 183 | |
paul@10 | 184 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@10 | 185 | if isMoin15(): |
paul@10 | 186 | results = search.searchPages(request, query) |
paul@10 | 187 | results.sortByPagename() |
paul@10 | 188 | else: |
paul@10 | 189 | results = search.searchPages(request, query, "page_name") |
paul@10 | 190 | |
paul@10 | 191 | cat_pattern = getCategoryPattern(request) |
paul@10 | 192 | pages = [] |
paul@10 | 193 | for page in results.hits: |
paul@10 | 194 | if not cat_pattern.match(page.page_name): |
paul@10 | 195 | pages.append(page) |
paul@10 | 196 | return pages |
paul@10 | 197 | |
paul@67 | 198 | # The main activity functions. |
paul@67 | 199 | |
paul@67 | 200 | class EventPage: |
paul@67 | 201 | |
paul@67 | 202 | "An event page." |
paul@67 | 203 | |
paul@67 | 204 | def __init__(self, page): |
paul@67 | 205 | self.page = page |
paul@69 | 206 | self.events = None |
paul@67 | 207 | self.body = None |
paul@67 | 208 | self.categories = None |
paul@67 | 209 | |
paul@67 | 210 | def copyPage(self, page): |
paul@67 | 211 | |
paul@67 | 212 | "Copy the body of the given 'page'." |
paul@67 | 213 | |
paul@67 | 214 | self.body = page.getBody() |
paul@67 | 215 | |
paul@67 | 216 | def getPageURL(self, request): |
paul@67 | 217 | |
paul@67 | 218 | "Using 'request', return the URL of this page." |
paul@24 | 219 | |
paul@67 | 220 | page = self.page |
paul@67 | 221 | |
paul@67 | 222 | if isMoin15(): |
paul@67 | 223 | return request.getQualifiedURL(page.url(request)) |
paul@67 | 224 | else: |
paul@67 | 225 | return request.getQualifiedURL(page.url(request, relative=0)) |
paul@67 | 226 | |
paul@67 | 227 | def getFormat(self): |
paul@67 | 228 | |
paul@67 | 229 | "Get the format used on this page." |
paul@24 | 230 | |
paul@67 | 231 | if isMoin15(): |
paul@67 | 232 | return "wiki" # page.pi_format |
paul@67 | 233 | else: |
paul@67 | 234 | return self.page.pi["format"] |
paul@67 | 235 | |
paul@67 | 236 | def getRevisions(self): |
paul@67 | 237 | |
paul@67 | 238 | "Return a list of page revisions." |
paul@67 | 239 | |
paul@67 | 240 | return self.page.getRevList() |
paul@67 | 241 | |
paul@67 | 242 | def getPageRevision(self): |
paul@24 | 243 | |
paul@67 | 244 | "Return the revision details dictionary for this page." |
paul@67 | 245 | |
paul@67 | 246 | return getPageRevision(self.page) |
paul@67 | 247 | |
paul@67 | 248 | def getPageName(self): |
paul@67 | 249 | |
paul@67 | 250 | "Return the page name." |
paul@67 | 251 | |
paul@67 | 252 | return self.page.page_name |
paul@24 | 253 | |
paul@67 | 254 | def getPrettyPageName(self): |
paul@67 | 255 | |
paul@67 | 256 | "Return a nicely formatted title/name for this page." |
paul@67 | 257 | |
paul@67 | 258 | return getPrettyPageName(self.page) |
paul@67 | 259 | |
paul@67 | 260 | def getBody(self): |
paul@67 | 261 | |
paul@67 | 262 | "Get the current page body." |
paul@47 | 263 | |
paul@67 | 264 | if self.body is None: |
paul@67 | 265 | self.body = self.page.get_raw_body() |
paul@67 | 266 | return self.body |
paul@67 | 267 | |
paul@69 | 268 | def getEvents(self): |
paul@69 | 269 | |
paul@69 | 270 | "Return a list of events from this page." |
paul@67 | 271 | |
paul@69 | 272 | if self.events is None: |
paul@69 | 273 | details = {} |
paul@69 | 274 | self.events = [Event(self, details)] |
paul@47 | 275 | |
paul@67 | 276 | if self.getFormat() == "wiki": |
paul@67 | 277 | for match in definition_list_regexp.finditer(self.getBody()): |
paul@67 | 278 | |
paul@67 | 279 | # Skip commented-out items. |
paul@47 | 280 | |
paul@67 | 281 | if match.group("optcomment"): |
paul@67 | 282 | continue |
paul@67 | 283 | |
paul@67 | 284 | # Permit case-insensitive list terms. |
paul@67 | 285 | |
paul@67 | 286 | term = match.group("term").lower() |
paul@67 | 287 | desc = match.group("desc") |
paul@67 | 288 | |
paul@67 | 289 | # Special value type handling. |
paul@27 | 290 | |
paul@67 | 291 | # Dates. |
paul@67 | 292 | |
paul@67 | 293 | if term in ("start", "end"): |
paul@67 | 294 | desc = getDate(desc) |
paul@67 | 295 | |
paul@67 | 296 | # Lists (whose elements may be quoted). |
paul@67 | 297 | |
paul@67 | 298 | elif term in ("topics", "categories"): |
paul@84 | 299 | desc = [getSimpleWikiText(value.strip()) for value in desc.split(",") if value.strip()] |
paul@67 | 300 | |
paul@67 | 301 | # Labels which may well be quoted. |
paul@67 | 302 | |
paul@67 | 303 | elif term in ("title", "summary", "description"): |
paul@67 | 304 | desc = getSimpleWikiText(desc) |
paul@67 | 305 | |
paul@67 | 306 | if desc is not None: |
paul@69 | 307 | |
paul@69 | 308 | # Handle apparent duplicates by creating a new set of |
paul@69 | 309 | # details. |
paul@69 | 310 | |
paul@69 | 311 | if details.has_key(term): |
paul@69 | 312 | details = {} |
paul@69 | 313 | self.events.append(Event(self, details)) |
paul@67 | 314 | |
paul@69 | 315 | details[term] = desc |
paul@69 | 316 | |
paul@69 | 317 | return self.events |
paul@69 | 318 | |
paul@69 | 319 | def setEvents(self, events): |
paul@69 | 320 | |
paul@69 | 321 | "Set the given 'events' on this page." |
paul@69 | 322 | |
paul@69 | 323 | self.events = events |
paul@67 | 324 | |
paul@67 | 325 | def getCategoryMembership(self): |
paul@27 | 326 | |
paul@67 | 327 | "Get the category names from this page." |
paul@67 | 328 | |
paul@67 | 329 | if self.categories is None: |
paul@67 | 330 | body = self.getBody() |
paul@67 | 331 | match = category_membership_regexp.search(body) |
paul@67 | 332 | self.categories = match.findall().split() |
paul@67 | 333 | |
paul@67 | 334 | return self.categories |
paul@67 | 335 | |
paul@67 | 336 | def setCategoryMembership(self, category_names): |
paul@10 | 337 | |
paul@67 | 338 | """ |
paul@67 | 339 | Set the category membership for the page using the specified |
paul@67 | 340 | 'category_names'. |
paul@67 | 341 | """ |
paul@67 | 342 | |
paul@67 | 343 | self.categories = category_names |
paul@67 | 344 | |
paul@67 | 345 | def flushEventDetails(self): |
paul@67 | 346 | |
paul@67 | 347 | "Flush the current event details to this page's body text." |
paul@10 | 348 | |
paul@67 | 349 | new_body_parts = [] |
paul@67 | 350 | end_of_last_match = 0 |
paul@67 | 351 | body = self.getBody() |
paul@69 | 352 | |
paul@69 | 353 | events = iter(self.getEvents()) |
paul@69 | 354 | |
paul@69 | 355 | event = events.next() |
paul@69 | 356 | event_details = event.getDetails() |
paul@69 | 357 | replaced_terms = set() |
paul@67 | 358 | |
paul@67 | 359 | for match in definition_list_regexp.finditer(body): |
paul@47 | 360 | |
paul@10 | 361 | # Permit case-insensitive list terms. |
paul@10 | 362 | |
paul@10 | 363 | term = match.group("term").lower() |
paul@10 | 364 | desc = match.group("desc") |
paul@10 | 365 | |
paul@69 | 366 | # Check that the term has not already been substituted. If so, |
paul@69 | 367 | # get the next event. |
paul@69 | 368 | |
paul@69 | 369 | if term in replaced_terms: |
paul@69 | 370 | try: |
paul@69 | 371 | event = events.next() |
paul@69 | 372 | |
paul@69 | 373 | # No more events. |
paul@69 | 374 | |
paul@69 | 375 | except StopIteration: |
paul@69 | 376 | break |
paul@69 | 377 | |
paul@69 | 378 | event_details = event.getDetails() |
paul@69 | 379 | replaced_terms = set() |
paul@69 | 380 | |
paul@75 | 381 | # Add preceding text to the new body. |
paul@75 | 382 | |
paul@75 | 383 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@75 | 384 | |
paul@75 | 385 | # Get the matching regions, adding the term to the new body. |
paul@75 | 386 | |
paul@75 | 387 | new_body_parts.append(match.group("wholeterm")) |
paul@75 | 388 | |
paul@10 | 389 | # Special value type handling. |
paul@10 | 390 | |
paul@67 | 391 | if event_details.has_key(term): |
paul@19 | 392 | |
paul@67 | 393 | # Dates. |
paul@47 | 394 | |
paul@67 | 395 | if term in ("start", "end"): |
paul@67 | 396 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@47 | 397 | |
paul@67 | 398 | # Lists (whose elements may be quoted). |
paul@47 | 399 | |
paul@67 | 400 | elif term in ("topics", "categories"): |
paul@67 | 401 | desc = ", ".join(getEncodedWikiText(event_details[term])) |
paul@47 | 402 | |
paul@67 | 403 | # Labels which may well be quoted. |
paul@47 | 404 | |
paul@67 | 405 | elif term in ("title", "summary"): |
paul@67 | 406 | desc = getEncodedWikiText(event_details[term]) |
paul@47 | 407 | |
paul@67 | 408 | # Text which need not be quoted, but it will be Wiki text. |
paul@55 | 409 | |
paul@88 | 410 | elif term in ("description", "link"): |
paul@67 | 411 | desc = event_details[term] |
paul@55 | 412 | |
paul@69 | 413 | replaced_terms.add(term) |
paul@69 | 414 | |
paul@75 | 415 | # Add the replaced value. |
paul@75 | 416 | |
paul@67 | 417 | new_body_parts.append(desc) |
paul@10 | 418 | |
paul@69 | 419 | # Remember where in the page has been processed. |
paul@69 | 420 | |
paul@69 | 421 | end_of_last_match = match.end() |
paul@69 | 422 | |
paul@69 | 423 | # Write the rest of the page. |
paul@69 | 424 | |
paul@69 | 425 | new_body_parts.append(body[end_of_last_match:]) |
paul@10 | 426 | |
paul@67 | 427 | self.body = "".join(new_body_parts) |
paul@11 | 428 | |
paul@67 | 429 | def flushCategoryMembership(self): |
paul@17 | 430 | |
paul@67 | 431 | "Flush the category membership to the page body." |
paul@11 | 432 | |
paul@67 | 433 | body = self.getBody() |
paul@67 | 434 | category_names = self.getCategoryMembership() |
paul@67 | 435 | match = category_membership_regexp.search(body) |
paul@10 | 436 | |
paul@67 | 437 | if match: |
paul@67 | 438 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@10 | 439 | |
paul@67 | 440 | def saveChanges(self): |
paul@10 | 441 | |
paul@67 | 442 | "Save changes to the event." |
paul@10 | 443 | |
paul@67 | 444 | self.flushEventDetails() |
paul@67 | 445 | self.flushCategoryMembership() |
paul@67 | 446 | self.page.saveText(self.getBody(), 0) |
paul@10 | 447 | |
paul@67 | 448 | def linkToPage(self, request, text, query_string=None): |
paul@11 | 449 | |
paul@67 | 450 | """ |
paul@67 | 451 | Using 'request', return a link to this page with the given link 'text' |
paul@67 | 452 | and optional 'query_string'. |
paul@67 | 453 | """ |
paul@11 | 454 | |
paul@67 | 455 | return linkToPage(request, self.page, text, query_string) |
paul@13 | 456 | |
paul@69 | 457 | class Event: |
paul@69 | 458 | |
paul@69 | 459 | "A description of an event." |
paul@69 | 460 | |
paul@69 | 461 | def __init__(self, page, details): |
paul@69 | 462 | self.page = page |
paul@69 | 463 | self.details = details |
paul@69 | 464 | |
paul@69 | 465 | def __cmp__(self, other): |
paul@69 | 466 | |
paul@69 | 467 | """ |
paul@69 | 468 | Compare this object with 'other' using the event start and end details. |
paul@69 | 469 | """ |
paul@69 | 470 | |
paul@69 | 471 | details1 = self.details |
paul@69 | 472 | details2 = other.details |
paul@69 | 473 | return cmp( |
paul@69 | 474 | (details1["start"], details1["end"]), |
paul@69 | 475 | (details2["start"], details2["end"]) |
paul@69 | 476 | ) |
paul@69 | 477 | |
paul@69 | 478 | def getPage(self): |
paul@69 | 479 | |
paul@69 | 480 | "Return the page describing this event." |
paul@69 | 481 | |
paul@69 | 482 | return self.page |
paul@69 | 483 | |
paul@69 | 484 | def setPage(self, page): |
paul@69 | 485 | |
paul@69 | 486 | "Set the 'page' describing this event." |
paul@69 | 487 | |
paul@69 | 488 | self.page = page |
paul@69 | 489 | |
paul@69 | 490 | def getSummary(self, event_parent=None): |
paul@69 | 491 | |
paul@69 | 492 | """ |
paul@69 | 493 | Return either the given title or summary of the event according to the |
paul@69 | 494 | event details, or a summary made from using the pretty version of the |
paul@69 | 495 | page name. |
paul@69 | 496 | |
paul@69 | 497 | If the optional 'event_parent' is specified, any page beneath the given |
paul@69 | 498 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@69 | 499 | if its name is used as the summary. |
paul@69 | 500 | """ |
paul@69 | 501 | |
paul@69 | 502 | event_details = self.details |
paul@69 | 503 | |
paul@69 | 504 | if event_details.has_key("title"): |
paul@69 | 505 | return event_details["title"] |
paul@69 | 506 | elif event_details.has_key("summary"): |
paul@69 | 507 | return event_details["summary"] |
paul@69 | 508 | else: |
paul@69 | 509 | # If appropriate, remove the parent details and "/" character. |
paul@69 | 510 | |
paul@69 | 511 | title = self.page.getPageName() |
paul@69 | 512 | |
paul@74 | 513 | if event_parent and title.startswith(event_parent): |
paul@69 | 514 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@69 | 515 | |
paul@69 | 516 | return getPrettyTitle(title) |
paul@69 | 517 | |
paul@69 | 518 | def getDetails(self): |
paul@69 | 519 | |
paul@69 | 520 | "Return the details for this event." |
paul@69 | 521 | |
paul@69 | 522 | return self.details |
paul@69 | 523 | |
paul@69 | 524 | def setDetails(self, event_details): |
paul@69 | 525 | |
paul@69 | 526 | "Set the 'event_details' for this event." |
paul@69 | 527 | |
paul@69 | 528 | self.details = event_details |
paul@69 | 529 | |
paul@10 | 530 | def getEvents(request, category_names, calendar_start=None, calendar_end=None): |
paul@10 | 531 | |
paul@10 | 532 | """ |
paul@10 | 533 | Using the 'request', generate a list of events found on pages belonging to |
paul@10 | 534 | the specified 'category_names', using the optional 'calendar_start' and |
paul@10 | 535 | 'calendar_end' month tuples of the form (year, month) to indicate a window |
paul@10 | 536 | of interest. |
paul@10 | 537 | |
paul@10 | 538 | Return a list of events, a dictionary mapping months to event lists (within |
paul@10 | 539 | the window of interest), a list of all events within the window of interest, |
paul@10 | 540 | the earliest month of an event within the window of interest, and the latest |
paul@10 | 541 | month of an event within the window of interest. |
paul@10 | 542 | """ |
paul@10 | 543 | |
paul@12 | 544 | # Re-order the window, if appropriate. |
paul@12 | 545 | |
paul@12 | 546 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@12 | 547 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@12 | 548 | |
paul@10 | 549 | events = [] |
paul@10 | 550 | shown_events = {} |
paul@10 | 551 | all_shown_events = [] |
paul@17 | 552 | processed_pages = set() |
paul@10 | 553 | |
paul@10 | 554 | earliest = None |
paul@10 | 555 | latest = None |
paul@10 | 556 | |
paul@10 | 557 | for category_name in category_names: |
paul@10 | 558 | |
paul@10 | 559 | # Get the pages and page names in the category. |
paul@10 | 560 | |
paul@67 | 561 | pages_in_category = getCategoryPages(category_name, request) |
paul@10 | 562 | |
paul@10 | 563 | # Visit each page in the category. |
paul@10 | 564 | |
paul@10 | 565 | for page_in_category in pages_in_category: |
paul@10 | 566 | pagename = page_in_category.page_name |
paul@10 | 567 | |
paul@17 | 568 | # Only process each page once. |
paul@17 | 569 | |
paul@17 | 570 | if pagename in processed_pages: |
paul@17 | 571 | continue |
paul@17 | 572 | else: |
paul@17 | 573 | processed_pages.add(pagename) |
paul@17 | 574 | |
paul@10 | 575 | # Get a real page, not a result page. |
paul@10 | 576 | |
paul@67 | 577 | event_page = EventPage(Page(request, pagename)) |
paul@10 | 578 | |
paul@69 | 579 | # Get all events described in the page. |
paul@10 | 580 | |
paul@69 | 581 | for event in event_page.getEvents(): |
paul@69 | 582 | event_details = event.getDetails() |
paul@10 | 583 | |
paul@69 | 584 | # Remember the event. |
paul@69 | 585 | |
paul@69 | 586 | events.append(event) |
paul@10 | 587 | |
paul@69 | 588 | # Test for the suitability of the event. |
paul@69 | 589 | |
paul@69 | 590 | if event_details.has_key("start") and event_details.has_key("end"): |
paul@10 | 591 | |
paul@69 | 592 | start_month = event_details["start"].as_month() |
paul@69 | 593 | end_month = event_details["end"].as_month() |
paul@10 | 594 | |
paul@69 | 595 | # Compare the months of the dates to the requested calendar |
paul@69 | 596 | # window, if any. |
paul@10 | 597 | |
paul@69 | 598 | if (calendar_start is None or end_month >= calendar_start) and \ |
paul@69 | 599 | (calendar_end is None or start_month <= calendar_end): |
paul@10 | 600 | |
paul@69 | 601 | all_shown_events.append(event) |
paul@10 | 602 | |
paul@69 | 603 | if earliest is None or start_month < earliest: |
paul@69 | 604 | earliest = start_month |
paul@69 | 605 | if latest is None or end_month > latest: |
paul@69 | 606 | latest = end_month |
paul@10 | 607 | |
paul@69 | 608 | # Store the event in the month-specific dictionary. |
paul@10 | 609 | |
paul@69 | 610 | first = max(start_month, calendar_start or start_month) |
paul@69 | 611 | last = min(end_month, calendar_end or end_month) |
paul@10 | 612 | |
paul@69 | 613 | for event_month in first.months_until(last): |
paul@69 | 614 | if not shown_events.has_key(event_month): |
paul@69 | 615 | shown_events[event_month] = [] |
paul@69 | 616 | shown_events[event_month].append(event) |
paul@10 | 617 | |
paul@10 | 618 | return events, shown_events, all_shown_events, earliest, latest |
paul@10 | 619 | |
paul@29 | 620 | def setEventTimestamps(request, events): |
paul@29 | 621 | |
paul@29 | 622 | """ |
paul@29 | 623 | Using 'request', set timestamp details in the details dictionary of each of |
paul@67 | 624 | the 'events'. |
paul@29 | 625 | |
paul@29 | 626 | Retutn the latest timestamp found. |
paul@29 | 627 | """ |
paul@29 | 628 | |
paul@29 | 629 | latest = None |
paul@29 | 630 | |
paul@69 | 631 | for event in events: |
paul@69 | 632 | event_details = event.getDetails() |
paul@69 | 633 | event_page = event.getPage() |
paul@29 | 634 | |
paul@29 | 635 | # Get the initial revision of the page. |
paul@29 | 636 | |
paul@67 | 637 | revisions = event_page.getRevisions() |
paul@67 | 638 | event_page_initial = Page(request, event_page.getPageName(), rev=revisions[-1]) |
paul@29 | 639 | |
paul@29 | 640 | # Get the created and last modified times. |
paul@29 | 641 | |
paul@30 | 642 | initial_revision = getPageRevision(event_page_initial) |
paul@30 | 643 | event_details["created"] = initial_revision["timestamp"] |
paul@67 | 644 | latest_revision = event_page.getPageRevision() |
paul@30 | 645 | event_details["last-modified"] = latest_revision["timestamp"] |
paul@29 | 646 | event_details["sequence"] = len(revisions) - 1 |
paul@30 | 647 | event_details["last-comment"] = latest_revision["comment"] |
paul@29 | 648 | |
paul@29 | 649 | if latest is None or latest < event_details["last-modified"]: |
paul@29 | 650 | latest = event_details["last-modified"] |
paul@29 | 651 | |
paul@29 | 652 | return latest |
paul@29 | 653 | |
paul@26 | 654 | def getOrderedEvents(events): |
paul@26 | 655 | |
paul@26 | 656 | """ |
paul@26 | 657 | Return a list with the given 'events' ordered according to their start and |
paul@67 | 658 | end dates. |
paul@26 | 659 | """ |
paul@26 | 660 | |
paul@26 | 661 | ordered_events = events[:] |
paul@68 | 662 | ordered_events.sort() |
paul@26 | 663 | return ordered_events |
paul@26 | 664 | |
paul@13 | 665 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest): |
paul@13 | 666 | |
paul@13 | 667 | """ |
paul@13 | 668 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@13 | 669 | indicating that no restriction is imposed on the period for each of the |
paul@13 | 670 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@13 | 671 | specific period of interest. |
paul@13 | 672 | """ |
paul@13 | 673 | |
paul@13 | 674 | # Define the period as starting with any specified start month or the |
paul@13 | 675 | # earliest event known, ending with any specified end month or the latest |
paul@13 | 676 | # event known. |
paul@13 | 677 | |
paul@13 | 678 | first = calendar_start or earliest |
paul@13 | 679 | last = calendar_end or latest |
paul@13 | 680 | |
paul@13 | 681 | # If there is no range of months to show, perhaps because there are no |
paul@13 | 682 | # events in the requested period, and there was no start or end month |
paul@13 | 683 | # specified, show only the month indicated by the start or end of the |
paul@13 | 684 | # requested period. If all events were to be shown but none were found show |
paul@13 | 685 | # the current month. |
paul@13 | 686 | |
paul@13 | 687 | if first is None: |
paul@13 | 688 | first = last or getCurrentMonth() |
paul@13 | 689 | if last is None: |
paul@13 | 690 | last = first or getCurrentMonth() |
paul@13 | 691 | |
paul@13 | 692 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@13 | 693 | |
paul@13 | 694 | return min(first, last), last |
paul@13 | 695 | |
paul@15 | 696 | def getCoverage(start, end, events): |
paul@15 | 697 | |
paul@15 | 698 | """ |
paul@15 | 699 | Within the period defined by the 'start' and 'end' dates, determine the |
paul@15 | 700 | coverage of the days in the period by the given 'events', returning a set of |
paul@15 | 701 | covered days, along with a list of slots, where each slot contains a tuple |
paul@15 | 702 | of the form (set of covered days, events). |
paul@15 | 703 | """ |
paul@15 | 704 | |
paul@15 | 705 | all_events = [] |
paul@15 | 706 | full_coverage = set() |
paul@15 | 707 | |
paul@15 | 708 | # Get event details. |
paul@15 | 709 | |
paul@69 | 710 | for event in events: |
paul@69 | 711 | event_details = event.getDetails() |
paul@15 | 712 | |
paul@15 | 713 | # Test for the event in the period. |
paul@15 | 714 | |
paul@15 | 715 | if event_details["start"] <= end and event_details["end"] >= start: |
paul@15 | 716 | |
paul@15 | 717 | # Find the coverage of this period for the event. |
paul@15 | 718 | |
paul@15 | 719 | event_start = max(event_details["start"], start) |
paul@15 | 720 | event_end = min(event_details["end"], end) |
paul@67 | 721 | event_coverage = set(event_start.days_until(event_end)) |
paul@15 | 722 | |
paul@15 | 723 | # Update the overall coverage. |
paul@15 | 724 | |
paul@15 | 725 | full_coverage.update(event_coverage) |
paul@15 | 726 | |
paul@15 | 727 | # Try and fit the event into the events list. |
paul@15 | 728 | |
paul@15 | 729 | for i, (coverage, covered_events) in enumerate(all_events): |
paul@15 | 730 | |
paul@15 | 731 | # Where the event does not overlap with the current |
paul@15 | 732 | # element, add it alongside existing events. |
paul@15 | 733 | |
paul@15 | 734 | if not coverage.intersection(event_coverage): |
paul@69 | 735 | covered_events.append(event) |
paul@15 | 736 | all_events[i] = coverage.union(event_coverage), covered_events |
paul@15 | 737 | break |
paul@15 | 738 | |
paul@15 | 739 | # Make a new element in the list if the event cannot be |
paul@15 | 740 | # marked alongside existing events. |
paul@15 | 741 | |
paul@15 | 742 | else: |
paul@69 | 743 | all_events.append((event_coverage, [event])) |
paul@15 | 744 | |
paul@15 | 745 | return full_coverage, all_events |
paul@15 | 746 | |
paul@67 | 747 | # Date-related functions. |
paul@67 | 748 | |
paul@67 | 749 | class Period: |
paul@67 | 750 | |
paul@67 | 751 | "A simple period of time." |
paul@67 | 752 | |
paul@67 | 753 | def __init__(self, data): |
paul@67 | 754 | self.data = data |
paul@67 | 755 | |
paul@67 | 756 | def months(self): |
paul@67 | 757 | return self.data[0] * 12 + self.data[1] |
paul@67 | 758 | |
paul@67 | 759 | class Month: |
paul@67 | 760 | |
paul@67 | 761 | "A simple year-month representation." |
paul@67 | 762 | |
paul@67 | 763 | def __init__(self, data): |
paul@67 | 764 | self.data = tuple(data) |
paul@67 | 765 | |
paul@67 | 766 | def __repr__(self): |
paul@67 | 767 | return "%s(%r)" % (self.__class__.__name__, self.data) |
paul@67 | 768 | |
paul@67 | 769 | def __str__(self): |
paul@67 | 770 | return "%04d-%02d" % self.as_tuple()[:2] |
paul@67 | 771 | |
paul@67 | 772 | def __hash__(self): |
paul@67 | 773 | return hash(self.as_tuple()) |
paul@67 | 774 | |
paul@67 | 775 | def as_tuple(self): |
paul@67 | 776 | return self.data |
paul@67 | 777 | |
paul@67 | 778 | def as_date(self, day): |
paul@67 | 779 | return Date(self.as_tuple() + (day,)) |
paul@67 | 780 | |
paul@67 | 781 | def year(self): |
paul@67 | 782 | return self.data[0] |
paul@67 | 783 | |
paul@67 | 784 | def month(self): |
paul@67 | 785 | return self.data[1] |
paul@67 | 786 | |
paul@67 | 787 | def month_properties(self): |
paul@67 | 788 | |
paul@67 | 789 | """ |
paul@67 | 790 | Return the weekday of the 1st of the month, along with the number of |
paul@67 | 791 | days, as a tuple. |
paul@67 | 792 | """ |
paul@67 | 793 | |
paul@67 | 794 | year, month = self.data |
paul@67 | 795 | return calendar.monthrange(year, month) |
paul@67 | 796 | |
paul@67 | 797 | def month_update(self, n=1): |
paul@67 | 798 | |
paul@67 | 799 | "Return the month updated by 'n' months." |
paul@67 | 800 | |
paul@67 | 801 | year, month = self.data |
paul@67 | 802 | return Month((year + (month - 1 + n) / 12, (month - 1 + n) % 12 + 1)) |
paul@67 | 803 | |
paul@67 | 804 | def next_month(self): |
paul@67 | 805 | |
paul@67 | 806 | "Return the month following this one." |
paul@67 | 807 | |
paul@67 | 808 | return self.month_update(1) |
paul@67 | 809 | |
paul@67 | 810 | def previous_month(self): |
paul@67 | 811 | |
paul@67 | 812 | "Return the month preceding this one." |
paul@67 | 813 | |
paul@67 | 814 | return self.month_update(-1) |
paul@67 | 815 | |
paul@67 | 816 | def __sub__(self, start): |
paul@67 | 817 | |
paul@67 | 818 | """ |
paul@67 | 819 | Return the difference in years and months between this month and the |
paul@67 | 820 | 'start' month as a period. |
paul@67 | 821 | """ |
paul@67 | 822 | |
paul@67 | 823 | return Period([(x - y) for x, y in zip(self.data, start.data)]) |
paul@67 | 824 | |
paul@67 | 825 | def __cmp__(self, other): |
paul@67 | 826 | return cmp(self.data, other.data) |
paul@67 | 827 | |
paul@67 | 828 | def until(self, end, nextfn, prevfn): |
paul@67 | 829 | month = self |
paul@67 | 830 | months = [month] |
paul@67 | 831 | if month < end: |
paul@67 | 832 | while month < end: |
paul@67 | 833 | month = nextfn(month) |
paul@67 | 834 | months.append(month) |
paul@67 | 835 | elif month > end: |
paul@67 | 836 | while month > end: |
paul@67 | 837 | month = prevfn(month) |
paul@67 | 838 | months.append(month) |
paul@67 | 839 | return months |
paul@67 | 840 | |
paul@67 | 841 | def months_until(self, end): |
paul@67 | 842 | return self.until(end, Month.next_month, Month.previous_month) |
paul@67 | 843 | |
paul@67 | 844 | class Date(Month): |
paul@67 | 845 | |
paul@67 | 846 | "A simple year-month-day representation." |
paul@67 | 847 | |
paul@67 | 848 | def __str__(self): |
paul@67 | 849 | return "%04d-%02d-%02d" % self.as_tuple()[:3] |
paul@67 | 850 | |
paul@67 | 851 | def as_month(self): |
paul@67 | 852 | return Month(self.data[:2]) |
paul@67 | 853 | |
paul@67 | 854 | def day(self): |
paul@67 | 855 | return self.data[2] |
paul@67 | 856 | |
paul@67 | 857 | def next_day(self): |
paul@67 | 858 | |
paul@67 | 859 | "Return the date following this one." |
paul@67 | 860 | |
paul@67 | 861 | year, month, day = self.data |
paul@67 | 862 | _wd, end_day = calendar.monthrange(year, month) |
paul@67 | 863 | if day == end_day: |
paul@67 | 864 | if month == 12: |
paul@67 | 865 | return Date((year + 1, 1, 1)) |
paul@67 | 866 | else: |
paul@67 | 867 | return Date((year, month + 1, 1)) |
paul@67 | 868 | else: |
paul@67 | 869 | return Date((year, month, day + 1)) |
paul@67 | 870 | |
paul@67 | 871 | def previous_day(self): |
paul@67 | 872 | |
paul@67 | 873 | "Return the date preceding this one." |
paul@67 | 874 | |
paul@67 | 875 | year, month, day = self.data |
paul@67 | 876 | if day == 1: |
paul@67 | 877 | if month == 1: |
paul@67 | 878 | return Date((year - 1, 12, 31)) |
paul@67 | 879 | else: |
paul@67 | 880 | _wd, end_day = calendar.monthrange(year, month - 1) |
paul@67 | 881 | return Date((year, month - 1, end_day)) |
paul@67 | 882 | else: |
paul@67 | 883 | return Date((year, month, day - 1)) |
paul@67 | 884 | |
paul@67 | 885 | def days_until(self, end): |
paul@67 | 886 | return self.until(end, Date.next_day, Date.previous_day) |
paul@67 | 887 | |
paul@67 | 888 | def getDate(s): |
paul@67 | 889 | |
paul@88 | 890 | "Parse the string 's', extracting and returning a date object." |
paul@67 | 891 | |
paul@67 | 892 | m = date_regexp.search(s) |
paul@67 | 893 | if m: |
paul@67 | 894 | return Date(map(int, m.groups())) |
paul@67 | 895 | else: |
paul@67 | 896 | return None |
paul@67 | 897 | |
paul@88 | 898 | def getDateStrings(s): |
paul@88 | 899 | |
paul@88 | 900 | "Parse the string 's', extracting and returning all date strings." |
paul@88 | 901 | |
paul@88 | 902 | start = 0 |
paul@88 | 903 | m = date_regexp.search(s, start) |
paul@88 | 904 | l = [] |
paul@88 | 905 | while m: |
paul@88 | 906 | l.append("-".join(m.groups())) |
paul@88 | 907 | m = date_regexp.search(s, m.end()) |
paul@88 | 908 | return l |
paul@88 | 909 | |
paul@67 | 910 | def getMonth(s): |
paul@67 | 911 | |
paul@88 | 912 | "Parse the string 's', extracting and returning a month object." |
paul@67 | 913 | |
paul@67 | 914 | m = month_regexp.search(s) |
paul@67 | 915 | if m: |
paul@67 | 916 | return Month(map(int, m.groups())) |
paul@67 | 917 | else: |
paul@67 | 918 | return None |
paul@67 | 919 | |
paul@67 | 920 | def getCurrentMonth(): |
paul@67 | 921 | |
paul@67 | 922 | "Return the current month as a (year, month) tuple." |
paul@67 | 923 | |
paul@67 | 924 | today = datetime.date.today() |
paul@67 | 925 | return Month((today.year, today.month)) |
paul@67 | 926 | |
paul@67 | 927 | def getCurrentYear(): |
paul@67 | 928 | |
paul@67 | 929 | "Return the current year." |
paul@67 | 930 | |
paul@67 | 931 | today = datetime.date.today() |
paul@67 | 932 | return today.year |
paul@67 | 933 | |
paul@19 | 934 | # User interface functions. |
paul@19 | 935 | |
paul@55 | 936 | def getParameter(request, name, default=None): |
paul@55 | 937 | return request.form.get(name, [default])[0] |
paul@23 | 938 | |
paul@58 | 939 | def getQualifiedParameter(request, calendar_name, argname, default=None): |
paul@58 | 940 | argname = getQualifiedParameterName(calendar_name, argname) |
paul@58 | 941 | return getParameter(request, argname, default) |
paul@58 | 942 | |
paul@58 | 943 | def getQualifiedParameterName(calendar_name, argname): |
paul@58 | 944 | if calendar_name is None: |
paul@58 | 945 | return argname |
paul@58 | 946 | else: |
paul@58 | 947 | return "%s-%s" % (calendar_name, argname) |
paul@58 | 948 | |
paul@19 | 949 | def getParameterMonth(arg): |
paul@67 | 950 | |
paul@67 | 951 | "Interpret 'arg', recognising keywords and simple arithmetic operations." |
paul@67 | 952 | |
paul@19 | 953 | n = None |
paul@19 | 954 | |
paul@19 | 955 | if arg.startswith("current"): |
paul@19 | 956 | date = getCurrentMonth() |
paul@19 | 957 | if len(arg) > 8: |
paul@19 | 958 | n = int(arg[7:]) |
paul@19 | 959 | |
paul@19 | 960 | elif arg.startswith("yearstart"): |
paul@67 | 961 | date = Month((getCurrentYear(), 1)) |
paul@19 | 962 | if len(arg) > 10: |
paul@19 | 963 | n = int(arg[9:]) |
paul@19 | 964 | |
paul@19 | 965 | elif arg.startswith("yearend"): |
paul@67 | 966 | date = Month((getCurrentYear(), 12)) |
paul@19 | 967 | if len(arg) > 8: |
paul@19 | 968 | n = int(arg[7:]) |
paul@19 | 969 | |
paul@19 | 970 | else: |
paul@19 | 971 | date = getMonth(arg) |
paul@19 | 972 | |
paul@19 | 973 | if n is not None: |
paul@67 | 974 | date = date.month_update(n) |
paul@19 | 975 | |
paul@19 | 976 | return date |
paul@19 | 977 | |
paul@19 | 978 | def getFormMonth(request, calendar_name, argname): |
paul@67 | 979 | |
paul@67 | 980 | """ |
paul@67 | 981 | Return the month from the 'request' for the calendar with the given |
paul@67 | 982 | 'calendar_name' using the parameter having the given 'argname'. |
paul@67 | 983 | """ |
paul@67 | 984 | |
paul@58 | 985 | arg = getQualifiedParameter(request, calendar_name, argname) |
paul@19 | 986 | if arg is not None: |
paul@19 | 987 | return getParameterMonth(arg) |
paul@19 | 988 | else: |
paul@19 | 989 | return None |
paul@19 | 990 | |
paul@23 | 991 | def getFormMonthPair(request, yeararg, montharg): |
paul@67 | 992 | |
paul@67 | 993 | """ |
paul@67 | 994 | Return the month from the 'request' for the calendar with the given |
paul@67 | 995 | 'calendar_name' using the parameters having the given 'yeararg' and |
paul@67 | 996 | 'montharg' names. |
paul@67 | 997 | """ |
paul@67 | 998 | |
paul@23 | 999 | year = getParameter(request, yeararg) |
paul@23 | 1000 | month = getParameter(request, montharg) |
paul@23 | 1001 | if year and month: |
paul@67 | 1002 | return Month((int(year), int(month))) |
paul@23 | 1003 | else: |
paul@23 | 1004 | return None |
paul@23 | 1005 | |
paul@67 | 1006 | # Page-related functions. |
paul@67 | 1007 | |
paul@19 | 1008 | def getPrettyPageName(page): |
paul@19 | 1009 | |
paul@19 | 1010 | "Return a nicely formatted title/name for the given 'page'." |
paul@19 | 1011 | |
paul@27 | 1012 | if isMoin15(): |
paul@27 | 1013 | title = page.split_title(page.request, force=1) |
paul@27 | 1014 | else: |
paul@27 | 1015 | title = page.split_title(force=1) |
paul@27 | 1016 | |
paul@55 | 1017 | return getPrettyTitle(title) |
paul@55 | 1018 | |
paul@27 | 1019 | def linkToPage(request, page, text, query_string=None): |
paul@27 | 1020 | |
paul@27 | 1021 | """ |
paul@27 | 1022 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@27 | 1023 | optional 'query_string'. |
paul@27 | 1024 | """ |
paul@27 | 1025 | |
paul@27 | 1026 | text = wikiutil.escape(text) |
paul@27 | 1027 | |
paul@27 | 1028 | if isMoin15(): |
paul@27 | 1029 | url = wikiutil.quoteWikinameURL(page.page_name) |
paul@27 | 1030 | if query_string is not None: |
paul@27 | 1031 | url = "%s?%s" % (url, query_string) |
paul@27 | 1032 | return wikiutil.link_tag(request, url, text, getattr(page, "formatter", None)) |
paul@27 | 1033 | else: |
paul@27 | 1034 | return page.link_to_raw(request, text, query_string) |
paul@27 | 1035 | |
paul@88 | 1036 | def getFullPageName(parent, title): |
paul@88 | 1037 | |
paul@88 | 1038 | """ |
paul@88 | 1039 | Return a full page name from the given 'parent' page (can be empty or None) |
paul@88 | 1040 | and 'title' (a simple page name). |
paul@88 | 1041 | """ |
paul@88 | 1042 | |
paul@88 | 1043 | if parent: |
paul@88 | 1044 | return "%s/%s" % (parent.rstrip("/"), title) |
paul@88 | 1045 | else: |
paul@88 | 1046 | return title |
paul@88 | 1047 | |
paul@88 | 1048 | def fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames): |
paul@88 | 1049 | |
paul@88 | 1050 | """ |
paul@88 | 1051 | Using the given 'template_page', complete the 'new_page' by copying the |
paul@88 | 1052 | template and adding the given 'event_details' (a dictionary of event |
paul@88 | 1053 | fields), setting also the 'category_pagenames' to define category |
paul@88 | 1054 | membership. |
paul@88 | 1055 | """ |
paul@88 | 1056 | |
paul@88 | 1057 | event_page = EventPage(template_page) |
paul@88 | 1058 | new_event_page = EventPage(new_page) |
paul@88 | 1059 | new_event_page.copyPage(event_page) |
paul@88 | 1060 | |
paul@88 | 1061 | if new_event_page.getFormat() == "wiki": |
paul@88 | 1062 | new_event = Event(new_event_page, event_details) |
paul@88 | 1063 | new_event_page.setEvents([new_event]) |
paul@88 | 1064 | new_event_page.setCategoryMembership(category_pagenames) |
paul@88 | 1065 | new_event_page.saveChanges() |
paul@88 | 1066 | |
paul@10 | 1067 | # vim: tabstop=4 expandtab shiftwidth=4 |