paul@441 | 1 | #!/usr/bin/env python |
paul@441 | 2 | |
paul@441 | 3 | """ |
paul@441 | 4 | Common calendar client utilities. |
paul@441 | 5 | |
paul@441 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@441 | 7 | |
paul@441 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@441 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@441 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@441 | 11 | version. |
paul@441 | 12 | |
paul@441 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@441 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@441 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@441 | 16 | details. |
paul@441 | 17 | |
paul@441 | 18 | You should have received a copy of the GNU General Public License along with |
paul@441 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@441 | 20 | """ |
paul@441 | 21 | |
paul@601 | 22 | from datetime import datetime |
paul@668 | 23 | from imiptools.config import MANAGER_INTERFACE |
paul@606 | 24 | from imiptools.data import Object, get_address, get_uri, get_window_end, \ |
paul@606 | 25 | is_new_object, make_freebusy, to_part, \ |
paul@604 | 26 | uri_dict, uri_items, uri_values |
paul@669 | 27 | from imiptools.dates import check_permitted_values, format_datetime, get_default_timezone, \ |
paul@627 | 28 | get_timestamp, to_timezone |
paul@606 | 29 | from imiptools.period import can_schedule, remove_period, \ |
paul@606 | 30 | remove_additional_periods, remove_affected_period, \ |
paul@606 | 31 | update_freebusy |
paul@443 | 32 | from imiptools.profile import Preferences |
paul@604 | 33 | import imip_store |
paul@441 | 34 | |
paul@443 | 35 | class Client: |
paul@443 | 36 | |
paul@443 | 37 | "Common handler and manager methods." |
paul@443 | 38 | |
paul@467 | 39 | default_window_size = 100 |
paul@467 | 40 | |
paul@639 | 41 | def __init__(self, user, messenger=None, store=None, publisher=None, preferences_dir=None): |
paul@443 | 42 | self.user = user |
paul@601 | 43 | self.messenger = messenger |
paul@604 | 44 | self.store = store or imip_store.FileStore() |
paul@604 | 45 | |
paul@604 | 46 | try: |
paul@604 | 47 | self.publisher = publisher or imip_store.FilePublisher() |
paul@604 | 48 | except OSError: |
paul@604 | 49 | self.publisher = None |
paul@604 | 50 | |
paul@639 | 51 | self.preferences_dir = preferences_dir |
paul@443 | 52 | self.preferences = None |
paul@443 | 53 | |
paul@443 | 54 | def get_preferences(self): |
paul@467 | 55 | if not self.preferences and self.user: |
paul@639 | 56 | self.preferences = Preferences(self.user, self.preferences_dir) |
paul@443 | 57 | return self.preferences |
paul@443 | 58 | |
paul@443 | 59 | def get_tzid(self): |
paul@443 | 60 | prefs = self.get_preferences() |
paul@467 | 61 | return prefs and prefs.get("TZID") or get_default_timezone() |
paul@443 | 62 | |
paul@443 | 63 | def get_window_size(self): |
paul@443 | 64 | prefs = self.get_preferences() |
paul@443 | 65 | try: |
paul@467 | 66 | return prefs and int(prefs.get("window_size")) or self.default_window_size |
paul@443 | 67 | except (TypeError, ValueError): |
paul@467 | 68 | return self.default_window_size |
paul@443 | 69 | |
paul@443 | 70 | def get_window_end(self): |
paul@443 | 71 | return get_window_end(self.get_tzid(), self.get_window_size()) |
paul@443 | 72 | |
paul@667 | 73 | def is_participating(self): |
paul@667 | 74 | prefs = self.get_preferences() |
paul@667 | 75 | return prefs and prefs.get("participating", "participate") != "no" or False |
paul@667 | 76 | |
paul@443 | 77 | def is_sharing(self): |
paul@467 | 78 | prefs = self.get_preferences() |
paul@467 | 79 | return prefs and prefs.get("freebusy_sharing") == "share" or False |
paul@443 | 80 | |
paul@443 | 81 | def is_bundling(self): |
paul@467 | 82 | prefs = self.get_preferences() |
paul@467 | 83 | return prefs and prefs.get("freebusy_bundling") == "always" or False |
paul@467 | 84 | |
paul@467 | 85 | def is_notifying(self): |
paul@467 | 86 | prefs = self.get_preferences() |
paul@467 | 87 | return prefs and prefs.get("freebusy_messages") == "notify" or False |
paul@443 | 88 | |
paul@668 | 89 | def have_manager(self): |
paul@668 | 90 | return MANAGER_INTERFACE |
paul@668 | 91 | |
paul@669 | 92 | def get_permitted_values(self): |
paul@655 | 93 | |
paul@655 | 94 | """ |
paul@655 | 95 | Decode a specification of one of the following forms... |
paul@655 | 96 | |
paul@655 | 97 | <minute values> |
paul@655 | 98 | <hour values>:<minute values> |
paul@655 | 99 | <hour values>:<minute values>:<second values> |
paul@655 | 100 | |
paul@655 | 101 | ...with each list of values being comma-separated. |
paul@655 | 102 | """ |
paul@655 | 103 | |
paul@655 | 104 | prefs = self.get_preferences() |
paul@669 | 105 | permitted_values = prefs and prefs.get("permitted_times") |
paul@669 | 106 | if permitted_values: |
paul@655 | 107 | try: |
paul@655 | 108 | l = [] |
paul@669 | 109 | for component in permitted_values.split(":")[:3]: |
paul@655 | 110 | if component: |
paul@655 | 111 | l.append(map(int, component.split(","))) |
paul@655 | 112 | else: |
paul@655 | 113 | l.append(None) |
paul@655 | 114 | |
paul@655 | 115 | # NOTE: Should probably report an error somehow. |
paul@655 | 116 | |
paul@655 | 117 | except ValueError: |
paul@655 | 118 | return None |
paul@655 | 119 | else: |
paul@655 | 120 | l = (len(l) < 2 and [None] or []) + l + (len(l) < 3 and [None] or []) |
paul@655 | 121 | return l |
paul@655 | 122 | else: |
paul@655 | 123 | return None |
paul@655 | 124 | |
paul@581 | 125 | # Common operations on calendar data. |
paul@581 | 126 | |
paul@619 | 127 | def update_attendees(self, obj, attendees, removed): |
paul@619 | 128 | |
paul@619 | 129 | """ |
paul@619 | 130 | Update the attendees in 'obj' with the given 'attendees' and 'removed' |
paul@619 | 131 | attendee lists. A list is returned containing the attendees whose |
paul@619 | 132 | attendance should be cancelled. |
paul@619 | 133 | """ |
paul@619 | 134 | |
paul@619 | 135 | to_cancel = [] |
paul@619 | 136 | |
paul@619 | 137 | existing_attendees = uri_values(obj.get_values("ATTENDEE") or []) |
paul@619 | 138 | added = set(attendees).difference(existing_attendees) |
paul@619 | 139 | |
paul@619 | 140 | if added or removed: |
paul@619 | 141 | attendees = uri_items(obj.get_items("ATTENDEE") or []) |
paul@619 | 142 | sequence = obj.get_value("SEQUENCE") |
paul@619 | 143 | |
paul@619 | 144 | if removed: |
paul@619 | 145 | remaining = [] |
paul@619 | 146 | |
paul@619 | 147 | for attendee, attendee_attr in attendees: |
paul@619 | 148 | if attendee in removed: |
paul@619 | 149 | |
paul@619 | 150 | # Without a sequence number, assume that the event has not |
paul@619 | 151 | # been published and that attendees can be silently removed. |
paul@619 | 152 | |
paul@619 | 153 | if sequence is not None: |
paul@619 | 154 | to_cancel.append((attendee, attendee_attr)) |
paul@619 | 155 | else: |
paul@619 | 156 | remaining.append((attendee, attendee_attr)) |
paul@619 | 157 | |
paul@619 | 158 | attendees = remaining |
paul@619 | 159 | |
paul@619 | 160 | if added: |
paul@619 | 161 | for attendee in added: |
paul@619 | 162 | attendee = attendee.strip() |
paul@619 | 163 | if attendee: |
paul@619 | 164 | attendees.append((get_uri(attendee), {"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"})) |
paul@619 | 165 | |
paul@619 | 166 | obj["ATTENDEE"] = attendees |
paul@619 | 167 | |
paul@619 | 168 | return to_cancel |
paul@619 | 169 | |
paul@582 | 170 | def update_participation(self, obj, partstat=None): |
paul@581 | 171 | |
paul@581 | 172 | """ |
paul@581 | 173 | Update the participation in 'obj' of the user with the given 'partstat'. |
paul@581 | 174 | """ |
paul@581 | 175 | |
paul@581 | 176 | attendee_attr = uri_dict(obj.get_value_map("ATTENDEE")).get(self.user) |
paul@582 | 177 | if not attendee_attr: |
paul@582 | 178 | return None |
paul@582 | 179 | if partstat: |
paul@582 | 180 | attendee_attr["PARTSTAT"] = partstat |
paul@581 | 181 | if attendee_attr.has_key("RSVP"): |
paul@581 | 182 | del attendee_attr["RSVP"] |
paul@584 | 183 | self.update_sender(attendee_attr) |
paul@581 | 184 | return attendee_attr |
paul@581 | 185 | |
paul@584 | 186 | def update_sender(self, attr): |
paul@584 | 187 | |
paul@584 | 188 | "Update the SENT-BY attribute of the 'attr' sender metadata." |
paul@584 | 189 | |
paul@584 | 190 | if self.messenger and self.messenger.sender != get_address(self.user): |
paul@584 | 191 | attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@584 | 192 | |
paul@606 | 193 | def get_periods(self, obj): |
paul@606 | 194 | |
paul@606 | 195 | """ |
paul@606 | 196 | Return periods for the given 'obj'. Interpretation of periods can depend |
paul@606 | 197 | on the time zone, which is obtained for the current user. |
paul@606 | 198 | """ |
paul@606 | 199 | |
paul@606 | 200 | return obj.get_periods(self.get_tzid(), self.get_window_end()) |
paul@606 | 201 | |
paul@606 | 202 | # Store operations. |
paul@606 | 203 | |
paul@606 | 204 | def get_stored_object(self, uid, recurrenceid): |
paul@606 | 205 | |
paul@606 | 206 | """ |
paul@606 | 207 | Return the stored object for the current user, with the given 'uid' and |
paul@606 | 208 | 'recurrenceid'. |
paul@606 | 209 | """ |
paul@606 | 210 | |
paul@606 | 211 | fragment = self.store.get_event(self.user, uid, recurrenceid) |
paul@606 | 212 | return fragment and Object(fragment) |
paul@606 | 213 | |
paul@604 | 214 | # Free/busy operations. |
paul@604 | 215 | |
paul@606 | 216 | def get_freebusy_part(self, freebusy=None): |
paul@604 | 217 | |
paul@604 | 218 | """ |
paul@606 | 219 | Return a message part containing free/busy information for the user, |
paul@606 | 220 | either specified as 'freebusy' or obtained from the store directly. |
paul@604 | 221 | """ |
paul@604 | 222 | |
paul@604 | 223 | if self.is_sharing() and self.is_bundling(): |
paul@604 | 224 | |
paul@604 | 225 | # Invent a unique identifier. |
paul@604 | 226 | |
paul@604 | 227 | utcnow = get_timestamp() |
paul@604 | 228 | uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) |
paul@604 | 229 | |
paul@606 | 230 | freebusy = freebusy or self.store.get_freebusy(self.user) |
paul@604 | 231 | |
paul@604 | 232 | user_attr = {} |
paul@604 | 233 | self.update_sender(user_attr) |
paul@604 | 234 | return to_part("PUBLISH", [make_freebusy(freebusy, uid, self.user, user_attr)]) |
paul@604 | 235 | |
paul@604 | 236 | return None |
paul@604 | 237 | |
paul@606 | 238 | def update_freebusy(self, freebusy, periods, transp, uid, recurrenceid, summary, organiser): |
paul@606 | 239 | |
paul@606 | 240 | """ |
paul@606 | 241 | Update the 'freebusy' collection with the given 'periods', indicating a |
paul@606 | 242 | 'transp' status, explicit 'uid' and 'recurrenceid' to indicate either a |
paul@606 | 243 | recurrence or the parent event. The 'summary' and 'organiser' must also |
paul@606 | 244 | be provided. |
paul@606 | 245 | """ |
paul@606 | 246 | |
paul@612 | 247 | update_freebusy(freebusy, periods, transp, uid, recurrenceid, summary, organiser) |
paul@606 | 248 | |
paul@601 | 249 | class ClientForObject(Client): |
paul@601 | 250 | |
paul@601 | 251 | "A client maintaining a specific object." |
paul@601 | 252 | |
paul@639 | 253 | def __init__(self, obj, user, messenger=None, store=None, publisher=None, preferences_dir=None): |
paul@639 | 254 | Client.__init__(self, user, messenger, store, publisher, preferences_dir) |
paul@601 | 255 | self.set_object(obj) |
paul@601 | 256 | |
paul@601 | 257 | def set_object(self, obj): |
paul@606 | 258 | |
paul@606 | 259 | "Set the current object to 'obj', obtaining metadata details." |
paul@606 | 260 | |
paul@601 | 261 | self.obj = obj |
paul@601 | 262 | self.uid = obj and self.obj.get_uid() |
paul@601 | 263 | self.recurrenceid = obj and self.obj.get_recurrenceid() |
paul@601 | 264 | self.sequence = obj and self.obj.get_value("SEQUENCE") |
paul@601 | 265 | self.dtstamp = obj and self.obj.get_value("DTSTAMP") |
paul@601 | 266 | |
paul@604 | 267 | # Object update methods. |
paul@601 | 268 | |
paul@601 | 269 | def update_dtstamp(self): |
paul@601 | 270 | |
paul@601 | 271 | "Update the DTSTAMP in the current object." |
paul@601 | 272 | |
paul@601 | 273 | dtstamp = self.obj.get_utc_datetime("DTSTAMP") |
paul@601 | 274 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@601 | 275 | self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@601 | 276 | |
paul@601 | 277 | def set_sequence(self, increment=False): |
paul@601 | 278 | |
paul@601 | 279 | "Update the SEQUENCE in the current object." |
paul@601 | 280 | |
paul@601 | 281 | sequence = self.obj.get_value("SEQUENCE") or "0" |
paul@601 | 282 | self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@601 | 283 | |
paul@606 | 284 | def merge_attendance(self, attendees): |
paul@606 | 285 | |
paul@606 | 286 | """ |
paul@606 | 287 | Merge attendance from the current object's 'attendees' into the version |
paul@606 | 288 | stored for the current user. |
paul@606 | 289 | """ |
paul@606 | 290 | |
paul@606 | 291 | obj = self.get_stored_object_version() |
paul@606 | 292 | |
paul@606 | 293 | if not obj or not self.have_new_object(obj): |
paul@606 | 294 | return False |
paul@606 | 295 | |
paul@606 | 296 | # Get attendee details in a usable form. |
paul@606 | 297 | |
paul@606 | 298 | attendee_map = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@606 | 299 | |
paul@606 | 300 | for attendee, attendee_attr in attendees.items(): |
paul@606 | 301 | |
paul@606 | 302 | # Update attendance in the loaded object. |
paul@606 | 303 | |
paul@606 | 304 | attendee_map[attendee] = attendee_attr |
paul@606 | 305 | |
paul@606 | 306 | # Set the new details and store the object. |
paul@606 | 307 | |
paul@606 | 308 | obj["ATTENDEE"] = attendee_map.items() |
paul@606 | 309 | |
paul@606 | 310 | # Set the complete event if not an additional occurrence. |
paul@606 | 311 | |
paul@606 | 312 | event = obj.to_node() |
paul@606 | 313 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@606 | 314 | |
paul@606 | 315 | return True |
paul@606 | 316 | |
paul@606 | 317 | # Object-related tests. |
paul@606 | 318 | |
paul@606 | 319 | def get_attendance(self, user=None): |
paul@606 | 320 | |
paul@606 | 321 | """ |
paul@606 | 322 | Return the attendance attributes for 'user', or the current user if |
paul@606 | 323 | 'user' is not specified. |
paul@606 | 324 | """ |
paul@606 | 325 | |
paul@606 | 326 | attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@606 | 327 | return attendees.get(user or self.user) or {} |
paul@606 | 328 | |
paul@609 | 329 | def is_participating(self, user, as_organiser=False): |
paul@609 | 330 | |
paul@609 | 331 | """ |
paul@609 | 332 | Return whether, subject to the 'user' indicating an identity and the |
paul@609 | 333 | 'as_organiser' status of that identity, the user concerned is actually |
paul@609 | 334 | participating in the current object event. |
paul@609 | 335 | """ |
paul@609 | 336 | |
paul@609 | 337 | attr = self.get_attendance(user) |
paul@609 | 338 | return as_organiser or not attr or attr.get("PARTSTAT") != "DECLINED" |
paul@609 | 339 | |
paul@609 | 340 | def get_overriding_transparency(self, user, as_organiser=False): |
paul@609 | 341 | |
paul@609 | 342 | """ |
paul@609 | 343 | Return the overriding transparency to be associated with the free/busy |
paul@609 | 344 | records for an event, subject to the 'user' indicating an identity and |
paul@609 | 345 | the 'as_organiser' status of that identity. |
paul@609 | 346 | |
paul@609 | 347 | Where an identity is only an organiser and not attending, "ORG" is |
paul@609 | 348 | returned. Otherwise, no overriding transparency is defined and None is |
paul@609 | 349 | returned. |
paul@609 | 350 | """ |
paul@609 | 351 | |
paul@609 | 352 | attr = self.get_attendance(user) |
paul@609 | 353 | return as_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None |
paul@609 | 354 | |
paul@606 | 355 | def is_attendee(self, identity, obj=None): |
paul@606 | 356 | |
paul@606 | 357 | """ |
paul@606 | 358 | Return whether 'identity' is an attendee in the current object, or in |
paul@606 | 359 | 'obj' if specified. |
paul@606 | 360 | """ |
paul@606 | 361 | |
paul@606 | 362 | return identity in uri_values((obj or self.obj).get_values("ATTENDEE")) |
paul@606 | 363 | |
paul@606 | 364 | def can_schedule(self, freebusy, periods): |
paul@606 | 365 | |
paul@606 | 366 | """ |
paul@606 | 367 | Indicate whether within 'freebusy' the given 'periods' can be scheduled. |
paul@606 | 368 | """ |
paul@606 | 369 | |
paul@606 | 370 | return can_schedule(freebusy, periods, self.uid, self.recurrenceid) |
paul@606 | 371 | |
paul@606 | 372 | def have_new_object(self, obj=None): |
paul@606 | 373 | |
paul@606 | 374 | """ |
paul@606 | 375 | Return whether the current object is new to the current user (or if the |
paul@606 | 376 | given 'obj' is new). |
paul@606 | 377 | """ |
paul@606 | 378 | |
paul@606 | 379 | obj = obj or self.get_stored_object_version() |
paul@606 | 380 | |
paul@606 | 381 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@606 | 382 | |
paul@606 | 383 | if obj: |
paul@606 | 384 | sequence = obj.get_value("SEQUENCE") |
paul@606 | 385 | dtstamp = obj.get_value("DTSTAMP") |
paul@606 | 386 | |
paul@606 | 387 | # If the request refers to an older version of the object, ignore |
paul@606 | 388 | # it. |
paul@606 | 389 | |
paul@606 | 390 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, |
paul@606 | 391 | self.is_partstat_updated(obj)) |
paul@606 | 392 | |
paul@606 | 393 | return True |
paul@606 | 394 | |
paul@606 | 395 | def is_partstat_updated(self, obj): |
paul@606 | 396 | |
paul@606 | 397 | """ |
paul@606 | 398 | Return whether the participant status has been updated in the current |
paul@606 | 399 | object in comparison to the given 'obj'. |
paul@606 | 400 | |
paul@606 | 401 | NOTE: Some clients like Claws Mail erase time information from DTSTAMP |
paul@606 | 402 | NOTE: and make it invalid. Thus, such attendance information may also be |
paul@606 | 403 | NOTE: incorporated into any new object assessment. |
paul@606 | 404 | """ |
paul@606 | 405 | |
paul@606 | 406 | old_attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@606 | 407 | new_attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@606 | 408 | |
paul@606 | 409 | for attendee, attr in old_attendees.items(): |
paul@606 | 410 | old_partstat = attr.get("PARTSTAT") |
paul@606 | 411 | new_attr = new_attendees.get(attendee) |
paul@606 | 412 | new_partstat = new_attr and new_attr.get("PARTSTAT") |
paul@606 | 413 | |
paul@606 | 414 | if old_partstat == "NEEDS-ACTION" and new_partstat and \ |
paul@606 | 415 | new_partstat != old_partstat: |
paul@606 | 416 | |
paul@606 | 417 | return True |
paul@606 | 418 | |
paul@606 | 419 | return False |
paul@606 | 420 | |
paul@655 | 421 | # Constraint application on event periods. |
paul@655 | 422 | |
paul@655 | 423 | def check_object(self): |
paul@655 | 424 | |
paul@655 | 425 | "Check the object against any scheduling constraints." |
paul@655 | 426 | |
paul@669 | 427 | permitted_values = self.get_permitted_values() |
paul@669 | 428 | if not permitted_values: |
paul@655 | 429 | return None |
paul@655 | 430 | |
paul@655 | 431 | invalid = [] |
paul@655 | 432 | |
paul@660 | 433 | for period in self.obj.get_periods(self.get_tzid()): |
paul@655 | 434 | start = period.get_start() |
paul@655 | 435 | end = period.get_end() |
paul@669 | 436 | start_errors = check_permitted_values(start, permitted_values) |
paul@669 | 437 | end_errors = check_permitted_values(end, permitted_values) |
paul@656 | 438 | if start_errors or end_errors: |
paul@656 | 439 | invalid.append((period.origin, start_errors, end_errors)) |
paul@655 | 440 | |
paul@655 | 441 | return invalid |
paul@655 | 442 | |
paul@660 | 443 | def correct_object(self): |
paul@655 | 444 | |
paul@660 | 445 | "Correct the object according to any scheduling constraints." |
paul@655 | 446 | |
paul@669 | 447 | permitted_values = self.get_permitted_values() |
paul@669 | 448 | return permitted_values and self.obj.correct_object(self.get_tzid(), permitted_values) |
paul@655 | 449 | |
paul@606 | 450 | # Object retrieval. |
paul@606 | 451 | |
paul@606 | 452 | def get_stored_object_version(self): |
paul@606 | 453 | |
paul@606 | 454 | """ |
paul@606 | 455 | Return the stored object to which the current object refers for the |
paul@606 | 456 | current user. |
paul@606 | 457 | """ |
paul@606 | 458 | |
paul@606 | 459 | return self.get_stored_object(self.uid, self.recurrenceid) |
paul@606 | 460 | |
paul@606 | 461 | def get_definitive_object(self, from_organiser): |
paul@606 | 462 | |
paul@606 | 463 | """ |
paul@606 | 464 | Return an object considered definitive for the current transaction, |
paul@606 | 465 | using 'from_organiser' to select the current transaction's object if |
paul@606 | 466 | true, or selecting a stored object if false. |
paul@606 | 467 | """ |
paul@606 | 468 | |
paul@606 | 469 | return from_organiser and self.obj or self.get_stored_object_version() |
paul@606 | 470 | |
paul@606 | 471 | def get_parent_object(self): |
paul@606 | 472 | |
paul@606 | 473 | """ |
paul@606 | 474 | Return the parent object to which the current object refers for the |
paul@606 | 475 | current user. |
paul@606 | 476 | """ |
paul@606 | 477 | |
paul@606 | 478 | return self.recurrenceid and self.get_stored_object(self.uid, None) or None |
paul@606 | 479 | |
paul@606 | 480 | # Convenience methods for modifying free/busy collections. |
paul@606 | 481 | |
paul@606 | 482 | def get_recurrence_start_point(self, recurrenceid): |
paul@606 | 483 | |
paul@606 | 484 | "Get 'recurrenceid' in a form suitable for matching free/busy entries." |
paul@606 | 485 | |
paul@627 | 486 | return self.obj.get_recurrence_start_point(recurrenceid, self.get_tzid()) |
paul@606 | 487 | |
paul@606 | 488 | def remove_from_freebusy(self, freebusy): |
paul@606 | 489 | |
paul@606 | 490 | "Remove this event from the given 'freebusy' collection." |
paul@606 | 491 | |
paul@606 | 492 | if not remove_period(freebusy, self.uid, self.recurrenceid) and self.recurrenceid: |
paul@606 | 493 | remove_affected_period(freebusy, self.uid, self.get_recurrence_start_point(self.recurrenceid)) |
paul@606 | 494 | |
paul@606 | 495 | def remove_freebusy_for_recurrences(self, freebusy, recurrenceids=None): |
paul@606 | 496 | |
paul@606 | 497 | """ |
paul@606 | 498 | Remove from 'freebusy' any original recurrence from parent free/busy |
paul@606 | 499 | details for the current object, if the current object is a specific |
paul@606 | 500 | additional recurrence. Otherwise, remove all additional recurrence |
paul@606 | 501 | information corresponding to 'recurrenceids', or if omitted, all |
paul@606 | 502 | recurrences. |
paul@606 | 503 | """ |
paul@606 | 504 | |
paul@606 | 505 | if self.recurrenceid: |
paul@606 | 506 | recurrenceid = self.get_recurrence_start_point(self.recurrenceid) |
paul@606 | 507 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@606 | 508 | else: |
paul@606 | 509 | # Remove obsolete recurrence periods. |
paul@606 | 510 | |
paul@606 | 511 | remove_additional_periods(freebusy, self.uid, recurrenceids) |
paul@606 | 512 | |
paul@606 | 513 | # Remove original periods affected by additional recurrences. |
paul@606 | 514 | |
paul@606 | 515 | if recurrenceids: |
paul@606 | 516 | for recurrenceid in recurrenceids: |
paul@606 | 517 | recurrenceid = self.get_recurrence_start_point(recurrenceid) |
paul@606 | 518 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@606 | 519 | |
paul@606 | 520 | def update_freebusy(self, freebusy, user, for_organiser): |
paul@606 | 521 | |
paul@606 | 522 | """ |
paul@606 | 523 | Update the 'freebusy' collection for this event with the periods and |
paul@606 | 524 | transparency associated with the current object, subject to the 'user' |
paul@606 | 525 | identity and the attendance details provided for them, indicating |
paul@606 | 526 | whether the update is 'for_organiser' or not. |
paul@606 | 527 | """ |
paul@606 | 528 | |
paul@606 | 529 | # Obtain the stored object if the current object is not issued by the |
paul@606 | 530 | # organiser. Attendees do not have the opportunity to redefine the |
paul@606 | 531 | # periods. |
paul@606 | 532 | |
paul@606 | 533 | obj = self.get_definitive_object(for_organiser) |
paul@606 | 534 | if not obj: |
paul@606 | 535 | return |
paul@606 | 536 | |
paul@606 | 537 | # Obtain the affected periods. |
paul@606 | 538 | |
paul@606 | 539 | periods = self.get_periods(obj) |
paul@606 | 540 | |
paul@606 | 541 | # Define an overriding transparency, the indicated event transparency, |
paul@606 | 542 | # or the default transparency for the free/busy entry. |
paul@606 | 543 | |
paul@606 | 544 | transp = self.get_overriding_transparency(user, for_organiser) or \ |
paul@606 | 545 | obj.get_value("TRANSP") or \ |
paul@606 | 546 | "OPAQUE" |
paul@606 | 547 | |
paul@606 | 548 | # Perform the low-level update. |
paul@606 | 549 | |
paul@606 | 550 | Client.update_freebusy(self, freebusy, periods, transp, |
paul@606 | 551 | self.uid, self.recurrenceid, |
paul@606 | 552 | obj.get_value("SUMMARY"), |
paul@606 | 553 | obj.get_value("ORGANIZER")) |
paul@606 | 554 | |
paul@606 | 555 | def update_freebusy_for_participant(self, freebusy, user, for_organiser=False, |
paul@606 | 556 | updating_other=False): |
paul@606 | 557 | |
paul@606 | 558 | """ |
paul@606 | 559 | Update the 'freebusy' collection using the given 'periods', involving |
paul@606 | 560 | the given 'user', indicating whether the update is 'for_organiser' or |
paul@606 | 561 | not, and whether it is 'updating_other' (meaning another user's |
paul@606 | 562 | details). |
paul@606 | 563 | """ |
paul@606 | 564 | |
paul@606 | 565 | # Record in the free/busy details unless a non-participating attendee. |
paul@606 | 566 | # Use any attendee information for an organiser, not the organiser's own |
paul@606 | 567 | # attributes. |
paul@606 | 568 | |
paul@606 | 569 | if self.is_participating(user, for_organiser and not updating_other): |
paul@606 | 570 | self.update_freebusy(freebusy, user, for_organiser) |
paul@606 | 571 | else: |
paul@606 | 572 | self.remove_from_freebusy(freebusy) |
paul@606 | 573 | |
paul@606 | 574 | # Convenience methods for updating stored free/busy information received |
paul@606 | 575 | # from other users. |
paul@606 | 576 | |
paul@606 | 577 | def update_freebusy_from_participant(self, user, for_organiser): |
paul@606 | 578 | |
paul@606 | 579 | """ |
paul@606 | 580 | For the current user, record the free/busy information for another |
paul@606 | 581 | 'user', indicating whether the update is 'for_organiser' or not, thus |
paul@606 | 582 | maintaining a separate record of their free/busy details. |
paul@606 | 583 | """ |
paul@606 | 584 | |
paul@606 | 585 | # A user does not store free/busy information for themself as another |
paul@606 | 586 | # party. |
paul@606 | 587 | |
paul@606 | 588 | if user == self.user: |
paul@606 | 589 | return |
paul@606 | 590 | |
paul@606 | 591 | freebusy = self.store.get_freebusy_for_other(self.user, user) |
paul@606 | 592 | self.update_freebusy_for_participant(freebusy, user, for_organiser, True) |
paul@606 | 593 | |
paul@606 | 594 | # Tidy up any obsolete recurrences. |
paul@606 | 595 | |
paul@606 | 596 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@606 | 597 | self.store.set_freebusy_for_other(self.user, freebusy, user) |
paul@606 | 598 | |
paul@606 | 599 | def update_freebusy_from_organiser(self, organiser): |
paul@606 | 600 | |
paul@606 | 601 | "For the current user, record free/busy information from 'organiser'." |
paul@606 | 602 | |
paul@606 | 603 | self.update_freebusy_from_participant(organiser, True) |
paul@606 | 604 | |
paul@606 | 605 | def update_freebusy_from_attendees(self, attendees): |
paul@606 | 606 | |
paul@606 | 607 | "For the current user, record free/busy information from 'attendees'." |
paul@606 | 608 | |
paul@606 | 609 | for attendee in attendees.keys(): |
paul@606 | 610 | self.update_freebusy_from_participant(attendee, False) |
paul@606 | 611 | |
paul@441 | 612 | # vim: tabstop=4 expandtab shiftwidth=4 |