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@845 | 27 | from imipweb.data import event_period_from_period, form_period_from_period, \ |
paul@845 | 28 | FormDate, PeriodError |
paul@446 | 29 | from imipweb.env import CGIEnvironment |
paul@777 | 30 | from urllib import urlencode |
paul@446 | 31 | import babel.dates |
paul@446 | 32 | import imip_store |
paul@446 | 33 | import markup |
paul@764 | 34 | import pytz |
paul@446 | 35 | |
paul@756 | 36 | class Resource: |
paul@446 | 37 | |
paul@756 | 38 | "A Web application resource." |
paul@446 | 39 | |
paul@446 | 40 | def __init__(self, resource=None): |
paul@756 | 41 | |
paul@756 | 42 | """ |
paul@756 | 43 | Initialise a resource, allowing it to share the environment of any given |
paul@756 | 44 | existing 'resource'. |
paul@756 | 45 | """ |
paul@756 | 46 | |
paul@446 | 47 | self.encoding = "utf-8" |
paul@446 | 48 | self.env = CGIEnvironment(self.encoding) |
paul@446 | 49 | |
paul@756 | 50 | self.objects = {} |
paul@446 | 51 | self.locale = None |
paul@446 | 52 | self.requests = None |
paul@446 | 53 | |
paul@446 | 54 | self.out = resource and resource.out or self.env.get_output() |
paul@446 | 55 | self.page = resource and resource.page or markup.page() |
paul@446 | 56 | self.html_ids = None |
paul@446 | 57 | |
paul@446 | 58 | # Presentation methods. |
paul@446 | 59 | |
paul@446 | 60 | def new_page(self, title): |
paul@446 | 61 | self.page.init(title=title, charset=self.encoding, css=self.env.new_url("styles.css")) |
paul@446 | 62 | self.html_ids = set() |
paul@446 | 63 | |
paul@446 | 64 | def status(self, code, message): |
paul@446 | 65 | self.header("Status", "%s %s" % (code, message)) |
paul@446 | 66 | |
paul@446 | 67 | def header(self, header, value): |
paul@446 | 68 | print >>self.out, "%s: %s" % (header, value) |
paul@446 | 69 | |
paul@446 | 70 | def no_user(self): |
paul@446 | 71 | self.status(403, "Forbidden") |
paul@446 | 72 | self.new_page(title="Forbidden") |
paul@446 | 73 | self.page.p("You are not logged in and thus cannot access scheduling requests.") |
paul@446 | 74 | |
paul@446 | 75 | def no_page(self): |
paul@446 | 76 | self.status(404, "Not Found") |
paul@446 | 77 | self.new_page(title="Not Found") |
paul@446 | 78 | self.page.p("No page is provided at the given address.") |
paul@446 | 79 | |
paul@446 | 80 | def redirect(self, url): |
paul@446 | 81 | self.status(302, "Redirect") |
paul@446 | 82 | self.header("Location", url) |
paul@446 | 83 | self.new_page(title="Redirect") |
paul@446 | 84 | self.page.p("Redirecting to: %s" % url) |
paul@446 | 85 | |
paul@777 | 86 | def link_to(self, uid, recurrenceid=None, args=None): |
paul@755 | 87 | |
paul@755 | 88 | """ |
paul@763 | 89 | Return a link to an object with the given 'uid' and 'recurrenceid'. |
paul@763 | 90 | See get_identifiers for the decoding of such links. |
paul@777 | 91 | |
paul@777 | 92 | If 'args' is specified, the given dictionary is encoded and included. |
paul@755 | 93 | """ |
paul@755 | 94 | |
paul@755 | 95 | path = [uid] |
paul@446 | 96 | if recurrenceid: |
paul@755 | 97 | path.append(recurrenceid) |
paul@777 | 98 | return "%s%s" % (self.env.new_url("/".join(path)), args and ("?%s" % urlencode(args)) or "") |
paul@446 | 99 | |
paul@623 | 100 | # Access to objects. |
paul@623 | 101 | |
paul@755 | 102 | def get_identifiers(self, path_info): |
paul@755 | 103 | |
paul@755 | 104 | """ |
paul@755 | 105 | Return identifiers provided by 'path_info', potentially encoded by |
paul@755 | 106 | 'link_to'. |
paul@755 | 107 | """ |
paul@755 | 108 | |
paul@446 | 109 | parts = path_info.lstrip("/").split("/") |
paul@755 | 110 | |
paul@755 | 111 | # UID only. |
paul@755 | 112 | |
paul@446 | 113 | if len(parts) == 1: |
paul@763 | 114 | return parts[0], None |
paul@755 | 115 | |
paul@763 | 116 | # UID and RECURRENCE-ID. |
paul@755 | 117 | |
paul@446 | 118 | else: |
paul@763 | 119 | return parts[:2] |
paul@446 | 120 | |
paul@769 | 121 | def _get_object(self, uid, recurrenceid=None, section=None, username=None): |
paul@769 | 122 | if self.objects.has_key((uid, recurrenceid, section, username)): |
paul@769 | 123 | return self.objects[(uid, recurrenceid, section, username)] |
paul@446 | 124 | |
paul@769 | 125 | obj = self.objects[(uid, recurrenceid, section, username)] = self.get_stored_object(uid, recurrenceid, section, username) |
paul@446 | 126 | return obj |
paul@446 | 127 | |
paul@446 | 128 | def _get_recurrences(self, uid): |
paul@446 | 129 | return self.store.get_recurrences(self.user, uid) |
paul@446 | 130 | |
paul@694 | 131 | def _get_active_recurrences(self, uid): |
paul@694 | 132 | return self.store.get_active_recurrences(self.user, uid) |
paul@694 | 133 | |
paul@446 | 134 | def _get_requests(self): |
paul@446 | 135 | if self.requests is None: |
paul@699 | 136 | self.requests = self.store.get_requests(self.user) |
paul@446 | 137 | return self.requests |
paul@446 | 138 | |
paul@755 | 139 | def _have_request(self, uid, recurrenceid=None, type=None, strict=False): |
paul@755 | 140 | return self.store.have_request(self._get_requests(), uid, recurrenceid, type, strict) |
paul@751 | 141 | |
paul@818 | 142 | def _is_request(self): |
paul@818 | 143 | return self._have_request(self.uid, self.recurrenceid) |
paul@818 | 144 | |
paul@766 | 145 | def _get_counters(self, uid, recurrenceid=None): |
paul@766 | 146 | return self.store.get_counters(self.user, uid, recurrenceid) |
paul@766 | 147 | |
paul@446 | 148 | def _get_request_summary(self): |
paul@630 | 149 | |
paul@630 | 150 | "Return a list of periods comprising the request summary." |
paul@630 | 151 | |
paul@446 | 152 | summary = [] |
paul@630 | 153 | |
paul@751 | 154 | for uid, recurrenceid, request_type in self._get_requests(): |
paul@769 | 155 | |
paul@769 | 156 | # Obtain either normal objects or counter-proposals. |
paul@769 | 157 | |
paul@769 | 158 | if not request_type: |
paul@769 | 159 | objs = [self._get_object(uid, recurrenceid)] |
paul@769 | 160 | elif request_type == "COUNTER": |
paul@769 | 161 | objs = [] |
paul@769 | 162 | for attendee in self.store.get_counters(self.user, uid, recurrenceid): |
paul@769 | 163 | objs.append(self._get_object(uid, recurrenceid, "counters", attendee)) |
paul@446 | 164 | |
paul@769 | 165 | # For each object, obtain the periods involved. |
paul@769 | 166 | |
paul@769 | 167 | for obj in objs: |
paul@769 | 168 | if obj: |
paul@855 | 169 | recurrenceids = self._get_recurrences(uid) |
paul@446 | 170 | |
paul@769 | 171 | # Obtain only active periods, not those replaced by redefined |
paul@769 | 172 | # recurrences, converting to free/busy periods. |
paul@769 | 173 | |
paul@769 | 174 | for p in obj.get_active_periods(recurrenceids, self.get_tzid(), self.get_window_end()): |
paul@769 | 175 | summary.append(obj.get_freebusy_period(p)) |
paul@446 | 176 | |
paul@446 | 177 | return summary |
paul@446 | 178 | |
paul@446 | 179 | # Preference methods. |
paul@446 | 180 | |
paul@446 | 181 | def get_user_locale(self): |
paul@446 | 182 | if not self.locale: |
paul@446 | 183 | self.locale = self.get_preferences().get("LANG", "en") |
paul@446 | 184 | return self.locale |
paul@446 | 185 | |
paul@446 | 186 | # Prettyprinting of dates and times. |
paul@446 | 187 | |
paul@446 | 188 | def format_date(self, dt, format): |
paul@446 | 189 | return self._format_datetime(babel.dates.format_date, dt, format) |
paul@446 | 190 | |
paul@446 | 191 | def format_time(self, dt, format): |
paul@446 | 192 | return self._format_datetime(babel.dates.format_time, dt, format) |
paul@446 | 193 | |
paul@446 | 194 | def format_datetime(self, dt, format): |
paul@446 | 195 | return self._format_datetime( |
paul@446 | 196 | isinstance(dt, datetime) and babel.dates.format_datetime or babel.dates.format_date, |
paul@446 | 197 | dt, format) |
paul@446 | 198 | |
paul@446 | 199 | def _format_datetime(self, fn, dt, format): |
paul@446 | 200 | return fn(dt, format=format, locale=self.get_user_locale()) |
paul@446 | 201 | |
paul@756 | 202 | class ResourceClient(Resource, Client): |
paul@446 | 203 | |
paul@756 | 204 | "A Web application resource and calendar client." |
paul@446 | 205 | |
paul@756 | 206 | def __init__(self, resource=None): |
paul@756 | 207 | Resource.__init__(self, resource) |
paul@756 | 208 | user = self.env.get_user() |
paul@756 | 209 | Client.__init__(self, user and get_uri(user) or None) |
paul@446 | 210 | |
paul@756 | 211 | class ResourceClientForObject(Resource, ClientForObject): |
paul@447 | 212 | |
paul@756 | 213 | "A Web application resource and calendar client for a specific object." |
paul@672 | 214 | |
paul@807 | 215 | def __init__(self, resource=None, messenger=None): |
paul@756 | 216 | Resource.__init__(self, resource) |
paul@756 | 217 | user = self.env.get_user() |
paul@807 | 218 | ClientForObject.__init__(self, None, user and get_uri(user) or None, messenger) |
paul@807 | 219 | |
paul@807 | 220 | # Communication methods. |
paul@807 | 221 | |
paul@807 | 222 | def send_message(self, method, sender, from_organiser, parts=None): |
paul@807 | 223 | |
paul@807 | 224 | """ |
paul@807 | 225 | Create a full calendar object employing the given 'method', and send it |
paul@807 | 226 | to the appropriate recipients, also sending a copy to the 'sender'. The |
paul@807 | 227 | 'from_organiser' value indicates whether the organiser is sending this |
paul@807 | 228 | message (and is thus equivalent to "as organiser"). |
paul@807 | 229 | """ |
paul@807 | 230 | |
paul@807 | 231 | parts = parts or [self.obj.to_part(method)] |
paul@807 | 232 | |
paul@807 | 233 | # As organiser, send an invitation to attendees, excluding oneself if |
paul@807 | 234 | # also attending. The updated event will be saved by the outgoing |
paul@807 | 235 | # handler. |
paul@807 | 236 | |
paul@807 | 237 | organiser = get_uri(self.obj.get_value("ORGANIZER")) |
paul@807 | 238 | attendees = uri_values(self.obj.get_values("ATTENDEE")) |
paul@807 | 239 | |
paul@807 | 240 | if from_organiser: |
paul@807 | 241 | recipients = [get_address(attendee) for attendee in attendees if attendee != self.user] |
paul@807 | 242 | else: |
paul@807 | 243 | recipients = [get_address(organiser)] |
paul@807 | 244 | |
paul@807 | 245 | # Since the outgoing handler updates this user's free/busy details, |
paul@807 | 246 | # the stored details will probably not have the updated details at |
paul@807 | 247 | # this point, so we update our copy for serialisation as the bundled |
paul@807 | 248 | # free/busy object. |
paul@807 | 249 | |
paul@807 | 250 | freebusy = self.store.get_freebusy(self.user) |
paul@807 | 251 | self.update_freebusy(freebusy, self.user, from_organiser) |
paul@807 | 252 | |
paul@807 | 253 | # Bundle free/busy information if appropriate. |
paul@807 | 254 | |
paul@807 | 255 | part = self.get_freebusy_part(freebusy) |
paul@807 | 256 | if part: |
paul@807 | 257 | parts.append(part) |
paul@807 | 258 | |
paul@809 | 259 | self._send_message(sender, recipients, parts) |
paul@809 | 260 | |
paul@809 | 261 | def _send_message(self, sender, recipients, parts): |
paul@809 | 262 | |
paul@835 | 263 | """ |
paul@835 | 264 | Send a message, explicitly specifying the 'sender' as an outgoing BCC |
paul@835 | 265 | recipient since the generic calendar user will be the actual sender. |
paul@835 | 266 | """ |
paul@807 | 267 | |
paul@807 | 268 | message = self.messenger.make_outgoing_message(parts, recipients, outgoing_bcc=sender) |
paul@807 | 269 | self.messenger.sendmail(recipients, message.as_string(), outgoing_bcc=sender) |
paul@807 | 270 | |
paul@835 | 271 | def _send_message_to_self(self, parts): |
paul@835 | 272 | |
paul@835 | 273 | "Send a message composed of the given 'parts' to the given user." |
paul@835 | 274 | |
paul@835 | 275 | sender = get_address(self.user) |
paul@835 | 276 | message = self.messenger.make_outgoing_message(parts, [sender]) |
paul@835 | 277 | self.messenger.sendmail([sender], message.as_string()) |
paul@835 | 278 | |
paul@807 | 279 | # Action methods. |
paul@807 | 280 | |
paul@809 | 281 | def process_declined_counter(self, attendee): |
paul@809 | 282 | |
paul@809 | 283 | "Process a declined counter-proposal." |
paul@809 | 284 | |
paul@809 | 285 | # Obtain the counter-proposal for the attendee. |
paul@809 | 286 | |
paul@809 | 287 | obj = self.get_stored_object(self.uid, self.recurrenceid, "counters", attendee) |
paul@809 | 288 | if not obj: |
paul@809 | 289 | return False |
paul@809 | 290 | |
paul@809 | 291 | method = "DECLINECOUNTER" |
paul@851 | 292 | self.update_senders(obj=obj) |
paul@809 | 293 | obj.update_dtstamp() |
paul@809 | 294 | obj.update_sequence(False) |
paul@809 | 295 | self._send_message(get_address(self.user), [get_address(attendee)], parts=[obj.to_part(method)]) |
paul@809 | 296 | return True |
paul@809 | 297 | |
paul@818 | 298 | def process_received_request(self, changed=False): |
paul@807 | 299 | |
paul@807 | 300 | """ |
paul@807 | 301 | Process the current request for the current user. Return whether any |
paul@818 | 302 | action was taken. If 'changed' is set to a true value, or if 'attendees' |
paul@818 | 303 | is specified and differs from the stored attendees, a counter-proposal |
paul@818 | 304 | will be sent instead of a reply. |
paul@807 | 305 | """ |
paul@807 | 306 | |
paul@807 | 307 | # Reply only on behalf of this user. |
paul@807 | 308 | |
paul@818 | 309 | attendee_attr = self.update_participation() |
paul@807 | 310 | |
paul@807 | 311 | if not attendee_attr: |
paul@807 | 312 | return False |
paul@807 | 313 | |
paul@818 | 314 | if not changed: |
paul@818 | 315 | self.obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@848 | 316 | else: |
paul@851 | 317 | self.update_senders() |
paul@818 | 318 | |
paul@807 | 319 | self.update_dtstamp() |
paul@809 | 320 | self.update_sequence(False) |
paul@818 | 321 | self.send_message(changed and "COUNTER" or "REPLY", get_address(self.user), from_organiser=False) |
paul@807 | 322 | return True |
paul@807 | 323 | |
paul@807 | 324 | def process_created_request(self, method, to_cancel=None, to_unschedule=None): |
paul@807 | 325 | |
paul@807 | 326 | """ |
paul@807 | 327 | Process the current request, sending a created request of the given |
paul@807 | 328 | 'method' to attendees. Return whether any action was taken. |
paul@807 | 329 | |
paul@807 | 330 | If 'to_cancel' is specified, a list of participants to be sent cancel |
paul@807 | 331 | messages is provided. |
paul@807 | 332 | |
paul@807 | 333 | If 'to_unschedule' is specified, a list of periods to be unscheduled is |
paul@807 | 334 | provided. |
paul@807 | 335 | """ |
paul@807 | 336 | |
paul@807 | 337 | # Here, the organiser should be the current user. |
paul@807 | 338 | |
paul@807 | 339 | organiser, organiser_attr = uri_item(self.obj.get_item("ORGANIZER")) |
paul@807 | 340 | |
paul@807 | 341 | self.update_sender(organiser_attr) |
paul@851 | 342 | self.update_senders() |
paul@807 | 343 | self.update_dtstamp() |
paul@809 | 344 | self.update_sequence(True) |
paul@807 | 345 | |
paul@807 | 346 | parts = [self.obj.to_part(method)] |
paul@807 | 347 | |
paul@807 | 348 | # Add message parts with cancelled occurrence information. |
paul@807 | 349 | # NOTE: This could probably be merged with the updated event message. |
paul@807 | 350 | |
paul@807 | 351 | if to_unschedule: |
paul@807 | 352 | obj = self.obj.copy() |
paul@807 | 353 | obj.remove_all(["RRULE", "RDATE", "DTSTART", "DTEND", "DURATION"]) |
paul@807 | 354 | |
paul@807 | 355 | for p in to_unschedule: |
paul@807 | 356 | if not p.origin: |
paul@807 | 357 | continue |
paul@807 | 358 | obj["RECURRENCE-ID"] = [(format_datetime(p.get_start()), p.get_start_attr())] |
paul@807 | 359 | parts.append(obj.to_part("CANCEL")) |
paul@807 | 360 | |
paul@807 | 361 | # Send the updated event, along with a cancellation for each of the |
paul@807 | 362 | # unscheduled occurrences. |
paul@807 | 363 | |
paul@807 | 364 | self.send_message("CANCEL", get_address(organiser), from_organiser=True, parts=parts) |
paul@807 | 365 | |
paul@807 | 366 | # When cancelling, replace the attendees with those for whom the event |
paul@807 | 367 | # is now cancelled. |
paul@807 | 368 | |
paul@807 | 369 | if to_cancel: |
paul@807 | 370 | obj = self.obj.copy() |
paul@807 | 371 | obj["ATTENDEE"] = to_cancel |
paul@807 | 372 | |
paul@807 | 373 | # Send a cancellation to all uninvited attendees. |
paul@807 | 374 | |
paul@807 | 375 | self.send_message("CANCEL", get_address(organiser), from_organiser=True) |
paul@807 | 376 | |
paul@835 | 377 | # Since the organiser can update the SEQUENCE but this can leave any |
paul@835 | 378 | # mail/calendar client lagging, issue a PUBLISH message to the user's |
paul@835 | 379 | # address. |
paul@835 | 380 | |
paul@835 | 381 | self._send_message_to_self([self.obj.to_part("PUBLISH")]) |
paul@835 | 382 | |
paul@807 | 383 | return True |
paul@446 | 384 | |
paul@764 | 385 | class FormUtilities: |
paul@764 | 386 | |
paul@765 | 387 | "Utility methods resource mix-in." |
paul@764 | 388 | |
paul@813 | 389 | def prefixed_args(self, prefix, convert=None): |
paul@813 | 390 | |
paul@813 | 391 | """ |
paul@813 | 392 | Return values for all arguments having the given 'prefix' in their |
paul@813 | 393 | names, removing the prefix to obtain each value from the argument name |
paul@813 | 394 | itself. The 'convert' callable can be specified to perform a conversion |
paul@813 | 395 | (to int, for example). |
paul@813 | 396 | """ |
paul@813 | 397 | |
paul@813 | 398 | args = self.env.get_args() |
paul@813 | 399 | |
paul@813 | 400 | values = [] |
paul@813 | 401 | for name in args.keys(): |
paul@813 | 402 | if name.startswith(prefix): |
paul@813 | 403 | value = name[len(prefix):] |
paul@813 | 404 | if convert: |
paul@813 | 405 | try: |
paul@813 | 406 | value = convert(value) |
paul@813 | 407 | except ValueError: |
paul@813 | 408 | pass |
paul@813 | 409 | values.append(value) |
paul@813 | 410 | return values |
paul@813 | 411 | |
paul@764 | 412 | def control(self, name, type, value, selected=False, **kw): |
paul@764 | 413 | |
paul@764 | 414 | """ |
paul@764 | 415 | Show a control with the given 'name', 'type' and 'value', with |
paul@764 | 416 | 'selected' indicating whether it should be selected (checked or |
paul@764 | 417 | equivalent), and with keyword arguments setting other properties. |
paul@764 | 418 | """ |
paul@764 | 419 | |
paul@764 | 420 | page = self.page |
paul@779 | 421 | if type in ("checkbox", "radio") and selected: |
paul@852 | 422 | page.input(name=name, type=type, value=value, checked="checked", **kw) |
paul@764 | 423 | else: |
paul@764 | 424 | page.input(name=name, type=type, value=value, **kw) |
paul@764 | 425 | |
paul@764 | 426 | def menu(self, name, default, items, class_="", index=None): |
paul@764 | 427 | |
paul@764 | 428 | """ |
paul@764 | 429 | Show a select menu having the given 'name', set to the given 'default', |
paul@764 | 430 | providing the given (value, label) 'items', and employing the given CSS |
paul@764 | 431 | 'class_' if specified. |
paul@764 | 432 | """ |
paul@764 | 433 | |
paul@764 | 434 | page = self.page |
paul@764 | 435 | values = self.env.get_args().get(name, [default]) |
paul@764 | 436 | if index is not None: |
paul@764 | 437 | values = values[index:] |
paul@764 | 438 | values = values and values[0:1] or [default] |
paul@764 | 439 | |
paul@764 | 440 | page.select(name=name, class_=class_) |
paul@764 | 441 | for v, label in items: |
paul@764 | 442 | if v is None: |
paul@764 | 443 | continue |
paul@764 | 444 | if v in values: |
paul@764 | 445 | page.option(label, value=v, selected="selected") |
paul@764 | 446 | else: |
paul@764 | 447 | page.option(label, value=v) |
paul@764 | 448 | page.select.close() |
paul@764 | 449 | |
paul@764 | 450 | def date_controls(self, name, default, index=None, show_tzid=True, read_only=False): |
paul@764 | 451 | |
paul@764 | 452 | """ |
paul@764 | 453 | Show date controls for a field with the given 'name' and 'default' form |
paul@764 | 454 | date value. |
paul@764 | 455 | |
paul@764 | 456 | If 'index' is specified, default field values will be overridden by the |
paul@764 | 457 | element from a collection of existing form values with the specified |
paul@764 | 458 | index; otherwise, field values will be overridden by a single form |
paul@764 | 459 | value. |
paul@764 | 460 | |
paul@764 | 461 | If 'show_tzid' is set to a false value, the time zone menu will not be |
paul@764 | 462 | provided. |
paul@764 | 463 | |
paul@764 | 464 | If 'read_only' is set to a true value, the controls will be hidden and |
paul@764 | 465 | labels will be employed instead. |
paul@764 | 466 | """ |
paul@764 | 467 | |
paul@764 | 468 | page = self.page |
paul@764 | 469 | |
paul@764 | 470 | # Show dates for up to one week around the current date. |
paul@764 | 471 | |
paul@764 | 472 | dt = default.as_datetime() |
paul@764 | 473 | if not dt: |
paul@764 | 474 | dt = date.today() |
paul@764 | 475 | |
paul@764 | 476 | base = to_date(dt) |
paul@764 | 477 | |
paul@764 | 478 | # Show a date label with a hidden field if read-only. |
paul@764 | 479 | |
paul@764 | 480 | if read_only: |
paul@764 | 481 | self.control("%s-date" % name, "hidden", format_datetime(base)) |
paul@764 | 482 | page.span(self.format_date(base, "long")) |
paul@764 | 483 | |
paul@764 | 484 | # Show dates for up to one week around the current date. |
paul@764 | 485 | # NOTE: Support paging to other dates. |
paul@764 | 486 | |
paul@764 | 487 | else: |
paul@764 | 488 | items = [] |
paul@764 | 489 | for i in range(-7, 8): |
paul@764 | 490 | d = base + timedelta(i) |
paul@764 | 491 | items.append((format_datetime(d), self.format_date(d, "full"))) |
paul@764 | 492 | self.menu("%s-date" % name, format_datetime(base), items, index=index) |
paul@764 | 493 | |
paul@764 | 494 | # Show time details. |
paul@764 | 495 | |
paul@764 | 496 | page.span(class_="time enabled") |
paul@764 | 497 | |
paul@764 | 498 | if read_only: |
paul@764 | 499 | page.span("%s:%s:%s" % (default.get_hour(), default.get_minute(), default.get_second())) |
paul@764 | 500 | self.control("%s-hour" % name, "hidden", default.get_hour()) |
paul@764 | 501 | self.control("%s-minute" % name, "hidden", default.get_minute()) |
paul@764 | 502 | self.control("%s-second" % name, "hidden", default.get_second()) |
paul@764 | 503 | else: |
paul@764 | 504 | self.control("%s-hour" % name, "text", default.get_hour(), maxlength=2, size=2) |
paul@764 | 505 | page.add(":") |
paul@764 | 506 | self.control("%s-minute" % name, "text", default.get_minute(), maxlength=2, size=2) |
paul@764 | 507 | page.add(":") |
paul@764 | 508 | self.control("%s-second" % name, "text", default.get_second(), maxlength=2, size=2) |
paul@764 | 509 | |
paul@764 | 510 | # Show time zone details. |
paul@764 | 511 | |
paul@764 | 512 | if show_tzid: |
paul@764 | 513 | page.add(" ") |
paul@764 | 514 | tzid = default.get_tzid() or self.get_tzid() |
paul@764 | 515 | |
paul@764 | 516 | # Show a label if read-only or a menu otherwise. |
paul@764 | 517 | |
paul@764 | 518 | if read_only: |
paul@764 | 519 | self.control("%s-tzid" % name, "hidden", tzid) |
paul@764 | 520 | page.span(tzid) |
paul@764 | 521 | else: |
paul@764 | 522 | self.timezone_menu("%s-tzid" % name, tzid, index) |
paul@764 | 523 | |
paul@764 | 524 | page.span.close() |
paul@764 | 525 | |
paul@764 | 526 | def timezone_menu(self, name, default, index=None): |
paul@764 | 527 | |
paul@764 | 528 | """ |
paul@764 | 529 | Show timezone controls using a menu with the given 'name', set to the |
paul@764 | 530 | given 'default' unless a field of the given 'name' provides a value. |
paul@764 | 531 | """ |
paul@764 | 532 | |
paul@764 | 533 | entries = [(tzid, tzid) for tzid in pytz.all_timezones] |
paul@764 | 534 | self.menu(name, default, entries, index=index) |
paul@764 | 535 | |
paul@765 | 536 | class DateTimeFormUtilities: |
paul@765 | 537 | |
paul@765 | 538 | "Date/time control methods resource mix-in." |
paul@765 | 539 | |
paul@776 | 540 | # Control naming helpers. |
paul@776 | 541 | |
paul@776 | 542 | def element_identifier(self, name, index=None): |
paul@776 | 543 | return index is not None and "%s-%d" % (name, index) or name |
paul@776 | 544 | |
paul@776 | 545 | def element_name(self, name, suffix, index=None): |
paul@776 | 546 | return index is not None and "%s-%s" % (name, suffix) or name |
paul@776 | 547 | |
paul@776 | 548 | def element_enable(self, index=None): |
paul@776 | 549 | return index is not None and str(index) or "enable" |
paul@776 | 550 | |
paul@765 | 551 | def show_object_datetime_controls(self, period, index=None): |
paul@765 | 552 | |
paul@765 | 553 | """ |
paul@765 | 554 | Show datetime-related controls if already active or if an object needs |
paul@765 | 555 | them for the given 'period'. The given 'index' is used to parameterise |
paul@765 | 556 | individual controls for dynamic manipulation. |
paul@765 | 557 | """ |
paul@765 | 558 | |
paul@765 | 559 | p = form_period_from_period(period) |
paul@765 | 560 | |
paul@765 | 561 | page = self.page |
paul@765 | 562 | args = self.env.get_args() |
paul@765 | 563 | _id = self.element_identifier |
paul@765 | 564 | _name = self.element_name |
paul@765 | 565 | _enable = self.element_enable |
paul@765 | 566 | |
paul@765 | 567 | # Add a dynamic stylesheet to permit the controls to modify the display. |
paul@765 | 568 | # NOTE: The style details need to be coordinated with the static |
paul@765 | 569 | # NOTE: stylesheet. |
paul@765 | 570 | |
paul@765 | 571 | if index is not None: |
paul@765 | 572 | page.style(type="text/css") |
paul@765 | 573 | |
paul@765 | 574 | # Unlike the rules for object properties, these affect recurrence |
paul@765 | 575 | # properties. |
paul@765 | 576 | |
paul@765 | 577 | page.add("""\ |
paul@765 | 578 | input#dttimes-enable-%(index)d, |
paul@765 | 579 | input#dtend-enable-%(index)d, |
paul@765 | 580 | input#dttimes-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue .time.enabled, |
paul@765 | 581 | input#dttimes-enable-%(index)d:checked ~ .recurrence td.objectvalue .time.disabled, |
paul@765 | 582 | input#dtend-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue.dtend .dt.enabled, |
paul@765 | 583 | input#dtend-enable-%(index)d:checked ~ .recurrence td.objectvalue.dtend .dt.disabled { |
paul@765 | 584 | display: none; |
paul@765 | 585 | }""" % {"index" : index}) |
paul@765 | 586 | |
paul@765 | 587 | page.style.close() |
paul@765 | 588 | |
paul@765 | 589 | self.control( |
paul@765 | 590 | _name("dtend-control", "recur", index), "checkbox", |
paul@765 | 591 | _enable(index), p.end_enabled, |
paul@765 | 592 | id=_id("dtend-enable", index) |
paul@765 | 593 | ) |
paul@765 | 594 | |
paul@765 | 595 | self.control( |
paul@765 | 596 | _name("dttimes-control", "recur", index), "checkbox", |
paul@765 | 597 | _enable(index), p.times_enabled, |
paul@765 | 598 | id=_id("dttimes-enable", index) |
paul@765 | 599 | ) |
paul@765 | 600 | |
paul@765 | 601 | def show_datetime_controls(self, formdate, show_start): |
paul@765 | 602 | |
paul@765 | 603 | """ |
paul@765 | 604 | Show datetime details from the current object for the 'formdate', |
paul@765 | 605 | showing start details if 'show_start' is set to a true value. Details |
paul@765 | 606 | will appear as controls for organisers and labels for attendees. |
paul@765 | 607 | """ |
paul@765 | 608 | |
paul@765 | 609 | page = self.page |
paul@765 | 610 | |
paul@765 | 611 | # Show controls for editing as organiser. |
paul@765 | 612 | |
paul@818 | 613 | if self.can_change_object(): |
paul@765 | 614 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@765 | 615 | |
paul@765 | 616 | if show_start: |
paul@765 | 617 | page.div(class_="dt enabled") |
paul@765 | 618 | self.date_controls("dtstart", formdate) |
paul@765 | 619 | page.br() |
paul@765 | 620 | page.label("Specify times", for_="dttimes-enable", class_="time disabled enable") |
paul@765 | 621 | page.label("Specify dates only", for_="dttimes-enable", class_="time enabled disable") |
paul@765 | 622 | page.div.close() |
paul@765 | 623 | |
paul@765 | 624 | else: |
paul@765 | 625 | page.div(class_="dt disabled") |
paul@765 | 626 | page.label("Specify end date", for_="dtend-enable", class_="enable") |
paul@765 | 627 | page.div.close() |
paul@765 | 628 | page.div(class_="dt enabled") |
paul@765 | 629 | self.date_controls("dtend", formdate) |
paul@765 | 630 | page.br() |
paul@765 | 631 | page.label("End on same day", for_="dtend-enable", class_="disable") |
paul@765 | 632 | page.div.close() |
paul@765 | 633 | |
paul@765 | 634 | page.td.close() |
paul@765 | 635 | |
paul@765 | 636 | # Show a label as attendee. |
paul@765 | 637 | |
paul@765 | 638 | else: |
paul@765 | 639 | dt = formdate.as_datetime() |
paul@765 | 640 | if dt: |
paul@765 | 641 | page.td(self.format_datetime(dt, "full")) |
paul@765 | 642 | else: |
paul@765 | 643 | page.td("(Unrecognised date)") |
paul@765 | 644 | |
paul@765 | 645 | def show_recurrence_controls(self, index, period, recurrenceid, recurrenceids, show_start): |
paul@765 | 646 | |
paul@765 | 647 | """ |
paul@765 | 648 | Show datetime details from the current object for the recurrence having |
paul@765 | 649 | the given 'index', with the recurrence period described by 'period', |
paul@765 | 650 | indicating a start, end and origin of the period from the event details, |
paul@765 | 651 | employing any 'recurrenceid' and 'recurrenceids' for the object to |
paul@765 | 652 | configure the displayed information. |
paul@765 | 653 | |
paul@765 | 654 | If 'show_start' is set to a true value, the start details will be shown; |
paul@765 | 655 | otherwise, the end details will be shown. |
paul@765 | 656 | """ |
paul@765 | 657 | |
paul@765 | 658 | page = self.page |
paul@765 | 659 | _id = self.element_identifier |
paul@765 | 660 | _name = self.element_name |
paul@765 | 661 | |
paul@845 | 662 | try: |
paul@845 | 663 | p = event_period_from_period(period) |
paul@845 | 664 | except PeriodError, exc: |
paul@845 | 665 | replaced = False |
paul@845 | 666 | errors = exc.args |
paul@845 | 667 | else: |
paul@845 | 668 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@845 | 669 | errors = [] |
paul@845 | 670 | |
paul@845 | 671 | period = form_period_from_period(period) |
paul@765 | 672 | |
paul@765 | 673 | # Show controls for editing as organiser. |
paul@765 | 674 | |
paul@818 | 675 | if self.can_change_object() and not replaced: |
paul@845 | 676 | error = errors and (show_start and ("dtstart", index) in errors or not show_start and ("dtend", index) in errors) and " error" or "" |
paul@845 | 677 | page.td(class_="objectvalue dt%s%s" % (show_start and "start" or "end", error)) |
paul@765 | 678 | |
paul@765 | 679 | read_only = period.origin == "RRULE" |
paul@765 | 680 | |
paul@765 | 681 | if show_start: |
paul@765 | 682 | page.div(class_="dt enabled") |
paul@845 | 683 | self.date_controls(_name("dtstart", "recur", index), period.get_form_start(), index=index, read_only=read_only) |
paul@765 | 684 | if not read_only: |
paul@765 | 685 | page.br() |
paul@765 | 686 | page.label("Specify times", for_=_id("dttimes-enable", index), class_="time disabled enable") |
paul@765 | 687 | page.label("Specify dates only", for_=_id("dttimes-enable", index), class_="time enabled disable") |
paul@765 | 688 | page.div.close() |
paul@765 | 689 | |
paul@765 | 690 | # Put the origin somewhere. |
paul@765 | 691 | |
paul@845 | 692 | self.control("recur-origin", "hidden", period.origin or "") |
paul@765 | 693 | |
paul@765 | 694 | else: |
paul@765 | 695 | page.div(class_="dt disabled") |
paul@765 | 696 | if not read_only: |
paul@765 | 697 | page.label("Specify end date", for_=_id("dtend-enable", index), class_="enable") |
paul@765 | 698 | page.div.close() |
paul@765 | 699 | page.div(class_="dt enabled") |
paul@845 | 700 | self.date_controls(_name("dtend", "recur", index), period.get_form_end(), index=index, show_tzid=False, read_only=read_only) |
paul@765 | 701 | if not read_only: |
paul@765 | 702 | page.br() |
paul@765 | 703 | page.label("End on same day", for_=_id("dtend-enable", index), class_="disable") |
paul@765 | 704 | page.div.close() |
paul@765 | 705 | |
paul@765 | 706 | page.td.close() |
paul@765 | 707 | |
paul@765 | 708 | # Show label as attendee. |
paul@765 | 709 | |
paul@765 | 710 | else: |
paul@852 | 711 | self.show_recurrence_label(index, period, recurrenceid, recurrenceids, show_start) |
paul@765 | 712 | |
paul@852 | 713 | def show_recurrence_label(self, index, period, recurrenceid, recurrenceids, show_start): |
paul@765 | 714 | |
paul@765 | 715 | """ |
paul@852 | 716 | Show datetime details from the current object for the recurrence having |
paul@852 | 717 | the given 'index', for the given recurrence 'period', employing any |
paul@765 | 718 | 'recurrenceid' and 'recurrenceids' for the object to configure the |
paul@765 | 719 | displayed information. |
paul@765 | 720 | |
paul@765 | 721 | If 'show_start' is set to a true value, the start details will be shown; |
paul@765 | 722 | otherwise, the end details will be shown. |
paul@765 | 723 | """ |
paul@765 | 724 | |
paul@765 | 725 | page = self.page |
paul@852 | 726 | _name = self.element_name |
paul@765 | 727 | |
paul@845 | 728 | try: |
paul@845 | 729 | p = event_period_from_period(period) |
paul@845 | 730 | except PeriodError, exc: |
paul@845 | 731 | replaced = False |
paul@845 | 732 | affected = False |
paul@845 | 733 | errors = exc.args |
paul@845 | 734 | else: |
paul@845 | 735 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@845 | 736 | affected = p.is_affected(recurrenceid) |
paul@845 | 737 | errors = [] |
paul@845 | 738 | |
paul@845 | 739 | period = form_period_from_period(period) |
paul@765 | 740 | |
paul@765 | 741 | css = " ".join([ |
paul@765 | 742 | replaced and "replaced" or "", |
paul@845 | 743 | affected and "affected" or "" |
paul@765 | 744 | ]) |
paul@765 | 745 | |
paul@845 | 746 | formdate = show_start and period.get_form_start() or period.get_form_end() |
paul@765 | 747 | dt = formdate.as_datetime() |
paul@765 | 748 | if dt: |
paul@852 | 749 | page.td(class_=css) |
paul@852 | 750 | if show_start: |
paul@852 | 751 | self.date_controls(_name("dtstart", "recur", index), period.get_form_start(), index=index, read_only=True) |
paul@852 | 752 | self.control("recur-origin", "hidden", period.origin or "") |
paul@852 | 753 | else: |
paul@852 | 754 | self.date_controls(_name("dtend", "recur", index), period.get_form_end(), index=index, show_tzid=False, read_only=True) |
paul@852 | 755 | page.td.close() |
paul@765 | 756 | else: |
paul@765 | 757 | page.td("(Unrecognised date)") |
paul@765 | 758 | |
paul@765 | 759 | def get_date_control_values(self, name, multiple=False, tzid_name=None): |
paul@765 | 760 | |
paul@765 | 761 | """ |
paul@787 | 762 | Return a form date object representing fields starting with 'name'. If |
paul@787 | 763 | 'multiple' is set to a true value, many date objects will be returned |
paul@787 | 764 | corresponding to a collection of datetimes. |
paul@787 | 765 | |
paul@787 | 766 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@787 | 767 | from fields starting with 'tzid_name' instead of 'name'. |
paul@765 | 768 | """ |
paul@765 | 769 | |
paul@765 | 770 | args = self.env.get_args() |
paul@765 | 771 | |
paul@765 | 772 | dates = args.get("%s-date" % name, []) |
paul@765 | 773 | hours = args.get("%s-hour" % name, []) |
paul@765 | 774 | minutes = args.get("%s-minute" % name, []) |
paul@765 | 775 | seconds = args.get("%s-second" % name, []) |
paul@765 | 776 | tzids = args.get("%s-tzid" % (tzid_name or name), []) |
paul@765 | 777 | |
paul@765 | 778 | # Handle absent values by employing None values. |
paul@765 | 779 | |
paul@765 | 780 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@765 | 781 | |
paul@765 | 782 | if not field_values and not multiple: |
paul@765 | 783 | all_values = FormDate() |
paul@765 | 784 | else: |
paul@765 | 785 | all_values = [] |
paul@765 | 786 | for date, hour, minute, second, tzid in field_values: |
paul@765 | 787 | value = FormDate(date, hour, minute, second, tzid or self.get_tzid()) |
paul@765 | 788 | |
paul@765 | 789 | # Return a single value or append to a collection of all values. |
paul@765 | 790 | |
paul@765 | 791 | if not multiple: |
paul@765 | 792 | return value |
paul@765 | 793 | else: |
paul@765 | 794 | all_values.append(value) |
paul@765 | 795 | |
paul@765 | 796 | return all_values |
paul@765 | 797 | |
paul@787 | 798 | def set_date_control_values(self, name, formdates, tzid_name=None): |
paul@787 | 799 | |
paul@787 | 800 | """ |
paul@787 | 801 | Replace form fields starting with 'name' using the values of the given |
paul@787 | 802 | 'formdates'. |
paul@787 | 803 | |
paul@787 | 804 | If 'tzid_name' is specified, the time zone information will be stored in |
paul@787 | 805 | fields starting with 'tzid_name' instead of 'name'. |
paul@787 | 806 | """ |
paul@787 | 807 | |
paul@787 | 808 | args = self.env.get_args() |
paul@787 | 809 | |
paul@787 | 810 | args["%s-date" % name] = [d.date for d in formdates] |
paul@787 | 811 | args["%s-hour" % name] = [d.hour for d in formdates] |
paul@787 | 812 | args["%s-minute" % name] = [d.minute for d in formdates] |
paul@787 | 813 | args["%s-second" % name] = [d.second for d in formdates] |
paul@787 | 814 | args["%s-tzid" % (tzid_name or name)] = [d.tzid for d in formdates] |
paul@787 | 815 | |
paul@446 | 816 | # vim: tabstop=4 expandtab shiftwidth=4 |