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