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