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