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