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