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@757 | 23 | from imiptools.data import get_uri, uri_dict, uri_items, 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@757 | 395 | still_to_remove = [] |
paul@757 | 396 | |
paul@485 | 397 | for i in args["remove"]: |
paul@496 | 398 | try: |
paul@496 | 399 | attendee = attendees[int(i)] |
paul@496 | 400 | except IndexError: |
paul@496 | 401 | continue |
paul@496 | 402 | |
paul@485 | 403 | existing = attendee in existing_attendees |
paul@485 | 404 | |
paul@756 | 405 | if not existing or not self.obj.is_shared() or attendee == self.user: |
paul@485 | 406 | attendees.remove(attendee) |
paul@757 | 407 | else: |
paul@757 | 408 | still_to_remove.append(i) |
paul@757 | 409 | |
paul@757 | 410 | args["remove"] = still_to_remove |
paul@477 | 411 | |
paul@477 | 412 | return attendees |
paul@477 | 413 | |
paul@446 | 414 | # Page fragment methods. |
paul@446 | 415 | |
paul@756 | 416 | def show_request_controls(self): |
paul@446 | 417 | |
paul@756 | 418 | "Show form controls for a request." |
paul@446 | 419 | |
paul@446 | 420 | page = self.page |
paul@446 | 421 | args = self.env.get_args() |
paul@446 | 422 | |
paul@756 | 423 | attendees = self.get_current_attendees() |
paul@446 | 424 | is_attendee = self.user in attendees |
paul@756 | 425 | is_request = self._have_request(self.uid, self.recurrenceid) |
paul@446 | 426 | |
paul@446 | 427 | # Show appropriate options depending on the role of the user. |
paul@446 | 428 | |
paul@756 | 429 | if is_attendee and not self.is_organiser(): |
paul@446 | 430 | page.p("An action is required for this request:") |
paul@446 | 431 | |
paul@446 | 432 | page.p() |
paul@518 | 433 | self._control("reply", "submit", "Send reply") |
paul@446 | 434 | page.add(" ") |
paul@518 | 435 | self._control("discard", "submit", "Discard event") |
paul@446 | 436 | page.add(" ") |
paul@518 | 437 | self._control("ignore", "submit", "Do nothing for now") |
paul@446 | 438 | page.p.close() |
paul@446 | 439 | |
paul@756 | 440 | if self.is_organiser(): |
paul@446 | 441 | page.p("As organiser, you can perform the following:") |
paul@446 | 442 | |
paul@512 | 443 | page.p() |
paul@756 | 444 | self._control("create", "submit", not self.obj.is_shared() and "Create event" or "Update event") |
paul@512 | 445 | page.add(" ") |
paul@512 | 446 | |
paul@756 | 447 | if self.obj.is_shared() and not is_request: |
paul@518 | 448 | self._control("cancel", "submit", "Cancel event") |
paul@446 | 449 | else: |
paul@518 | 450 | self._control("discard", "submit", "Discard event") |
paul@512 | 451 | |
paul@512 | 452 | page.add(" ") |
paul@518 | 453 | self._control("save", "submit", "Save without sending") |
paul@512 | 454 | page.p.close() |
paul@446 | 455 | |
paul@756 | 456 | def show_object_on_page(self, errors=None): |
paul@446 | 457 | |
paul@446 | 458 | """ |
paul@756 | 459 | Show the calendar object on the current page. If 'errors' is given, show |
paul@756 | 460 | a suitable message for the different errors provided. |
paul@446 | 461 | """ |
paul@446 | 462 | |
paul@446 | 463 | page = self.page |
paul@446 | 464 | page.form(method="POST") |
paul@446 | 465 | |
paul@595 | 466 | # Add a hidden control to help determine whether editing has already begun. |
paul@595 | 467 | |
paul@518 | 468 | self._control("editing", "hidden", "true") |
paul@446 | 469 | |
paul@446 | 470 | args = self.env.get_args() |
paul@446 | 471 | |
paul@487 | 472 | # Obtain basic event information, generating any necessary editing controls. |
paul@446 | 473 | |
paul@756 | 474 | if self.is_initial_load() or not self.is_organiser(): |
paul@756 | 475 | attendees = self.get_stored_attendees() |
paul@484 | 476 | else: |
paul@756 | 477 | attendees = self.update_attendees_from_page() |
paul@446 | 478 | |
paul@756 | 479 | p = self.get_current_main_period() |
paul@498 | 480 | self.show_object_datetime_controls(p) |
paul@446 | 481 | |
paul@487 | 482 | # Obtain any separate recurrences for this event. |
paul@487 | 483 | |
paul@756 | 484 | recurrenceids = self._get_active_recurrences(self.uid) |
paul@756 | 485 | replaced = not self.recurrenceid and p.is_replaced(recurrenceids) |
paul@487 | 486 | |
paul@446 | 487 | # Provide a summary of the object. |
paul@446 | 488 | |
paul@446 | 489 | page.table(class_="object", cellspacing=5, cellpadding=5) |
paul@446 | 490 | page.thead() |
paul@446 | 491 | page.tr() |
paul@446 | 492 | page.th("Event", class_="mainheading", colspan=2) |
paul@446 | 493 | page.tr.close() |
paul@446 | 494 | page.thead.close() |
paul@446 | 495 | page.tbody() |
paul@446 | 496 | |
paul@446 | 497 | for name, label in self.property_items: |
paul@446 | 498 | field = name.lower() |
paul@446 | 499 | |
paul@757 | 500 | items = uri_items(self.obj.get_items(name) or []) |
paul@446 | 501 | rowspan = len(items) |
paul@446 | 502 | |
paul@446 | 503 | if name == "ATTENDEE": |
paul@473 | 504 | rowspan = len(attendees) + 1 # for the add button |
paul@446 | 505 | elif not items: |
paul@446 | 506 | continue |
paul@446 | 507 | |
paul@446 | 508 | page.tr() |
paul@492 | 509 | page.th(label, class_="objectheading %s%s" % (field, errors and field in errors and " error" or ""), rowspan=rowspan) |
paul@446 | 510 | |
paul@446 | 511 | # Handle datetimes specially. |
paul@446 | 512 | |
paul@446 | 513 | if name in ["DTSTART", "DTEND"]: |
paul@487 | 514 | if not replaced: |
paul@446 | 515 | |
paul@487 | 516 | # Obtain the datetime. |
paul@487 | 517 | |
paul@498 | 518 | is_start = name == "DTSTART" |
paul@446 | 519 | |
paul@487 | 520 | # Where no end datetime exists, use the start datetime as the |
paul@487 | 521 | # basis of any potential datetime specified if dt-control is |
paul@487 | 522 | # set. |
paul@487 | 523 | |
paul@756 | 524 | self.show_datetime_controls(is_start and p.get_form_start() or p.get_form_end(), is_start) |
paul@446 | 525 | |
paul@487 | 526 | elif name == "DTSTART": |
paul@487 | 527 | page.td(class_="objectvalue %s replaced" % field, rowspan=2) |
paul@756 | 528 | page.a("First occurrence replaced by a separate event", href=self.link_to(self.uid, replaced)) |
paul@487 | 529 | page.td.close() |
paul@446 | 530 | |
paul@446 | 531 | page.tr.close() |
paul@446 | 532 | |
paul@446 | 533 | # Handle the summary specially. |
paul@446 | 534 | |
paul@446 | 535 | elif name == "SUMMARY": |
paul@756 | 536 | value = args.get("summary", [self.obj.get_value(name)])[0] |
paul@446 | 537 | |
paul@490 | 538 | page.td(class_="objectvalue summary") |
paul@756 | 539 | if self.is_organiser(): |
paul@518 | 540 | self._control("summary", "text", value, size=80) |
paul@446 | 541 | else: |
paul@446 | 542 | page.add(value) |
paul@446 | 543 | page.td.close() |
paul@446 | 544 | page.tr.close() |
paul@446 | 545 | |
paul@473 | 546 | # Handle attendees specially. |
paul@473 | 547 | |
paul@473 | 548 | elif name == "ATTENDEE": |
paul@473 | 549 | attendee_map = dict(items) |
paul@473 | 550 | first = True |
paul@473 | 551 | |
paul@473 | 552 | for i, value in enumerate(attendees): |
paul@473 | 553 | if not first: |
paul@473 | 554 | page.tr() |
paul@473 | 555 | else: |
paul@473 | 556 | first = False |
paul@473 | 557 | |
paul@479 | 558 | # Obtain details of attendees to supply attributes. |
paul@473 | 559 | |
paul@756 | 560 | self.show_attendee(i, value, attendee_map.get(value)) |
paul@473 | 561 | page.tr.close() |
paul@473 | 562 | |
paul@473 | 563 | # Allow more attendees to be specified. |
paul@473 | 564 | |
paul@756 | 565 | if self.is_organiser(): |
paul@473 | 566 | if not first: |
paul@473 | 567 | page.tr() |
paul@473 | 568 | |
paul@473 | 569 | page.td() |
paul@518 | 570 | self._control("add", "submit", "add", id="add", class_="add") |
paul@496 | 571 | page.label("Add attendee", for_="add", class_="add") |
paul@473 | 572 | page.td.close() |
paul@473 | 573 | page.tr.close() |
paul@473 | 574 | |
paul@473 | 575 | # Handle potentially many values of other kinds. |
paul@446 | 576 | |
paul@446 | 577 | else: |
paul@446 | 578 | first = True |
paul@446 | 579 | |
paul@446 | 580 | for i, (value, attr) in enumerate(items): |
paul@446 | 581 | if not first: |
paul@446 | 582 | page.tr() |
paul@446 | 583 | else: |
paul@446 | 584 | first = False |
paul@446 | 585 | |
paul@490 | 586 | page.td(class_="objectvalue %s" % field) |
paul@473 | 587 | page.add(value) |
paul@446 | 588 | page.td.close() |
paul@446 | 589 | page.tr.close() |
paul@446 | 590 | |
paul@446 | 591 | page.tbody.close() |
paul@446 | 592 | page.table.close() |
paul@446 | 593 | |
paul@756 | 594 | self.show_recurrences(errors) |
paul@756 | 595 | self.show_conflicting_events() |
paul@756 | 596 | self.show_request_controls() |
paul@446 | 597 | |
paul@446 | 598 | page.form.close() |
paul@446 | 599 | |
paul@756 | 600 | def show_attendee(self, i, attendee, attendee_attr): |
paul@479 | 601 | |
paul@479 | 602 | """ |
paul@756 | 603 | For the current object, show the attendee in position 'i' with the given |
paul@756 | 604 | 'attendee' value, having 'attendee_attr' as any stored attributes. |
paul@479 | 605 | """ |
paul@479 | 606 | |
paul@479 | 607 | page = self.page |
paul@479 | 608 | args = self.env.get_args() |
paul@479 | 609 | |
paul@479 | 610 | existing = attendee_attr is not None |
paul@479 | 611 | partstat = attendee_attr and attendee_attr.get("PARTSTAT") |
paul@479 | 612 | |
paul@479 | 613 | page.td(class_="objectvalue") |
paul@479 | 614 | |
paul@479 | 615 | # Show a form control as organiser for new attendees. |
paul@479 | 616 | |
paul@756 | 617 | if self.is_organiser() and (not existing or not self.obj.is_shared()): |
paul@518 | 618 | self._control("attendee", "value", attendee, size="40") |
paul@479 | 619 | else: |
paul@518 | 620 | self._control("attendee", "hidden", attendee) |
paul@479 | 621 | page.add(attendee) |
paul@479 | 622 | page.add(" ") |
paul@479 | 623 | |
paul@479 | 624 | # Show participation status, editable for the current user. |
paul@479 | 625 | |
paul@479 | 626 | if attendee == self.user: |
paul@479 | 627 | self._show_menu("partstat", partstat, self.partstat_items, "partstat") |
paul@479 | 628 | |
paul@479 | 629 | # Allow the participation indicator to act as a submit |
paul@479 | 630 | # button in order to refresh the page and show a control for |
paul@479 | 631 | # the current user, if indicated. |
paul@479 | 632 | |
paul@756 | 633 | elif self.is_organiser() and not existing: |
paul@518 | 634 | self._control("partstat-refresh", "submit", "refresh", id="partstat-%d" % i, class_="refresh") |
paul@479 | 635 | page.label(dict(self.partstat_items).get(partstat, ""), for_="partstat-%s" % i, class_="partstat") |
paul@479 | 636 | else: |
paul@479 | 637 | page.span(dict(self.partstat_items).get(partstat, ""), class_="partstat") |
paul@479 | 638 | |
paul@479 | 639 | # Permit organisers to remove attendees. |
paul@479 | 640 | |
paul@756 | 641 | if self.is_organiser(): |
paul@479 | 642 | |
paul@479 | 643 | # Permit the removal of newly-added attendees. |
paul@479 | 644 | |
paul@756 | 645 | remove_type = (not existing or not self.obj.is_shared() or attendee == self.user) and "submit" or "checkbox" |
paul@485 | 646 | self._control("remove", remove_type, str(i), str(i) in args.get("remove", []), id="remove-%d" % i, class_="remove") |
paul@479 | 647 | |
paul@479 | 648 | page.label("Remove", for_="remove-%d" % i, class_="remove") |
paul@701 | 649 | page.label(for_="remove-%d" % i, class_="removed") |
paul@701 | 650 | page.add("(Uninvited)") |
paul@701 | 651 | page.span("Re-invite", class_="action") |
paul@701 | 652 | page.label.close() |
paul@479 | 653 | |
paul@479 | 654 | page.td.close() |
paul@479 | 655 | |
paul@756 | 656 | def show_recurrences(self, errors=None): |
paul@446 | 657 | |
paul@492 | 658 | """ |
paul@756 | 659 | Show recurrences for the current object. If 'errors' is given, show a |
paul@756 | 660 | suitable message for the different errors provided. |
paul@492 | 661 | """ |
paul@446 | 662 | |
paul@446 | 663 | page = self.page |
paul@446 | 664 | |
paul@446 | 665 | # Obtain any parent object if this object is a specific recurrence. |
paul@446 | 666 | |
paul@756 | 667 | if self.recurrenceid: |
paul@756 | 668 | parent = self.get_stored_object(self.uid, None) |
paul@489 | 669 | if not parent: |
paul@446 | 670 | return |
paul@446 | 671 | |
paul@489 | 672 | page.p() |
paul@756 | 673 | page.a("This event modifies a recurring event.", href=self.link_to(self.uid)) |
paul@489 | 674 | page.p.close() |
paul@446 | 675 | |
paul@499 | 676 | # Obtain the periods associated with the event. |
paul@595 | 677 | # NOTE: Add a control to add recurrences here. |
paul@446 | 678 | |
paul@756 | 679 | recurrences = self.get_current_recurrences() |
paul@446 | 680 | |
paul@499 | 681 | if len(recurrences) < 1: |
paul@446 | 682 | return |
paul@446 | 683 | |
paul@756 | 684 | recurrenceids = self._get_recurrences(self.uid) |
paul@446 | 685 | |
paul@499 | 686 | page.p("This event occurs on the following occasions within the next %d days:" % self.get_window_size()) |
paul@499 | 687 | |
paul@446 | 688 | # Show each recurrence in a separate table if editable. |
paul@446 | 689 | |
paul@756 | 690 | if self.is_organiser() and recurrences: |
paul@446 | 691 | |
paul@519 | 692 | for index, period in enumerate(recurrences): |
paul@756 | 693 | self.show_recurrence(index, period, self.recurrenceid, recurrenceids, errors) |
paul@446 | 694 | |
paul@446 | 695 | # Otherwise, use a compact single table. |
paul@446 | 696 | |
paul@499 | 697 | else: |
paul@499 | 698 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@499 | 699 | page.caption("Occurrences") |
paul@499 | 700 | page.thead() |
paul@499 | 701 | page.tr() |
paul@499 | 702 | page.th("Start", class_="objectheading start") |
paul@499 | 703 | page.th("End", class_="objectheading end") |
paul@499 | 704 | page.tr.close() |
paul@499 | 705 | page.thead.close() |
paul@499 | 706 | page.tbody() |
paul@446 | 707 | |
paul@519 | 708 | for index, period in enumerate(recurrences): |
paul@499 | 709 | page.tr() |
paul@756 | 710 | self.show_recurrence_label(period, self.recurrenceid, recurrenceids, True) |
paul@756 | 711 | self.show_recurrence_label(period, self.recurrenceid, recurrenceids, False) |
paul@499 | 712 | page.tr.close() |
paul@446 | 713 | |
paul@499 | 714 | page.tbody.close() |
paul@499 | 715 | page.table.close() |
paul@446 | 716 | |
paul@756 | 717 | def show_recurrence(self, index, period, recurrenceid, recurrenceids, errors=None): |
paul@519 | 718 | |
paul@519 | 719 | """ |
paul@756 | 720 | Show recurrence controls for a recurrence provided by the current object |
paul@756 | 721 | with the given 'index' position in the list of periods, the given |
paul@756 | 722 | 'period' details, where a 'recurrenceid' indicates any specific |
paul@756 | 723 | recurrence, and where 'recurrenceids' indicates all known additional |
paul@756 | 724 | recurrences for the object. |
paul@519 | 725 | |
paul@519 | 726 | If 'errors' is given, show a suitable message for the different errors |
paul@519 | 727 | provided. |
paul@519 | 728 | """ |
paul@519 | 729 | |
paul@519 | 730 | page = self.page |
paul@526 | 731 | args = self.env.get_args() |
paul@526 | 732 | |
paul@528 | 733 | p = event_period_from_period(period) |
paul@633 | 734 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@528 | 735 | |
paul@519 | 736 | # Isolate the controls from neighbouring tables. |
paul@519 | 737 | |
paul@519 | 738 | page.div() |
paul@519 | 739 | |
paul@519 | 740 | self.show_object_datetime_controls(period, index) |
paul@519 | 741 | |
paul@519 | 742 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@519 | 743 | page.caption(period.origin == "RRULE" and "Occurrence from rule" or "Occurrence") |
paul@519 | 744 | page.tbody() |
paul@519 | 745 | |
paul@519 | 746 | page.tr() |
paul@519 | 747 | error = errors and ("dtstart", index) in errors and " error" or "" |
paul@519 | 748 | page.th("Start", class_="objectheading start%s" % error) |
paul@756 | 749 | self.show_recurrence_controls(index, period, recurrenceid, recurrenceids, True) |
paul@519 | 750 | page.tr.close() |
paul@519 | 751 | page.tr() |
paul@519 | 752 | error = errors and ("dtend", index) in errors and " error" or "" |
paul@519 | 753 | page.th("End", class_="objectheading end%s" % error) |
paul@756 | 754 | self.show_recurrence_controls(index, period, recurrenceid, recurrenceids, False) |
paul@519 | 755 | page.tr.close() |
paul@519 | 756 | |
paul@526 | 757 | # Permit the removal of recurrences. |
paul@526 | 758 | |
paul@528 | 759 | if not replaced: |
paul@528 | 760 | page.tr() |
paul@528 | 761 | page.th("") |
paul@528 | 762 | page.td() |
paul@526 | 763 | |
paul@756 | 764 | remove_type = not self.obj.is_shared() or not period.origin and "submit" or "checkbox" |
paul@528 | 765 | self._control("recur-remove", remove_type, str(index), |
paul@528 | 766 | str(index) in args.get("recur-remove", []), |
paul@528 | 767 | id="recur-remove-%d" % index, class_="remove") |
paul@526 | 768 | |
paul@528 | 769 | page.label("Remove", for_="recur-remove-%d" % index, class_="remove") |
paul@701 | 770 | page.label(for_="recur-remove-%d" % index, class_="removed") |
paul@701 | 771 | page.add("(Removed)") |
paul@701 | 772 | page.span("Re-add", class_="action") |
paul@701 | 773 | page.label.close() |
paul@526 | 774 | |
paul@528 | 775 | page.td.close() |
paul@528 | 776 | page.tr.close() |
paul@526 | 777 | |
paul@519 | 778 | page.tbody.close() |
paul@519 | 779 | page.table.close() |
paul@519 | 780 | |
paul@519 | 781 | page.div.close() |
paul@519 | 782 | |
paul@756 | 783 | def show_conflicting_events(self): |
paul@446 | 784 | |
paul@756 | 785 | "Show conflicting events for the current object." |
paul@446 | 786 | |
paul@446 | 787 | page = self.page |
paul@756 | 788 | recurrenceids = self._get_active_recurrences(self.uid) |
paul@446 | 789 | |
paul@446 | 790 | # Obtain the user's timezone. |
paul@446 | 791 | |
paul@446 | 792 | tzid = self.get_tzid() |
paul@756 | 793 | periods = self.get_periods(self.obj) |
paul@446 | 794 | |
paul@446 | 795 | # Indicate whether there are conflicting events. |
paul@446 | 796 | |
paul@484 | 797 | conflicts = [] |
paul@756 | 798 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@446 | 799 | |
paul@756 | 800 | for participant in self.get_current_attendees(): |
paul@484 | 801 | if participant == self.user: |
paul@484 | 802 | freebusy = self.store.get_freebusy(participant) |
paul@484 | 803 | else: |
paul@484 | 804 | freebusy = self.store.get_freebusy_for_other(self.user, participant) |
paul@484 | 805 | |
paul@484 | 806 | if not freebusy: |
paul@484 | 807 | continue |
paul@446 | 808 | |
paul@446 | 809 | # Obtain any time zone details from the suggested event. |
paul@446 | 810 | |
paul@756 | 811 | _dtstart, attr = self.obj.get_item("DTSTART") |
paul@446 | 812 | tzid = attr.get("TZID", tzid) |
paul@446 | 813 | |
paul@484 | 814 | # Show any conflicts with periods of actual attendance. |
paul@446 | 815 | |
paul@611 | 816 | participant_attr = attendee_map.get(participant) |
paul@611 | 817 | partstat = participant_attr and participant_attr.get("PARTSTAT") |
paul@756 | 818 | recurrences = self.obj.get_recurrence_start_points(recurrenceids, tzid) |
paul@611 | 819 | |
paul@484 | 820 | for p in have_conflict(freebusy, periods, True): |
paul@756 | 821 | if not self.recurrenceid and p.is_replaced(recurrences): |
paul@528 | 822 | continue |
paul@611 | 823 | |
paul@611 | 824 | if ( # Unidentified or different event |
paul@756 | 825 | (p.uid != self.uid or self.recurrenceid and p.recurrenceid and p.recurrenceid != self.recurrenceid) and |
paul@611 | 826 | # Different period or unclear participation with the same period |
paul@611 | 827 | (p not in periods or not partstat in ("ACCEPTED", "TENTATIVE")) and |
paul@611 | 828 | # Participant not limited to organising |
paul@611 | 829 | p.transp != "ORG" |
paul@611 | 830 | ): |
paul@611 | 831 | |
paul@484 | 832 | conflicts.append(p) |
paul@446 | 833 | |
paul@484 | 834 | conflicts.sort() |
paul@484 | 835 | |
paul@484 | 836 | # Show any conflicts with periods of actual attendance. |
paul@446 | 837 | |
paul@484 | 838 | if conflicts: |
paul@484 | 839 | page.p("This event conflicts with others:") |
paul@446 | 840 | |
paul@484 | 841 | page.table(cellspacing=5, cellpadding=5, class_="conflicts") |
paul@484 | 842 | page.thead() |
paul@484 | 843 | page.tr() |
paul@484 | 844 | page.th("Event") |
paul@484 | 845 | page.th("Start") |
paul@484 | 846 | page.th("End") |
paul@484 | 847 | page.tr.close() |
paul@484 | 848 | page.thead.close() |
paul@484 | 849 | page.tbody() |
paul@446 | 850 | |
paul@484 | 851 | for p in conflicts: |
paul@484 | 852 | |
paul@484 | 853 | # Provide details of any conflicting event. |
paul@446 | 854 | |
paul@535 | 855 | start = self.format_datetime(to_timezone(p.get_start(), tzid), "long") |
paul@535 | 856 | end = self.format_datetime(to_timezone(p.get_end(), tzid), "long") |
paul@446 | 857 | |
paul@484 | 858 | page.tr() |
paul@446 | 859 | |
paul@484 | 860 | # Show the event summary for the conflicting event. |
paul@446 | 861 | |
paul@484 | 862 | page.td() |
paul@484 | 863 | if p.summary: |
paul@488 | 864 | page.a(p.summary, href=self.link_to(p.uid, p.recurrenceid)) |
paul@484 | 865 | else: |
paul@484 | 866 | page.add("(Unspecified event)") |
paul@484 | 867 | page.td.close() |
paul@446 | 868 | |
paul@484 | 869 | page.td(start) |
paul@484 | 870 | page.td(end) |
paul@446 | 871 | |
paul@484 | 872 | page.tr.close() |
paul@446 | 873 | |
paul@484 | 874 | page.tbody.close() |
paul@484 | 875 | page.table.close() |
paul@446 | 876 | |
paul@474 | 877 | # Generation of controls within page fragments. |
paul@474 | 878 | |
paul@498 | 879 | def show_object_datetime_controls(self, period, index=None): |
paul@474 | 880 | |
paul@474 | 881 | """ |
paul@474 | 882 | Show datetime-related controls if already active or if an object needs |
paul@498 | 883 | them for the given 'period'. The given 'index' is used to parameterise |
paul@498 | 884 | individual controls for dynamic manipulation. |
paul@474 | 885 | """ |
paul@474 | 886 | |
paul@499 | 887 | p = form_period_from_period(period) |
paul@498 | 888 | |
paul@474 | 889 | page = self.page |
paul@474 | 890 | args = self.env.get_args() |
paul@622 | 891 | _id = self.element_identifier |
paul@622 | 892 | _name = self.element_name |
paul@623 | 893 | _enable = self.element_enable |
paul@474 | 894 | |
paul@474 | 895 | # Add a dynamic stylesheet to permit the controls to modify the display. |
paul@474 | 896 | # NOTE: The style details need to be coordinated with the static |
paul@474 | 897 | # NOTE: stylesheet. |
paul@474 | 898 | |
paul@474 | 899 | if index is not None: |
paul@474 | 900 | page.style(type="text/css") |
paul@474 | 901 | |
paul@474 | 902 | # Unlike the rules for object properties, these affect recurrence |
paul@474 | 903 | # properties. |
paul@474 | 904 | |
paul@474 | 905 | page.add("""\ |
paul@474 | 906 | input#dttimes-enable-%(index)d, |
paul@474 | 907 | input#dtend-enable-%(index)d, |
paul@474 | 908 | input#dttimes-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue .time.enabled, |
paul@474 | 909 | input#dttimes-enable-%(index)d:checked ~ .recurrence td.objectvalue .time.disabled, |
paul@474 | 910 | input#dtend-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue.dtend .dt.enabled, |
paul@474 | 911 | input#dtend-enable-%(index)d:checked ~ .recurrence td.objectvalue.dtend .dt.disabled { |
paul@474 | 912 | display: none; |
paul@474 | 913 | }""" % {"index" : index}) |
paul@474 | 914 | |
paul@474 | 915 | page.style.close() |
paul@474 | 916 | |
paul@474 | 917 | self._control( |
paul@622 | 918 | _name("dtend-control", "recur", index), "checkbox", |
paul@623 | 919 | _enable(index), p.end_enabled, |
paul@622 | 920 | id=_id("dtend-enable", index) |
paul@474 | 921 | ) |
paul@474 | 922 | |
paul@474 | 923 | self._control( |
paul@622 | 924 | _name("dttimes-control", "recur", index), "checkbox", |
paul@623 | 925 | _enable(index), p.times_enabled, |
paul@622 | 926 | id=_id("dttimes-enable", index) |
paul@474 | 927 | ) |
paul@474 | 928 | |
paul@756 | 929 | def show_datetime_controls(self, formdate, show_start): |
paul@474 | 930 | |
paul@474 | 931 | """ |
paul@756 | 932 | Show datetime details from the current object for the 'formdate', |
paul@756 | 933 | showing start details if 'show_start' is set to a true value. Details |
paul@756 | 934 | will appear as controls for organisers and labels for attendees. |
paul@474 | 935 | """ |
paul@474 | 936 | |
paul@474 | 937 | page = self.page |
paul@474 | 938 | |
paul@474 | 939 | # Show controls for editing as organiser. |
paul@474 | 940 | |
paul@756 | 941 | if self.is_organiser(): |
paul@474 | 942 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@474 | 943 | |
paul@474 | 944 | if show_start: |
paul@474 | 945 | page.div(class_="dt enabled") |
paul@498 | 946 | self._show_date_controls("dtstart", formdate) |
paul@474 | 947 | page.br() |
paul@474 | 948 | page.label("Specify times", for_="dttimes-enable", class_="time disabled enable") |
paul@474 | 949 | page.label("Specify dates only", for_="dttimes-enable", class_="time enabled disable") |
paul@474 | 950 | page.div.close() |
paul@474 | 951 | |
paul@474 | 952 | else: |
paul@474 | 953 | page.div(class_="dt disabled") |
paul@474 | 954 | page.label("Specify end date", for_="dtend-enable", class_="enable") |
paul@474 | 955 | page.div.close() |
paul@474 | 956 | page.div(class_="dt enabled") |
paul@498 | 957 | self._show_date_controls("dtend", formdate) |
paul@474 | 958 | page.br() |
paul@474 | 959 | page.label("End on same day", for_="dtend-enable", class_="disable") |
paul@474 | 960 | page.div.close() |
paul@474 | 961 | |
paul@474 | 962 | page.td.close() |
paul@474 | 963 | |
paul@474 | 964 | # Show a label as attendee. |
paul@474 | 965 | |
paul@474 | 966 | else: |
paul@528 | 967 | dt = formdate.as_datetime() |
paul@528 | 968 | if dt: |
paul@498 | 969 | page.td(self.format_datetime(dt, "full")) |
paul@498 | 970 | else: |
paul@498 | 971 | page.td("(Unrecognised date)") |
paul@474 | 972 | |
paul@756 | 973 | def show_recurrence_controls(self, index, period, recurrenceid, recurrenceids, show_start): |
paul@474 | 974 | |
paul@474 | 975 | """ |
paul@756 | 976 | Show datetime details from the current object for the recurrence having |
paul@756 | 977 | the given 'index', with the recurrence period described by 'period', |
paul@494 | 978 | indicating a start, end and origin of the period from the event details, |
paul@494 | 979 | employing any 'recurrenceid' and 'recurrenceids' for the object to |
paul@494 | 980 | configure the displayed information. |
paul@474 | 981 | |
paul@474 | 982 | If 'show_start' is set to a true value, the start details will be shown; |
paul@474 | 983 | otherwise, the end details will be shown. |
paul@474 | 984 | """ |
paul@474 | 985 | |
paul@474 | 986 | page = self.page |
paul@622 | 987 | _id = self.element_identifier |
paul@622 | 988 | _name = self.element_name |
paul@499 | 989 | |
paul@499 | 990 | p = event_period_from_period(period) |
paul@633 | 991 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@474 | 992 | |
paul@474 | 993 | # Show controls for editing as organiser. |
paul@474 | 994 | |
paul@756 | 995 | if self.is_organiser() and not replaced: |
paul@474 | 996 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@474 | 997 | |
paul@517 | 998 | read_only = period.origin == "RRULE" |
paul@517 | 999 | |
paul@474 | 1000 | if show_start: |
paul@474 | 1001 | page.div(class_="dt enabled") |
paul@622 | 1002 | self._show_date_controls(_name("dtstart", "recur", index), p.get_form_start(), index=index, read_only=read_only) |
paul@517 | 1003 | if not read_only: |
paul@517 | 1004 | page.br() |
paul@622 | 1005 | page.label("Specify times", for_=_id("dttimes-enable", index), class_="time disabled enable") |
paul@622 | 1006 | page.label("Specify dates only", for_=_id("dttimes-enable", index), class_="time enabled disable") |
paul@474 | 1007 | page.div.close() |
paul@474 | 1008 | |
paul@517 | 1009 | # Put the origin somewhere. |
paul@517 | 1010 | |
paul@518 | 1011 | self._control("recur-origin", "hidden", p.origin or "") |
paul@517 | 1012 | |
paul@474 | 1013 | else: |
paul@474 | 1014 | page.div(class_="dt disabled") |
paul@517 | 1015 | if not read_only: |
paul@622 | 1016 | page.label("Specify end date", for_=_id("dtend-enable", index), class_="enable") |
paul@474 | 1017 | page.div.close() |
paul@474 | 1018 | page.div(class_="dt enabled") |
paul@622 | 1019 | self._show_date_controls(_name("dtend", "recur", index), p.get_form_end(), index=index, show_tzid=False, read_only=read_only) |
paul@517 | 1020 | if not read_only: |
paul@517 | 1021 | page.br() |
paul@622 | 1022 | page.label("End on same day", for_=_id("dtend-enable", index), class_="disable") |
paul@474 | 1023 | page.div.close() |
paul@474 | 1024 | |
paul@474 | 1025 | page.td.close() |
paul@474 | 1026 | |
paul@474 | 1027 | # Show label as attendee. |
paul@474 | 1028 | |
paul@474 | 1029 | else: |
paul@498 | 1030 | self.show_recurrence_label(p, recurrenceid, recurrenceids, show_start) |
paul@498 | 1031 | |
paul@499 | 1032 | def show_recurrence_label(self, period, recurrenceid, recurrenceids, show_start): |
paul@498 | 1033 | |
paul@498 | 1034 | """ |
paul@499 | 1035 | Show datetime details for the given 'period', employing any |
paul@498 | 1036 | 'recurrenceid' and 'recurrenceids' for the object to configure the |
paul@498 | 1037 | displayed information. |
paul@498 | 1038 | |
paul@498 | 1039 | If 'show_start' is set to a true value, the start details will be shown; |
paul@498 | 1040 | otherwise, the end details will be shown. |
paul@498 | 1041 | """ |
paul@498 | 1042 | |
paul@498 | 1043 | page = self.page |
paul@498 | 1044 | |
paul@499 | 1045 | p = event_period_from_period(period) |
paul@633 | 1046 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@499 | 1047 | |
paul@498 | 1048 | css = " ".join([ |
paul@535 | 1049 | replaced and "replaced" or "", |
paul@633 | 1050 | p.is_affected(recurrenceid) and "affected" or "" |
paul@498 | 1051 | ]) |
paul@498 | 1052 | |
paul@498 | 1053 | formdate = show_start and p.get_form_start() or p.get_form_end() |
paul@528 | 1054 | dt = formdate.as_datetime() |
paul@528 | 1055 | if dt: |
paul@498 | 1056 | page.td(self.format_datetime(dt, "long"), class_=css) |
paul@498 | 1057 | else: |
paul@498 | 1058 | page.td("(Unrecognised date)") |
paul@474 | 1059 | |
paul@446 | 1060 | # Full page output methods. |
paul@446 | 1061 | |
paul@446 | 1062 | def show(self, path_info): |
paul@446 | 1063 | |
paul@446 | 1064 | "Show an object request using the given 'path_info' for the current user." |
paul@446 | 1065 | |
paul@755 | 1066 | uid, recurrenceid, section = self.get_identifiers(path_info) |
paul@755 | 1067 | obj = self.get_stored_object(uid, recurrenceid, section) |
paul@756 | 1068 | self.set_object(obj) |
paul@446 | 1069 | |
paul@446 | 1070 | if not obj: |
paul@446 | 1071 | return False |
paul@446 | 1072 | |
paul@756 | 1073 | errors = self.handle_request() |
paul@446 | 1074 | |
paul@492 | 1075 | if not errors: |
paul@446 | 1076 | return True |
paul@446 | 1077 | |
paul@446 | 1078 | self.new_page(title="Event") |
paul@756 | 1079 | self.show_object_on_page(errors) |
paul@446 | 1080 | |
paul@446 | 1081 | return True |
paul@446 | 1082 | |
paul@446 | 1083 | # Utility methods. |
paul@446 | 1084 | |
paul@518 | 1085 | def _control(self, name, type, value, selected=False, **kw): |
paul@474 | 1086 | |
paul@474 | 1087 | """ |
paul@474 | 1088 | Show a control with the given 'name', 'type' and 'value', with |
paul@474 | 1089 | 'selected' indicating whether it should be selected (checked or |
paul@474 | 1090 | equivalent), and with keyword arguments setting other properties. |
paul@474 | 1091 | """ |
paul@474 | 1092 | |
paul@474 | 1093 | page = self.page |
paul@474 | 1094 | if selected: |
paul@474 | 1095 | page.input(name=name, type=type, value=value, checked=selected, **kw) |
paul@474 | 1096 | else: |
paul@474 | 1097 | page.input(name=name, type=type, value=value, **kw) |
paul@474 | 1098 | |
paul@446 | 1099 | def _show_menu(self, name, default, items, class_="", index=None): |
paul@446 | 1100 | |
paul@446 | 1101 | """ |
paul@446 | 1102 | Show a select menu having the given 'name', set to the given 'default', |
paul@446 | 1103 | providing the given (value, label) 'items', and employing the given CSS |
paul@446 | 1104 | 'class_' if specified. |
paul@446 | 1105 | """ |
paul@446 | 1106 | |
paul@446 | 1107 | page = self.page |
paul@446 | 1108 | values = self.env.get_args().get(name, [default]) |
paul@446 | 1109 | if index is not None: |
paul@446 | 1110 | values = values[index:] |
paul@446 | 1111 | values = values and values[0:1] or [default] |
paul@446 | 1112 | |
paul@446 | 1113 | page.select(name=name, class_=class_) |
paul@446 | 1114 | for v, label in items: |
paul@446 | 1115 | if v is None: |
paul@446 | 1116 | continue |
paul@446 | 1117 | if v in values: |
paul@446 | 1118 | page.option(label, value=v, selected="selected") |
paul@446 | 1119 | else: |
paul@446 | 1120 | page.option(label, value=v) |
paul@446 | 1121 | page.select.close() |
paul@446 | 1122 | |
paul@517 | 1123 | def _show_date_controls(self, name, default, index=None, show_tzid=True, read_only=False): |
paul@446 | 1124 | |
paul@446 | 1125 | """ |
paul@498 | 1126 | Show date controls for a field with the given 'name' and 'default' form |
paul@498 | 1127 | date value. |
paul@498 | 1128 | |
paul@498 | 1129 | If 'index' is specified, default field values will be overridden by the |
paul@498 | 1130 | element from a collection of existing form values with the specified |
paul@498 | 1131 | index; otherwise, field values will be overridden by a single form |
paul@498 | 1132 | value. |
paul@461 | 1133 | |
paul@461 | 1134 | If 'show_tzid' is set to a false value, the time zone menu will not be |
paul@461 | 1135 | provided. |
paul@517 | 1136 | |
paul@517 | 1137 | If 'read_only' is set to a true value, the controls will be hidden and |
paul@517 | 1138 | labels will be employed instead. |
paul@446 | 1139 | """ |
paul@446 | 1140 | |
paul@446 | 1141 | page = self.page |
paul@446 | 1142 | |
paul@446 | 1143 | # Show dates for up to one week around the current date. |
paul@446 | 1144 | |
paul@528 | 1145 | dt = default.as_datetime() |
paul@528 | 1146 | if not dt: |
paul@498 | 1147 | dt = date.today() |
paul@498 | 1148 | |
paul@498 | 1149 | base = to_date(dt) |
paul@517 | 1150 | |
paul@517 | 1151 | # Show a date label with a hidden field if read-only. |
paul@517 | 1152 | |
paul@517 | 1153 | if read_only: |
paul@518 | 1154 | self._control("%s-date" % name, "hidden", format_datetime(base)) |
paul@517 | 1155 | page.span(self.format_date(base, "long")) |
paul@446 | 1156 | |
paul@517 | 1157 | # Show dates for up to one week around the current date. |
paul@534 | 1158 | # NOTE: Support paging to other dates. |
paul@517 | 1159 | |
paul@517 | 1160 | else: |
paul@517 | 1161 | items = [] |
paul@517 | 1162 | for i in range(-7, 8): |
paul@517 | 1163 | d = base + timedelta(i) |
paul@517 | 1164 | items.append((format_datetime(d), self.format_date(d, "full"))) |
paul@517 | 1165 | self._show_menu("%s-date" % name, format_datetime(base), items, index=index) |
paul@446 | 1166 | |
paul@446 | 1167 | # Show time details. |
paul@446 | 1168 | |
paul@498 | 1169 | page.span(class_="time enabled") |
paul@517 | 1170 | |
paul@517 | 1171 | if read_only: |
paul@517 | 1172 | page.span("%s:%s:%s" % (default.get_hour(), default.get_minute(), default.get_second())) |
paul@518 | 1173 | self._control("%s-hour" % name, "hidden", default.get_hour()) |
paul@518 | 1174 | self._control("%s-minute" % name, "hidden", default.get_minute()) |
paul@518 | 1175 | self._control("%s-second" % name, "hidden", default.get_second()) |
paul@517 | 1176 | else: |
paul@518 | 1177 | self._control("%s-hour" % name, "text", default.get_hour(), maxlength=2, size=2) |
paul@517 | 1178 | page.add(":") |
paul@518 | 1179 | self._control("%s-minute" % name, "text", default.get_minute(), maxlength=2, size=2) |
paul@517 | 1180 | page.add(":") |
paul@518 | 1181 | self._control("%s-second" % name, "text", default.get_second(), maxlength=2, size=2) |
paul@517 | 1182 | |
paul@517 | 1183 | # Show time zone details. |
paul@446 | 1184 | |
paul@461 | 1185 | if show_tzid: |
paul@461 | 1186 | page.add(" ") |
paul@498 | 1187 | tzid = default.get_tzid() or self.get_tzid() |
paul@517 | 1188 | |
paul@517 | 1189 | # Show a label if read-only or a menu otherwise. |
paul@517 | 1190 | |
paul@517 | 1191 | if read_only: |
paul@518 | 1192 | self._control("%s-tzid" % name, "hidden", tzid) |
paul@517 | 1193 | page.span(tzid) |
paul@517 | 1194 | else: |
paul@517 | 1195 | self._show_timezone_menu("%s-tzid" % name, tzid, index) |
paul@498 | 1196 | |
paul@446 | 1197 | page.span.close() |
paul@446 | 1198 | |
paul@446 | 1199 | def _show_timezone_menu(self, name, default, index=None): |
paul@446 | 1200 | |
paul@446 | 1201 | """ |
paul@446 | 1202 | Show timezone controls using a menu with the given 'name', set to the |
paul@446 | 1203 | given 'default' unless a field of the given 'name' provides a value. |
paul@446 | 1204 | """ |
paul@446 | 1205 | |
paul@446 | 1206 | entries = [(tzid, tzid) for tzid in pytz.all_timezones] |
paul@446 | 1207 | self._show_menu(name, default, entries, index=index) |
paul@446 | 1208 | |
paul@446 | 1209 | # vim: tabstop=4 expandtab shiftwidth=4 |