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