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@879 | 108 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.obj.get_main_period_items() |
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@920 | 219 | page.th("Event", class_="mainheading", colspan=3) |
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@920 | 265 | page.td(class_="objectvalue %s replaced" % field, rowspan=2, colspan=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@920 | 274 | page.td(class_="objectvalue %s excluded" % field, rowspan=2, colspan=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@920 | 284 | page.td(colspan=2) |
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@920 | 295 | page.td(class_="objectvalue summary", colspan=2) |
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@920 | 325 | page.td(colspan=2) |
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@920 | 342 | page.td(class_="objectvalue %s" % field, colspan=2) |
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@920 | 402 | page.td.close() |
paul@920 | 403 | page.td() |
paul@920 | 404 | |
paul@479 | 405 | # Permit organisers to remove attendees. |
paul@479 | 406 | |
paul@854 | 407 | if self.can_remove_attendee(attendee_uri) or self.is_organiser(): |
paul@479 | 408 | |
paul@479 | 409 | # Permit the removal of newly-added attendees. |
paul@479 | 410 | |
paul@794 | 411 | remove_type = self.can_remove_attendee(attendee_uri) and "submit" or "checkbox" |
paul@764 | 412 | self.control("remove", remove_type, str(i), str(i) in args.get("remove", []), id="remove-%d" % i, class_="remove") |
paul@479 | 413 | |
paul@479 | 414 | page.label("Remove", for_="remove-%d" % i, class_="remove") |
paul@701 | 415 | page.label(for_="remove-%d" % i, class_="removed") |
paul@701 | 416 | page.add("(Uninvited)") |
paul@701 | 417 | page.span("Re-invite", class_="action") |
paul@701 | 418 | page.label.close() |
paul@479 | 419 | |
paul@479 | 420 | page.td.close() |
paul@479 | 421 | |
paul@756 | 422 | def show_recurrences(self, errors=None): |
paul@446 | 423 | |
paul@492 | 424 | """ |
paul@756 | 425 | Show recurrences for the current object. If 'errors' is given, show a |
paul@756 | 426 | suitable message for the different errors provided. |
paul@492 | 427 | """ |
paul@446 | 428 | |
paul@446 | 429 | page = self.page |
paul@446 | 430 | |
paul@446 | 431 | # Obtain any parent object if this object is a specific recurrence. |
paul@446 | 432 | |
paul@756 | 433 | if self.recurrenceid: |
paul@756 | 434 | parent = self.get_stored_object(self.uid, None) |
paul@489 | 435 | if not parent: |
paul@446 | 436 | return |
paul@446 | 437 | |
paul@489 | 438 | page.p() |
paul@756 | 439 | page.a("This event modifies a recurring event.", href=self.link_to(self.uid)) |
paul@489 | 440 | page.p.close() |
paul@446 | 441 | |
paul@499 | 442 | # Obtain the periods associated with the event. |
paul@446 | 443 | |
paul@787 | 444 | recurrences = self.get_current_recurrences() |
paul@446 | 445 | |
paul@499 | 446 | if len(recurrences) < 1: |
paul@446 | 447 | return |
paul@446 | 448 | |
paul@499 | 449 | page.p("This event occurs on the following occasions within the next %d days:" % self.get_window_size()) |
paul@499 | 450 | |
paul@867 | 451 | # Show each recurrence in a separate table. |
paul@446 | 452 | |
paul@867 | 453 | for index, period in enumerate(recurrences): |
paul@868 | 454 | self.show_recurrence(index, period, self.recurrenceid, errors) |
paul@446 | 455 | |
paul@868 | 456 | def show_recurrence(self, index, period, recurrenceid, errors=None): |
paul@519 | 457 | |
paul@519 | 458 | """ |
paul@756 | 459 | Show recurrence controls for a recurrence provided by the current object |
paul@756 | 460 | with the given 'index' position in the list of periods, the given |
paul@756 | 461 | 'period' details, where a 'recurrenceid' indicates any specific |
paul@868 | 462 | recurrence. |
paul@519 | 463 | |
paul@519 | 464 | If 'errors' is given, show a suitable message for the different errors |
paul@519 | 465 | provided. |
paul@519 | 466 | """ |
paul@519 | 467 | |
paul@519 | 468 | page = self.page |
paul@526 | 469 | args = self.env.get_args() |
paul@526 | 470 | |
paul@519 | 471 | # Isolate the controls from neighbouring tables. |
paul@519 | 472 | |
paul@519 | 473 | page.div() |
paul@519 | 474 | |
paul@519 | 475 | self.show_object_datetime_controls(period, index) |
paul@519 | 476 | |
paul@519 | 477 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@519 | 478 | page.caption(period.origin == "RRULE" and "Occurrence from rule" or "Occurrence") |
paul@519 | 479 | page.tbody() |
paul@519 | 480 | |
paul@519 | 481 | page.tr() |
paul@519 | 482 | error = errors and ("dtstart", index) in errors and " error" or "" |
paul@519 | 483 | page.th("Start", class_="objectheading start%s" % error) |
paul@868 | 484 | self.show_recurrence_controls(index, period, recurrenceid, True) |
paul@519 | 485 | page.tr.close() |
paul@519 | 486 | page.tr() |
paul@519 | 487 | error = errors and ("dtend", index) in errors and " error" or "" |
paul@519 | 488 | page.th("End", class_="objectheading end%s" % error) |
paul@868 | 489 | self.show_recurrence_controls(index, period, recurrenceid, False) |
paul@519 | 490 | page.tr.close() |
paul@519 | 491 | |
paul@526 | 492 | # Permit the removal of recurrences. |
paul@526 | 493 | |
paul@868 | 494 | if not period.replaced: |
paul@528 | 495 | page.tr() |
paul@528 | 496 | page.th("") |
paul@528 | 497 | page.td() |
paul@526 | 498 | |
paul@853 | 499 | # Attendees can instantly remove recurrences and thus produce a |
paul@853 | 500 | # counter-proposal. Organisers may need to unschedule recurrences |
paul@853 | 501 | # instead. |
paul@853 | 502 | |
paul@854 | 503 | remove_type = self.can_remove_recurrence(period) and "submit" or "checkbox" |
paul@784 | 504 | |
paul@764 | 505 | self.control("recur-remove", remove_type, str(index), |
paul@528 | 506 | str(index) in args.get("recur-remove", []), |
paul@528 | 507 | id="recur-remove-%d" % index, class_="remove") |
paul@526 | 508 | |
paul@528 | 509 | page.label("Remove", for_="recur-remove-%d" % index, class_="remove") |
paul@701 | 510 | page.label(for_="recur-remove-%d" % index, class_="removed") |
paul@701 | 511 | page.add("(Removed)") |
paul@701 | 512 | page.span("Re-add", class_="action") |
paul@701 | 513 | page.label.close() |
paul@526 | 514 | |
paul@528 | 515 | page.td.close() |
paul@528 | 516 | page.tr.close() |
paul@526 | 517 | |
paul@519 | 518 | page.tbody.close() |
paul@519 | 519 | page.table.close() |
paul@519 | 520 | |
paul@519 | 521 | page.div.close() |
paul@519 | 522 | |
paul@766 | 523 | def show_counters(self): |
paul@766 | 524 | |
paul@766 | 525 | "Show any counter-proposals for the current object." |
paul@766 | 526 | |
paul@766 | 527 | page = self.page |
paul@777 | 528 | query = self.env.get_query() |
paul@777 | 529 | counter = query.get("counter", [None])[0] |
paul@777 | 530 | |
paul@766 | 531 | attendees = self._get_counters(self.uid, self.recurrenceid) |
paul@766 | 532 | tzid = self.get_tzid() |
paul@766 | 533 | |
paul@766 | 534 | if not attendees: |
paul@766 | 535 | return |
paul@766 | 536 | |
paul@809 | 537 | attendees = self.get_verbose_attendees(attendees) |
paul@838 | 538 | current_attendees = [uri for (name, uri) in uri_parts(self.get_current_attendees())] |
paul@856 | 539 | current_periods = set(self.get_periods(self.obj)) |
paul@809 | 540 | |
paul@819 | 541 | # Get suggestions. Attendees are aggregated and reference the existing |
paul@819 | 542 | # attendees suggesting them. Periods are referenced by each existing |
paul@819 | 543 | # attendee. |
paul@819 | 544 | |
paul@819 | 545 | suggested_attendees = {} |
paul@819 | 546 | suggested_periods = {} |
paul@819 | 547 | |
paul@819 | 548 | for i, attendee in enumerate(attendees): |
paul@819 | 549 | attendee_uri = get_uri(attendee) |
paul@819 | 550 | obj = self.get_stored_object(self.uid, self.recurrenceid, "counters", attendee_uri) |
paul@819 | 551 | |
paul@819 | 552 | # Get suggested attendees. |
paul@819 | 553 | |
paul@819 | 554 | for suggested_uri, suggested_attr in uri_dict(obj.get_value_map("ATTENDEE")).items(): |
paul@838 | 555 | if suggested_uri == attendee_uri or suggested_uri in current_attendees: |
paul@819 | 556 | continue |
paul@819 | 557 | suggested = get_verbose_address(suggested_uri, suggested_attr) |
paul@819 | 558 | |
paul@819 | 559 | if not suggested_attendees.has_key(suggested): |
paul@819 | 560 | suggested_attendees[suggested] = [] |
paul@819 | 561 | suggested_attendees[suggested].append(attendee) |
paul@819 | 562 | |
paul@819 | 563 | # Get suggested periods. |
paul@819 | 564 | |
paul@837 | 565 | periods = self.get_periods(obj) |
paul@856 | 566 | if current_periods.symmetric_difference(periods): |
paul@837 | 567 | suggested_periods[attendee] = periods |
paul@819 | 568 | |
paul@819 | 569 | # Present the suggested attendees. |
paul@819 | 570 | |
paul@837 | 571 | if suggested_attendees: |
paul@837 | 572 | page.p("The following attendees have been suggested for this event:") |
paul@766 | 573 | |
paul@837 | 574 | page.table(cellspacing=5, cellpadding=5, class_="counters") |
paul@837 | 575 | page.thead() |
paul@837 | 576 | page.tr() |
paul@837 | 577 | page.th("Attendee") |
paul@837 | 578 | page.th("Suggested by...") |
paul@837 | 579 | page.tr.close() |
paul@837 | 580 | page.thead.close() |
paul@837 | 581 | page.tbody() |
paul@819 | 582 | |
paul@837 | 583 | suggested_attendees = list(suggested_attendees.items()) |
paul@837 | 584 | suggested_attendees.sort() |
paul@819 | 585 | |
paul@838 | 586 | for i, (suggested, attendees) in enumerate(suggested_attendees): |
paul@837 | 587 | page.tr() |
paul@837 | 588 | page.td(suggested) |
paul@837 | 589 | page.td(", ".join(attendees)) |
paul@838 | 590 | page.td() |
paul@838 | 591 | self.control("suggested-attendee", "hidden", suggested) |
paul@838 | 592 | self.control("add-suggested-attendee-%d" % i, "submit", "Add") |
paul@838 | 593 | page.td.close() |
paul@837 | 594 | page.tr.close() |
paul@819 | 595 | |
paul@837 | 596 | page.tbody.close() |
paul@837 | 597 | page.table.close() |
paul@819 | 598 | |
paul@819 | 599 | # Present the suggested periods. |
paul@819 | 600 | |
paul@837 | 601 | if suggested_periods: |
paul@837 | 602 | page.p("The following periods have been suggested for this event:") |
paul@819 | 603 | |
paul@837 | 604 | page.table(cellspacing=5, cellpadding=5, class_="counters") |
paul@837 | 605 | page.thead() |
paul@837 | 606 | page.tr() |
paul@837 | 607 | page.th("Periods", colspan=2) |
paul@837 | 608 | page.th("Suggested by...", rowspan=2) |
paul@837 | 609 | page.tr.close() |
paul@837 | 610 | page.tr() |
paul@837 | 611 | page.th("Start") |
paul@837 | 612 | page.th("End") |
paul@837 | 613 | page.tr.close() |
paul@837 | 614 | page.thead.close() |
paul@837 | 615 | page.tbody() |
paul@766 | 616 | |
paul@856 | 617 | recurrenceids = self._get_recurrences(self.uid) |
paul@856 | 618 | |
paul@837 | 619 | suggested_periods = list(suggested_periods.items()) |
paul@837 | 620 | suggested_periods.sort() |
paul@766 | 621 | |
paul@837 | 622 | for attendee, periods in suggested_periods: |
paul@837 | 623 | first = True |
paul@837 | 624 | for p in periods: |
paul@856 | 625 | replaced = not self.recurrenceid and p.is_replaced(recurrenceids) |
paul@837 | 626 | identifier = "%s-%s" % (format_datetime(p.get_start_point()), format_datetime(p.get_end_point())) |
paul@837 | 627 | css = identifier == counter and "selected" or "" |
paul@837 | 628 | |
paul@837 | 629 | page.tr(class_=css) |
paul@805 | 630 | |
paul@837 | 631 | start = self.format_datetime(to_timezone(p.get_start(), tzid), "long") |
paul@837 | 632 | end = self.format_datetime(to_timezone(p.get_end(), tzid), "long") |
paul@766 | 633 | |
paul@837 | 634 | # Show each period. |
paul@819 | 635 | |
paul@856 | 636 | css = replaced and "replaced" or "" |
paul@856 | 637 | page.td(start, class_=css) |
paul@856 | 638 | page.td(end, class_=css) |
paul@766 | 639 | |
paul@837 | 640 | # Show attendees and controls alongside the first period in each |
paul@837 | 641 | # attendee's collection. |
paul@819 | 642 | |
paul@837 | 643 | if first: |
paul@837 | 644 | page.td(attendee, rowspan=len(periods)) |
paul@837 | 645 | page.td(rowspan=len(periods)) |
paul@837 | 646 | self.control("accept-%d" % i, "submit", "Accept") |
paul@837 | 647 | self.control("decline-%d" % i, "submit", "Decline") |
paul@837 | 648 | self.control("counter", "hidden", attendee) |
paul@837 | 649 | page.td.close() |
paul@809 | 650 | |
paul@837 | 651 | page.tr.close() |
paul@837 | 652 | first = False |
paul@766 | 653 | |
paul@837 | 654 | page.tbody.close() |
paul@837 | 655 | page.table.close() |
paul@766 | 656 | |
paul@756 | 657 | def show_conflicting_events(self): |
paul@446 | 658 | |
paul@756 | 659 | "Show conflicting events for the current object." |
paul@446 | 660 | |
paul@446 | 661 | page = self.page |
paul@756 | 662 | recurrenceids = self._get_active_recurrences(self.uid) |
paul@446 | 663 | |
paul@446 | 664 | # Obtain the user's timezone. |
paul@446 | 665 | |
paul@446 | 666 | tzid = self.get_tzid() |
paul@756 | 667 | periods = self.get_periods(self.obj) |
paul@446 | 668 | |
paul@446 | 669 | # Indicate whether there are conflicting events. |
paul@446 | 670 | |
paul@844 | 671 | conflicts = set() |
paul@756 | 672 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@446 | 673 | |
paul@839 | 674 | for name, participant in uri_parts(self.get_current_attendees()): |
paul@484 | 675 | if participant == self.user: |
paul@484 | 676 | freebusy = self.store.get_freebusy(participant) |
paul@787 | 677 | elif participant: |
paul@787 | 678 | freebusy = self.store.get_freebusy_for_other(self.user, participant) |
paul@484 | 679 | else: |
paul@787 | 680 | continue |
paul@484 | 681 | |
paul@484 | 682 | if not freebusy: |
paul@484 | 683 | continue |
paul@446 | 684 | |
paul@446 | 685 | # Obtain any time zone details from the suggested event. |
paul@446 | 686 | |
paul@756 | 687 | _dtstart, attr = self.obj.get_item("DTSTART") |
paul@446 | 688 | tzid = attr.get("TZID", tzid) |
paul@446 | 689 | |
paul@484 | 690 | # Show any conflicts with periods of actual attendance. |
paul@446 | 691 | |
paul@611 | 692 | participant_attr = attendee_map.get(participant) |
paul@611 | 693 | partstat = participant_attr and participant_attr.get("PARTSTAT") |
paul@756 | 694 | recurrences = self.obj.get_recurrence_start_points(recurrenceids, tzid) |
paul@611 | 695 | |
paul@484 | 696 | for p in have_conflict(freebusy, periods, True): |
paul@756 | 697 | if not self.recurrenceid and p.is_replaced(recurrences): |
paul@528 | 698 | continue |
paul@611 | 699 | |
paul@611 | 700 | if ( # Unidentified or different event |
paul@756 | 701 | (p.uid != self.uid or self.recurrenceid and p.recurrenceid and p.recurrenceid != self.recurrenceid) and |
paul@611 | 702 | # Different period or unclear participation with the same period |
paul@611 | 703 | (p not in periods or not partstat in ("ACCEPTED", "TENTATIVE")) and |
paul@611 | 704 | # Participant not limited to organising |
paul@611 | 705 | p.transp != "ORG" |
paul@611 | 706 | ): |
paul@611 | 707 | |
paul@844 | 708 | conflicts.add(p) |
paul@446 | 709 | |
paul@844 | 710 | conflicts = list(conflicts) |
paul@484 | 711 | conflicts.sort() |
paul@484 | 712 | |
paul@484 | 713 | # Show any conflicts with periods of actual attendance. |
paul@446 | 714 | |
paul@484 | 715 | if conflicts: |
paul@484 | 716 | page.p("This event conflicts with others:") |
paul@446 | 717 | |
paul@484 | 718 | page.table(cellspacing=5, cellpadding=5, class_="conflicts") |
paul@484 | 719 | page.thead() |
paul@484 | 720 | page.tr() |
paul@484 | 721 | page.th("Event") |
paul@484 | 722 | page.th("Start") |
paul@484 | 723 | page.th("End") |
paul@484 | 724 | page.tr.close() |
paul@484 | 725 | page.thead.close() |
paul@484 | 726 | page.tbody() |
paul@446 | 727 | |
paul@484 | 728 | for p in conflicts: |
paul@484 | 729 | |
paul@484 | 730 | # Provide details of any conflicting event. |
paul@446 | 731 | |
paul@535 | 732 | start = self.format_datetime(to_timezone(p.get_start(), tzid), "long") |
paul@535 | 733 | end = self.format_datetime(to_timezone(p.get_end(), tzid), "long") |
paul@446 | 734 | |
paul@484 | 735 | page.tr() |
paul@446 | 736 | |
paul@484 | 737 | # Show the event summary for the conflicting event. |
paul@446 | 738 | |
paul@484 | 739 | page.td() |
paul@484 | 740 | if p.summary: |
paul@488 | 741 | page.a(p.summary, href=self.link_to(p.uid, p.recurrenceid)) |
paul@484 | 742 | else: |
paul@484 | 743 | page.add("(Unspecified event)") |
paul@484 | 744 | page.td.close() |
paul@446 | 745 | |
paul@484 | 746 | page.td(start) |
paul@484 | 747 | page.td(end) |
paul@446 | 748 | |
paul@484 | 749 | page.tr.close() |
paul@446 | 750 | |
paul@484 | 751 | page.tbody.close() |
paul@484 | 752 | page.table.close() |
paul@446 | 753 | |
paul@765 | 754 | class EventPage(EventPageFragment): |
paul@765 | 755 | |
paul@765 | 756 | "A request handler for the event page." |
paul@765 | 757 | |
paul@765 | 758 | def __init__(self, resource=None, messenger=None): |
paul@807 | 759 | ResourceClientForObject.__init__(self, resource, messenger or Messenger()) |
paul@474 | 760 | |
paul@877 | 761 | def link_to(self, uid=None, recurrenceid=None): |
paul@877 | 762 | args = self.env.get_query() |
paul@877 | 763 | d = {} |
paul@877 | 764 | for name in ("start", "end"): |
paul@877 | 765 | if args.get(name): |
paul@877 | 766 | d[name] = args[name][0] |
paul@877 | 767 | return ResourceClientForObject.link_to(self, uid, recurrenceid, d) |
paul@877 | 768 | |
paul@765 | 769 | # Request logic methods. |
paul@765 | 770 | |
paul@765 | 771 | def is_initial_load(self): |
paul@765 | 772 | |
paul@765 | 773 | "Return whether the event is being loaded and shown for the first time." |
paul@765 | 774 | |
paul@765 | 775 | return not self.env.get_args().has_key("editing") |
paul@765 | 776 | |
paul@765 | 777 | def handle_request(self): |
paul@474 | 778 | |
paul@474 | 779 | """ |
paul@765 | 780 | Handle actions involving the current object, returning an error if one |
paul@765 | 781 | occurred, or None if the request was successfully handled. |
paul@474 | 782 | """ |
paul@474 | 783 | |
paul@765 | 784 | # Handle a submitted form. |
paul@498 | 785 | |
paul@474 | 786 | args = self.env.get_args() |
paul@765 | 787 | |
paul@765 | 788 | # Get the possible actions. |
paul@765 | 789 | |
paul@765 | 790 | reply = args.has_key("reply") |
paul@765 | 791 | discard = args.has_key("discard") |
paul@765 | 792 | create = args.has_key("create") |
paul@765 | 793 | cancel = args.has_key("cancel") |
paul@765 | 794 | ignore = args.has_key("ignore") |
paul@765 | 795 | save = args.has_key("save") |
paul@841 | 796 | uncounter = args.has_key("uncounter") |
paul@813 | 797 | accept = self.prefixed_args("accept-", int) |
paul@813 | 798 | decline = self.prefixed_args("decline-", int) |
paul@809 | 799 | |
paul@841 | 800 | have_action = reply or discard or create or cancel or ignore or save or accept or decline or uncounter |
paul@765 | 801 | |
paul@765 | 802 | if not have_action: |
paul@765 | 803 | return ["action"] |
paul@474 | 804 | |
paul@765 | 805 | # If ignoring the object, return to the calendar. |
paul@474 | 806 | |
paul@765 | 807 | if ignore: |
paul@877 | 808 | self.redirect(self.link_to()) |
paul@765 | 809 | return None |
paul@765 | 810 | |
paul@765 | 811 | # Update the object. |
paul@474 | 812 | |
paul@765 | 813 | single_user = False |
paul@818 | 814 | changed = False |
paul@765 | 815 | |
paul@765 | 816 | if reply or create or cancel or save: |
paul@765 | 817 | |
paul@867 | 818 | # Update time periods (main and recurring). |
paul@765 | 819 | |
paul@867 | 820 | try: |
paul@867 | 821 | period = self.handle_main_period() |
paul@867 | 822 | except PeriodError, exc: |
paul@867 | 823 | return exc.args |
paul@474 | 824 | |
paul@867 | 825 | try: |
paul@867 | 826 | periods = self.handle_recurrence_periods() |
paul@867 | 827 | except PeriodError, exc: |
paul@867 | 828 | return exc.args |
paul@474 | 829 | |
paul@867 | 830 | # Set the periods in the object, first obtaining removed and |
paul@867 | 831 | # modified period information. |
paul@872 | 832 | # NOTE: Currently, rules are not updated. |
paul@765 | 833 | |
paul@867 | 834 | to_unschedule, to_exclude = self.get_removed_periods(periods) |
paul@867 | 835 | periods = set(periods) |
paul@878 | 836 | active_periods = [p for p in periods if not p.replaced] |
paul@765 | 837 | |
paul@867 | 838 | changed = self.obj.set_period(period) or changed |
paul@867 | 839 | changed = self.obj.set_periods(periods) or changed |
paul@878 | 840 | |
paul@878 | 841 | # Add and remove exceptions. |
paul@878 | 842 | |
paul@878 | 843 | changed = self.obj.update_exceptions(to_exclude, active_periods) or changed |
paul@868 | 844 | |
paul@868 | 845 | # Assert periods restored after cancellation. |
paul@868 | 846 | |
paul@878 | 847 | changed = self.revert_cancellations(active_periods) or changed |
paul@474 | 848 | |
paul@867 | 849 | # Organiser-only changes... |
paul@818 | 850 | |
paul@867 | 851 | if self.is_organiser(): |
paul@818 | 852 | |
paul@867 | 853 | # Update summary. |
paul@765 | 854 | |
paul@867 | 855 | if args.has_key("summary"): |
paul@867 | 856 | self.obj["SUMMARY"] = [(args["summary"][0], {})] |
paul@765 | 857 | |
paul@867 | 858 | # Obtain any new participants and those to be removed. |
paul@474 | 859 | |
paul@867 | 860 | attendees = self.get_attendees_from_page() |
paul@867 | 861 | removed = [attendees[int(i)] for i in args.get("remove", [])] |
paul@867 | 862 | added, to_cancel = self.update_attendees(attendees, removed) |
paul@867 | 863 | single_user = not attendees or attendees == [self.user] |
paul@867 | 864 | changed = added or changed |
paul@765 | 865 | |
paul@765 | 866 | # Update attendee participation for the current user. |
paul@474 | 867 | |
paul@765 | 868 | if args.has_key("partstat"): |
paul@818 | 869 | self.update_participation(args["partstat"][0]) |
paul@765 | 870 | |
paul@765 | 871 | # Process any action. |
paul@474 | 872 | |
paul@765 | 873 | invite = not save and create and not single_user |
paul@765 | 874 | save = save or create and single_user |
paul@474 | 875 | |
paul@765 | 876 | handled = True |
paul@474 | 877 | |
paul@765 | 878 | if reply or invite or cancel: |
paul@765 | 879 | |
paul@765 | 880 | # Process the object and remove it from the list of requests. |
paul@765 | 881 | |
paul@818 | 882 | if reply and self.process_received_request(changed): |
paul@917 | 883 | if self.has_indicated_attendance(): |
paul@917 | 884 | self.remove_request() |
paul@765 | 885 | |
paul@765 | 886 | elif self.is_organiser() and (invite or cancel): |
paul@765 | 887 | |
paul@765 | 888 | # Invitation, uninvitation and unscheduling... |
paul@765 | 889 | |
paul@807 | 890 | if self.process_created_request( |
paul@765 | 891 | invite and "REQUEST" or "CANCEL", to_cancel, to_unschedule): |
paul@765 | 892 | |
paul@813 | 893 | self.remove_request() |
paul@474 | 894 | |
paul@765 | 895 | # Save single user events. |
paul@765 | 896 | |
paul@765 | 897 | elif save: |
paul@765 | 898 | self.store.set_event(self.user, self.uid, self.recurrenceid, node=self.obj.to_node()) |
paul@765 | 899 | self.update_event_in_freebusy() |
paul@813 | 900 | self.remove_request() |
paul@474 | 901 | |
paul@765 | 902 | # Remove the request and the object. |
paul@474 | 903 | |
paul@765 | 904 | elif discard: |
paul@765 | 905 | self.remove_event_from_freebusy() |
paul@813 | 906 | self.remove_event() |
paul@813 | 907 | self.remove_request() |
paul@813 | 908 | |
paul@813 | 909 | # Update counter-proposal records synchronously instead of assuming |
paul@813 | 910 | # that the outgoing handler will have done so before the form is |
paul@813 | 911 | # refreshed. |
paul@813 | 912 | |
paul@813 | 913 | # Accept a counter-proposal and decline all others, sending a new |
paul@813 | 914 | # request to all attendees. |
paul@813 | 915 | |
paul@813 | 916 | elif accept: |
paul@474 | 917 | |
paul@813 | 918 | # Take the first accepted proposal, although there should be only |
paul@813 | 919 | # one anyway. |
paul@813 | 920 | |
paul@813 | 921 | for i in accept: |
paul@813 | 922 | attendee_uri = get_uri(args.get("counter", [])[i]) |
paul@813 | 923 | obj = self.get_stored_object(self.uid, self.recurrenceid, "counters", attendee_uri) |
paul@813 | 924 | self.obj.set_periods(self.get_periods(obj)) |
paul@872 | 925 | self.obj.set_rule(obj.get_item("RRULE")) |
paul@872 | 926 | self.obj.set_exceptions(obj.get_items("EXDATE")) |
paul@813 | 927 | break |
paul@813 | 928 | |
paul@813 | 929 | # Remove counter-proposals and issue a new invitation. |
paul@813 | 930 | |
paul@813 | 931 | attendees = uri_values(args.get("counter", [])) |
paul@813 | 932 | self.remove_counters(attendees) |
paul@813 | 933 | self.process_created_request("REQUEST") |
paul@813 | 934 | |
paul@813 | 935 | # Decline a counter-proposal individually. |
paul@811 | 936 | |
paul@811 | 937 | elif decline: |
paul@813 | 938 | for i in decline: |
paul@813 | 939 | attendee_uri = get_uri(args.get("counter", [])[i]) |
paul@813 | 940 | self.process_declined_counter(attendee_uri) |
paul@813 | 941 | self.remove_counter(attendee_uri) |
paul@811 | 942 | |
paul@811 | 943 | # Redirect to the event. |
paul@811 | 944 | |
paul@811 | 945 | self.redirect(self.env.get_url()) |
paul@811 | 946 | handled = False |
paul@811 | 947 | |
paul@841 | 948 | # Remove counter-proposals without acknowledging them. |
paul@841 | 949 | |
paul@841 | 950 | elif uncounter: |
paul@841 | 951 | self.store.remove_counters(self.user, self.uid, self.recurrenceid) |
paul@841 | 952 | self.remove_request() |
paul@841 | 953 | |
paul@841 | 954 | # Redirect to the event. |
paul@841 | 955 | |
paul@841 | 956 | self.redirect(self.env.get_url()) |
paul@841 | 957 | handled = False |
paul@841 | 958 | |
paul@474 | 959 | else: |
paul@765 | 960 | handled = False |
paul@765 | 961 | |
paul@765 | 962 | # Upon handling an action, redirect to the main page. |
paul@765 | 963 | |
paul@765 | 964 | if handled: |
paul@877 | 965 | self.redirect(self.link_to()) |
paul@765 | 966 | |
paul@765 | 967 | return None |
paul@765 | 968 | |
paul@765 | 969 | def handle_main_period(self): |
paul@765 | 970 | |
paul@765 | 971 | "Return period details for the main start/end period in an event." |
paul@765 | 972 | |
paul@765 | 973 | return self.get_main_period_from_page().as_event_period() |
paul@765 | 974 | |
paul@765 | 975 | def handle_recurrence_periods(self): |
paul@765 | 976 | |
paul@765 | 977 | "Return period details for the recurrences specified for an event." |
paul@765 | 978 | |
paul@867 | 979 | return [p.as_event_period(i) for i, p in enumerate(self.get_recurrences_from_page())] |
paul@765 | 980 | |
paul@765 | 981 | # Access to form-originating object information. |
paul@765 | 982 | |
paul@765 | 983 | def get_main_period_from_page(self): |
paul@765 | 984 | |
paul@765 | 985 | "Return the main period defined in the event form." |
paul@765 | 986 | |
paul@765 | 987 | args = self.env.get_args() |
paul@765 | 988 | |
paul@765 | 989 | dtend_enabled = args.get("dtend-control", [None])[0] |
paul@765 | 990 | dttimes_enabled = args.get("dttimes-control", [None])[0] |
paul@765 | 991 | start = self.get_date_control_values("dtstart") |
paul@765 | 992 | end = self.get_date_control_values("dtend") |
paul@765 | 993 | |
paul@813 | 994 | period = FormPeriod(start, end, dtend_enabled, dttimes_enabled, self.get_tzid(), "DTSTART") |
paul@785 | 995 | |
paul@785 | 996 | # Handle absent main period details. |
paul@785 | 997 | |
paul@785 | 998 | if not period.get_start(): |
paul@785 | 999 | return self.get_stored_main_period() |
paul@785 | 1000 | else: |
paul@785 | 1001 | return period |
paul@474 | 1002 | |
paul@765 | 1003 | def get_recurrences_from_page(self): |
paul@765 | 1004 | |
paul@765 | 1005 | "Return the recurrences defined in the event form." |
paul@765 | 1006 | |
paul@765 | 1007 | args = self.env.get_args() |
paul@765 | 1008 | |
paul@765 | 1009 | all_dtend_enabled = args.get("dtend-control-recur", []) |
paul@765 | 1010 | all_dttimes_enabled = args.get("dttimes-control-recur", []) |
paul@765 | 1011 | all_starts = self.get_date_control_values("dtstart-recur", multiple=True) |
paul@765 | 1012 | all_ends = self.get_date_control_values("dtend-recur", multiple=True, tzid_name="dtstart-recur") |
paul@765 | 1013 | all_origins = args.get("recur-origin", []) |
paul@868 | 1014 | all_replaced = args.get("recur-replaced", []) |
paul@765 | 1015 | |
paul@765 | 1016 | periods = [] |
paul@765 | 1017 | |
paul@868 | 1018 | for index, (start, end, origin) in \ |
paul@868 | 1019 | enumerate(map(None, all_starts, all_ends, all_origins)): |
paul@765 | 1020 | |
paul@765 | 1021 | dtend_enabled = str(index) in all_dtend_enabled |
paul@765 | 1022 | dttimes_enabled = str(index) in all_dttimes_enabled |
paul@868 | 1023 | replaced = str(index) in all_replaced |
paul@868 | 1024 | |
paul@868 | 1025 | period = FormPeriod(start, end, dtend_enabled, dttimes_enabled, self.get_tzid(), origin, replaced) |
paul@765 | 1026 | periods.append(period) |
paul@765 | 1027 | |
paul@765 | 1028 | return periods |
paul@765 | 1029 | |
paul@787 | 1030 | def set_recurrences_in_page(self, recurrences): |
paul@787 | 1031 | |
paul@787 | 1032 | "Set the recurrences defined in the event form." |
paul@787 | 1033 | |
paul@787 | 1034 | args = self.env.get_args() |
paul@787 | 1035 | |
paul@787 | 1036 | args["dtend-control-recur"] = [] |
paul@787 | 1037 | args["dttimes-control-recur"] = [] |
paul@787 | 1038 | args["recur-origin"] = [] |
paul@868 | 1039 | args["recur-replaced"] = [] |
paul@787 | 1040 | |
paul@787 | 1041 | all_starts = [] |
paul@787 | 1042 | all_ends = [] |
paul@787 | 1043 | |
paul@787 | 1044 | for index, period in enumerate(recurrences): |
paul@787 | 1045 | if period.end_enabled: |
paul@787 | 1046 | args["dtend-control-recur"].append(str(index)) |
paul@787 | 1047 | if period.times_enabled: |
paul@787 | 1048 | args["dttimes-control-recur"].append(str(index)) |
paul@868 | 1049 | if period.replaced: |
paul@868 | 1050 | args["recur-replaced"].append(str(index)) |
paul@787 | 1051 | args["recur-origin"].append(period.origin or "") |
paul@787 | 1052 | |
paul@787 | 1053 | all_starts.append(period.get_form_start()) |
paul@787 | 1054 | all_ends.append(period.get_form_end()) |
paul@787 | 1055 | |
paul@787 | 1056 | self.set_date_control_values("dtstart-recur", all_starts) |
paul@787 | 1057 | self.set_date_control_values("dtend-recur", all_ends, tzid_name="dtstart-recur") |
paul@787 | 1058 | |
paul@783 | 1059 | def get_removed_periods(self, periods): |
paul@765 | 1060 | |
paul@783 | 1061 | """ |
paul@783 | 1062 | Return those from the recurrence 'periods' to remove upon updating an |
paul@784 | 1063 | event along with those to exclude in a tuple of the form (unscheduled, |
paul@784 | 1064 | excluded). |
paul@783 | 1065 | """ |
paul@765 | 1066 | |
paul@784 | 1067 | args = self.env.get_args() |
paul@765 | 1068 | to_unschedule = [] |
paul@784 | 1069 | to_exclude = [] |
paul@784 | 1070 | |
paul@765 | 1071 | for i in args.get("recur-remove", []): |
paul@784 | 1072 | try: |
paul@784 | 1073 | period = periods[int(i)] |
paul@784 | 1074 | except (IndexError, ValueError): |
paul@784 | 1075 | continue |
paul@784 | 1076 | |
paul@871 | 1077 | if not self.can_edit_recurrence(period) and self.is_organiser(): |
paul@784 | 1078 | to_unschedule.append(period) |
paul@784 | 1079 | else: |
paul@784 | 1080 | to_exclude.append(period) |
paul@784 | 1081 | |
paul@784 | 1082 | return to_unschedule, to_exclude |
paul@765 | 1083 | |
paul@765 | 1084 | def get_attendees_from_page(self): |
paul@474 | 1085 | |
paul@809 | 1086 | """ |
paul@809 | 1087 | Return attendees from the request, using any stored attributes to obtain |
paul@809 | 1088 | verbose details. |
paul@809 | 1089 | """ |
paul@809 | 1090 | |
paul@809 | 1091 | return self.get_verbose_attendees(self.env.get_args().get("attendee", [])) |
paul@474 | 1092 | |
paul@809 | 1093 | def get_verbose_attendees(self, attendees): |
paul@809 | 1094 | |
paul@809 | 1095 | """ |
paul@809 | 1096 | Use any stored attributes to obtain verbose details for the given |
paul@809 | 1097 | 'attendees'. |
paul@809 | 1098 | """ |
paul@809 | 1099 | |
paul@809 | 1100 | attendee_map = self.obj.get_value_map("ATTENDEE") |
paul@809 | 1101 | return [get_verbose_address(value, attendee_map.get(value)) for value in attendees] |
paul@765 | 1102 | |
paul@765 | 1103 | def update_attendees_from_page(self): |
paul@765 | 1104 | |
paul@765 | 1105 | "Add or remove attendees. This does not affect the stored object." |
paul@765 | 1106 | |
paul@765 | 1107 | args = self.env.get_args() |
paul@765 | 1108 | |
paul@765 | 1109 | attendees = self.get_attendees_from_page() |
paul@474 | 1110 | |
paul@765 | 1111 | if args.has_key("add"): |
paul@765 | 1112 | attendees.append("") |
paul@517 | 1113 | |
paul@838 | 1114 | # Add attendees suggested in counter-proposals. |
paul@838 | 1115 | |
paul@838 | 1116 | add_suggested = self.prefixed_args("add-suggested-attendee-", int) |
paul@838 | 1117 | |
paul@838 | 1118 | if add_suggested: |
paul@838 | 1119 | for i in add_suggested: |
paul@838 | 1120 | try: |
paul@838 | 1121 | suggested = args["suggested-attendee"][i] |
paul@838 | 1122 | except (IndexError, KeyError): |
paul@838 | 1123 | continue |
paul@838 | 1124 | if suggested not in attendees: |
paul@838 | 1125 | attendees.append(suggested) |
paul@838 | 1126 | |
paul@765 | 1127 | # Only actually remove attendees if the event is unsent, if the attendee |
paul@765 | 1128 | # is new, or if it is the current user being removed. |
paul@765 | 1129 | |
paul@765 | 1130 | if args.has_key("remove"): |
paul@765 | 1131 | still_to_remove = [] |
paul@795 | 1132 | correction = 0 |
paul@517 | 1133 | |
paul@765 | 1134 | for i in args["remove"]: |
paul@765 | 1135 | try: |
paul@795 | 1136 | i = int(i) - correction |
paul@795 | 1137 | attendee = attendees[i] |
paul@795 | 1138 | except (IndexError, ValueError): |
paul@765 | 1139 | continue |
paul@474 | 1140 | |
paul@795 | 1141 | if self.can_remove_attendee(get_uri(attendee)): |
paul@795 | 1142 | del attendees[i] |
paul@795 | 1143 | correction += 1 |
paul@765 | 1144 | else: |
paul@795 | 1145 | still_to_remove.append(str(i)) |
paul@474 | 1146 | |
paul@765 | 1147 | args["remove"] = still_to_remove |
paul@765 | 1148 | |
paul@787 | 1149 | args["attendee"] = attendees |
paul@765 | 1150 | return attendees |
paul@498 | 1151 | |
paul@780 | 1152 | def update_recurrences_from_page(self): |
paul@780 | 1153 | |
paul@780 | 1154 | "Add or remove recurrences. This does not affect the stored object." |
paul@780 | 1155 | |
paul@780 | 1156 | args = self.env.get_args() |
paul@780 | 1157 | |
paul@780 | 1158 | recurrences = self.get_recurrences_from_page() |
paul@780 | 1159 | |
paul@845 | 1160 | if args.has_key("recur-add"): |
paul@845 | 1161 | period = self.get_current_main_period().as_form_period() |
paul@845 | 1162 | period.origin = "RDATE" |
paul@845 | 1163 | recurrences.append(period) |
paul@780 | 1164 | |
paul@780 | 1165 | # Only actually remove recurrences if the event is unsent, or if the |
paul@784 | 1166 | # recurrence is new, but only for explicit recurrences. |
paul@780 | 1167 | |
paul@780 | 1168 | if args.has_key("recur-remove"): |
paul@780 | 1169 | still_to_remove = [] |
paul@795 | 1170 | correction = 0 |
paul@780 | 1171 | |
paul@780 | 1172 | for i in args["recur-remove"]: |
paul@780 | 1173 | try: |
paul@795 | 1174 | i = int(i) - correction |
paul@795 | 1175 | recurrence = recurrences[i] |
paul@795 | 1176 | except (IndexError, ValueError): |
paul@780 | 1177 | continue |
paul@780 | 1178 | |
paul@854 | 1179 | if self.can_remove_recurrence(recurrence): |
paul@795 | 1180 | del recurrences[i] |
paul@795 | 1181 | correction += 1 |
paul@780 | 1182 | else: |
paul@795 | 1183 | still_to_remove.append(str(i)) |
paul@780 | 1184 | |
paul@780 | 1185 | args["recur-remove"] = still_to_remove |
paul@780 | 1186 | |
paul@787 | 1187 | self.set_recurrences_in_page(recurrences) |
paul@780 | 1188 | return recurrences |
paul@780 | 1189 | |
paul@765 | 1190 | # Access to current object information. |
paul@765 | 1191 | |
paul@765 | 1192 | def get_current_main_period(self): |
paul@498 | 1193 | |
paul@498 | 1194 | """ |
paul@765 | 1195 | Return the currently active main period for the current object depending |
paul@765 | 1196 | on whether editing has begun or whether the object has just been loaded. |
paul@765 | 1197 | """ |
paul@498 | 1198 | |
paul@867 | 1199 | if self.is_initial_load(): |
paul@765 | 1200 | return self.get_stored_main_period() |
paul@765 | 1201 | else: |
paul@765 | 1202 | return self.get_main_period_from_page() |
paul@765 | 1203 | |
paul@765 | 1204 | def get_current_recurrences(self): |
paul@765 | 1205 | |
paul@765 | 1206 | """ |
paul@765 | 1207 | Return recurrences for the current object using the original object |
paul@765 | 1208 | details where no editing is in progress, using form data otherwise. |
paul@498 | 1209 | """ |
paul@498 | 1210 | |
paul@867 | 1211 | if self.is_initial_load(): |
paul@765 | 1212 | return self.get_stored_recurrences() |
paul@765 | 1213 | else: |
paul@765 | 1214 | return self.get_recurrences_from_page() |
paul@498 | 1215 | |
paul@780 | 1216 | def update_current_recurrences(self): |
paul@780 | 1217 | |
paul@780 | 1218 | "Return an updated collection of recurrences for the current object." |
paul@780 | 1219 | |
paul@867 | 1220 | if self.is_initial_load(): |
paul@780 | 1221 | return self.get_stored_recurrences() |
paul@780 | 1222 | else: |
paul@780 | 1223 | return self.update_recurrences_from_page() |
paul@780 | 1224 | |
paul@765 | 1225 | def get_current_attendees(self): |
paul@765 | 1226 | |
paul@765 | 1227 | """ |
paul@765 | 1228 | Return attendees for the current object depending on whether the object |
paul@765 | 1229 | has been edited or instead provides such information from its stored |
paul@765 | 1230 | form. |
paul@765 | 1231 | """ |
paul@499 | 1232 | |
paul@867 | 1233 | if self.is_initial_load(): |
paul@765 | 1234 | return self.get_stored_attendees() |
paul@765 | 1235 | else: |
paul@765 | 1236 | return self.get_attendees_from_page() |
paul@765 | 1237 | |
paul@765 | 1238 | def update_current_attendees(self): |
paul@498 | 1239 | |
paul@765 | 1240 | "Return an updated collection of attendees for the current object." |
paul@765 | 1241 | |
paul@867 | 1242 | if self.is_initial_load(): |
paul@765 | 1243 | return self.get_stored_attendees() |
paul@498 | 1244 | else: |
paul@765 | 1245 | return self.update_attendees_from_page() |
paul@474 | 1246 | |
paul@446 | 1247 | # Full page output methods. |
paul@446 | 1248 | |
paul@446 | 1249 | def show(self, path_info): |
paul@446 | 1250 | |
paul@446 | 1251 | "Show an object request using the given 'path_info' for the current user." |
paul@446 | 1252 | |
paul@763 | 1253 | uid, recurrenceid = self.get_identifiers(path_info) |
paul@763 | 1254 | obj = self.get_stored_object(uid, recurrenceid) |
paul@756 | 1255 | self.set_object(obj) |
paul@446 | 1256 | |
paul@446 | 1257 | if not obj: |
paul@446 | 1258 | return False |
paul@446 | 1259 | |
paul@756 | 1260 | errors = self.handle_request() |
paul@446 | 1261 | |
paul@492 | 1262 | if not errors: |
paul@446 | 1263 | return True |
paul@446 | 1264 | |
paul@787 | 1265 | self.update_current_attendees() |
paul@787 | 1266 | self.update_current_recurrences() |
paul@787 | 1267 | |
paul@446 | 1268 | self.new_page(title="Event") |
paul@756 | 1269 | self.show_object_on_page(errors) |
paul@446 | 1270 | |
paul@446 | 1271 | return True |
paul@446 | 1272 | |
paul@446 | 1273 | # vim: tabstop=4 expandtab shiftwidth=4 |