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