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@807 | 24 | from imiptools.data import get_address, get_uri, uri_item, 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@807 | 219 | def __init__(self, resource=None, messenger=None): |
paul@756 | 220 | Resource.__init__(self, resource) |
paul@756 | 221 | user = self.env.get_user() |
paul@807 | 222 | ClientForObject.__init__(self, None, user and get_uri(user) or None, messenger) |
paul@807 | 223 | |
paul@807 | 224 | # Communication methods. |
paul@807 | 225 | |
paul@807 | 226 | def send_message(self, method, sender, from_organiser, parts=None): |
paul@807 | 227 | |
paul@807 | 228 | """ |
paul@807 | 229 | Create a full calendar object employing the given 'method', and send it |
paul@807 | 230 | to the appropriate recipients, also sending a copy to the 'sender'. The |
paul@807 | 231 | 'from_organiser' value indicates whether the organiser is sending this |
paul@807 | 232 | message (and is thus equivalent to "as organiser"). |
paul@807 | 233 | """ |
paul@807 | 234 | |
paul@807 | 235 | parts = parts or [self.obj.to_part(method)] |
paul@807 | 236 | |
paul@807 | 237 | # As organiser, send an invitation to attendees, excluding oneself if |
paul@807 | 238 | # also attending. The updated event will be saved by the outgoing |
paul@807 | 239 | # handler. |
paul@807 | 240 | |
paul@807 | 241 | organiser = get_uri(self.obj.get_value("ORGANIZER")) |
paul@807 | 242 | attendees = uri_values(self.obj.get_values("ATTENDEE")) |
paul@807 | 243 | |
paul@807 | 244 | if from_organiser: |
paul@807 | 245 | recipients = [get_address(attendee) for attendee in attendees if attendee != self.user] |
paul@807 | 246 | else: |
paul@807 | 247 | recipients = [get_address(organiser)] |
paul@807 | 248 | |
paul@807 | 249 | # Since the outgoing handler updates this user's free/busy details, |
paul@807 | 250 | # the stored details will probably not have the updated details at |
paul@807 | 251 | # this point, so we update our copy for serialisation as the bundled |
paul@807 | 252 | # free/busy object. |
paul@807 | 253 | |
paul@807 | 254 | freebusy = self.store.get_freebusy(self.user) |
paul@807 | 255 | self.update_freebusy(freebusy, self.user, from_organiser) |
paul@807 | 256 | |
paul@807 | 257 | # Bundle free/busy information if appropriate. |
paul@807 | 258 | |
paul@807 | 259 | part = self.get_freebusy_part(freebusy) |
paul@807 | 260 | if part: |
paul@807 | 261 | parts.append(part) |
paul@807 | 262 | |
paul@809 | 263 | self._send_message(sender, recipients, parts) |
paul@809 | 264 | |
paul@809 | 265 | def _send_message(self, sender, recipients, parts): |
paul@809 | 266 | |
paul@807 | 267 | # Explicitly specify the outgoing BCC recipient since we are sending as |
paul@807 | 268 | # the generic calendar user. |
paul@807 | 269 | |
paul@807 | 270 | message = self.messenger.make_outgoing_message(parts, recipients, outgoing_bcc=sender) |
paul@807 | 271 | self.messenger.sendmail(recipients, message.as_string(), outgoing_bcc=sender) |
paul@807 | 272 | |
paul@807 | 273 | # Action methods. |
paul@807 | 274 | |
paul@809 | 275 | def process_declined_counter(self, attendee): |
paul@809 | 276 | |
paul@809 | 277 | "Process a declined counter-proposal." |
paul@809 | 278 | |
paul@809 | 279 | # Obtain the counter-proposal for the attendee. |
paul@809 | 280 | |
paul@809 | 281 | obj = self.get_stored_object(self.uid, self.recurrenceid, "counters", attendee) |
paul@809 | 282 | if not obj: |
paul@809 | 283 | return False |
paul@809 | 284 | |
paul@809 | 285 | method = "DECLINECOUNTER" |
paul@809 | 286 | obj.update_dtstamp() |
paul@809 | 287 | obj.update_sequence(False) |
paul@809 | 288 | self._send_message(get_address(self.user), [get_address(attendee)], parts=[obj.to_part(method)]) |
paul@809 | 289 | return True |
paul@809 | 290 | |
paul@807 | 291 | def process_received_request(self): |
paul@807 | 292 | |
paul@807 | 293 | """ |
paul@807 | 294 | Process the current request for the current user. Return whether any |
paul@807 | 295 | action was taken. |
paul@807 | 296 | """ |
paul@807 | 297 | |
paul@807 | 298 | # Reply only on behalf of this user. |
paul@807 | 299 | |
paul@807 | 300 | attendee_attr = self.update_participation(self.obj) |
paul@807 | 301 | |
paul@807 | 302 | if not attendee_attr: |
paul@807 | 303 | return False |
paul@807 | 304 | |
paul@807 | 305 | self.obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@807 | 306 | self.update_dtstamp() |
paul@809 | 307 | self.update_sequence(False) |
paul@807 | 308 | self.send_message("REPLY", get_address(self.user), from_organiser=False) |
paul@807 | 309 | return True |
paul@807 | 310 | |
paul@807 | 311 | def process_created_request(self, method, to_cancel=None, to_unschedule=None): |
paul@807 | 312 | |
paul@807 | 313 | """ |
paul@807 | 314 | Process the current request, sending a created request of the given |
paul@807 | 315 | 'method' to attendees. Return whether any action was taken. |
paul@807 | 316 | |
paul@807 | 317 | If 'to_cancel' is specified, a list of participants to be sent cancel |
paul@807 | 318 | messages is provided. |
paul@807 | 319 | |
paul@807 | 320 | If 'to_unschedule' is specified, a list of periods to be unscheduled is |
paul@807 | 321 | provided. |
paul@807 | 322 | """ |
paul@807 | 323 | |
paul@807 | 324 | # Here, the organiser should be the current user. |
paul@807 | 325 | |
paul@807 | 326 | organiser, organiser_attr = uri_item(self.obj.get_item("ORGANIZER")) |
paul@807 | 327 | |
paul@807 | 328 | self.update_sender(organiser_attr) |
paul@807 | 329 | self.update_dtstamp() |
paul@809 | 330 | self.update_sequence(True) |
paul@807 | 331 | |
paul@807 | 332 | parts = [self.obj.to_part(method)] |
paul@807 | 333 | |
paul@807 | 334 | # Add message parts with cancelled occurrence information. |
paul@807 | 335 | # NOTE: This could probably be merged with the updated event message. |
paul@807 | 336 | |
paul@807 | 337 | if to_unschedule: |
paul@807 | 338 | obj = self.obj.copy() |
paul@807 | 339 | obj.remove_all(["RRULE", "RDATE", "DTSTART", "DTEND", "DURATION"]) |
paul@807 | 340 | |
paul@807 | 341 | for p in to_unschedule: |
paul@807 | 342 | if not p.origin: |
paul@807 | 343 | continue |
paul@807 | 344 | obj["RECURRENCE-ID"] = [(format_datetime(p.get_start()), p.get_start_attr())] |
paul@807 | 345 | parts.append(obj.to_part("CANCEL")) |
paul@807 | 346 | |
paul@807 | 347 | # Send the updated event, along with a cancellation for each of the |
paul@807 | 348 | # unscheduled occurrences. |
paul@807 | 349 | |
paul@807 | 350 | self.send_message("CANCEL", get_address(organiser), from_organiser=True, parts=parts) |
paul@807 | 351 | |
paul@807 | 352 | # When cancelling, replace the attendees with those for whom the event |
paul@807 | 353 | # is now cancelled. |
paul@807 | 354 | |
paul@807 | 355 | if to_cancel: |
paul@807 | 356 | obj = self.obj.copy() |
paul@807 | 357 | obj["ATTENDEE"] = to_cancel |
paul@807 | 358 | |
paul@807 | 359 | # Send a cancellation to all uninvited attendees. |
paul@807 | 360 | |
paul@807 | 361 | self.send_message("CANCEL", get_address(organiser), from_organiser=True) |
paul@807 | 362 | |
paul@807 | 363 | return True |
paul@446 | 364 | |
paul@764 | 365 | class FormUtilities: |
paul@764 | 366 | |
paul@765 | 367 | "Utility methods resource mix-in." |
paul@764 | 368 | |
paul@764 | 369 | def control(self, name, type, value, selected=False, **kw): |
paul@764 | 370 | |
paul@764 | 371 | """ |
paul@764 | 372 | Show a control with the given 'name', 'type' and 'value', with |
paul@764 | 373 | 'selected' indicating whether it should be selected (checked or |
paul@764 | 374 | equivalent), and with keyword arguments setting other properties. |
paul@764 | 375 | """ |
paul@764 | 376 | |
paul@764 | 377 | page = self.page |
paul@779 | 378 | if type in ("checkbox", "radio") and selected: |
paul@764 | 379 | page.input(name=name, type=type, value=value, checked=selected, **kw) |
paul@764 | 380 | else: |
paul@764 | 381 | page.input(name=name, type=type, value=value, **kw) |
paul@764 | 382 | |
paul@764 | 383 | def menu(self, name, default, items, class_="", index=None): |
paul@764 | 384 | |
paul@764 | 385 | """ |
paul@764 | 386 | Show a select menu having the given 'name', set to the given 'default', |
paul@764 | 387 | providing the given (value, label) 'items', and employing the given CSS |
paul@764 | 388 | 'class_' if specified. |
paul@764 | 389 | """ |
paul@764 | 390 | |
paul@764 | 391 | page = self.page |
paul@764 | 392 | values = self.env.get_args().get(name, [default]) |
paul@764 | 393 | if index is not None: |
paul@764 | 394 | values = values[index:] |
paul@764 | 395 | values = values and values[0:1] or [default] |
paul@764 | 396 | |
paul@764 | 397 | page.select(name=name, class_=class_) |
paul@764 | 398 | for v, label in items: |
paul@764 | 399 | if v is None: |
paul@764 | 400 | continue |
paul@764 | 401 | if v in values: |
paul@764 | 402 | page.option(label, value=v, selected="selected") |
paul@764 | 403 | else: |
paul@764 | 404 | page.option(label, value=v) |
paul@764 | 405 | page.select.close() |
paul@764 | 406 | |
paul@764 | 407 | def date_controls(self, name, default, index=None, show_tzid=True, read_only=False): |
paul@764 | 408 | |
paul@764 | 409 | """ |
paul@764 | 410 | Show date controls for a field with the given 'name' and 'default' form |
paul@764 | 411 | date value. |
paul@764 | 412 | |
paul@764 | 413 | If 'index' is specified, default field values will be overridden by the |
paul@764 | 414 | element from a collection of existing form values with the specified |
paul@764 | 415 | index; otherwise, field values will be overridden by a single form |
paul@764 | 416 | value. |
paul@764 | 417 | |
paul@764 | 418 | If 'show_tzid' is set to a false value, the time zone menu will not be |
paul@764 | 419 | provided. |
paul@764 | 420 | |
paul@764 | 421 | If 'read_only' is set to a true value, the controls will be hidden and |
paul@764 | 422 | labels will be employed instead. |
paul@764 | 423 | """ |
paul@764 | 424 | |
paul@764 | 425 | page = self.page |
paul@764 | 426 | |
paul@764 | 427 | # Show dates for up to one week around the current date. |
paul@764 | 428 | |
paul@764 | 429 | dt = default.as_datetime() |
paul@764 | 430 | if not dt: |
paul@764 | 431 | dt = date.today() |
paul@764 | 432 | |
paul@764 | 433 | base = to_date(dt) |
paul@764 | 434 | |
paul@764 | 435 | # Show a date label with a hidden field if read-only. |
paul@764 | 436 | |
paul@764 | 437 | if read_only: |
paul@764 | 438 | self.control("%s-date" % name, "hidden", format_datetime(base)) |
paul@764 | 439 | page.span(self.format_date(base, "long")) |
paul@764 | 440 | |
paul@764 | 441 | # Show dates for up to one week around the current date. |
paul@764 | 442 | # NOTE: Support paging to other dates. |
paul@764 | 443 | |
paul@764 | 444 | else: |
paul@764 | 445 | items = [] |
paul@764 | 446 | for i in range(-7, 8): |
paul@764 | 447 | d = base + timedelta(i) |
paul@764 | 448 | items.append((format_datetime(d), self.format_date(d, "full"))) |
paul@764 | 449 | self.menu("%s-date" % name, format_datetime(base), items, index=index) |
paul@764 | 450 | |
paul@764 | 451 | # Show time details. |
paul@764 | 452 | |
paul@764 | 453 | page.span(class_="time enabled") |
paul@764 | 454 | |
paul@764 | 455 | if read_only: |
paul@764 | 456 | page.span("%s:%s:%s" % (default.get_hour(), default.get_minute(), default.get_second())) |
paul@764 | 457 | self.control("%s-hour" % name, "hidden", default.get_hour()) |
paul@764 | 458 | self.control("%s-minute" % name, "hidden", default.get_minute()) |
paul@764 | 459 | self.control("%s-second" % name, "hidden", default.get_second()) |
paul@764 | 460 | else: |
paul@764 | 461 | self.control("%s-hour" % name, "text", default.get_hour(), maxlength=2, size=2) |
paul@764 | 462 | page.add(":") |
paul@764 | 463 | self.control("%s-minute" % name, "text", default.get_minute(), maxlength=2, size=2) |
paul@764 | 464 | page.add(":") |
paul@764 | 465 | self.control("%s-second" % name, "text", default.get_second(), maxlength=2, size=2) |
paul@764 | 466 | |
paul@764 | 467 | # Show time zone details. |
paul@764 | 468 | |
paul@764 | 469 | if show_tzid: |
paul@764 | 470 | page.add(" ") |
paul@764 | 471 | tzid = default.get_tzid() or self.get_tzid() |
paul@764 | 472 | |
paul@764 | 473 | # Show a label if read-only or a menu otherwise. |
paul@764 | 474 | |
paul@764 | 475 | if read_only: |
paul@764 | 476 | self.control("%s-tzid" % name, "hidden", tzid) |
paul@764 | 477 | page.span(tzid) |
paul@764 | 478 | else: |
paul@764 | 479 | self.timezone_menu("%s-tzid" % name, tzid, index) |
paul@764 | 480 | |
paul@764 | 481 | page.span.close() |
paul@764 | 482 | |
paul@764 | 483 | def timezone_menu(self, name, default, index=None): |
paul@764 | 484 | |
paul@764 | 485 | """ |
paul@764 | 486 | Show timezone controls using a menu with the given 'name', set to the |
paul@764 | 487 | given 'default' unless a field of the given 'name' provides a value. |
paul@764 | 488 | """ |
paul@764 | 489 | |
paul@764 | 490 | entries = [(tzid, tzid) for tzid in pytz.all_timezones] |
paul@764 | 491 | self.menu(name, default, entries, index=index) |
paul@764 | 492 | |
paul@765 | 493 | class DateTimeFormUtilities: |
paul@765 | 494 | |
paul@765 | 495 | "Date/time control methods resource mix-in." |
paul@765 | 496 | |
paul@776 | 497 | # Control naming helpers. |
paul@776 | 498 | |
paul@776 | 499 | def element_identifier(self, name, index=None): |
paul@776 | 500 | return index is not None and "%s-%d" % (name, index) or name |
paul@776 | 501 | |
paul@776 | 502 | def element_name(self, name, suffix, index=None): |
paul@776 | 503 | return index is not None and "%s-%s" % (name, suffix) or name |
paul@776 | 504 | |
paul@776 | 505 | def element_enable(self, index=None): |
paul@776 | 506 | return index is not None and str(index) or "enable" |
paul@776 | 507 | |
paul@765 | 508 | def show_object_datetime_controls(self, period, index=None): |
paul@765 | 509 | |
paul@765 | 510 | """ |
paul@765 | 511 | Show datetime-related controls if already active or if an object needs |
paul@765 | 512 | them for the given 'period'. The given 'index' is used to parameterise |
paul@765 | 513 | individual controls for dynamic manipulation. |
paul@765 | 514 | """ |
paul@765 | 515 | |
paul@765 | 516 | p = form_period_from_period(period) |
paul@765 | 517 | |
paul@765 | 518 | page = self.page |
paul@765 | 519 | args = self.env.get_args() |
paul@765 | 520 | _id = self.element_identifier |
paul@765 | 521 | _name = self.element_name |
paul@765 | 522 | _enable = self.element_enable |
paul@765 | 523 | |
paul@765 | 524 | # Add a dynamic stylesheet to permit the controls to modify the display. |
paul@765 | 525 | # NOTE: The style details need to be coordinated with the static |
paul@765 | 526 | # NOTE: stylesheet. |
paul@765 | 527 | |
paul@765 | 528 | if index is not None: |
paul@765 | 529 | page.style(type="text/css") |
paul@765 | 530 | |
paul@765 | 531 | # Unlike the rules for object properties, these affect recurrence |
paul@765 | 532 | # properties. |
paul@765 | 533 | |
paul@765 | 534 | page.add("""\ |
paul@765 | 535 | input#dttimes-enable-%(index)d, |
paul@765 | 536 | input#dtend-enable-%(index)d, |
paul@765 | 537 | input#dttimes-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue .time.enabled, |
paul@765 | 538 | input#dttimes-enable-%(index)d:checked ~ .recurrence td.objectvalue .time.disabled, |
paul@765 | 539 | input#dtend-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue.dtend .dt.enabled, |
paul@765 | 540 | input#dtend-enable-%(index)d:checked ~ .recurrence td.objectvalue.dtend .dt.disabled { |
paul@765 | 541 | display: none; |
paul@765 | 542 | }""" % {"index" : index}) |
paul@765 | 543 | |
paul@765 | 544 | page.style.close() |
paul@765 | 545 | |
paul@765 | 546 | self.control( |
paul@765 | 547 | _name("dtend-control", "recur", index), "checkbox", |
paul@765 | 548 | _enable(index), p.end_enabled, |
paul@765 | 549 | id=_id("dtend-enable", index) |
paul@765 | 550 | ) |
paul@765 | 551 | |
paul@765 | 552 | self.control( |
paul@765 | 553 | _name("dttimes-control", "recur", index), "checkbox", |
paul@765 | 554 | _enable(index), p.times_enabled, |
paul@765 | 555 | id=_id("dttimes-enable", index) |
paul@765 | 556 | ) |
paul@765 | 557 | |
paul@765 | 558 | def show_datetime_controls(self, formdate, show_start): |
paul@765 | 559 | |
paul@765 | 560 | """ |
paul@765 | 561 | Show datetime details from the current object for the 'formdate', |
paul@765 | 562 | showing start details if 'show_start' is set to a true value. Details |
paul@765 | 563 | will appear as controls for organisers and labels for attendees. |
paul@765 | 564 | """ |
paul@765 | 565 | |
paul@765 | 566 | page = self.page |
paul@765 | 567 | |
paul@765 | 568 | # Show controls for editing as organiser. |
paul@765 | 569 | |
paul@765 | 570 | if self.is_organiser(): |
paul@765 | 571 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@765 | 572 | |
paul@765 | 573 | if show_start: |
paul@765 | 574 | page.div(class_="dt enabled") |
paul@765 | 575 | self.date_controls("dtstart", formdate) |
paul@765 | 576 | page.br() |
paul@765 | 577 | page.label("Specify times", for_="dttimes-enable", class_="time disabled enable") |
paul@765 | 578 | page.label("Specify dates only", for_="dttimes-enable", class_="time enabled disable") |
paul@765 | 579 | page.div.close() |
paul@765 | 580 | |
paul@765 | 581 | else: |
paul@765 | 582 | page.div(class_="dt disabled") |
paul@765 | 583 | page.label("Specify end date", for_="dtend-enable", class_="enable") |
paul@765 | 584 | page.div.close() |
paul@765 | 585 | page.div(class_="dt enabled") |
paul@765 | 586 | self.date_controls("dtend", formdate) |
paul@765 | 587 | page.br() |
paul@765 | 588 | page.label("End on same day", for_="dtend-enable", class_="disable") |
paul@765 | 589 | page.div.close() |
paul@765 | 590 | |
paul@765 | 591 | page.td.close() |
paul@765 | 592 | |
paul@765 | 593 | # Show a label as attendee. |
paul@765 | 594 | |
paul@765 | 595 | else: |
paul@765 | 596 | dt = formdate.as_datetime() |
paul@765 | 597 | if dt: |
paul@765 | 598 | page.td(self.format_datetime(dt, "full")) |
paul@765 | 599 | else: |
paul@765 | 600 | page.td("(Unrecognised date)") |
paul@765 | 601 | |
paul@765 | 602 | def show_recurrence_controls(self, index, period, recurrenceid, recurrenceids, show_start): |
paul@765 | 603 | |
paul@765 | 604 | """ |
paul@765 | 605 | Show datetime details from the current object for the recurrence having |
paul@765 | 606 | the given 'index', with the recurrence period described by 'period', |
paul@765 | 607 | indicating a start, end and origin of the period from the event details, |
paul@765 | 608 | employing any 'recurrenceid' and 'recurrenceids' for the object to |
paul@765 | 609 | configure the displayed information. |
paul@765 | 610 | |
paul@765 | 611 | If 'show_start' is set to a true value, the start details will be shown; |
paul@765 | 612 | otherwise, the end details will be shown. |
paul@765 | 613 | """ |
paul@765 | 614 | |
paul@765 | 615 | page = self.page |
paul@765 | 616 | _id = self.element_identifier |
paul@765 | 617 | _name = self.element_name |
paul@765 | 618 | |
paul@765 | 619 | p = event_period_from_period(period) |
paul@765 | 620 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@765 | 621 | |
paul@765 | 622 | # Show controls for editing as organiser. |
paul@765 | 623 | |
paul@765 | 624 | if self.is_organiser() and not replaced: |
paul@765 | 625 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@765 | 626 | |
paul@765 | 627 | read_only = period.origin == "RRULE" |
paul@765 | 628 | |
paul@765 | 629 | if show_start: |
paul@765 | 630 | page.div(class_="dt enabled") |
paul@765 | 631 | self.date_controls(_name("dtstart", "recur", index), p.get_form_start(), index=index, read_only=read_only) |
paul@765 | 632 | if not read_only: |
paul@765 | 633 | page.br() |
paul@765 | 634 | page.label("Specify times", for_=_id("dttimes-enable", index), class_="time disabled enable") |
paul@765 | 635 | page.label("Specify dates only", for_=_id("dttimes-enable", index), class_="time enabled disable") |
paul@765 | 636 | page.div.close() |
paul@765 | 637 | |
paul@765 | 638 | # Put the origin somewhere. |
paul@765 | 639 | |
paul@765 | 640 | self.control("recur-origin", "hidden", p.origin or "") |
paul@765 | 641 | |
paul@765 | 642 | else: |
paul@765 | 643 | page.div(class_="dt disabled") |
paul@765 | 644 | if not read_only: |
paul@765 | 645 | page.label("Specify end date", for_=_id("dtend-enable", index), class_="enable") |
paul@765 | 646 | page.div.close() |
paul@765 | 647 | page.div(class_="dt enabled") |
paul@765 | 648 | self.date_controls(_name("dtend", "recur", index), p.get_form_end(), index=index, show_tzid=False, read_only=read_only) |
paul@765 | 649 | if not read_only: |
paul@765 | 650 | page.br() |
paul@765 | 651 | page.label("End on same day", for_=_id("dtend-enable", index), class_="disable") |
paul@765 | 652 | page.div.close() |
paul@765 | 653 | |
paul@765 | 654 | page.td.close() |
paul@765 | 655 | |
paul@765 | 656 | # Show label as attendee. |
paul@765 | 657 | |
paul@765 | 658 | else: |
paul@765 | 659 | self.show_recurrence_label(p, recurrenceid, recurrenceids, show_start) |
paul@765 | 660 | |
paul@765 | 661 | def show_recurrence_label(self, period, recurrenceid, recurrenceids, show_start): |
paul@765 | 662 | |
paul@765 | 663 | """ |
paul@765 | 664 | Show datetime details for the given 'period', employing any |
paul@765 | 665 | 'recurrenceid' and 'recurrenceids' for the object to configure the |
paul@765 | 666 | displayed information. |
paul@765 | 667 | |
paul@765 | 668 | If 'show_start' is set to a true value, the start details will be shown; |
paul@765 | 669 | otherwise, the end details will be shown. |
paul@765 | 670 | """ |
paul@765 | 671 | |
paul@765 | 672 | page = self.page |
paul@765 | 673 | |
paul@765 | 674 | p = event_period_from_period(period) |
paul@765 | 675 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@765 | 676 | |
paul@765 | 677 | css = " ".join([ |
paul@765 | 678 | replaced and "replaced" or "", |
paul@765 | 679 | p.is_affected(recurrenceid) and "affected" or "" |
paul@765 | 680 | ]) |
paul@765 | 681 | |
paul@765 | 682 | formdate = show_start and p.get_form_start() or p.get_form_end() |
paul@765 | 683 | dt = formdate.as_datetime() |
paul@765 | 684 | if dt: |
paul@765 | 685 | page.td(self.format_datetime(dt, "long"), class_=css) |
paul@765 | 686 | else: |
paul@765 | 687 | page.td("(Unrecognised date)") |
paul@765 | 688 | |
paul@765 | 689 | def get_date_control_values(self, name, multiple=False, tzid_name=None): |
paul@765 | 690 | |
paul@765 | 691 | """ |
paul@787 | 692 | Return a form date object representing fields starting with 'name'. If |
paul@787 | 693 | 'multiple' is set to a true value, many date objects will be returned |
paul@787 | 694 | corresponding to a collection of datetimes. |
paul@787 | 695 | |
paul@787 | 696 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@787 | 697 | from fields starting with 'tzid_name' instead of 'name'. |
paul@765 | 698 | """ |
paul@765 | 699 | |
paul@765 | 700 | args = self.env.get_args() |
paul@765 | 701 | |
paul@765 | 702 | dates = args.get("%s-date" % name, []) |
paul@765 | 703 | hours = args.get("%s-hour" % name, []) |
paul@765 | 704 | minutes = args.get("%s-minute" % name, []) |
paul@765 | 705 | seconds = args.get("%s-second" % name, []) |
paul@765 | 706 | tzids = args.get("%s-tzid" % (tzid_name or name), []) |
paul@765 | 707 | |
paul@765 | 708 | # Handle absent values by employing None values. |
paul@765 | 709 | |
paul@765 | 710 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@765 | 711 | |
paul@765 | 712 | if not field_values and not multiple: |
paul@765 | 713 | all_values = FormDate() |
paul@765 | 714 | else: |
paul@765 | 715 | all_values = [] |
paul@765 | 716 | for date, hour, minute, second, tzid in field_values: |
paul@765 | 717 | value = FormDate(date, hour, minute, second, tzid or self.get_tzid()) |
paul@765 | 718 | |
paul@765 | 719 | # Return a single value or append to a collection of all values. |
paul@765 | 720 | |
paul@765 | 721 | if not multiple: |
paul@765 | 722 | return value |
paul@765 | 723 | else: |
paul@765 | 724 | all_values.append(value) |
paul@765 | 725 | |
paul@765 | 726 | return all_values |
paul@765 | 727 | |
paul@787 | 728 | def set_date_control_values(self, name, formdates, tzid_name=None): |
paul@787 | 729 | |
paul@787 | 730 | """ |
paul@787 | 731 | Replace form fields starting with 'name' using the values of the given |
paul@787 | 732 | 'formdates'. |
paul@787 | 733 | |
paul@787 | 734 | If 'tzid_name' is specified, the time zone information will be stored in |
paul@787 | 735 | fields starting with 'tzid_name' instead of 'name'. |
paul@787 | 736 | """ |
paul@787 | 737 | |
paul@787 | 738 | args = self.env.get_args() |
paul@787 | 739 | |
paul@787 | 740 | args["%s-date" % name] = [d.date for d in formdates] |
paul@787 | 741 | args["%s-hour" % name] = [d.hour for d in formdates] |
paul@787 | 742 | args["%s-minute" % name] = [d.minute for d in formdates] |
paul@787 | 743 | args["%s-second" % name] = [d.second for d in formdates] |
paul@787 | 744 | args["%s-tzid" % (tzid_name or name)] = [d.tzid for d in formdates] |
paul@787 | 745 | |
paul@446 | 746 | # vim: tabstop=4 expandtab shiftwidth=4 |