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