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