paul@446 | 1 | #!/usr/bin/env python |
paul@446 | 2 | |
paul@446 | 3 | """ |
paul@446 | 4 | Common resource functionality for Web calendar clients. |
paul@446 | 5 | |
paul@446 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@446 | 7 | |
paul@446 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@446 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@446 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@446 | 11 | version. |
paul@446 | 12 | |
paul@446 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@446 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@446 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@446 | 16 | details. |
paul@446 | 17 | |
paul@446 | 18 | You should have received a copy of the GNU General Public License along with |
paul@446 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@446 | 20 | """ |
paul@446 | 21 | |
paul@764 | 22 | from datetime import datetime, timedelta |
paul@756 | 23 | from imiptools.client import Client, ClientForObject |
paul@622 | 24 | from imiptools.data import get_uri, uri_values |
paul@764 | 25 | from imiptools.dates import format_datetime, get_recurrence_start_point, to_date |
paul@678 | 26 | from imiptools.period import remove_period, remove_affected_period |
paul@765 | 27 | from imipweb.data import event_period_from_period, form_period_from_period, FormDate |
paul@446 | 28 | from imipweb.env import CGIEnvironment |
paul@777 | 29 | from urllib import urlencode |
paul@446 | 30 | import babel.dates |
paul@446 | 31 | import imip_store |
paul@446 | 32 | import markup |
paul@764 | 33 | import pytz |
paul@446 | 34 | |
paul@756 | 35 | class Resource: |
paul@446 | 36 | |
paul@756 | 37 | "A Web application resource." |
paul@446 | 38 | |
paul@446 | 39 | def __init__(self, resource=None): |
paul@756 | 40 | |
paul@756 | 41 | """ |
paul@756 | 42 | Initialise a resource, allowing it to share the environment of any given |
paul@756 | 43 | existing 'resource'. |
paul@756 | 44 | """ |
paul@756 | 45 | |
paul@446 | 46 | self.encoding = "utf-8" |
paul@446 | 47 | self.env = CGIEnvironment(self.encoding) |
paul@446 | 48 | |
paul@756 | 49 | self.objects = {} |
paul@446 | 50 | self.locale = None |
paul@446 | 51 | self.requests = None |
paul@446 | 52 | |
paul@446 | 53 | self.out = resource and resource.out or self.env.get_output() |
paul@446 | 54 | self.page = resource and resource.page or markup.page() |
paul@446 | 55 | self.html_ids = None |
paul@446 | 56 | |
paul@446 | 57 | # Presentation methods. |
paul@446 | 58 | |
paul@446 | 59 | def new_page(self, title): |
paul@446 | 60 | self.page.init(title=title, charset=self.encoding, css=self.env.new_url("styles.css")) |
paul@446 | 61 | self.html_ids = set() |
paul@446 | 62 | |
paul@446 | 63 | def status(self, code, message): |
paul@446 | 64 | self.header("Status", "%s %s" % (code, message)) |
paul@446 | 65 | |
paul@446 | 66 | def header(self, header, value): |
paul@446 | 67 | print >>self.out, "%s: %s" % (header, value) |
paul@446 | 68 | |
paul@446 | 69 | def no_user(self): |
paul@446 | 70 | self.status(403, "Forbidden") |
paul@446 | 71 | self.new_page(title="Forbidden") |
paul@446 | 72 | self.page.p("You are not logged in and thus cannot access scheduling requests.") |
paul@446 | 73 | |
paul@446 | 74 | def no_page(self): |
paul@446 | 75 | self.status(404, "Not Found") |
paul@446 | 76 | self.new_page(title="Not Found") |
paul@446 | 77 | self.page.p("No page is provided at the given address.") |
paul@446 | 78 | |
paul@446 | 79 | def redirect(self, url): |
paul@446 | 80 | self.status(302, "Redirect") |
paul@446 | 81 | self.header("Location", url) |
paul@446 | 82 | self.new_page(title="Redirect") |
paul@446 | 83 | self.page.p("Redirecting to: %s" % url) |
paul@446 | 84 | |
paul@777 | 85 | def link_to(self, uid, recurrenceid=None, args=None): |
paul@755 | 86 | |
paul@755 | 87 | """ |
paul@763 | 88 | Return a link to an object with the given 'uid' and 'recurrenceid'. |
paul@763 | 89 | See get_identifiers for the decoding of such links. |
paul@777 | 90 | |
paul@777 | 91 | If 'args' is specified, the given dictionary is encoded and included. |
paul@755 | 92 | """ |
paul@755 | 93 | |
paul@755 | 94 | path = [uid] |
paul@446 | 95 | if recurrenceid: |
paul@755 | 96 | path.append(recurrenceid) |
paul@777 | 97 | return "%s%s" % (self.env.new_url("/".join(path)), args and ("?%s" % urlencode(args)) or "") |
paul@446 | 98 | |
paul@623 | 99 | # Access to objects. |
paul@623 | 100 | |
paul@755 | 101 | def get_identifiers(self, path_info): |
paul@755 | 102 | |
paul@755 | 103 | """ |
paul@755 | 104 | Return identifiers provided by 'path_info', potentially encoded by |
paul@755 | 105 | 'link_to'. |
paul@755 | 106 | """ |
paul@755 | 107 | |
paul@446 | 108 | parts = path_info.lstrip("/").split("/") |
paul@755 | 109 | |
paul@755 | 110 | # UID only. |
paul@755 | 111 | |
paul@446 | 112 | if len(parts) == 1: |
paul@763 | 113 | return parts[0], None |
paul@755 | 114 | |
paul@763 | 115 | # UID and RECURRENCE-ID. |
paul@755 | 116 | |
paul@446 | 117 | else: |
paul@763 | 118 | return parts[:2] |
paul@446 | 119 | |
paul@769 | 120 | def _get_object(self, uid, recurrenceid=None, section=None, username=None): |
paul@769 | 121 | if self.objects.has_key((uid, recurrenceid, section, username)): |
paul@769 | 122 | return self.objects[(uid, recurrenceid, section, username)] |
paul@446 | 123 | |
paul@769 | 124 | obj = self.objects[(uid, recurrenceid, section, username)] = self.get_stored_object(uid, recurrenceid, section, username) |
paul@446 | 125 | return obj |
paul@446 | 126 | |
paul@446 | 127 | def _get_recurrences(self, uid): |
paul@446 | 128 | return self.store.get_recurrences(self.user, uid) |
paul@446 | 129 | |
paul@694 | 130 | def _get_active_recurrences(self, uid): |
paul@694 | 131 | return self.store.get_active_recurrences(self.user, uid) |
paul@694 | 132 | |
paul@446 | 133 | def _get_requests(self): |
paul@446 | 134 | if self.requests is None: |
paul@699 | 135 | self.requests = self.store.get_requests(self.user) |
paul@446 | 136 | return self.requests |
paul@446 | 137 | |
paul@755 | 138 | def _have_request(self, uid, recurrenceid=None, type=None, strict=False): |
paul@755 | 139 | return self.store.have_request(self._get_requests(), uid, recurrenceid, type, strict) |
paul@751 | 140 | |
paul@766 | 141 | def _get_counters(self, uid, recurrenceid=None): |
paul@766 | 142 | return self.store.get_counters(self.user, uid, recurrenceid) |
paul@766 | 143 | |
paul@446 | 144 | def _get_request_summary(self): |
paul@630 | 145 | |
paul@630 | 146 | "Return a list of periods comprising the request summary." |
paul@630 | 147 | |
paul@446 | 148 | summary = [] |
paul@630 | 149 | |
paul@751 | 150 | for uid, recurrenceid, request_type in self._get_requests(): |
paul@769 | 151 | |
paul@769 | 152 | # Obtain either normal objects or counter-proposals. |
paul@769 | 153 | |
paul@769 | 154 | if not request_type: |
paul@769 | 155 | objs = [self._get_object(uid, recurrenceid)] |
paul@769 | 156 | elif request_type == "COUNTER": |
paul@769 | 157 | objs = [] |
paul@769 | 158 | for attendee in self.store.get_counters(self.user, uid, recurrenceid): |
paul@769 | 159 | objs.append(self._get_object(uid, recurrenceid, "counters", attendee)) |
paul@446 | 160 | |
paul@769 | 161 | # For each object, obtain the periods involved. |
paul@769 | 162 | |
paul@769 | 163 | for obj in objs: |
paul@769 | 164 | if obj: |
paul@769 | 165 | recurrenceids = self._get_active_recurrences(uid) |
paul@446 | 166 | |
paul@769 | 167 | # Obtain only active periods, not those replaced by redefined |
paul@769 | 168 | # recurrences, converting to free/busy periods. |
paul@769 | 169 | |
paul@769 | 170 | for p in obj.get_active_periods(recurrenceids, self.get_tzid(), self.get_window_end()): |
paul@769 | 171 | summary.append(obj.get_freebusy_period(p)) |
paul@446 | 172 | |
paul@446 | 173 | return summary |
paul@446 | 174 | |
paul@446 | 175 | # Preference methods. |
paul@446 | 176 | |
paul@446 | 177 | def get_user_locale(self): |
paul@446 | 178 | if not self.locale: |
paul@446 | 179 | self.locale = self.get_preferences().get("LANG", "en") |
paul@446 | 180 | return self.locale |
paul@446 | 181 | |
paul@446 | 182 | # Prettyprinting of dates and times. |
paul@446 | 183 | |
paul@446 | 184 | def format_date(self, dt, format): |
paul@446 | 185 | return self._format_datetime(babel.dates.format_date, dt, format) |
paul@446 | 186 | |
paul@446 | 187 | def format_time(self, dt, format): |
paul@446 | 188 | return self._format_datetime(babel.dates.format_time, dt, format) |
paul@446 | 189 | |
paul@446 | 190 | def format_datetime(self, dt, format): |
paul@446 | 191 | return self._format_datetime( |
paul@446 | 192 | isinstance(dt, datetime) and babel.dates.format_datetime or babel.dates.format_date, |
paul@446 | 193 | dt, format) |
paul@446 | 194 | |
paul@446 | 195 | def _format_datetime(self, fn, dt, format): |
paul@446 | 196 | return fn(dt, format=format, locale=self.get_user_locale()) |
paul@446 | 197 | |
paul@446 | 198 | # Data management methods. |
paul@446 | 199 | |
paul@446 | 200 | def remove_request(self, uid, recurrenceid=None): |
paul@446 | 201 | return self.store.dequeue_request(self.user, uid, recurrenceid) |
paul@446 | 202 | |
paul@446 | 203 | def remove_event(self, uid, recurrenceid=None): |
paul@446 | 204 | return self.store.remove_event(self.user, uid, recurrenceid) |
paul@446 | 205 | |
paul@756 | 206 | class ResourceClient(Resource, Client): |
paul@446 | 207 | |
paul@756 | 208 | "A Web application resource and calendar client." |
paul@446 | 209 | |
paul@756 | 210 | def __init__(self, resource=None): |
paul@756 | 211 | Resource.__init__(self, resource) |
paul@756 | 212 | user = self.env.get_user() |
paul@756 | 213 | Client.__init__(self, user and get_uri(user) or None) |
paul@446 | 214 | |
paul@756 | 215 | class ResourceClientForObject(Resource, ClientForObject): |
paul@447 | 216 | |
paul@756 | 217 | "A Web application resource and calendar client for a specific object." |
paul@672 | 218 | |
paul@756 | 219 | def __init__(self, resource=None): |
paul@756 | 220 | Resource.__init__(self, resource) |
paul@756 | 221 | user = self.env.get_user() |
paul@756 | 222 | ClientForObject.__init__(self, None, user and get_uri(user) or None) |
paul@446 | 223 | |
paul@764 | 224 | class FormUtilities: |
paul@764 | 225 | |
paul@765 | 226 | "Utility methods resource mix-in." |
paul@764 | 227 | |
paul@764 | 228 | def control(self, name, type, value, selected=False, **kw): |
paul@764 | 229 | |
paul@764 | 230 | """ |
paul@764 | 231 | Show a control with the given 'name', 'type' and 'value', with |
paul@764 | 232 | 'selected' indicating whether it should be selected (checked or |
paul@764 | 233 | equivalent), and with keyword arguments setting other properties. |
paul@764 | 234 | """ |
paul@764 | 235 | |
paul@764 | 236 | page = self.page |
paul@779 | 237 | if type in ("checkbox", "radio") and selected: |
paul@764 | 238 | page.input(name=name, type=type, value=value, checked=selected, **kw) |
paul@764 | 239 | else: |
paul@764 | 240 | page.input(name=name, type=type, value=value, **kw) |
paul@764 | 241 | |
paul@764 | 242 | def menu(self, name, default, items, class_="", index=None): |
paul@764 | 243 | |
paul@764 | 244 | """ |
paul@764 | 245 | Show a select menu having the given 'name', set to the given 'default', |
paul@764 | 246 | providing the given (value, label) 'items', and employing the given CSS |
paul@764 | 247 | 'class_' if specified. |
paul@764 | 248 | """ |
paul@764 | 249 | |
paul@764 | 250 | page = self.page |
paul@764 | 251 | values = self.env.get_args().get(name, [default]) |
paul@764 | 252 | if index is not None: |
paul@764 | 253 | values = values[index:] |
paul@764 | 254 | values = values and values[0:1] or [default] |
paul@764 | 255 | |
paul@764 | 256 | page.select(name=name, class_=class_) |
paul@764 | 257 | for v, label in items: |
paul@764 | 258 | if v is None: |
paul@764 | 259 | continue |
paul@764 | 260 | if v in values: |
paul@764 | 261 | page.option(label, value=v, selected="selected") |
paul@764 | 262 | else: |
paul@764 | 263 | page.option(label, value=v) |
paul@764 | 264 | page.select.close() |
paul@764 | 265 | |
paul@764 | 266 | def date_controls(self, name, default, index=None, show_tzid=True, read_only=False): |
paul@764 | 267 | |
paul@764 | 268 | """ |
paul@764 | 269 | Show date controls for a field with the given 'name' and 'default' form |
paul@764 | 270 | date value. |
paul@764 | 271 | |
paul@764 | 272 | If 'index' is specified, default field values will be overridden by the |
paul@764 | 273 | element from a collection of existing form values with the specified |
paul@764 | 274 | index; otherwise, field values will be overridden by a single form |
paul@764 | 275 | value. |
paul@764 | 276 | |
paul@764 | 277 | If 'show_tzid' is set to a false value, the time zone menu will not be |
paul@764 | 278 | provided. |
paul@764 | 279 | |
paul@764 | 280 | If 'read_only' is set to a true value, the controls will be hidden and |
paul@764 | 281 | labels will be employed instead. |
paul@764 | 282 | """ |
paul@764 | 283 | |
paul@764 | 284 | page = self.page |
paul@764 | 285 | |
paul@764 | 286 | # Show dates for up to one week around the current date. |
paul@764 | 287 | |
paul@764 | 288 | dt = default.as_datetime() |
paul@764 | 289 | if not dt: |
paul@764 | 290 | dt = date.today() |
paul@764 | 291 | |
paul@764 | 292 | base = to_date(dt) |
paul@764 | 293 | |
paul@764 | 294 | # Show a date label with a hidden field if read-only. |
paul@764 | 295 | |
paul@764 | 296 | if read_only: |
paul@764 | 297 | self.control("%s-date" % name, "hidden", format_datetime(base)) |
paul@764 | 298 | page.span(self.format_date(base, "long")) |
paul@764 | 299 | |
paul@764 | 300 | # Show dates for up to one week around the current date. |
paul@764 | 301 | # NOTE: Support paging to other dates. |
paul@764 | 302 | |
paul@764 | 303 | else: |
paul@764 | 304 | items = [] |
paul@764 | 305 | for i in range(-7, 8): |
paul@764 | 306 | d = base + timedelta(i) |
paul@764 | 307 | items.append((format_datetime(d), self.format_date(d, "full"))) |
paul@764 | 308 | self.menu("%s-date" % name, format_datetime(base), items, index=index) |
paul@764 | 309 | |
paul@764 | 310 | # Show time details. |
paul@764 | 311 | |
paul@764 | 312 | page.span(class_="time enabled") |
paul@764 | 313 | |
paul@764 | 314 | if read_only: |
paul@764 | 315 | page.span("%s:%s:%s" % (default.get_hour(), default.get_minute(), default.get_second())) |
paul@764 | 316 | self.control("%s-hour" % name, "hidden", default.get_hour()) |
paul@764 | 317 | self.control("%s-minute" % name, "hidden", default.get_minute()) |
paul@764 | 318 | self.control("%s-second" % name, "hidden", default.get_second()) |
paul@764 | 319 | else: |
paul@764 | 320 | self.control("%s-hour" % name, "text", default.get_hour(), maxlength=2, size=2) |
paul@764 | 321 | page.add(":") |
paul@764 | 322 | self.control("%s-minute" % name, "text", default.get_minute(), maxlength=2, size=2) |
paul@764 | 323 | page.add(":") |
paul@764 | 324 | self.control("%s-second" % name, "text", default.get_second(), maxlength=2, size=2) |
paul@764 | 325 | |
paul@764 | 326 | # Show time zone details. |
paul@764 | 327 | |
paul@764 | 328 | if show_tzid: |
paul@764 | 329 | page.add(" ") |
paul@764 | 330 | tzid = default.get_tzid() or self.get_tzid() |
paul@764 | 331 | |
paul@764 | 332 | # Show a label if read-only or a menu otherwise. |
paul@764 | 333 | |
paul@764 | 334 | if read_only: |
paul@764 | 335 | self.control("%s-tzid" % name, "hidden", tzid) |
paul@764 | 336 | page.span(tzid) |
paul@764 | 337 | else: |
paul@764 | 338 | self.timezone_menu("%s-tzid" % name, tzid, index) |
paul@764 | 339 | |
paul@764 | 340 | page.span.close() |
paul@764 | 341 | |
paul@764 | 342 | def timezone_menu(self, name, default, index=None): |
paul@764 | 343 | |
paul@764 | 344 | """ |
paul@764 | 345 | Show timezone controls using a menu with the given 'name', set to the |
paul@764 | 346 | given 'default' unless a field of the given 'name' provides a value. |
paul@764 | 347 | """ |
paul@764 | 348 | |
paul@764 | 349 | entries = [(tzid, tzid) for tzid in pytz.all_timezones] |
paul@764 | 350 | self.menu(name, default, entries, index=index) |
paul@764 | 351 | |
paul@765 | 352 | class DateTimeFormUtilities: |
paul@765 | 353 | |
paul@765 | 354 | "Date/time control methods resource mix-in." |
paul@765 | 355 | |
paul@776 | 356 | # Control naming helpers. |
paul@776 | 357 | |
paul@776 | 358 | def element_identifier(self, name, index=None): |
paul@776 | 359 | return index is not None and "%s-%d" % (name, index) or name |
paul@776 | 360 | |
paul@776 | 361 | def element_name(self, name, suffix, index=None): |
paul@776 | 362 | return index is not None and "%s-%s" % (name, suffix) or name |
paul@776 | 363 | |
paul@776 | 364 | def element_enable(self, index=None): |
paul@776 | 365 | return index is not None and str(index) or "enable" |
paul@776 | 366 | |
paul@765 | 367 | def show_object_datetime_controls(self, period, index=None): |
paul@765 | 368 | |
paul@765 | 369 | """ |
paul@765 | 370 | Show datetime-related controls if already active or if an object needs |
paul@765 | 371 | them for the given 'period'. The given 'index' is used to parameterise |
paul@765 | 372 | individual controls for dynamic manipulation. |
paul@765 | 373 | """ |
paul@765 | 374 | |
paul@765 | 375 | p = form_period_from_period(period) |
paul@765 | 376 | |
paul@765 | 377 | page = self.page |
paul@765 | 378 | args = self.env.get_args() |
paul@765 | 379 | _id = self.element_identifier |
paul@765 | 380 | _name = self.element_name |
paul@765 | 381 | _enable = self.element_enable |
paul@765 | 382 | |
paul@765 | 383 | # Add a dynamic stylesheet to permit the controls to modify the display. |
paul@765 | 384 | # NOTE: The style details need to be coordinated with the static |
paul@765 | 385 | # NOTE: stylesheet. |
paul@765 | 386 | |
paul@765 | 387 | if index is not None: |
paul@765 | 388 | page.style(type="text/css") |
paul@765 | 389 | |
paul@765 | 390 | # Unlike the rules for object properties, these affect recurrence |
paul@765 | 391 | # properties. |
paul@765 | 392 | |
paul@765 | 393 | page.add("""\ |
paul@765 | 394 | input#dttimes-enable-%(index)d, |
paul@765 | 395 | input#dtend-enable-%(index)d, |
paul@765 | 396 | input#dttimes-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue .time.enabled, |
paul@765 | 397 | input#dttimes-enable-%(index)d:checked ~ .recurrence td.objectvalue .time.disabled, |
paul@765 | 398 | input#dtend-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue.dtend .dt.enabled, |
paul@765 | 399 | input#dtend-enable-%(index)d:checked ~ .recurrence td.objectvalue.dtend .dt.disabled { |
paul@765 | 400 | display: none; |
paul@765 | 401 | }""" % {"index" : index}) |
paul@765 | 402 | |
paul@765 | 403 | page.style.close() |
paul@765 | 404 | |
paul@765 | 405 | self.control( |
paul@765 | 406 | _name("dtend-control", "recur", index), "checkbox", |
paul@765 | 407 | _enable(index), p.end_enabled, |
paul@765 | 408 | id=_id("dtend-enable", index) |
paul@765 | 409 | ) |
paul@765 | 410 | |
paul@765 | 411 | self.control( |
paul@765 | 412 | _name("dttimes-control", "recur", index), "checkbox", |
paul@765 | 413 | _enable(index), p.times_enabled, |
paul@765 | 414 | id=_id("dttimes-enable", index) |
paul@765 | 415 | ) |
paul@765 | 416 | |
paul@765 | 417 | def show_datetime_controls(self, formdate, show_start): |
paul@765 | 418 | |
paul@765 | 419 | """ |
paul@765 | 420 | Show datetime details from the current object for the 'formdate', |
paul@765 | 421 | showing start details if 'show_start' is set to a true value. Details |
paul@765 | 422 | will appear as controls for organisers and labels for attendees. |
paul@765 | 423 | """ |
paul@765 | 424 | |
paul@765 | 425 | page = self.page |
paul@765 | 426 | |
paul@765 | 427 | # Show controls for editing as organiser. |
paul@765 | 428 | |
paul@765 | 429 | if self.is_organiser(): |
paul@765 | 430 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@765 | 431 | |
paul@765 | 432 | if show_start: |
paul@765 | 433 | page.div(class_="dt enabled") |
paul@765 | 434 | self.date_controls("dtstart", formdate) |
paul@765 | 435 | page.br() |
paul@765 | 436 | page.label("Specify times", for_="dttimes-enable", class_="time disabled enable") |
paul@765 | 437 | page.label("Specify dates only", for_="dttimes-enable", class_="time enabled disable") |
paul@765 | 438 | page.div.close() |
paul@765 | 439 | |
paul@765 | 440 | else: |
paul@765 | 441 | page.div(class_="dt disabled") |
paul@765 | 442 | page.label("Specify end date", for_="dtend-enable", class_="enable") |
paul@765 | 443 | page.div.close() |
paul@765 | 444 | page.div(class_="dt enabled") |
paul@765 | 445 | self.date_controls("dtend", formdate) |
paul@765 | 446 | page.br() |
paul@765 | 447 | page.label("End on same day", for_="dtend-enable", class_="disable") |
paul@765 | 448 | page.div.close() |
paul@765 | 449 | |
paul@765 | 450 | page.td.close() |
paul@765 | 451 | |
paul@765 | 452 | # Show a label as attendee. |
paul@765 | 453 | |
paul@765 | 454 | else: |
paul@765 | 455 | dt = formdate.as_datetime() |
paul@765 | 456 | if dt: |
paul@765 | 457 | page.td(self.format_datetime(dt, "full")) |
paul@765 | 458 | else: |
paul@765 | 459 | page.td("(Unrecognised date)") |
paul@765 | 460 | |
paul@765 | 461 | def show_recurrence_controls(self, index, period, recurrenceid, recurrenceids, show_start): |
paul@765 | 462 | |
paul@765 | 463 | """ |
paul@765 | 464 | Show datetime details from the current object for the recurrence having |
paul@765 | 465 | the given 'index', with the recurrence period described by 'period', |
paul@765 | 466 | indicating a start, end and origin of the period from the event details, |
paul@765 | 467 | employing any 'recurrenceid' and 'recurrenceids' for the object to |
paul@765 | 468 | configure the displayed information. |
paul@765 | 469 | |
paul@765 | 470 | If 'show_start' is set to a true value, the start details will be shown; |
paul@765 | 471 | otherwise, the end details will be shown. |
paul@765 | 472 | """ |
paul@765 | 473 | |
paul@765 | 474 | page = self.page |
paul@765 | 475 | _id = self.element_identifier |
paul@765 | 476 | _name = self.element_name |
paul@765 | 477 | |
paul@765 | 478 | p = event_period_from_period(period) |
paul@765 | 479 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@765 | 480 | |
paul@765 | 481 | # Show controls for editing as organiser. |
paul@765 | 482 | |
paul@765 | 483 | if self.is_organiser() and not replaced: |
paul@765 | 484 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@765 | 485 | |
paul@765 | 486 | read_only = period.origin == "RRULE" |
paul@765 | 487 | |
paul@765 | 488 | if show_start: |
paul@765 | 489 | page.div(class_="dt enabled") |
paul@765 | 490 | self.date_controls(_name("dtstart", "recur", index), p.get_form_start(), index=index, read_only=read_only) |
paul@765 | 491 | if not read_only: |
paul@765 | 492 | page.br() |
paul@765 | 493 | page.label("Specify times", for_=_id("dttimes-enable", index), class_="time disabled enable") |
paul@765 | 494 | page.label("Specify dates only", for_=_id("dttimes-enable", index), class_="time enabled disable") |
paul@765 | 495 | page.div.close() |
paul@765 | 496 | |
paul@765 | 497 | # Put the origin somewhere. |
paul@765 | 498 | |
paul@765 | 499 | self.control("recur-origin", "hidden", p.origin or "") |
paul@765 | 500 | |
paul@765 | 501 | else: |
paul@765 | 502 | page.div(class_="dt disabled") |
paul@765 | 503 | if not read_only: |
paul@765 | 504 | page.label("Specify end date", for_=_id("dtend-enable", index), class_="enable") |
paul@765 | 505 | page.div.close() |
paul@765 | 506 | page.div(class_="dt enabled") |
paul@765 | 507 | self.date_controls(_name("dtend", "recur", index), p.get_form_end(), index=index, show_tzid=False, read_only=read_only) |
paul@765 | 508 | if not read_only: |
paul@765 | 509 | page.br() |
paul@765 | 510 | page.label("End on same day", for_=_id("dtend-enable", index), class_="disable") |
paul@765 | 511 | page.div.close() |
paul@765 | 512 | |
paul@765 | 513 | page.td.close() |
paul@765 | 514 | |
paul@765 | 515 | # Show label as attendee. |
paul@765 | 516 | |
paul@765 | 517 | else: |
paul@765 | 518 | self.show_recurrence_label(p, recurrenceid, recurrenceids, show_start) |
paul@765 | 519 | |
paul@765 | 520 | def show_recurrence_label(self, period, recurrenceid, recurrenceids, show_start): |
paul@765 | 521 | |
paul@765 | 522 | """ |
paul@765 | 523 | Show datetime details for the given 'period', employing any |
paul@765 | 524 | 'recurrenceid' and 'recurrenceids' for the object to configure the |
paul@765 | 525 | displayed information. |
paul@765 | 526 | |
paul@765 | 527 | If 'show_start' is set to a true value, the start details will be shown; |
paul@765 | 528 | otherwise, the end details will be shown. |
paul@765 | 529 | """ |
paul@765 | 530 | |
paul@765 | 531 | page = self.page |
paul@765 | 532 | |
paul@765 | 533 | p = event_period_from_period(period) |
paul@765 | 534 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@765 | 535 | |
paul@765 | 536 | css = " ".join([ |
paul@765 | 537 | replaced and "replaced" or "", |
paul@765 | 538 | p.is_affected(recurrenceid) and "affected" or "" |
paul@765 | 539 | ]) |
paul@765 | 540 | |
paul@765 | 541 | formdate = show_start and p.get_form_start() or p.get_form_end() |
paul@765 | 542 | dt = formdate.as_datetime() |
paul@765 | 543 | if dt: |
paul@765 | 544 | page.td(self.format_datetime(dt, "long"), class_=css) |
paul@765 | 545 | else: |
paul@765 | 546 | page.td("(Unrecognised date)") |
paul@765 | 547 | |
paul@765 | 548 | def get_date_control_values(self, name, multiple=False, tzid_name=None): |
paul@765 | 549 | |
paul@765 | 550 | """ |
paul@765 | 551 | Return a dictionary containing date, time and tzid entries for fields |
paul@765 | 552 | starting with 'name'. If 'multiple' is set to a true value, many |
paul@765 | 553 | dictionaries will be returned corresponding to a collection of |
paul@765 | 554 | datetimes. If 'tzid_name' is specified, the time zone information will |
paul@765 | 555 | be acquired from a field starting with 'tzid_name' instead of 'name'. |
paul@765 | 556 | """ |
paul@765 | 557 | |
paul@765 | 558 | args = self.env.get_args() |
paul@765 | 559 | |
paul@765 | 560 | dates = args.get("%s-date" % name, []) |
paul@765 | 561 | hours = args.get("%s-hour" % name, []) |
paul@765 | 562 | minutes = args.get("%s-minute" % name, []) |
paul@765 | 563 | seconds = args.get("%s-second" % name, []) |
paul@765 | 564 | tzids = args.get("%s-tzid" % (tzid_name or name), []) |
paul@765 | 565 | |
paul@765 | 566 | # Handle absent values by employing None values. |
paul@765 | 567 | |
paul@765 | 568 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@765 | 569 | |
paul@765 | 570 | if not field_values and not multiple: |
paul@765 | 571 | all_values = FormDate() |
paul@765 | 572 | else: |
paul@765 | 573 | all_values = [] |
paul@765 | 574 | for date, hour, minute, second, tzid in field_values: |
paul@765 | 575 | value = FormDate(date, hour, minute, second, tzid or self.get_tzid()) |
paul@765 | 576 | |
paul@765 | 577 | # Return a single value or append to a collection of all values. |
paul@765 | 578 | |
paul@765 | 579 | if not multiple: |
paul@765 | 580 | return value |
paul@765 | 581 | else: |
paul@765 | 582 | all_values.append(value) |
paul@765 | 583 | |
paul@765 | 584 | return all_values |
paul@765 | 585 | |
paul@446 | 586 | # vim: tabstop=4 expandtab shiftwidth=4 |