paul@446 | 1 | #!/usr/bin/env python |
paul@446 | 2 | |
paul@446 | 3 | """ |
paul@446 | 4 | A Web interface to a calendar event. |
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@446 | 22 | from datetime import datetime, timedelta |
paul@473 | 23 | from imiptools.client import update_attendees, update_participation |
paul@446 | 24 | from imiptools.data import get_uri, uri_dict, uri_values |
paul@446 | 25 | from imiptools.dates import format_datetime, to_date, get_datetime, \ |
paul@446 | 26 | get_datetime_item, get_period_item, \ |
paul@446 | 27 | get_start_of_day, to_timezone |
paul@446 | 28 | from imiptools.mail import Messenger |
paul@495 | 29 | from imiptools.period import have_conflict, Period |
paul@446 | 30 | from imipweb.handler import ManagerHandler |
paul@446 | 31 | from imipweb.resource import Resource |
paul@446 | 32 | import pytz |
paul@446 | 33 | |
paul@495 | 34 | class EventPeriod(Period): |
paul@495 | 35 | |
paul@495 | 36 | "A simple period plus attribute details, compatible with RecurringPeriod." |
paul@495 | 37 | |
paul@495 | 38 | def __init__(self, start, end, start_attr=None, end_attr=None): |
paul@495 | 39 | Period.__init__(self, start, end) |
paul@495 | 40 | self.start_attr = start_attr |
paul@495 | 41 | self.end_attr = end_attr |
paul@495 | 42 | |
paul@495 | 43 | def as_tuple(self): |
paul@495 | 44 | return self.start, self.end, self.start_attr, self.end_attr |
paul@495 | 45 | |
paul@495 | 46 | def __repr__(self): |
paul@495 | 47 | return "EventPeriod(%r, %r, %r, %r)" % self.as_tuple() |
paul@495 | 48 | |
paul@446 | 49 | class EventPage(Resource): |
paul@446 | 50 | |
paul@446 | 51 | "A request handler for the event page." |
paul@446 | 52 | |
paul@446 | 53 | def __init__(self, resource=None, messenger=None): |
paul@446 | 54 | Resource.__init__(self, resource) |
paul@446 | 55 | self.messenger = messenger or Messenger() |
paul@446 | 56 | |
paul@474 | 57 | # Various property values and labels. |
paul@474 | 58 | |
paul@474 | 59 | property_items = [ |
paul@474 | 60 | ("SUMMARY", "Summary"), |
paul@474 | 61 | ("DTSTART", "Start"), |
paul@474 | 62 | ("DTEND", "End"), |
paul@474 | 63 | ("ORGANIZER", "Organiser"), |
paul@474 | 64 | ("ATTENDEE", "Attendee"), |
paul@474 | 65 | ] |
paul@474 | 66 | |
paul@474 | 67 | partstat_items = [ |
paul@474 | 68 | ("NEEDS-ACTION", "Not confirmed"), |
paul@474 | 69 | ("ACCEPTED", "Attending"), |
paul@474 | 70 | ("TENTATIVE", "Tentatively attending"), |
paul@474 | 71 | ("DECLINED", "Not attending"), |
paul@474 | 72 | ("DELEGATED", "Delegated"), |
paul@474 | 73 | (None, "Not indicated"), |
paul@474 | 74 | ] |
paul@474 | 75 | |
paul@484 | 76 | def is_organiser(self, obj): |
paul@484 | 77 | return get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@484 | 78 | |
paul@446 | 79 | # Request logic methods. |
paul@446 | 80 | |
paul@446 | 81 | def handle_request(self, uid, recurrenceid, obj): |
paul@446 | 82 | |
paul@446 | 83 | """ |
paul@446 | 84 | Handle actions involving the given 'uid', 'recurrenceid', and 'obj' as |
paul@446 | 85 | the object's representation, returning an error if one occurred, or None |
paul@446 | 86 | if the request was successfully handled. |
paul@446 | 87 | """ |
paul@446 | 88 | |
paul@446 | 89 | # Handle a submitted form. |
paul@446 | 90 | |
paul@446 | 91 | args = self.env.get_args() |
paul@446 | 92 | |
paul@446 | 93 | # Get the possible actions. |
paul@446 | 94 | |
paul@446 | 95 | reply = args.has_key("reply") |
paul@446 | 96 | discard = args.has_key("discard") |
paul@446 | 97 | invite = args.has_key("invite") |
paul@446 | 98 | cancel = args.has_key("cancel") |
paul@446 | 99 | save = args.has_key("save") |
paul@446 | 100 | ignore = args.has_key("ignore") |
paul@446 | 101 | |
paul@446 | 102 | have_action = reply or discard or invite or cancel or save or ignore |
paul@446 | 103 | |
paul@446 | 104 | if not have_action: |
paul@446 | 105 | return ["action"] |
paul@446 | 106 | |
paul@446 | 107 | # If ignoring the object, return to the calendar. |
paul@446 | 108 | |
paul@446 | 109 | if ignore: |
paul@446 | 110 | self.redirect(self.env.get_path()) |
paul@446 | 111 | return None |
paul@446 | 112 | |
paul@446 | 113 | update = False |
paul@472 | 114 | |
paul@472 | 115 | # Update the object. |
paul@472 | 116 | |
paul@472 | 117 | if reply or invite or cancel or save: |
paul@472 | 118 | |
paul@473 | 119 | # Update principal event details if organiser. |
paul@473 | 120 | |
paul@484 | 121 | if self.is_organiser(obj): |
paul@473 | 122 | |
paul@473 | 123 | # Update time periods (main and recurring). |
paul@473 | 124 | |
paul@495 | 125 | period, errors = self.handle_main_period() |
paul@495 | 126 | if errors: |
paul@495 | 127 | return errors |
paul@495 | 128 | |
paul@495 | 129 | periods, errors = self.handle_recurrence_periods() |
paul@495 | 130 | if errors: |
paul@495 | 131 | return errors |
paul@495 | 132 | |
paul@495 | 133 | self.set_period_in_object(obj, period) |
paul@495 | 134 | self.set_periods_in_object(obj, periods) |
paul@472 | 135 | |
paul@473 | 136 | # Update summary. |
paul@473 | 137 | |
paul@473 | 138 | if args.has_key("summary"): |
paul@473 | 139 | obj["SUMMARY"] = [(args["summary"][0], {})] |
paul@446 | 140 | |
paul@473 | 141 | # Obtain any participants and those to be removed. |
paul@472 | 142 | |
paul@478 | 143 | attendees = self.get_attendees() |
paul@485 | 144 | removed = [attendees[int(i)] for i in args.get("remove", [])] |
paul@473 | 145 | to_cancel = update_attendees(obj, attendees, removed) |
paul@472 | 146 | |
paul@472 | 147 | # Update attendee participation. |
paul@472 | 148 | |
paul@472 | 149 | if args.has_key("partstat"): |
paul@473 | 150 | update_participation(obj, self.user, args["partstat"][0]) |
paul@446 | 151 | |
paul@446 | 152 | # Process any action. |
paul@446 | 153 | |
paul@446 | 154 | handled = True |
paul@446 | 155 | |
paul@446 | 156 | if reply or invite or cancel: |
paul@446 | 157 | |
paul@446 | 158 | handler = ManagerHandler(obj, self.user, self.messenger) |
paul@446 | 159 | |
paul@446 | 160 | # Process the object and remove it from the list of requests. |
paul@446 | 161 | |
paul@473 | 162 | if reply and handler.process_received_request(update): |
paul@473 | 163 | self.remove_request(uid, recurrenceid) |
paul@473 | 164 | |
paul@484 | 165 | elif self.is_organiser(obj) and (invite or cancel): |
paul@446 | 166 | |
paul@473 | 167 | if handler.process_created_request( |
paul@473 | 168 | invite and "REQUEST" or "CANCEL", update, to_cancel): |
paul@473 | 169 | |
paul@473 | 170 | self.remove_request(uid, recurrenceid) |
paul@446 | 171 | |
paul@446 | 172 | # Save single user events. |
paul@446 | 173 | |
paul@446 | 174 | elif save: |
paul@446 | 175 | self.store.set_event(self.user, uid, recurrenceid, node=obj.to_node()) |
paul@446 | 176 | self.update_freebusy(uid, recurrenceid, obj) |
paul@446 | 177 | self.remove_request(uid, recurrenceid) |
paul@446 | 178 | |
paul@446 | 179 | # Remove the request and the object. |
paul@446 | 180 | |
paul@446 | 181 | elif discard: |
paul@446 | 182 | self.remove_from_freebusy(uid, recurrenceid) |
paul@446 | 183 | self.remove_event(uid, recurrenceid) |
paul@446 | 184 | self.remove_request(uid, recurrenceid) |
paul@446 | 185 | |
paul@446 | 186 | else: |
paul@446 | 187 | handled = False |
paul@446 | 188 | |
paul@446 | 189 | # Upon handling an action, redirect to the main page. |
paul@446 | 190 | |
paul@446 | 191 | if handled: |
paul@446 | 192 | self.redirect(self.env.get_path()) |
paul@446 | 193 | |
paul@446 | 194 | return None |
paul@446 | 195 | |
paul@495 | 196 | def handle_main_period(self): |
paul@446 | 197 | |
paul@494 | 198 | "Return period details for the main start/end period in an event." |
paul@494 | 199 | |
paul@494 | 200 | args = self.env.get_args() |
paul@446 | 201 | |
paul@446 | 202 | dtend_enabled = args.get("dtend-control", [None])[0] |
paul@446 | 203 | dttimes_enabled = args.get("dttimes-control", [None])[0] |
paul@446 | 204 | start_values = self.get_date_control_values("dtstart") |
paul@446 | 205 | end_values = self.get_date_control_values("dtend") |
paul@446 | 206 | |
paul@446 | 207 | period, errors = self.handle_period_controls(start_values, end_values, dtend_enabled, dttimes_enabled) |
paul@446 | 208 | |
paul@446 | 209 | if errors: |
paul@446 | 210 | return None, errors |
paul@494 | 211 | else: |
paul@494 | 212 | return period, errors |
paul@446 | 213 | |
paul@495 | 214 | def handle_recurrence_periods(self): |
paul@446 | 215 | |
paul@494 | 216 | "Return period details for the recurrences specified for an event." |
paul@494 | 217 | |
paul@494 | 218 | args = self.env.get_args() |
paul@446 | 219 | |
paul@446 | 220 | all_dtend_enabled = args.get("dtend-control-recur", []) |
paul@446 | 221 | all_dttimes_enabled = args.get("dttimes-control-recur", []) |
paul@446 | 222 | all_start_values = self.get_date_control_values("dtstart-recur", multiple=True) |
paul@461 | 223 | all_end_values = self.get_date_control_values("dtend-recur", multiple=True, tzid_name="dtstart-recur") |
paul@446 | 224 | |
paul@494 | 225 | periods = [] |
paul@495 | 226 | errors = [] |
paul@494 | 227 | |
paul@446 | 228 | for index, (start_values, end_values, dtend_enabled, dttimes_enabled) in \ |
paul@446 | 229 | enumerate(map(None, all_start_values, all_end_values, all_dtend_enabled, all_dttimes_enabled)): |
paul@446 | 230 | |
paul@446 | 231 | dtend_enabled = str(index) in all_dtend_enabled |
paul@446 | 232 | dttimes_enabled = str(index) in all_dttimes_enabled |
paul@495 | 233 | period, _errors = self.handle_period_controls(start_values, end_values, dtend_enabled, dttimes_enabled, index) |
paul@446 | 234 | |
paul@446 | 235 | periods.append(period) |
paul@495 | 236 | errors += _errors |
paul@446 | 237 | |
paul@495 | 238 | return periods, errors |
paul@446 | 239 | |
paul@492 | 240 | def handle_period_controls(self, start_values, end_values, dtend_enabled, dttimes_enabled, index=None): |
paul@446 | 241 | |
paul@446 | 242 | """ |
paul@446 | 243 | Handle datetime controls for a particular period, described by the given |
paul@446 | 244 | 'start_values' and 'end_values', with 'dtend_enabled' and |
paul@446 | 245 | 'dttimes_enabled' affecting the usage of the provided values. |
paul@492 | 246 | |
paul@492 | 247 | If 'index' is specified, incorporate it into any error indicator. |
paul@446 | 248 | """ |
paul@446 | 249 | |
paul@446 | 250 | t = self.handle_date_control_values(start_values, dttimes_enabled) |
paul@446 | 251 | if t: |
paul@446 | 252 | dtstart, dtstart_attr = t |
paul@446 | 253 | else: |
paul@492 | 254 | return None, [index is not None and ("dtstart", index) or "dtstart"] |
paul@446 | 255 | |
paul@446 | 256 | # Handle specified end datetimes. |
paul@446 | 257 | |
paul@446 | 258 | if dtend_enabled: |
paul@446 | 259 | t = self.handle_date_control_values(end_values, dttimes_enabled) |
paul@446 | 260 | if t: |
paul@446 | 261 | dtend, dtend_attr = t |
paul@446 | 262 | |
paul@446 | 263 | # Convert end dates to iCalendar "next day" dates. |
paul@446 | 264 | |
paul@446 | 265 | if not isinstance(dtend, datetime): |
paul@446 | 266 | dtend += timedelta(1) |
paul@446 | 267 | else: |
paul@492 | 268 | return None, [index is not None and ("dtend", index) or "dtend"] |
paul@446 | 269 | |
paul@446 | 270 | # Otherwise, treat the end date as the start date. Datetimes are |
paul@446 | 271 | # handled by making the event occupy the rest of the day. |
paul@446 | 272 | |
paul@446 | 273 | else: |
paul@446 | 274 | dtend = dtstart + timedelta(1) |
paul@446 | 275 | dtend_attr = dtstart_attr |
paul@446 | 276 | |
paul@446 | 277 | if isinstance(dtstart, datetime): |
paul@446 | 278 | dtend = get_start_of_day(dtend, attr["TZID"]) |
paul@446 | 279 | |
paul@454 | 280 | if dtstart > dtend: |
paul@492 | 281 | return None, [ |
paul@492 | 282 | index is not None and ("dtstart", index) or "dtstart", |
paul@492 | 283 | index is not None and ("dtend", index) or "dtend" |
paul@492 | 284 | ] |
paul@446 | 285 | |
paul@495 | 286 | return EventPeriod(dtstart, dtend, dtstart_attr, dtend_attr), None |
paul@446 | 287 | |
paul@446 | 288 | def handle_date_control_values(self, values, with_time=True): |
paul@446 | 289 | |
paul@446 | 290 | """ |
paul@446 | 291 | Handle date control information for the given 'values', returning a |
paul@446 | 292 | (datetime, attr) tuple, or None if the fields cannot be used to |
paul@446 | 293 | construct a datetime object. |
paul@446 | 294 | """ |
paul@446 | 295 | |
paul@446 | 296 | if not values or not values["date"]: |
paul@446 | 297 | return None |
paul@446 | 298 | elif with_time: |
paul@446 | 299 | value = "%s%s" % (values["date"], values["time"]) |
paul@446 | 300 | attr = {"TZID" : values["tzid"], "VALUE" : "DATE-TIME"} |
paul@446 | 301 | dt = get_datetime(value, attr) |
paul@446 | 302 | else: |
paul@446 | 303 | attr = {"VALUE" : "DATE"} |
paul@446 | 304 | dt = get_datetime(values["date"]) |
paul@446 | 305 | |
paul@446 | 306 | if dt: |
paul@446 | 307 | return dt, attr |
paul@446 | 308 | |
paul@446 | 309 | return None |
paul@446 | 310 | |
paul@461 | 311 | def get_date_control_values(self, name, multiple=False, tzid_name=None): |
paul@446 | 312 | |
paul@446 | 313 | """ |
paul@446 | 314 | Return a dictionary containing date, time and tzid entries for fields |
paul@461 | 315 | starting with 'name'. If 'multiple' is set to a true value, many |
paul@461 | 316 | dictionaries will be returned corresponding to a collection of |
paul@461 | 317 | datetimes. If 'tzid_name' is specified, the time zone information will |
paul@461 | 318 | be acquired from a field starting with 'tzid_name' instead of 'name'. |
paul@446 | 319 | """ |
paul@446 | 320 | |
paul@446 | 321 | args = self.env.get_args() |
paul@446 | 322 | |
paul@446 | 323 | dates = args.get("%s-date" % name, []) |
paul@446 | 324 | hours = args.get("%s-hour" % name, []) |
paul@446 | 325 | minutes = args.get("%s-minute" % name, []) |
paul@446 | 326 | seconds = args.get("%s-second" % name, []) |
paul@461 | 327 | tzids = args.get("%s-tzid" % (tzid_name or name), []) |
paul@446 | 328 | |
paul@446 | 329 | # Handle absent values by employing None values. |
paul@446 | 330 | |
paul@446 | 331 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@446 | 332 | if not field_values and not multiple: |
paul@446 | 333 | field_values = [(None, None, None, None, None)] |
paul@446 | 334 | |
paul@446 | 335 | all_values = [] |
paul@446 | 336 | |
paul@446 | 337 | for date, hour, minute, second, tzid in field_values: |
paul@446 | 338 | |
paul@446 | 339 | # Construct a usable dictionary of values. |
paul@446 | 340 | |
paul@446 | 341 | time = (hour or minute or second) and \ |
paul@446 | 342 | "T%s%s%s" % ( |
paul@446 | 343 | (hour or "").rjust(2, "0")[:2], |
paul@446 | 344 | (minute or "").rjust(2, "0")[:2], |
paul@446 | 345 | (second or "").rjust(2, "0")[:2] |
paul@446 | 346 | ) or "" |
paul@446 | 347 | |
paul@446 | 348 | value = { |
paul@446 | 349 | "date" : date, |
paul@446 | 350 | "time" : time, |
paul@446 | 351 | "tzid" : tzid or self.get_tzid() |
paul@446 | 352 | } |
paul@446 | 353 | |
paul@446 | 354 | # Return a single value or append to a collection of all values. |
paul@446 | 355 | |
paul@446 | 356 | if not multiple: |
paul@446 | 357 | return value |
paul@446 | 358 | else: |
paul@446 | 359 | all_values.append(value) |
paul@446 | 360 | |
paul@446 | 361 | return all_values |
paul@446 | 362 | |
paul@446 | 363 | def set_period_in_object(self, obj, period): |
paul@446 | 364 | |
paul@446 | 365 | "Set in the given 'obj' the given 'period' as the main start and end." |
paul@446 | 366 | |
paul@495 | 367 | p = period |
paul@495 | 368 | result = self.set_datetime_in_object(p.start, p.start_attr and p.start_attr.get("TZID"), "DTSTART", obj) |
paul@495 | 369 | result = self.set_datetime_in_object(p.end, p.end_attr and p.end_attr.get("TZID"), "DTEND", obj) or result |
paul@457 | 370 | return result |
paul@446 | 371 | |
paul@446 | 372 | def set_periods_in_object(self, obj, periods): |
paul@446 | 373 | |
paul@446 | 374 | "Set in the given 'obj' the given 'periods'." |
paul@446 | 375 | |
paul@446 | 376 | update = False |
paul@446 | 377 | |
paul@446 | 378 | old_values = obj.get_values("RDATE") |
paul@446 | 379 | new_rdates = [] |
paul@446 | 380 | |
paul@446 | 381 | if obj.has_key("RDATE"): |
paul@446 | 382 | del obj["RDATE"] |
paul@446 | 383 | |
paul@495 | 384 | for p in periods: |
paul@495 | 385 | tzid = p.start_attr and p.start_attr.get("TZID") or p.end_attr and p.end_attr.get("TZID") |
paul@495 | 386 | new_rdates.append(get_period_item(p.start, p.end, tzid)) |
paul@446 | 387 | |
paul@446 | 388 | obj["RDATE"] = new_rdates |
paul@446 | 389 | |
paul@446 | 390 | # NOTE: To do: calculate the update status. |
paul@446 | 391 | return update |
paul@446 | 392 | |
paul@446 | 393 | def set_datetime_in_object(self, dt, tzid, property, obj): |
paul@446 | 394 | |
paul@446 | 395 | """ |
paul@446 | 396 | Set 'dt' and 'tzid' for the given 'property' in 'obj', returning whether |
paul@446 | 397 | an update has occurred. |
paul@446 | 398 | """ |
paul@446 | 399 | |
paul@446 | 400 | if dt: |
paul@446 | 401 | old_value = obj.get_value(property) |
paul@446 | 402 | obj[property] = [get_datetime_item(dt, tzid)] |
paul@446 | 403 | return format_datetime(dt) != old_value |
paul@446 | 404 | |
paul@446 | 405 | return False |
paul@446 | 406 | |
paul@446 | 407 | def get_event_period(self, obj): |
paul@446 | 408 | |
paul@446 | 409 | """ |
paul@446 | 410 | Return (dtstart, dtstart attributes), (dtend, dtend attributes) for |
paul@446 | 411 | 'obj'. |
paul@446 | 412 | """ |
paul@446 | 413 | |
paul@446 | 414 | dtstart, dtstart_attr = obj.get_datetime_item("DTSTART") |
paul@446 | 415 | if obj.has_key("DTEND"): |
paul@446 | 416 | dtend, dtend_attr = obj.get_datetime_item("DTEND") |
paul@446 | 417 | elif obj.has_key("DURATION"): |
paul@446 | 418 | duration = obj.get_duration("DURATION") |
paul@446 | 419 | dtend = dtstart + duration |
paul@446 | 420 | dtend_attr = dtstart_attr |
paul@446 | 421 | else: |
paul@446 | 422 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@446 | 423 | return (dtstart, dtstart_attr), (dtend, dtend_attr) |
paul@446 | 424 | |
paul@484 | 425 | def get_current_attendees(self, obj): |
paul@484 | 426 | |
paul@484 | 427 | """ |
paul@484 | 428 | Return attendees for 'obj' depending on whether the object is being |
paul@484 | 429 | edited. |
paul@484 | 430 | """ |
paul@484 | 431 | |
paul@484 | 432 | args = self.env.get_args() |
paul@484 | 433 | initial_load = not args.has_key("editing") |
paul@484 | 434 | |
paul@484 | 435 | if initial_load or not self.is_organiser(obj): |
paul@484 | 436 | return self.get_existing_attendees(obj) |
paul@484 | 437 | else: |
paul@484 | 438 | return self.get_attendees() |
paul@484 | 439 | |
paul@484 | 440 | def get_existing_attendees(self, obj): |
paul@484 | 441 | return uri_values(obj.get_values("ATTENDEE") or []) |
paul@484 | 442 | |
paul@478 | 443 | def get_attendees(self): |
paul@478 | 444 | |
paul@478 | 445 | """ |
paul@478 | 446 | Return attendees from the request, normalised for iCalendar purposes, |
paul@478 | 447 | and without duplicates. |
paul@478 | 448 | """ |
paul@478 | 449 | |
paul@478 | 450 | args = self.env.get_args() |
paul@478 | 451 | |
paul@478 | 452 | attendees = args.get("attendee", []) |
paul@478 | 453 | unique_attendees = set() |
paul@478 | 454 | ordered_attendees = [] |
paul@478 | 455 | |
paul@478 | 456 | for attendee in attendees: |
paul@484 | 457 | if not attendee.strip(): |
paul@484 | 458 | continue |
paul@478 | 459 | attendee = get_uri(attendee) |
paul@478 | 460 | if attendee not in unique_attendees: |
paul@478 | 461 | unique_attendees.add(attendee) |
paul@478 | 462 | ordered_attendees.append(attendee) |
paul@478 | 463 | |
paul@478 | 464 | return ordered_attendees |
paul@478 | 465 | |
paul@478 | 466 | def update_attendees(self, obj): |
paul@477 | 467 | |
paul@477 | 468 | "Add or remove attendees. This does not affect the stored object." |
paul@477 | 469 | |
paul@477 | 470 | args = self.env.get_args() |
paul@477 | 471 | |
paul@478 | 472 | attendees = self.get_attendees() |
paul@485 | 473 | existing_attendees = self.get_existing_attendees(obj) |
paul@485 | 474 | sequence = obj.get_value("SEQUENCE") |
paul@477 | 475 | |
paul@477 | 476 | if args.has_key("add"): |
paul@477 | 477 | attendees.append("") |
paul@477 | 478 | |
paul@494 | 479 | # Only actually remove attendees if the event is unsent, if the attendee |
paul@494 | 480 | # is new, or if it is the current user being removed. |
paul@485 | 481 | |
paul@477 | 482 | if args.has_key("remove"): |
paul@485 | 483 | for i in args["remove"]: |
paul@496 | 484 | try: |
paul@496 | 485 | attendee = attendees[int(i)] |
paul@496 | 486 | except IndexError: |
paul@496 | 487 | continue |
paul@496 | 488 | |
paul@485 | 489 | existing = attendee in existing_attendees |
paul@485 | 490 | |
paul@494 | 491 | if not existing or sequence is None or attendee == self.user: |
paul@485 | 492 | attendees.remove(attendee) |
paul@477 | 493 | |
paul@477 | 494 | return attendees |
paul@477 | 495 | |
paul@446 | 496 | # Page fragment methods. |
paul@446 | 497 | |
paul@446 | 498 | def show_request_controls(self, obj): |
paul@446 | 499 | |
paul@446 | 500 | "Show form controls for a request concerning 'obj'." |
paul@446 | 501 | |
paul@446 | 502 | page = self.page |
paul@446 | 503 | args = self.env.get_args() |
paul@446 | 504 | |
paul@484 | 505 | attendees = self.get_current_attendees(obj) |
paul@446 | 506 | is_attendee = self.user in attendees |
paul@446 | 507 | |
paul@446 | 508 | is_request = (obj.get_value("UID"), obj.get_value("RECURRENCE-ID")) in self._get_requests() |
paul@446 | 509 | |
paul@446 | 510 | have_other_attendees = len(attendees) > (is_attendee and 1 or 0) |
paul@446 | 511 | |
paul@446 | 512 | # Show appropriate options depending on the role of the user. |
paul@446 | 513 | |
paul@484 | 514 | if is_attendee and not self.is_organiser(obj): |
paul@446 | 515 | page.p("An action is required for this request:") |
paul@446 | 516 | |
paul@446 | 517 | page.p() |
paul@446 | 518 | page.input(name="reply", type="submit", value="Send reply") |
paul@446 | 519 | page.add(" ") |
paul@446 | 520 | page.input(name="discard", type="submit", value="Discard event") |
paul@446 | 521 | page.add(" ") |
paul@446 | 522 | page.input(name="ignore", type="submit", value="Do nothing for now") |
paul@446 | 523 | page.p.close() |
paul@446 | 524 | |
paul@484 | 525 | if self.is_organiser(obj): |
paul@446 | 526 | page.p("As organiser, you can perform the following:") |
paul@446 | 527 | |
paul@446 | 528 | if have_other_attendees: |
paul@446 | 529 | page.p() |
paul@446 | 530 | page.input(name="invite", type="submit", value="Invite/notify attendees") |
paul@446 | 531 | page.add(" ") |
paul@446 | 532 | if is_request: |
paul@446 | 533 | page.input(name="discard", type="submit", value="Discard event") |
paul@446 | 534 | else: |
paul@446 | 535 | page.input(name="cancel", type="submit", value="Cancel event") |
paul@446 | 536 | page.add(" ") |
paul@446 | 537 | page.input(name="ignore", type="submit", value="Do nothing for now") |
paul@446 | 538 | page.p.close() |
paul@446 | 539 | else: |
paul@446 | 540 | page.p() |
paul@446 | 541 | page.input(name="save", type="submit", value="Save event") |
paul@446 | 542 | page.add(" ") |
paul@446 | 543 | page.input(name="discard", type="submit", value="Discard event") |
paul@446 | 544 | page.add(" ") |
paul@446 | 545 | page.input(name="ignore", type="submit", value="Do nothing for now") |
paul@446 | 546 | page.p.close() |
paul@446 | 547 | |
paul@492 | 548 | def show_object_on_page(self, uid, obj, errors=None): |
paul@446 | 549 | |
paul@446 | 550 | """ |
paul@446 | 551 | Show the calendar object with the given 'uid' and representation 'obj' |
paul@492 | 552 | on the current page. If 'errors' is given, show a suitable message for |
paul@492 | 553 | the different errors provided. |
paul@446 | 554 | """ |
paul@446 | 555 | |
paul@446 | 556 | page = self.page |
paul@446 | 557 | page.form(method="POST") |
paul@446 | 558 | |
paul@446 | 559 | page.input(name="editing", type="hidden", value="true") |
paul@446 | 560 | |
paul@446 | 561 | args = self.env.get_args() |
paul@446 | 562 | |
paul@487 | 563 | # Obtain basic event information, generating any necessary editing controls. |
paul@446 | 564 | |
paul@473 | 565 | initial_load = not args.has_key("editing") |
paul@446 | 566 | |
paul@484 | 567 | if initial_load or not self.is_organiser(obj): |
paul@484 | 568 | attendees = self.get_existing_attendees(obj) |
paul@484 | 569 | else: |
paul@484 | 570 | attendees = self.update_attendees(obj) |
paul@446 | 571 | |
paul@446 | 572 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.get_event_period(obj) |
paul@446 | 573 | self.show_object_datetime_controls(dtstart, dtend) |
paul@446 | 574 | |
paul@487 | 575 | # Obtain any separate recurrences for this event. |
paul@487 | 576 | |
paul@487 | 577 | recurrenceid = format_datetime(obj.get_utc_datetime("RECURRENCE-ID")) |
paul@487 | 578 | recurrenceids = self._get_recurrences(uid) |
paul@487 | 579 | start_utc = format_datetime(to_timezone(dtstart, "UTC")) |
paul@487 | 580 | replaced = not recurrenceid and recurrenceids and start_utc in recurrenceids |
paul@487 | 581 | |
paul@446 | 582 | # Provide a summary of the object. |
paul@446 | 583 | |
paul@446 | 584 | page.table(class_="object", cellspacing=5, cellpadding=5) |
paul@446 | 585 | page.thead() |
paul@446 | 586 | page.tr() |
paul@446 | 587 | page.th("Event", class_="mainheading", colspan=2) |
paul@446 | 588 | page.tr.close() |
paul@446 | 589 | page.thead.close() |
paul@446 | 590 | page.tbody() |
paul@446 | 591 | |
paul@446 | 592 | for name, label in self.property_items: |
paul@446 | 593 | field = name.lower() |
paul@446 | 594 | |
paul@446 | 595 | items = obj.get_items(name) or [] |
paul@446 | 596 | rowspan = len(items) |
paul@446 | 597 | |
paul@446 | 598 | if name == "ATTENDEE": |
paul@473 | 599 | rowspan = len(attendees) + 1 # for the add button |
paul@446 | 600 | elif not items: |
paul@446 | 601 | continue |
paul@446 | 602 | |
paul@446 | 603 | page.tr() |
paul@492 | 604 | page.th(label, class_="objectheading %s%s" % (field, errors and field in errors and " error" or ""), rowspan=rowspan) |
paul@446 | 605 | |
paul@446 | 606 | # Handle datetimes specially. |
paul@446 | 607 | |
paul@446 | 608 | if name in ["DTSTART", "DTEND"]: |
paul@487 | 609 | if not replaced: |
paul@446 | 610 | |
paul@487 | 611 | # Obtain the datetime. |
paul@487 | 612 | |
paul@487 | 613 | if name == "DTSTART": |
paul@487 | 614 | dt, attr = dtstart, dtstart_attr |
paul@446 | 615 | |
paul@487 | 616 | # Where no end datetime exists, use the start datetime as the |
paul@487 | 617 | # basis of any potential datetime specified if dt-control is |
paul@487 | 618 | # set. |
paul@487 | 619 | |
paul@487 | 620 | else: |
paul@487 | 621 | dt, attr = dtend or dtstart, dtend_attr or dtstart_attr |
paul@446 | 622 | |
paul@487 | 623 | self.show_datetime_controls(obj, dt, attr, name == "DTSTART") |
paul@446 | 624 | |
paul@487 | 625 | elif name == "DTSTART": |
paul@487 | 626 | page.td(class_="objectvalue %s replaced" % field, rowspan=2) |
paul@487 | 627 | page.a("First occurrence replaced by a separate event", href=self.link_to(uid, start_utc)) |
paul@487 | 628 | page.td.close() |
paul@446 | 629 | |
paul@446 | 630 | page.tr.close() |
paul@446 | 631 | |
paul@446 | 632 | # Handle the summary specially. |
paul@446 | 633 | |
paul@446 | 634 | elif name == "SUMMARY": |
paul@446 | 635 | value = args.get("summary", [obj.get_value(name)])[0] |
paul@446 | 636 | |
paul@490 | 637 | page.td(class_="objectvalue summary") |
paul@484 | 638 | if self.is_organiser(obj): |
paul@446 | 639 | page.input(name="summary", type="text", value=value, size=80) |
paul@446 | 640 | else: |
paul@446 | 641 | page.add(value) |
paul@446 | 642 | page.td.close() |
paul@446 | 643 | page.tr.close() |
paul@446 | 644 | |
paul@473 | 645 | # Handle attendees specially. |
paul@473 | 646 | |
paul@473 | 647 | elif name == "ATTENDEE": |
paul@473 | 648 | attendee_map = dict(items) |
paul@473 | 649 | first = True |
paul@473 | 650 | |
paul@473 | 651 | for i, value in enumerate(attendees): |
paul@473 | 652 | if not first: |
paul@473 | 653 | page.tr() |
paul@473 | 654 | else: |
paul@473 | 655 | first = False |
paul@473 | 656 | |
paul@479 | 657 | # Obtain details of attendees to supply attributes. |
paul@473 | 658 | |
paul@479 | 659 | self.show_attendee(obj, i, value, attendee_map.get(value)) |
paul@473 | 660 | page.tr.close() |
paul@473 | 661 | |
paul@473 | 662 | # Allow more attendees to be specified. |
paul@473 | 663 | |
paul@484 | 664 | if self.is_organiser(obj): |
paul@473 | 665 | if not first: |
paul@473 | 666 | page.tr() |
paul@473 | 667 | |
paul@473 | 668 | page.td() |
paul@496 | 669 | page.input(name="add", type="submit", value="add", id="add", class_="add") |
paul@496 | 670 | page.label("Add attendee", for_="add", class_="add") |
paul@473 | 671 | page.td.close() |
paul@473 | 672 | page.tr.close() |
paul@473 | 673 | |
paul@473 | 674 | # Handle potentially many values of other kinds. |
paul@446 | 675 | |
paul@446 | 676 | else: |
paul@446 | 677 | first = True |
paul@446 | 678 | |
paul@446 | 679 | for i, (value, attr) in enumerate(items): |
paul@446 | 680 | if not first: |
paul@446 | 681 | page.tr() |
paul@446 | 682 | else: |
paul@446 | 683 | first = False |
paul@446 | 684 | |
paul@490 | 685 | page.td(class_="objectvalue %s" % field) |
paul@473 | 686 | page.add(value) |
paul@446 | 687 | page.td.close() |
paul@446 | 688 | page.tr.close() |
paul@446 | 689 | |
paul@446 | 690 | page.tbody.close() |
paul@446 | 691 | page.table.close() |
paul@446 | 692 | |
paul@492 | 693 | self.show_recurrences(obj, errors) |
paul@446 | 694 | self.show_conflicting_events(uid, obj) |
paul@446 | 695 | self.show_request_controls(obj) |
paul@446 | 696 | |
paul@446 | 697 | page.form.close() |
paul@446 | 698 | |
paul@479 | 699 | def show_attendee(self, obj, i, attendee, attendee_attr): |
paul@479 | 700 | |
paul@479 | 701 | """ |
paul@479 | 702 | For the given object 'obj', show the attendee in position 'i' with the |
paul@479 | 703 | given 'attendee' value, having 'attendee_attr' as any stored attributes. |
paul@479 | 704 | """ |
paul@479 | 705 | |
paul@479 | 706 | page = self.page |
paul@479 | 707 | args = self.env.get_args() |
paul@479 | 708 | |
paul@479 | 709 | existing = attendee_attr is not None |
paul@479 | 710 | partstat = attendee_attr and attendee_attr.get("PARTSTAT") |
paul@485 | 711 | sequence = obj.get_value("SEQUENCE") |
paul@479 | 712 | |
paul@479 | 713 | page.td(class_="objectvalue") |
paul@479 | 714 | |
paul@479 | 715 | # Show a form control as organiser for new attendees. |
paul@479 | 716 | |
paul@485 | 717 | if self.is_organiser(obj) and (not existing or sequence is None): |
paul@479 | 718 | page.input(name="attendee", type="value", value=attendee, size="40") |
paul@479 | 719 | else: |
paul@479 | 720 | page.input(name="attendee", type="hidden", value=attendee) |
paul@479 | 721 | page.add(attendee) |
paul@479 | 722 | page.add(" ") |
paul@479 | 723 | |
paul@479 | 724 | # Show participation status, editable for the current user. |
paul@479 | 725 | |
paul@479 | 726 | if attendee == self.user: |
paul@479 | 727 | self._show_menu("partstat", partstat, self.partstat_items, "partstat") |
paul@479 | 728 | |
paul@479 | 729 | # Allow the participation indicator to act as a submit |
paul@479 | 730 | # button in order to refresh the page and show a control for |
paul@479 | 731 | # the current user, if indicated. |
paul@479 | 732 | |
paul@484 | 733 | elif self.is_organiser(obj) and not existing: |
paul@479 | 734 | page.input(name="partstat-refresh", type="submit", value="refresh", id="partstat-%d" % i, class_="refresh") |
paul@479 | 735 | page.label(dict(self.partstat_items).get(partstat, ""), for_="partstat-%s" % i, class_="partstat") |
paul@479 | 736 | else: |
paul@479 | 737 | page.span(dict(self.partstat_items).get(partstat, ""), class_="partstat") |
paul@479 | 738 | |
paul@479 | 739 | # Permit organisers to remove attendees. |
paul@479 | 740 | |
paul@484 | 741 | if self.is_organiser(obj): |
paul@479 | 742 | |
paul@479 | 743 | # Permit the removal of newly-added attendees. |
paul@479 | 744 | |
paul@485 | 745 | remove_type = (not existing or sequence is None or attendee == self.user) and "submit" or "checkbox" |
paul@479 | 746 | |
paul@485 | 747 | self._control("remove", remove_type, str(i), str(i) in args.get("remove", []), id="remove-%d" % i, class_="remove") |
paul@479 | 748 | |
paul@479 | 749 | page.label("Remove", for_="remove-%d" % i, class_="remove") |
paul@479 | 750 | page.label("Uninvited", for_="remove-%d" % i, class_="removed") |
paul@479 | 751 | |
paul@479 | 752 | page.td.close() |
paul@479 | 753 | |
paul@492 | 754 | def show_recurrences(self, obj, errors=None): |
paul@446 | 755 | |
paul@492 | 756 | """ |
paul@492 | 757 | Show recurrences for the object having the given representation 'obj'. |
paul@492 | 758 | If 'errors' is given, show a suitable message for the different errors |
paul@492 | 759 | provided. |
paul@492 | 760 | """ |
paul@446 | 761 | |
paul@446 | 762 | page = self.page |
paul@446 | 763 | |
paul@446 | 764 | # Obtain any parent object if this object is a specific recurrence. |
paul@446 | 765 | |
paul@446 | 766 | uid = obj.get_value("UID") |
paul@446 | 767 | recurrenceid = format_datetime(obj.get_utc_datetime("RECURRENCE-ID")) |
paul@446 | 768 | |
paul@446 | 769 | if recurrenceid: |
paul@489 | 770 | parent = self._get_object(uid) |
paul@489 | 771 | if not parent: |
paul@446 | 772 | return |
paul@446 | 773 | |
paul@489 | 774 | page.p() |
paul@489 | 775 | page.a("This event modifies a recurring event.", href=self.link_to(uid)) |
paul@489 | 776 | page.p.close() |
paul@446 | 777 | |
paul@446 | 778 | # Obtain the periods associated with the event in the user's time zone. |
paul@446 | 779 | |
paul@458 | 780 | periods = obj.get_periods(self.get_tzid(), self.get_window_end()) |
paul@446 | 781 | recurrenceids = self._get_recurrences(uid) |
paul@446 | 782 | |
paul@446 | 783 | if len(periods) == 1: |
paul@446 | 784 | return |
paul@446 | 785 | |
paul@484 | 786 | if self.is_organiser(obj): |
paul@446 | 787 | page.p("This event recurs on the following occasions within the next %d days:" % self.get_window_size()) |
paul@446 | 788 | else: |
paul@446 | 789 | page.p("This event occurs on the following occasions within the next %d days:" % self.get_window_size()) |
paul@446 | 790 | |
paul@446 | 791 | # Determine whether any periods are explicitly created or are part of a |
paul@446 | 792 | # rule. |
paul@446 | 793 | |
paul@458 | 794 | explicit_periods = filter(lambda p: p.origin != "RRULE", periods) |
paul@446 | 795 | |
paul@446 | 796 | # Show each recurrence in a separate table if editable. |
paul@446 | 797 | |
paul@484 | 798 | if self.is_organiser(obj) and explicit_periods: |
paul@446 | 799 | |
paul@458 | 800 | for index, p in enumerate(periods[1:]): |
paul@446 | 801 | |
paul@446 | 802 | # Isolate the controls from neighbouring tables. |
paul@446 | 803 | |
paul@446 | 804 | page.div() |
paul@446 | 805 | |
paul@458 | 806 | self.show_object_datetime_controls(p.start, p.end, index) |
paul@446 | 807 | |
paul@446 | 808 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@446 | 809 | page.caption("Occurrence") |
paul@446 | 810 | page.tbody() |
paul@446 | 811 | page.tr() |
paul@492 | 812 | error = errors and ("dtstart", index) in errors and " error" or "" |
paul@492 | 813 | page.th("Start", class_="objectheading start%s" % error) |
paul@494 | 814 | self.show_recurrence_controls(obj, index, p, recurrenceid, recurrenceids, True) |
paul@446 | 815 | page.tr.close() |
paul@446 | 816 | page.tr() |
paul@492 | 817 | error = errors and ("dtend", index) in errors and " error" or "" |
paul@492 | 818 | page.th("End", class_="objectheading end%s" % error) |
paul@494 | 819 | self.show_recurrence_controls(obj, index, p, recurrenceid, recurrenceids, False) |
paul@446 | 820 | page.tr.close() |
paul@446 | 821 | page.tbody.close() |
paul@446 | 822 | page.table.close() |
paul@446 | 823 | |
paul@446 | 824 | page.div.close() |
paul@446 | 825 | |
paul@446 | 826 | # Otherwise, use a compact single table. |
paul@446 | 827 | |
paul@446 | 828 | else: |
paul@446 | 829 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@446 | 830 | page.caption("Occurrences") |
paul@446 | 831 | page.thead() |
paul@446 | 832 | page.tr() |
paul@446 | 833 | page.th("Start", class_="objectheading start") |
paul@446 | 834 | page.th("End", class_="objectheading end") |
paul@446 | 835 | page.tr.close() |
paul@446 | 836 | page.thead.close() |
paul@446 | 837 | page.tbody() |
paul@446 | 838 | |
paul@446 | 839 | # Show only subsequent periods if organiser, since the principal |
paul@446 | 840 | # period will be the start and end datetimes. |
paul@446 | 841 | |
paul@484 | 842 | for index, p in enumerate(self.is_organiser(obj) and periods[1:] or periods): |
paul@446 | 843 | page.tr() |
paul@494 | 844 | self.show_recurrence_controls(obj, index, p, recurrenceid, recurrenceids, True) |
paul@494 | 845 | self.show_recurrence_controls(obj, index, p, recurrenceid, recurrenceids, False) |
paul@446 | 846 | page.tr.close() |
paul@484 | 847 | |
paul@446 | 848 | page.tbody.close() |
paul@446 | 849 | page.table.close() |
paul@446 | 850 | |
paul@446 | 851 | def show_conflicting_events(self, uid, obj): |
paul@446 | 852 | |
paul@446 | 853 | """ |
paul@446 | 854 | Show conflicting events for the object having the given 'uid' and |
paul@446 | 855 | representation 'obj'. |
paul@446 | 856 | """ |
paul@446 | 857 | |
paul@446 | 858 | page = self.page |
paul@484 | 859 | recurrenceid = format_datetime(obj.get_utc_datetime("RECURRENCE-ID")) |
paul@446 | 860 | |
paul@446 | 861 | # Obtain the user's timezone. |
paul@446 | 862 | |
paul@446 | 863 | tzid = self.get_tzid() |
paul@446 | 864 | periods = obj.get_periods_for_freebusy(self.get_tzid(), self.get_window_end()) |
paul@446 | 865 | |
paul@446 | 866 | # Indicate whether there are conflicting events. |
paul@446 | 867 | |
paul@484 | 868 | conflicts = [] |
paul@446 | 869 | |
paul@484 | 870 | for participant in self.get_current_attendees(obj): |
paul@484 | 871 | if participant == self.user: |
paul@484 | 872 | freebusy = self.store.get_freebusy(participant) |
paul@484 | 873 | else: |
paul@484 | 874 | freebusy = self.store.get_freebusy_for_other(self.user, participant) |
paul@484 | 875 | |
paul@484 | 876 | if not freebusy: |
paul@484 | 877 | continue |
paul@446 | 878 | |
paul@446 | 879 | # Obtain any time zone details from the suggested event. |
paul@446 | 880 | |
paul@446 | 881 | _dtstart, attr = obj.get_item("DTSTART") |
paul@446 | 882 | tzid = attr.get("TZID", tzid) |
paul@446 | 883 | |
paul@484 | 884 | # Show any conflicts with periods of actual attendance. |
paul@446 | 885 | |
paul@484 | 886 | for p in have_conflict(freebusy, periods, True): |
paul@484 | 887 | if (p.uid != uid or p.recurrenceid != recurrenceid) and p.transp != "ORG": |
paul@484 | 888 | conflicts.append(p) |
paul@446 | 889 | |
paul@484 | 890 | conflicts.sort() |
paul@484 | 891 | |
paul@484 | 892 | # Show any conflicts with periods of actual attendance. |
paul@446 | 893 | |
paul@484 | 894 | if conflicts: |
paul@484 | 895 | page.p("This event conflicts with others:") |
paul@446 | 896 | |
paul@484 | 897 | page.table(cellspacing=5, cellpadding=5, class_="conflicts") |
paul@484 | 898 | page.thead() |
paul@484 | 899 | page.tr() |
paul@484 | 900 | page.th("Event") |
paul@484 | 901 | page.th("Start") |
paul@484 | 902 | page.th("End") |
paul@484 | 903 | page.tr.close() |
paul@484 | 904 | page.thead.close() |
paul@484 | 905 | page.tbody() |
paul@446 | 906 | |
paul@484 | 907 | for p in conflicts: |
paul@484 | 908 | |
paul@484 | 909 | # Provide details of any conflicting event. |
paul@446 | 910 | |
paul@484 | 911 | start = self.format_datetime(to_timezone(get_datetime(p.start), tzid), "long") |
paul@484 | 912 | end = self.format_datetime(to_timezone(get_datetime(p.end), tzid), "long") |
paul@446 | 913 | |
paul@484 | 914 | page.tr() |
paul@446 | 915 | |
paul@484 | 916 | # Show the event summary for the conflicting event. |
paul@446 | 917 | |
paul@484 | 918 | page.td() |
paul@484 | 919 | if p.summary: |
paul@488 | 920 | page.a(p.summary, href=self.link_to(p.uid, p.recurrenceid)) |
paul@484 | 921 | else: |
paul@484 | 922 | page.add("(Unspecified event)") |
paul@484 | 923 | page.td.close() |
paul@446 | 924 | |
paul@484 | 925 | page.td(start) |
paul@484 | 926 | page.td(end) |
paul@446 | 927 | |
paul@484 | 928 | page.tr.close() |
paul@446 | 929 | |
paul@484 | 930 | page.tbody.close() |
paul@484 | 931 | page.table.close() |
paul@446 | 932 | |
paul@474 | 933 | # Generation of controls within page fragments. |
paul@474 | 934 | |
paul@474 | 935 | def show_object_datetime_controls(self, start, end, index=None): |
paul@474 | 936 | |
paul@474 | 937 | """ |
paul@474 | 938 | Show datetime-related controls if already active or if an object needs |
paul@474 | 939 | them for the given 'start' to 'end' period. The given 'index' is used to |
paul@474 | 940 | parameterise individual controls for dynamic manipulation. |
paul@474 | 941 | """ |
paul@474 | 942 | |
paul@474 | 943 | page = self.page |
paul@474 | 944 | args = self.env.get_args() |
paul@474 | 945 | sn = self._suffixed_name |
paul@474 | 946 | ssn = self._simple_suffixed_name |
paul@474 | 947 | |
paul@474 | 948 | # Add a dynamic stylesheet to permit the controls to modify the display. |
paul@474 | 949 | # NOTE: The style details need to be coordinated with the static |
paul@474 | 950 | # NOTE: stylesheet. |
paul@474 | 951 | |
paul@474 | 952 | if index is not None: |
paul@474 | 953 | page.style(type="text/css") |
paul@474 | 954 | |
paul@474 | 955 | # Unlike the rules for object properties, these affect recurrence |
paul@474 | 956 | # properties. |
paul@474 | 957 | |
paul@474 | 958 | page.add("""\ |
paul@474 | 959 | input#dttimes-enable-%(index)d, |
paul@474 | 960 | input#dtend-enable-%(index)d, |
paul@474 | 961 | input#dttimes-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue .time.enabled, |
paul@474 | 962 | input#dttimes-enable-%(index)d:checked ~ .recurrence td.objectvalue .time.disabled, |
paul@474 | 963 | input#dtend-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue.dtend .dt.enabled, |
paul@474 | 964 | input#dtend-enable-%(index)d:checked ~ .recurrence td.objectvalue.dtend .dt.disabled { |
paul@474 | 965 | display: none; |
paul@474 | 966 | }""" % {"index" : index}) |
paul@474 | 967 | |
paul@474 | 968 | page.style.close() |
paul@474 | 969 | |
paul@474 | 970 | dtend_control = args.get(ssn("dtend-control", "recur", index), []) |
paul@474 | 971 | dttimes_control = args.get(ssn("dttimes-control", "recur", index), []) |
paul@474 | 972 | |
paul@474 | 973 | dtend_enabled = index is not None and str(index) in dtend_control or index is None and dtend_control |
paul@474 | 974 | dttimes_enabled = index is not None and str(index) in dttimes_control or index is None and dttimes_control |
paul@474 | 975 | |
paul@474 | 976 | initial_load = not args.has_key("editing") |
paul@474 | 977 | |
paul@474 | 978 | dtend_enabled = dtend_enabled or initial_load and (isinstance(end, datetime) or start != end - timedelta(1)) |
paul@474 | 979 | dttimes_enabled = dttimes_enabled or initial_load and (isinstance(start, datetime) or isinstance(end, datetime)) |
paul@474 | 980 | |
paul@474 | 981 | self._control( |
paul@474 | 982 | ssn("dtend-control", "recur", index), "checkbox", |
paul@474 | 983 | index is not None and str(index) or "enable", dtend_enabled, |
paul@474 | 984 | id=sn("dtend-enable", index) |
paul@474 | 985 | ) |
paul@474 | 986 | |
paul@474 | 987 | self._control( |
paul@474 | 988 | ssn("dttimes-control", "recur", index), "checkbox", |
paul@474 | 989 | index is not None and str(index) or "enable", dttimes_enabled, |
paul@474 | 990 | id=sn("dttimes-enable", index) |
paul@474 | 991 | ) |
paul@474 | 992 | |
paul@474 | 993 | def show_datetime_controls(self, obj, dt, attr, show_start): |
paul@474 | 994 | |
paul@474 | 995 | """ |
paul@474 | 996 | Show datetime details from the given 'obj' for the datetime 'dt' and |
paul@474 | 997 | attributes 'attr', showing start details if 'show_start' is set |
paul@474 | 998 | to a true value. Details will appear as controls for organisers and |
paul@474 | 999 | labels for attendees. |
paul@474 | 1000 | """ |
paul@474 | 1001 | |
paul@474 | 1002 | page = self.page |
paul@474 | 1003 | |
paul@474 | 1004 | # Change end dates to refer to the actual dates, not the iCalendar |
paul@474 | 1005 | # "next day" dates. |
paul@474 | 1006 | |
paul@474 | 1007 | if not show_start and not isinstance(dt, datetime): |
paul@474 | 1008 | dt -= timedelta(1) |
paul@474 | 1009 | |
paul@474 | 1010 | # Show controls for editing as organiser. |
paul@474 | 1011 | |
paul@484 | 1012 | if self.is_organiser(obj): |
paul@474 | 1013 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@474 | 1014 | |
paul@474 | 1015 | if show_start: |
paul@474 | 1016 | page.div(class_="dt enabled") |
paul@474 | 1017 | self._show_date_controls("dtstart", dt, attr.get("TZID")) |
paul@474 | 1018 | page.br() |
paul@474 | 1019 | page.label("Specify times", for_="dttimes-enable", class_="time disabled enable") |
paul@474 | 1020 | page.label("Specify dates only", for_="dttimes-enable", class_="time enabled disable") |
paul@474 | 1021 | page.div.close() |
paul@474 | 1022 | |
paul@474 | 1023 | else: |
paul@474 | 1024 | page.div(class_="dt disabled") |
paul@474 | 1025 | page.label("Specify end date", for_="dtend-enable", class_="enable") |
paul@474 | 1026 | page.div.close() |
paul@474 | 1027 | page.div(class_="dt enabled") |
paul@474 | 1028 | self._show_date_controls("dtend", dt, attr.get("TZID")) |
paul@474 | 1029 | page.br() |
paul@474 | 1030 | page.label("End on same day", for_="dtend-enable", class_="disable") |
paul@474 | 1031 | page.div.close() |
paul@474 | 1032 | |
paul@474 | 1033 | page.td.close() |
paul@474 | 1034 | |
paul@474 | 1035 | # Show a label as attendee. |
paul@474 | 1036 | |
paul@474 | 1037 | else: |
paul@474 | 1038 | page.td(self.format_datetime(dt, "full")) |
paul@474 | 1039 | |
paul@494 | 1040 | def show_recurrence_controls(self, obj, index, period, recurrenceid, recurrenceids, show_start): |
paul@474 | 1041 | |
paul@474 | 1042 | """ |
paul@474 | 1043 | Show datetime details from the given 'obj' for the recurrence having the |
paul@494 | 1044 | given 'index', with the recurrence period described by 'period', |
paul@494 | 1045 | indicating a start, end and origin of the period from the event details, |
paul@494 | 1046 | employing any 'recurrenceid' and 'recurrenceids' for the object to |
paul@494 | 1047 | configure the displayed information. |
paul@474 | 1048 | |
paul@474 | 1049 | If 'show_start' is set to a true value, the start details will be shown; |
paul@474 | 1050 | otherwise, the end details will be shown. |
paul@474 | 1051 | """ |
paul@474 | 1052 | |
paul@474 | 1053 | page = self.page |
paul@474 | 1054 | sn = self._suffixed_name |
paul@474 | 1055 | ssn = self._simple_suffixed_name |
paul@494 | 1056 | p = period |
paul@474 | 1057 | |
paul@474 | 1058 | # Change end dates to refer to the actual dates, not the iCalendar |
paul@474 | 1059 | # "next day" dates. |
paul@474 | 1060 | |
paul@494 | 1061 | if not isinstance(p.end, datetime): |
paul@494 | 1062 | p.end -= timedelta(1) |
paul@474 | 1063 | |
paul@494 | 1064 | start_utc = format_datetime(to_timezone(p.start, "UTC")) |
paul@474 | 1065 | replaced = recurrenceids and start_utc in recurrenceids and "replaced" or "" |
paul@474 | 1066 | css = " ".join([ |
paul@474 | 1067 | replaced, |
paul@474 | 1068 | recurrenceid and start_utc == recurrenceid and "affected" or "" |
paul@474 | 1069 | ]) |
paul@474 | 1070 | |
paul@474 | 1071 | # Show controls for editing as organiser. |
paul@474 | 1072 | |
paul@494 | 1073 | if self.is_organiser(obj) and not replaced and p.origin != "RRULE": |
paul@474 | 1074 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@474 | 1075 | |
paul@474 | 1076 | if show_start: |
paul@474 | 1077 | page.div(class_="dt enabled") |
paul@494 | 1078 | self._show_date_controls(ssn("dtstart", "recur", index), p.start, p.start_attr.get("TZID"), index=index) |
paul@474 | 1079 | page.br() |
paul@474 | 1080 | page.label("Specify times", for_=sn("dttimes-enable", index), class_="time disabled enable") |
paul@474 | 1081 | page.label("Specify dates only", for_=sn("dttimes-enable", index), class_="time enabled disable") |
paul@474 | 1082 | page.div.close() |
paul@474 | 1083 | |
paul@474 | 1084 | else: |
paul@474 | 1085 | page.div(class_="dt disabled") |
paul@474 | 1086 | page.label("Specify end date", for_=sn("dtend-enable", index), class_="enable") |
paul@474 | 1087 | page.div.close() |
paul@474 | 1088 | page.div(class_="dt enabled") |
paul@494 | 1089 | self._show_date_controls(ssn("dtend", "recur", index), p.end, index=index, show_tzid=False) |
paul@474 | 1090 | page.br() |
paul@474 | 1091 | page.label("End on same day", for_=sn("dtend-enable", index), class_="disable") |
paul@474 | 1092 | page.div.close() |
paul@474 | 1093 | |
paul@474 | 1094 | page.td.close() |
paul@474 | 1095 | |
paul@474 | 1096 | # Show label as attendee. |
paul@474 | 1097 | |
paul@474 | 1098 | else: |
paul@494 | 1099 | page.td(self.format_datetime(show_start and p.start or p.end, "long"), class_=css) |
paul@474 | 1100 | |
paul@446 | 1101 | # Full page output methods. |
paul@446 | 1102 | |
paul@446 | 1103 | def show(self, path_info): |
paul@446 | 1104 | |
paul@446 | 1105 | "Show an object request using the given 'path_info' for the current user." |
paul@446 | 1106 | |
paul@446 | 1107 | uid, recurrenceid = self._get_identifiers(path_info) |
paul@446 | 1108 | obj = self._get_object(uid, recurrenceid) |
paul@446 | 1109 | |
paul@446 | 1110 | if not obj: |
paul@446 | 1111 | return False |
paul@446 | 1112 | |
paul@492 | 1113 | errors = self.handle_request(uid, recurrenceid, obj) |
paul@446 | 1114 | |
paul@492 | 1115 | if not errors: |
paul@446 | 1116 | return True |
paul@446 | 1117 | |
paul@446 | 1118 | self.new_page(title="Event") |
paul@492 | 1119 | self.show_object_on_page(uid, obj, errors) |
paul@446 | 1120 | |
paul@446 | 1121 | return True |
paul@446 | 1122 | |
paul@446 | 1123 | # Utility methods. |
paul@446 | 1124 | |
paul@474 | 1125 | def _control(self, name, type, value, selected, **kw): |
paul@474 | 1126 | |
paul@474 | 1127 | """ |
paul@474 | 1128 | Show a control with the given 'name', 'type' and 'value', with |
paul@474 | 1129 | 'selected' indicating whether it should be selected (checked or |
paul@474 | 1130 | equivalent), and with keyword arguments setting other properties. |
paul@474 | 1131 | """ |
paul@474 | 1132 | |
paul@474 | 1133 | page = self.page |
paul@474 | 1134 | if selected: |
paul@474 | 1135 | page.input(name=name, type=type, value=value, checked=selected, **kw) |
paul@474 | 1136 | else: |
paul@474 | 1137 | page.input(name=name, type=type, value=value, **kw) |
paul@474 | 1138 | |
paul@446 | 1139 | def _show_menu(self, name, default, items, class_="", index=None): |
paul@446 | 1140 | |
paul@446 | 1141 | """ |
paul@446 | 1142 | Show a select menu having the given 'name', set to the given 'default', |
paul@446 | 1143 | providing the given (value, label) 'items', and employing the given CSS |
paul@446 | 1144 | 'class_' if specified. |
paul@446 | 1145 | """ |
paul@446 | 1146 | |
paul@446 | 1147 | page = self.page |
paul@446 | 1148 | values = self.env.get_args().get(name, [default]) |
paul@446 | 1149 | if index is not None: |
paul@446 | 1150 | values = values[index:] |
paul@446 | 1151 | values = values and values[0:1] or [default] |
paul@446 | 1152 | |
paul@446 | 1153 | page.select(name=name, class_=class_) |
paul@446 | 1154 | for v, label in items: |
paul@446 | 1155 | if v is None: |
paul@446 | 1156 | continue |
paul@446 | 1157 | if v in values: |
paul@446 | 1158 | page.option(label, value=v, selected="selected") |
paul@446 | 1159 | else: |
paul@446 | 1160 | page.option(label, value=v) |
paul@446 | 1161 | page.select.close() |
paul@446 | 1162 | |
paul@461 | 1163 | def _show_date_controls(self, name, default, tzid=None, index=None, show_tzid=True): |
paul@446 | 1164 | |
paul@446 | 1165 | """ |
paul@446 | 1166 | Show date controls for a field with the given 'name' and 'default' value |
paul@461 | 1167 | and 'tzid'. If 'index' is specified, default field values will be |
paul@461 | 1168 | overridden by the element from a collection of existing form values with |
paul@461 | 1169 | the specified index; otherwise, field values will be overridden by a |
paul@461 | 1170 | single form value. |
paul@461 | 1171 | |
paul@461 | 1172 | If 'show_tzid' is set to a false value, the time zone menu will not be |
paul@461 | 1173 | provided. |
paul@446 | 1174 | """ |
paul@446 | 1175 | |
paul@446 | 1176 | page = self.page |
paul@446 | 1177 | args = self.env.get_args() |
paul@446 | 1178 | |
paul@446 | 1179 | # Show dates for up to one week around the current date. |
paul@446 | 1180 | |
paul@446 | 1181 | base = to_date(default) |
paul@446 | 1182 | items = [] |
paul@446 | 1183 | for i in range(-7, 8): |
paul@446 | 1184 | d = base + timedelta(i) |
paul@446 | 1185 | items.append((format_datetime(d), self.format_date(d, "full"))) |
paul@446 | 1186 | |
paul@446 | 1187 | self._show_menu("%s-date" % name, format_datetime(base), items, index=index) |
paul@446 | 1188 | |
paul@446 | 1189 | # Show time details. |
paul@446 | 1190 | |
paul@446 | 1191 | default_time = isinstance(default, datetime) and default or None |
paul@446 | 1192 | |
paul@446 | 1193 | hour = args.get("%s-hour" % name, [])[index or 0:] |
paul@446 | 1194 | hour = hour and hour[0] or "%02d" % (default_time and default_time.hour or 0) |
paul@446 | 1195 | minute = args.get("%s-minute" % name, [])[index or 0:] |
paul@446 | 1196 | minute = minute and minute[0] or "%02d" % (default_time and default_time.minute or 0) |
paul@446 | 1197 | second = args.get("%s-second" % name, [])[index or 0:] |
paul@446 | 1198 | second = second and second[0] or "%02d" % (default_time and default_time.second or 0) |
paul@446 | 1199 | |
paul@446 | 1200 | page.span(class_="time enabled") |
paul@446 | 1201 | page.input(name="%s-hour" % name, type="text", value=hour, maxlength=2, size=2) |
paul@446 | 1202 | page.add(":") |
paul@446 | 1203 | page.input(name="%s-minute" % name, type="text", value=minute, maxlength=2, size=2) |
paul@446 | 1204 | page.add(":") |
paul@446 | 1205 | page.input(name="%s-second" % name, type="text", value=second, maxlength=2, size=2) |
paul@461 | 1206 | if show_tzid: |
paul@461 | 1207 | tzid = tzid or self.get_tzid() |
paul@461 | 1208 | page.add(" ") |
paul@461 | 1209 | self._show_timezone_menu("%s-tzid" % name, tzid, index) |
paul@446 | 1210 | page.span.close() |
paul@446 | 1211 | |
paul@446 | 1212 | def _show_timezone_menu(self, name, default, index=None): |
paul@446 | 1213 | |
paul@446 | 1214 | """ |
paul@446 | 1215 | Show timezone controls using a menu with the given 'name', set to the |
paul@446 | 1216 | given 'default' unless a field of the given 'name' provides a value. |
paul@446 | 1217 | """ |
paul@446 | 1218 | |
paul@446 | 1219 | entries = [(tzid, tzid) for tzid in pytz.all_timezones] |
paul@446 | 1220 | self._show_menu(name, default, entries, index=index) |
paul@446 | 1221 | |
paul@446 | 1222 | # vim: tabstop=4 expandtab shiftwidth=4 |