paul@446 | 1 | #!/usr/bin/env python |
paul@446 | 2 | |
paul@446 | 3 | """ |
paul@446 | 4 | A Web interface to a calendar event. |
paul@446 | 5 | |
paul@446 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@446 | 7 | |
paul@446 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@446 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@446 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@446 | 11 | version. |
paul@446 | 12 | |
paul@446 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@446 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@446 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@446 | 16 | details. |
paul@446 | 17 | |
paul@446 | 18 | You should have received a copy of the GNU General Public License along with |
paul@446 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@446 | 20 | """ |
paul@446 | 21 | |
paul@757 | 22 | from imiptools.data import get_uri, uri_dict, uri_items, uri_values |
paul@764 | 23 | from imiptools.dates import to_timezone |
paul@446 | 24 | from imiptools.mail import Messenger |
paul@529 | 25 | from imiptools.period import have_conflict |
paul@765 | 26 | from imipweb.data import EventPeriod, event_period_from_period, FormPeriod, PeriodError |
paul@601 | 27 | from imipweb.client import ManagerClient |
paul@765 | 28 | from imipweb.resource import DateTimeFormUtilities, FormUtilities, ResourceClientForObject |
paul@446 | 29 | |
paul@765 | 30 | class EventPageFragment(ResourceClientForObject, DateTimeFormUtilities, FormUtilities): |
paul@446 | 31 | |
paul@765 | 32 | "A resource presenting the details of an event." |
paul@765 | 33 | |
paul@765 | 34 | def __init__(self, resource=None): |
paul@756 | 35 | ResourceClientForObject.__init__(self, resource) |
paul@446 | 36 | |
paul@474 | 37 | # Various property values and labels. |
paul@474 | 38 | |
paul@474 | 39 | property_items = [ |
paul@474 | 40 | ("SUMMARY", "Summary"), |
paul@474 | 41 | ("DTSTART", "Start"), |
paul@474 | 42 | ("DTEND", "End"), |
paul@474 | 43 | ("ORGANIZER", "Organiser"), |
paul@474 | 44 | ("ATTENDEE", "Attendee"), |
paul@474 | 45 | ] |
paul@474 | 46 | |
paul@474 | 47 | partstat_items = [ |
paul@474 | 48 | ("NEEDS-ACTION", "Not confirmed"), |
paul@474 | 49 | ("ACCEPTED", "Attending"), |
paul@474 | 50 | ("TENTATIVE", "Tentatively attending"), |
paul@474 | 51 | ("DECLINED", "Not attending"), |
paul@474 | 52 | ("DELEGATED", "Delegated"), |
paul@474 | 53 | (None, "Not indicated"), |
paul@474 | 54 | ] |
paul@474 | 55 | |
paul@756 | 56 | def is_organiser(self): |
paul@756 | 57 | return get_uri(self.obj.get_value("ORGANIZER")) == self.user |
paul@619 | 58 | |
paul@758 | 59 | def can_remove_attendee(self, attendee): |
paul@758 | 60 | |
paul@758 | 61 | """ |
paul@758 | 62 | Return whether 'attendee' can be removed from the current object without |
paul@758 | 63 | notification. |
paul@758 | 64 | """ |
paul@758 | 65 | |
paul@758 | 66 | return self.can_edit_attendee(attendee) or attendee == self.user |
paul@758 | 67 | |
paul@758 | 68 | def can_edit_attendee(self, attendee): |
paul@758 | 69 | |
paul@758 | 70 | "Return whether 'attendee' can be edited by an organiser." |
paul@758 | 71 | |
paul@758 | 72 | return self.attendee_is_new(attendee) or not self.obj.is_shared() |
paul@758 | 73 | |
paul@758 | 74 | def attendee_is_new(self, attendee): |
paul@758 | 75 | |
paul@758 | 76 | "Return whether 'attendee' is new to the current object." |
paul@758 | 77 | |
paul@758 | 78 | return attendee not in self.get_stored_attendees() |
paul@758 | 79 | |
paul@765 | 80 | # Access to stored object information. |
paul@477 | 81 | |
paul@765 | 82 | def get_stored_attendees(self): |
paul@765 | 83 | return uri_values(self.obj.get_values("ATTENDEE") or []) |
paul@477 | 84 | |
paul@765 | 85 | def get_stored_main_period(self): |
paul@477 | 86 | |
paul@765 | 87 | "Return the main event period for the current object." |
paul@477 | 88 | |
paul@765 | 89 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.obj.get_main_period_items(self.get_tzid()) |
paul@765 | 90 | return EventPeriod(dtstart, dtend, self.get_tzid(), None, dtstart_attr, dtend_attr) |
paul@477 | 91 | |
paul@765 | 92 | def get_stored_recurrences(self): |
paul@765 | 93 | |
paul@765 | 94 | "Return recurrences computed using the current object." |
paul@485 | 95 | |
paul@765 | 96 | recurrences = [] |
paul@765 | 97 | for period in self.get_periods(self.obj): |
paul@765 | 98 | if period.origin != "DTSTART": |
paul@765 | 99 | recurrences.append(period) |
paul@765 | 100 | return recurrences |
paul@757 | 101 | |
paul@765 | 102 | # Access to current object information. |
paul@496 | 103 | |
paul@765 | 104 | def get_current_main_period(self): |
paul@765 | 105 | return self.get_stored_main_period() |
paul@757 | 106 | |
paul@765 | 107 | def get_current_recurrences(self): |
paul@765 | 108 | return self.get_stored_recurrences() |
paul@477 | 109 | |
paul@765 | 110 | def get_current_attendees(self): |
paul@765 | 111 | return self.get_stored_attendees() |
paul@477 | 112 | |
paul@446 | 113 | # Page fragment methods. |
paul@446 | 114 | |
paul@756 | 115 | def show_request_controls(self): |
paul@446 | 116 | |
paul@756 | 117 | "Show form controls for a request." |
paul@446 | 118 | |
paul@446 | 119 | page = self.page |
paul@446 | 120 | args = self.env.get_args() |
paul@446 | 121 | |
paul@756 | 122 | attendees = self.get_current_attendees() |
paul@446 | 123 | is_attendee = self.user in attendees |
paul@756 | 124 | is_request = self._have_request(self.uid, self.recurrenceid) |
paul@446 | 125 | |
paul@446 | 126 | # Show appropriate options depending on the role of the user. |
paul@446 | 127 | |
paul@756 | 128 | if is_attendee and not self.is_organiser(): |
paul@446 | 129 | page.p("An action is required for this request:") |
paul@446 | 130 | |
paul@446 | 131 | page.p() |
paul@764 | 132 | self.control("reply", "submit", "Send reply") |
paul@446 | 133 | page.add(" ") |
paul@764 | 134 | self.control("discard", "submit", "Discard event") |
paul@446 | 135 | page.add(" ") |
paul@764 | 136 | self.control("ignore", "submit", "Do nothing for now") |
paul@446 | 137 | page.p.close() |
paul@446 | 138 | |
paul@756 | 139 | if self.is_organiser(): |
paul@446 | 140 | page.p("As organiser, you can perform the following:") |
paul@446 | 141 | |
paul@512 | 142 | page.p() |
paul@764 | 143 | self.control("create", "submit", not self.obj.is_shared() and "Create event" or "Update event") |
paul@512 | 144 | page.add(" ") |
paul@512 | 145 | |
paul@756 | 146 | if self.obj.is_shared() and not is_request: |
paul@764 | 147 | self.control("cancel", "submit", "Cancel event") |
paul@446 | 148 | else: |
paul@764 | 149 | self.control("discard", "submit", "Discard event") |
paul@512 | 150 | |
paul@512 | 151 | page.add(" ") |
paul@764 | 152 | self.control("save", "submit", "Save without sending") |
paul@512 | 153 | page.p.close() |
paul@446 | 154 | |
paul@756 | 155 | def show_object_on_page(self, errors=None): |
paul@446 | 156 | |
paul@446 | 157 | """ |
paul@756 | 158 | Show the calendar object on the current page. If 'errors' is given, show |
paul@756 | 159 | a suitable message for the different errors provided. |
paul@446 | 160 | """ |
paul@446 | 161 | |
paul@446 | 162 | page = self.page |
paul@446 | 163 | page.form(method="POST") |
paul@446 | 164 | |
paul@595 | 165 | # Add a hidden control to help determine whether editing has already begun. |
paul@595 | 166 | |
paul@764 | 167 | self.control("editing", "hidden", "true") |
paul@446 | 168 | |
paul@446 | 169 | args = self.env.get_args() |
paul@446 | 170 | |
paul@487 | 171 | # Obtain basic event information, generating any necessary editing controls. |
paul@446 | 172 | |
paul@765 | 173 | attendees = self.update_current_attendees() |
paul@765 | 174 | period = self.get_current_main_period() |
paul@765 | 175 | self.show_object_datetime_controls(period) |
paul@446 | 176 | |
paul@487 | 177 | # Obtain any separate recurrences for this event. |
paul@487 | 178 | |
paul@756 | 179 | recurrenceids = self._get_active_recurrences(self.uid) |
paul@765 | 180 | replaced = not self.recurrenceid and period.is_replaced(recurrenceids) |
paul@487 | 181 | |
paul@446 | 182 | # Provide a summary of the object. |
paul@446 | 183 | |
paul@446 | 184 | page.table(class_="object", cellspacing=5, cellpadding=5) |
paul@446 | 185 | page.thead() |
paul@446 | 186 | page.tr() |
paul@446 | 187 | page.th("Event", class_="mainheading", colspan=2) |
paul@446 | 188 | page.tr.close() |
paul@446 | 189 | page.thead.close() |
paul@446 | 190 | page.tbody() |
paul@446 | 191 | |
paul@446 | 192 | for name, label in self.property_items: |
paul@446 | 193 | field = name.lower() |
paul@446 | 194 | |
paul@757 | 195 | items = uri_items(self.obj.get_items(name) or []) |
paul@446 | 196 | rowspan = len(items) |
paul@446 | 197 | |
paul@446 | 198 | if name == "ATTENDEE": |
paul@473 | 199 | rowspan = len(attendees) + 1 # for the add button |
paul@446 | 200 | elif not items: |
paul@446 | 201 | continue |
paul@446 | 202 | |
paul@446 | 203 | page.tr() |
paul@492 | 204 | page.th(label, class_="objectheading %s%s" % (field, errors and field in errors and " error" or ""), rowspan=rowspan) |
paul@446 | 205 | |
paul@446 | 206 | # Handle datetimes specially. |
paul@446 | 207 | |
paul@446 | 208 | if name in ["DTSTART", "DTEND"]: |
paul@487 | 209 | if not replaced: |
paul@446 | 210 | |
paul@487 | 211 | # Obtain the datetime. |
paul@487 | 212 | |
paul@498 | 213 | is_start = name == "DTSTART" |
paul@446 | 214 | |
paul@487 | 215 | # Where no end datetime exists, use the start datetime as the |
paul@487 | 216 | # basis of any potential datetime specified if dt-control is |
paul@487 | 217 | # set. |
paul@487 | 218 | |
paul@765 | 219 | self.show_datetime_controls(is_start and period.get_form_start() or period.get_form_end(), is_start) |
paul@446 | 220 | |
paul@487 | 221 | elif name == "DTSTART": |
paul@487 | 222 | page.td(class_="objectvalue %s replaced" % field, rowspan=2) |
paul@756 | 223 | page.a("First occurrence replaced by a separate event", href=self.link_to(self.uid, replaced)) |
paul@487 | 224 | page.td.close() |
paul@446 | 225 | |
paul@446 | 226 | page.tr.close() |
paul@446 | 227 | |
paul@446 | 228 | # Handle the summary specially. |
paul@446 | 229 | |
paul@446 | 230 | elif name == "SUMMARY": |
paul@756 | 231 | value = args.get("summary", [self.obj.get_value(name)])[0] |
paul@446 | 232 | |
paul@490 | 233 | page.td(class_="objectvalue summary") |
paul@756 | 234 | if self.is_organiser(): |
paul@764 | 235 | self.control("summary", "text", value, size=80) |
paul@446 | 236 | else: |
paul@446 | 237 | page.add(value) |
paul@446 | 238 | page.td.close() |
paul@446 | 239 | page.tr.close() |
paul@446 | 240 | |
paul@473 | 241 | # Handle attendees specially. |
paul@473 | 242 | |
paul@473 | 243 | elif name == "ATTENDEE": |
paul@473 | 244 | attendee_map = dict(items) |
paul@473 | 245 | first = True |
paul@473 | 246 | |
paul@473 | 247 | for i, value in enumerate(attendees): |
paul@473 | 248 | if not first: |
paul@473 | 249 | page.tr() |
paul@473 | 250 | else: |
paul@473 | 251 | first = False |
paul@473 | 252 | |
paul@479 | 253 | # Obtain details of attendees to supply attributes. |
paul@473 | 254 | |
paul@756 | 255 | self.show_attendee(i, value, attendee_map.get(value)) |
paul@473 | 256 | page.tr.close() |
paul@473 | 257 | |
paul@473 | 258 | # Allow more attendees to be specified. |
paul@473 | 259 | |
paul@756 | 260 | if self.is_organiser(): |
paul@473 | 261 | if not first: |
paul@473 | 262 | page.tr() |
paul@473 | 263 | |
paul@473 | 264 | page.td() |
paul@764 | 265 | self.control("add", "submit", "add", id="add", class_="add") |
paul@496 | 266 | page.label("Add attendee", for_="add", class_="add") |
paul@473 | 267 | page.td.close() |
paul@473 | 268 | page.tr.close() |
paul@473 | 269 | |
paul@473 | 270 | # Handle potentially many values of other kinds. |
paul@446 | 271 | |
paul@446 | 272 | else: |
paul@446 | 273 | first = True |
paul@446 | 274 | |
paul@446 | 275 | for i, (value, attr) in enumerate(items): |
paul@446 | 276 | if not first: |
paul@446 | 277 | page.tr() |
paul@446 | 278 | else: |
paul@446 | 279 | first = False |
paul@446 | 280 | |
paul@490 | 281 | page.td(class_="objectvalue %s" % field) |
paul@473 | 282 | page.add(value) |
paul@446 | 283 | page.td.close() |
paul@446 | 284 | page.tr.close() |
paul@446 | 285 | |
paul@446 | 286 | page.tbody.close() |
paul@446 | 287 | page.table.close() |
paul@446 | 288 | |
paul@756 | 289 | self.show_recurrences(errors) |
paul@766 | 290 | self.show_counters() |
paul@756 | 291 | self.show_conflicting_events() |
paul@756 | 292 | self.show_request_controls() |
paul@446 | 293 | |
paul@446 | 294 | page.form.close() |
paul@446 | 295 | |
paul@756 | 296 | def show_attendee(self, i, attendee, attendee_attr): |
paul@479 | 297 | |
paul@479 | 298 | """ |
paul@756 | 299 | For the current object, show the attendee in position 'i' with the given |
paul@756 | 300 | 'attendee' value, having 'attendee_attr' as any stored attributes. |
paul@479 | 301 | """ |
paul@479 | 302 | |
paul@479 | 303 | page = self.page |
paul@479 | 304 | args = self.env.get_args() |
paul@479 | 305 | |
paul@479 | 306 | partstat = attendee_attr and attendee_attr.get("PARTSTAT") |
paul@479 | 307 | |
paul@479 | 308 | page.td(class_="objectvalue") |
paul@479 | 309 | |
paul@479 | 310 | # Show a form control as organiser for new attendees. |
paul@479 | 311 | |
paul@758 | 312 | if self.is_organiser() and self.can_edit_attendee(attendee): |
paul@764 | 313 | self.control("attendee", "value", attendee, size="40") |
paul@479 | 314 | else: |
paul@764 | 315 | self.control("attendee", "hidden", attendee) |
paul@479 | 316 | page.add(attendee) |
paul@479 | 317 | page.add(" ") |
paul@479 | 318 | |
paul@479 | 319 | # Show participation status, editable for the current user. |
paul@479 | 320 | |
paul@479 | 321 | if attendee == self.user: |
paul@764 | 322 | self.menu("partstat", partstat, self.partstat_items, "partstat") |
paul@479 | 323 | |
paul@479 | 324 | # Allow the participation indicator to act as a submit |
paul@479 | 325 | # button in order to refresh the page and show a control for |
paul@479 | 326 | # the current user, if indicated. |
paul@479 | 327 | |
paul@758 | 328 | elif self.is_organiser() and self.attendee_is_new(attendee): |
paul@764 | 329 | self.control("partstat-refresh", "submit", "refresh", id="partstat-%d" % i, class_="refresh") |
paul@479 | 330 | page.label(dict(self.partstat_items).get(partstat, ""), for_="partstat-%s" % i, class_="partstat") |
paul@758 | 331 | |
paul@758 | 332 | # Otherwise, just show a label with the participation status. |
paul@758 | 333 | |
paul@479 | 334 | else: |
paul@479 | 335 | page.span(dict(self.partstat_items).get(partstat, ""), class_="partstat") |
paul@479 | 336 | |
paul@479 | 337 | # Permit organisers to remove attendees. |
paul@479 | 338 | |
paul@756 | 339 | if self.is_organiser(): |
paul@479 | 340 | |
paul@479 | 341 | # Permit the removal of newly-added attendees. |
paul@479 | 342 | |
paul@758 | 343 | remove_type = self.can_remove_attendee(attendee) and "submit" or "checkbox" |
paul@764 | 344 | self.control("remove", remove_type, str(i), str(i) in args.get("remove", []), id="remove-%d" % i, class_="remove") |
paul@479 | 345 | |
paul@479 | 346 | page.label("Remove", for_="remove-%d" % i, class_="remove") |
paul@701 | 347 | page.label(for_="remove-%d" % i, class_="removed") |
paul@701 | 348 | page.add("(Uninvited)") |
paul@701 | 349 | page.span("Re-invite", class_="action") |
paul@701 | 350 | page.label.close() |
paul@479 | 351 | |
paul@479 | 352 | page.td.close() |
paul@479 | 353 | |
paul@756 | 354 | def show_recurrences(self, errors=None): |
paul@446 | 355 | |
paul@492 | 356 | """ |
paul@756 | 357 | Show recurrences for the current object. If 'errors' is given, show a |
paul@756 | 358 | suitable message for the different errors provided. |
paul@492 | 359 | """ |
paul@446 | 360 | |
paul@446 | 361 | page = self.page |
paul@446 | 362 | |
paul@446 | 363 | # Obtain any parent object if this object is a specific recurrence. |
paul@446 | 364 | |
paul@756 | 365 | if self.recurrenceid: |
paul@756 | 366 | parent = self.get_stored_object(self.uid, None) |
paul@489 | 367 | if not parent: |
paul@446 | 368 | return |
paul@446 | 369 | |
paul@489 | 370 | page.p() |
paul@756 | 371 | page.a("This event modifies a recurring event.", href=self.link_to(self.uid)) |
paul@489 | 372 | page.p.close() |
paul@446 | 373 | |
paul@499 | 374 | # Obtain the periods associated with the event. |
paul@595 | 375 | # NOTE: Add a control to add recurrences here. |
paul@446 | 376 | |
paul@756 | 377 | recurrences = self.get_current_recurrences() |
paul@446 | 378 | |
paul@499 | 379 | if len(recurrences) < 1: |
paul@446 | 380 | return |
paul@446 | 381 | |
paul@756 | 382 | recurrenceids = self._get_recurrences(self.uid) |
paul@446 | 383 | |
paul@499 | 384 | page.p("This event occurs on the following occasions within the next %d days:" % self.get_window_size()) |
paul@499 | 385 | |
paul@446 | 386 | # Show each recurrence in a separate table if editable. |
paul@446 | 387 | |
paul@756 | 388 | if self.is_organiser() and recurrences: |
paul@446 | 389 | |
paul@519 | 390 | for index, period in enumerate(recurrences): |
paul@756 | 391 | self.show_recurrence(index, period, self.recurrenceid, recurrenceids, errors) |
paul@446 | 392 | |
paul@446 | 393 | # Otherwise, use a compact single table. |
paul@446 | 394 | |
paul@499 | 395 | else: |
paul@499 | 396 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@499 | 397 | page.caption("Occurrences") |
paul@499 | 398 | page.thead() |
paul@499 | 399 | page.tr() |
paul@499 | 400 | page.th("Start", class_="objectheading start") |
paul@499 | 401 | page.th("End", class_="objectheading end") |
paul@499 | 402 | page.tr.close() |
paul@499 | 403 | page.thead.close() |
paul@499 | 404 | page.tbody() |
paul@446 | 405 | |
paul@519 | 406 | for index, period in enumerate(recurrences): |
paul@499 | 407 | page.tr() |
paul@756 | 408 | self.show_recurrence_label(period, self.recurrenceid, recurrenceids, True) |
paul@756 | 409 | self.show_recurrence_label(period, self.recurrenceid, recurrenceids, False) |
paul@499 | 410 | page.tr.close() |
paul@446 | 411 | |
paul@499 | 412 | page.tbody.close() |
paul@499 | 413 | page.table.close() |
paul@446 | 414 | |
paul@756 | 415 | def show_recurrence(self, index, period, recurrenceid, recurrenceids, errors=None): |
paul@519 | 416 | |
paul@519 | 417 | """ |
paul@756 | 418 | Show recurrence controls for a recurrence provided by the current object |
paul@756 | 419 | with the given 'index' position in the list of periods, the given |
paul@756 | 420 | 'period' details, where a 'recurrenceid' indicates any specific |
paul@756 | 421 | recurrence, and where 'recurrenceids' indicates all known additional |
paul@756 | 422 | recurrences for the object. |
paul@519 | 423 | |
paul@519 | 424 | If 'errors' is given, show a suitable message for the different errors |
paul@519 | 425 | provided. |
paul@519 | 426 | """ |
paul@519 | 427 | |
paul@519 | 428 | page = self.page |
paul@526 | 429 | args = self.env.get_args() |
paul@526 | 430 | |
paul@528 | 431 | p = event_period_from_period(period) |
paul@633 | 432 | replaced = not recurrenceid and p.is_replaced(recurrenceids) |
paul@528 | 433 | |
paul@519 | 434 | # Isolate the controls from neighbouring tables. |
paul@519 | 435 | |
paul@519 | 436 | page.div() |
paul@519 | 437 | |
paul@519 | 438 | self.show_object_datetime_controls(period, index) |
paul@519 | 439 | |
paul@519 | 440 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@519 | 441 | page.caption(period.origin == "RRULE" and "Occurrence from rule" or "Occurrence") |
paul@519 | 442 | page.tbody() |
paul@519 | 443 | |
paul@519 | 444 | page.tr() |
paul@519 | 445 | error = errors and ("dtstart", index) in errors and " error" or "" |
paul@519 | 446 | page.th("Start", class_="objectheading start%s" % error) |
paul@756 | 447 | self.show_recurrence_controls(index, period, recurrenceid, recurrenceids, True) |
paul@519 | 448 | page.tr.close() |
paul@519 | 449 | page.tr() |
paul@519 | 450 | error = errors and ("dtend", index) in errors and " error" or "" |
paul@519 | 451 | page.th("End", class_="objectheading end%s" % error) |
paul@756 | 452 | self.show_recurrence_controls(index, period, recurrenceid, recurrenceids, False) |
paul@519 | 453 | page.tr.close() |
paul@519 | 454 | |
paul@526 | 455 | # Permit the removal of recurrences. |
paul@526 | 456 | |
paul@528 | 457 | if not replaced: |
paul@528 | 458 | page.tr() |
paul@528 | 459 | page.th("") |
paul@528 | 460 | page.td() |
paul@526 | 461 | |
paul@756 | 462 | remove_type = not self.obj.is_shared() or not period.origin and "submit" or "checkbox" |
paul@764 | 463 | self.control("recur-remove", remove_type, str(index), |
paul@528 | 464 | str(index) in args.get("recur-remove", []), |
paul@528 | 465 | id="recur-remove-%d" % index, class_="remove") |
paul@526 | 466 | |
paul@528 | 467 | page.label("Remove", for_="recur-remove-%d" % index, class_="remove") |
paul@701 | 468 | page.label(for_="recur-remove-%d" % index, class_="removed") |
paul@701 | 469 | page.add("(Removed)") |
paul@701 | 470 | page.span("Re-add", class_="action") |
paul@701 | 471 | page.label.close() |
paul@526 | 472 | |
paul@528 | 473 | page.td.close() |
paul@528 | 474 | page.tr.close() |
paul@526 | 475 | |
paul@519 | 476 | page.tbody.close() |
paul@519 | 477 | page.table.close() |
paul@519 | 478 | |
paul@519 | 479 | page.div.close() |
paul@519 | 480 | |
paul@766 | 481 | def show_counters(self): |
paul@766 | 482 | |
paul@766 | 483 | "Show any counter-proposals for the current object." |
paul@766 | 484 | |
paul@766 | 485 | page = self.page |
paul@766 | 486 | attendees = self._get_counters(self.uid, self.recurrenceid) |
paul@766 | 487 | tzid = self.get_tzid() |
paul@766 | 488 | |
paul@766 | 489 | if not attendees: |
paul@766 | 490 | return |
paul@766 | 491 | |
paul@766 | 492 | page.p("The following counter-proposals have been received for this event:") |
paul@766 | 493 | |
paul@766 | 494 | page.table(cellspacing=5, cellpadding=5, class_="counters") |
paul@766 | 495 | page.thead() |
paul@766 | 496 | page.tr() |
paul@766 | 497 | page.th("Attendee", rowspan=2) |
paul@766 | 498 | page.th("Periods", colspan=2) |
paul@766 | 499 | page.tr.close() |
paul@766 | 500 | page.tr() |
paul@766 | 501 | page.th("Start") |
paul@766 | 502 | page.th("End") |
paul@766 | 503 | page.tr.close() |
paul@766 | 504 | page.thead.close() |
paul@766 | 505 | page.tbody() |
paul@766 | 506 | |
paul@766 | 507 | for attendee in attendees: |
paul@766 | 508 | obj = self.get_stored_object(self.uid, self.recurrenceid, "counters", attendee) |
paul@766 | 509 | periods = self.get_periods(obj) |
paul@766 | 510 | |
paul@766 | 511 | page.tr(rowspan=len(periods)) |
paul@766 | 512 | page.td(attendee) |
paul@766 | 513 | |
paul@766 | 514 | first = True |
paul@766 | 515 | for p in periods: |
paul@766 | 516 | if not first: |
paul@766 | 517 | page.tr() |
paul@766 | 518 | else: |
paul@766 | 519 | first = False |
paul@766 | 520 | |
paul@766 | 521 | start = self.format_datetime(to_timezone(p.get_start(), tzid), "long") |
paul@766 | 522 | end = self.format_datetime(to_timezone(p.get_end(), tzid), "long") |
paul@766 | 523 | |
paul@766 | 524 | page.td(start) |
paul@766 | 525 | page.td(end) |
paul@766 | 526 | |
paul@766 | 527 | page.tr.close() |
paul@766 | 528 | |
paul@766 | 529 | page.tbody.close() |
paul@766 | 530 | page.table.close() |
paul@766 | 531 | |
paul@756 | 532 | def show_conflicting_events(self): |
paul@446 | 533 | |
paul@756 | 534 | "Show conflicting events for the current object." |
paul@446 | 535 | |
paul@446 | 536 | page = self.page |
paul@756 | 537 | recurrenceids = self._get_active_recurrences(self.uid) |
paul@446 | 538 | |
paul@446 | 539 | # Obtain the user's timezone. |
paul@446 | 540 | |
paul@446 | 541 | tzid = self.get_tzid() |
paul@756 | 542 | periods = self.get_periods(self.obj) |
paul@446 | 543 | |
paul@446 | 544 | # Indicate whether there are conflicting events. |
paul@446 | 545 | |
paul@484 | 546 | conflicts = [] |
paul@756 | 547 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@446 | 548 | |
paul@756 | 549 | for participant in self.get_current_attendees(): |
paul@484 | 550 | if participant == self.user: |
paul@484 | 551 | freebusy = self.store.get_freebusy(participant) |
paul@484 | 552 | else: |
paul@484 | 553 | freebusy = self.store.get_freebusy_for_other(self.user, participant) |
paul@484 | 554 | |
paul@484 | 555 | if not freebusy: |
paul@484 | 556 | continue |
paul@446 | 557 | |
paul@446 | 558 | # Obtain any time zone details from the suggested event. |
paul@446 | 559 | |
paul@756 | 560 | _dtstart, attr = self.obj.get_item("DTSTART") |
paul@446 | 561 | tzid = attr.get("TZID", tzid) |
paul@446 | 562 | |
paul@484 | 563 | # Show any conflicts with periods of actual attendance. |
paul@446 | 564 | |
paul@611 | 565 | participant_attr = attendee_map.get(participant) |
paul@611 | 566 | partstat = participant_attr and participant_attr.get("PARTSTAT") |
paul@756 | 567 | recurrences = self.obj.get_recurrence_start_points(recurrenceids, tzid) |
paul@611 | 568 | |
paul@484 | 569 | for p in have_conflict(freebusy, periods, True): |
paul@756 | 570 | if not self.recurrenceid and p.is_replaced(recurrences): |
paul@528 | 571 | continue |
paul@611 | 572 | |
paul@611 | 573 | if ( # Unidentified or different event |
paul@756 | 574 | (p.uid != self.uid or self.recurrenceid and p.recurrenceid and p.recurrenceid != self.recurrenceid) and |
paul@611 | 575 | # Different period or unclear participation with the same period |
paul@611 | 576 | (p not in periods or not partstat in ("ACCEPTED", "TENTATIVE")) and |
paul@611 | 577 | # Participant not limited to organising |
paul@611 | 578 | p.transp != "ORG" |
paul@611 | 579 | ): |
paul@611 | 580 | |
paul@484 | 581 | conflicts.append(p) |
paul@446 | 582 | |
paul@484 | 583 | conflicts.sort() |
paul@484 | 584 | |
paul@484 | 585 | # Show any conflicts with periods of actual attendance. |
paul@446 | 586 | |
paul@484 | 587 | if conflicts: |
paul@484 | 588 | page.p("This event conflicts with others:") |
paul@446 | 589 | |
paul@484 | 590 | page.table(cellspacing=5, cellpadding=5, class_="conflicts") |
paul@484 | 591 | page.thead() |
paul@484 | 592 | page.tr() |
paul@484 | 593 | page.th("Event") |
paul@484 | 594 | page.th("Start") |
paul@484 | 595 | page.th("End") |
paul@484 | 596 | page.tr.close() |
paul@484 | 597 | page.thead.close() |
paul@484 | 598 | page.tbody() |
paul@446 | 599 | |
paul@484 | 600 | for p in conflicts: |
paul@484 | 601 | |
paul@484 | 602 | # Provide details of any conflicting event. |
paul@446 | 603 | |
paul@535 | 604 | start = self.format_datetime(to_timezone(p.get_start(), tzid), "long") |
paul@535 | 605 | end = self.format_datetime(to_timezone(p.get_end(), tzid), "long") |
paul@446 | 606 | |
paul@484 | 607 | page.tr() |
paul@446 | 608 | |
paul@484 | 609 | # Show the event summary for the conflicting event. |
paul@446 | 610 | |
paul@484 | 611 | page.td() |
paul@484 | 612 | if p.summary: |
paul@488 | 613 | page.a(p.summary, href=self.link_to(p.uid, p.recurrenceid)) |
paul@484 | 614 | else: |
paul@484 | 615 | page.add("(Unspecified event)") |
paul@484 | 616 | page.td.close() |
paul@446 | 617 | |
paul@484 | 618 | page.td(start) |
paul@484 | 619 | page.td(end) |
paul@446 | 620 | |
paul@484 | 621 | page.tr.close() |
paul@446 | 622 | |
paul@484 | 623 | page.tbody.close() |
paul@484 | 624 | page.table.close() |
paul@446 | 625 | |
paul@765 | 626 | class EventPage(EventPageFragment): |
paul@765 | 627 | |
paul@765 | 628 | "A request handler for the event page." |
paul@765 | 629 | |
paul@765 | 630 | def __init__(self, resource=None, messenger=None): |
paul@765 | 631 | ResourceClientForObject.__init__(self, resource) |
paul@765 | 632 | self.messenger = messenger or Messenger() |
paul@474 | 633 | |
paul@765 | 634 | # Request logic methods. |
paul@765 | 635 | |
paul@765 | 636 | def is_initial_load(self): |
paul@765 | 637 | |
paul@765 | 638 | "Return whether the event is being loaded and shown for the first time." |
paul@765 | 639 | |
paul@765 | 640 | return not self.env.get_args().has_key("editing") |
paul@765 | 641 | |
paul@765 | 642 | def handle_request(self): |
paul@474 | 643 | |
paul@474 | 644 | """ |
paul@765 | 645 | Handle actions involving the current object, returning an error if one |
paul@765 | 646 | occurred, or None if the request was successfully handled. |
paul@474 | 647 | """ |
paul@474 | 648 | |
paul@765 | 649 | # Handle a submitted form. |
paul@498 | 650 | |
paul@474 | 651 | args = self.env.get_args() |
paul@765 | 652 | |
paul@765 | 653 | # Get the possible actions. |
paul@765 | 654 | |
paul@765 | 655 | reply = args.has_key("reply") |
paul@765 | 656 | discard = args.has_key("discard") |
paul@765 | 657 | create = args.has_key("create") |
paul@765 | 658 | cancel = args.has_key("cancel") |
paul@765 | 659 | ignore = args.has_key("ignore") |
paul@765 | 660 | save = args.has_key("save") |
paul@474 | 661 | |
paul@765 | 662 | have_action = reply or discard or create or cancel or ignore or save |
paul@765 | 663 | |
paul@765 | 664 | if not have_action: |
paul@765 | 665 | return ["action"] |
paul@474 | 666 | |
paul@765 | 667 | # If ignoring the object, return to the calendar. |
paul@474 | 668 | |
paul@765 | 669 | if ignore: |
paul@765 | 670 | self.redirect(self.env.get_path()) |
paul@765 | 671 | return None |
paul@765 | 672 | |
paul@765 | 673 | # Update the object. |
paul@474 | 674 | |
paul@765 | 675 | single_user = False |
paul@765 | 676 | |
paul@765 | 677 | if reply or create or cancel or save: |
paul@765 | 678 | |
paul@765 | 679 | # Update principal event details if organiser. |
paul@765 | 680 | |
paul@765 | 681 | if self.is_organiser(): |
paul@765 | 682 | |
paul@765 | 683 | # Update time periods (main and recurring). |
paul@474 | 684 | |
paul@765 | 685 | try: |
paul@765 | 686 | period = self.handle_main_period() |
paul@765 | 687 | except PeriodError, exc: |
paul@765 | 688 | return exc.args |
paul@474 | 689 | |
paul@765 | 690 | try: |
paul@765 | 691 | periods = self.handle_recurrence_periods() |
paul@765 | 692 | except PeriodError, exc: |
paul@765 | 693 | return exc.args |
paul@765 | 694 | |
paul@765 | 695 | # Set the periods in the object, first obtaining removed and |
paul@765 | 696 | # modified period information. |
paul@765 | 697 | |
paul@765 | 698 | to_unschedule = self.get_removed_periods() |
paul@765 | 699 | |
paul@765 | 700 | self.obj.set_period(period) |
paul@765 | 701 | self.obj.set_periods(periods) |
paul@474 | 702 | |
paul@765 | 703 | # Update summary. |
paul@765 | 704 | |
paul@765 | 705 | if args.has_key("summary"): |
paul@765 | 706 | self.obj["SUMMARY"] = [(args["summary"][0], {})] |
paul@765 | 707 | |
paul@765 | 708 | # Obtain any participants and those to be removed. |
paul@474 | 709 | |
paul@765 | 710 | attendees = self.get_attendees_from_page() |
paul@765 | 711 | removed = [attendees[int(i)] for i in args.get("remove", [])] |
paul@765 | 712 | to_cancel = self.update_attendees(self.obj, attendees, removed) |
paul@765 | 713 | single_user = not attendees or attendees == [self.user] |
paul@765 | 714 | |
paul@765 | 715 | # Update attendee participation for the current user. |
paul@474 | 716 | |
paul@765 | 717 | if args.has_key("partstat"): |
paul@765 | 718 | self.update_participation(self.obj, args["partstat"][0]) |
paul@765 | 719 | |
paul@765 | 720 | # Process any action. |
paul@474 | 721 | |
paul@765 | 722 | invite = not save and create and not single_user |
paul@765 | 723 | save = save or create and single_user |
paul@474 | 724 | |
paul@765 | 725 | handled = True |
paul@474 | 726 | |
paul@765 | 727 | if reply or invite or cancel: |
paul@765 | 728 | |
paul@765 | 729 | client = ManagerClient(self.obj, self.user, self.messenger) |
paul@474 | 730 | |
paul@765 | 731 | # Process the object and remove it from the list of requests. |
paul@765 | 732 | |
paul@765 | 733 | if reply and client.process_received_request(): |
paul@765 | 734 | self.remove_request(self.uid, self.recurrenceid) |
paul@765 | 735 | |
paul@765 | 736 | elif self.is_organiser() and (invite or cancel): |
paul@765 | 737 | |
paul@765 | 738 | # Invitation, uninvitation and unscheduling... |
paul@765 | 739 | |
paul@765 | 740 | if client.process_created_request( |
paul@765 | 741 | invite and "REQUEST" or "CANCEL", to_cancel, to_unschedule): |
paul@765 | 742 | |
paul@765 | 743 | self.remove_request(self.uid, self.recurrenceid) |
paul@474 | 744 | |
paul@765 | 745 | # Save single user events. |
paul@765 | 746 | |
paul@765 | 747 | elif save: |
paul@765 | 748 | self.store.set_event(self.user, self.uid, self.recurrenceid, node=self.obj.to_node()) |
paul@765 | 749 | self.update_event_in_freebusy() |
paul@765 | 750 | self.remove_request(self.uid, self.recurrenceid) |
paul@474 | 751 | |
paul@765 | 752 | # Remove the request and the object. |
paul@474 | 753 | |
paul@765 | 754 | elif discard: |
paul@765 | 755 | self.remove_event_from_freebusy() |
paul@765 | 756 | self.remove_event(self.uid, self.recurrenceid) |
paul@765 | 757 | self.remove_request(self.uid, self.recurrenceid) |
paul@474 | 758 | |
paul@474 | 759 | else: |
paul@765 | 760 | handled = False |
paul@765 | 761 | |
paul@765 | 762 | # Upon handling an action, redirect to the main page. |
paul@765 | 763 | |
paul@765 | 764 | if handled: |
paul@765 | 765 | self.redirect(self.env.get_path()) |
paul@765 | 766 | |
paul@765 | 767 | return None |
paul@765 | 768 | |
paul@765 | 769 | def handle_main_period(self): |
paul@765 | 770 | |
paul@765 | 771 | "Return period details for the main start/end period in an event." |
paul@765 | 772 | |
paul@765 | 773 | return self.get_main_period_from_page().as_event_period() |
paul@765 | 774 | |
paul@765 | 775 | def handle_recurrence_periods(self): |
paul@765 | 776 | |
paul@765 | 777 | "Return period details for the recurrences specified for an event." |
paul@765 | 778 | |
paul@765 | 779 | return [p.as_event_period(i) for i, p in enumerate(self.get_recurrences_from_page())] |
paul@765 | 780 | |
paul@765 | 781 | # Access to form-originating object information. |
paul@765 | 782 | |
paul@765 | 783 | def get_main_period_from_page(self): |
paul@765 | 784 | |
paul@765 | 785 | "Return the main period defined in the event form." |
paul@765 | 786 | |
paul@765 | 787 | args = self.env.get_args() |
paul@765 | 788 | |
paul@765 | 789 | dtend_enabled = args.get("dtend-control", [None])[0] |
paul@765 | 790 | dttimes_enabled = args.get("dttimes-control", [None])[0] |
paul@765 | 791 | start = self.get_date_control_values("dtstart") |
paul@765 | 792 | end = self.get_date_control_values("dtend") |
paul@765 | 793 | |
paul@765 | 794 | return FormPeriod(start, end, dtend_enabled, dttimes_enabled, self.get_tzid()) |
paul@474 | 795 | |
paul@765 | 796 | def get_recurrences_from_page(self): |
paul@765 | 797 | |
paul@765 | 798 | "Return the recurrences defined in the event form." |
paul@765 | 799 | |
paul@765 | 800 | args = self.env.get_args() |
paul@765 | 801 | |
paul@765 | 802 | all_dtend_enabled = args.get("dtend-control-recur", []) |
paul@765 | 803 | all_dttimes_enabled = args.get("dttimes-control-recur", []) |
paul@765 | 804 | all_starts = self.get_date_control_values("dtstart-recur", multiple=True) |
paul@765 | 805 | all_ends = self.get_date_control_values("dtend-recur", multiple=True, tzid_name="dtstart-recur") |
paul@765 | 806 | all_origins = args.get("recur-origin", []) |
paul@765 | 807 | |
paul@765 | 808 | periods = [] |
paul@765 | 809 | |
paul@765 | 810 | for index, (start, end, dtend_enabled, dttimes_enabled, origin) in \ |
paul@765 | 811 | enumerate(map(None, all_starts, all_ends, all_dtend_enabled, all_dttimes_enabled, all_origins)): |
paul@765 | 812 | |
paul@765 | 813 | dtend_enabled = str(index) in all_dtend_enabled |
paul@765 | 814 | dttimes_enabled = str(index) in all_dttimes_enabled |
paul@765 | 815 | period = FormPeriod(start, end, dtend_enabled, dttimes_enabled, self.get_tzid(), origin) |
paul@765 | 816 | periods.append(period) |
paul@765 | 817 | |
paul@765 | 818 | return periods |
paul@765 | 819 | |
paul@765 | 820 | def get_removed_periods(self): |
paul@765 | 821 | |
paul@765 | 822 | "Return a list of recurrence periods to remove upon updating an event." |
paul@765 | 823 | |
paul@765 | 824 | to_unschedule = [] |
paul@765 | 825 | args = self.env.get_args() |
paul@765 | 826 | for i in args.get("recur-remove", []): |
paul@765 | 827 | to_unschedule.append(periods[int(i)]) |
paul@765 | 828 | return to_unschedule |
paul@765 | 829 | |
paul@765 | 830 | def get_attendees_from_page(self): |
paul@474 | 831 | |
paul@474 | 832 | """ |
paul@765 | 833 | Return attendees from the request, normalised for iCalendar purposes, |
paul@765 | 834 | and without duplicates. |
paul@474 | 835 | """ |
paul@474 | 836 | |
paul@765 | 837 | args = self.env.get_args() |
paul@499 | 838 | |
paul@765 | 839 | attendees = args.get("attendee", []) |
paul@765 | 840 | unique_attendees = set() |
paul@765 | 841 | ordered_attendees = [] |
paul@474 | 842 | |
paul@765 | 843 | for attendee in attendees: |
paul@765 | 844 | if not attendee.strip(): |
paul@765 | 845 | continue |
paul@765 | 846 | attendee = get_uri(attendee) |
paul@765 | 847 | if attendee not in unique_attendees: |
paul@765 | 848 | unique_attendees.add(attendee) |
paul@765 | 849 | ordered_attendees.append(attendee) |
paul@517 | 850 | |
paul@765 | 851 | return ordered_attendees |
paul@765 | 852 | |
paul@765 | 853 | def update_attendees_from_page(self): |
paul@765 | 854 | |
paul@765 | 855 | "Add or remove attendees. This does not affect the stored object." |
paul@765 | 856 | |
paul@765 | 857 | args = self.env.get_args() |
paul@765 | 858 | |
paul@765 | 859 | attendees = self.get_attendees_from_page() |
paul@474 | 860 | |
paul@765 | 861 | if args.has_key("add"): |
paul@765 | 862 | attendees.append("") |
paul@517 | 863 | |
paul@765 | 864 | # Only actually remove attendees if the event is unsent, if the attendee |
paul@765 | 865 | # is new, or if it is the current user being removed. |
paul@765 | 866 | |
paul@765 | 867 | if args.has_key("remove"): |
paul@765 | 868 | still_to_remove = [] |
paul@517 | 869 | |
paul@765 | 870 | for i in args["remove"]: |
paul@765 | 871 | try: |
paul@765 | 872 | attendee = attendees[int(i)] |
paul@765 | 873 | except IndexError: |
paul@765 | 874 | continue |
paul@474 | 875 | |
paul@765 | 876 | if self.can_remove_attendee(attendee): |
paul@765 | 877 | attendees.remove(attendee) |
paul@765 | 878 | else: |
paul@765 | 879 | still_to_remove.append(i) |
paul@474 | 880 | |
paul@765 | 881 | args["remove"] = still_to_remove |
paul@765 | 882 | |
paul@765 | 883 | return attendees |
paul@498 | 884 | |
paul@765 | 885 | # Access to current object information. |
paul@765 | 886 | |
paul@765 | 887 | def get_current_main_period(self): |
paul@498 | 888 | |
paul@498 | 889 | """ |
paul@765 | 890 | Return the currently active main period for the current object depending |
paul@765 | 891 | on whether editing has begun or whether the object has just been loaded. |
paul@765 | 892 | """ |
paul@498 | 893 | |
paul@765 | 894 | if self.is_initial_load() or not self.is_organiser(): |
paul@765 | 895 | return self.get_stored_main_period() |
paul@765 | 896 | else: |
paul@765 | 897 | return self.get_main_period_from_page() |
paul@765 | 898 | |
paul@765 | 899 | def get_current_recurrences(self): |
paul@765 | 900 | |
paul@765 | 901 | """ |
paul@765 | 902 | Return recurrences for the current object using the original object |
paul@765 | 903 | details where no editing is in progress, using form data otherwise. |
paul@498 | 904 | """ |
paul@498 | 905 | |
paul@765 | 906 | if self.is_initial_load() or not self.is_organiser(): |
paul@765 | 907 | return self.get_stored_recurrences() |
paul@765 | 908 | else: |
paul@765 | 909 | return self.get_recurrences_from_page() |
paul@498 | 910 | |
paul@765 | 911 | def get_current_attendees(self): |
paul@765 | 912 | |
paul@765 | 913 | """ |
paul@765 | 914 | Return attendees for the current object depending on whether the object |
paul@765 | 915 | has been edited or instead provides such information from its stored |
paul@765 | 916 | form. |
paul@765 | 917 | """ |
paul@499 | 918 | |
paul@765 | 919 | if self.is_initial_load() or not self.is_organiser(): |
paul@765 | 920 | return self.get_stored_attendees() |
paul@765 | 921 | else: |
paul@765 | 922 | return self.get_attendees_from_page() |
paul@765 | 923 | |
paul@765 | 924 | def update_current_attendees(self): |
paul@498 | 925 | |
paul@765 | 926 | "Return an updated collection of attendees for the current object." |
paul@765 | 927 | |
paul@765 | 928 | if self.is_initial_load() or not self.is_organiser(): |
paul@765 | 929 | return self.get_stored_attendees() |
paul@498 | 930 | else: |
paul@765 | 931 | return self.update_attendees_from_page() |
paul@474 | 932 | |
paul@446 | 933 | # Full page output methods. |
paul@446 | 934 | |
paul@446 | 935 | def show(self, path_info): |
paul@446 | 936 | |
paul@446 | 937 | "Show an object request using the given 'path_info' for the current user." |
paul@446 | 938 | |
paul@763 | 939 | uid, recurrenceid = self.get_identifiers(path_info) |
paul@763 | 940 | obj = self.get_stored_object(uid, recurrenceid) |
paul@756 | 941 | self.set_object(obj) |
paul@446 | 942 | |
paul@446 | 943 | if not obj: |
paul@446 | 944 | return False |
paul@446 | 945 | |
paul@756 | 946 | errors = self.handle_request() |
paul@446 | 947 | |
paul@492 | 948 | if not errors: |
paul@446 | 949 | return True |
paul@446 | 950 | |
paul@446 | 951 | self.new_page(title="Event") |
paul@756 | 952 | self.show_object_on_page(errors) |
paul@446 | 953 | |
paul@446 | 954 | return True |
paul@446 | 955 | |
paul@446 | 956 | # vim: tabstop=4 expandtab shiftwidth=4 |