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