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