paul@418 | 1 | #!/usr/bin/env python |
paul@418 | 2 | |
paul@418 | 3 | """ |
paul@418 | 4 | General handler support for incoming calendar objects. |
paul@418 | 5 | |
paul@418 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@418 | 7 | |
paul@418 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@418 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@418 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@418 | 11 | version. |
paul@418 | 12 | |
paul@418 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@418 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@418 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@418 | 16 | details. |
paul@418 | 17 | |
paul@418 | 18 | You should have received a copy of the GNU General Public License along with |
paul@418 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@418 | 20 | """ |
paul@418 | 21 | |
paul@418 | 22 | from datetime import datetime |
paul@418 | 23 | from email.mime.text import MIMEText |
paul@468 | 24 | from imiptools.client import Client |
paul@418 | 25 | from imiptools.config import MANAGER_PATH, MANAGER_URL |
paul@418 | 26 | from imiptools.data import Object, \ |
paul@468 | 27 | get_address, get_uri, get_value, \ |
paul@418 | 28 | is_new_object, uri_dict, uri_item, uri_values |
paul@561 | 29 | from imiptools.dates import format_datetime, to_recurrence_start_point, \ |
paul@561 | 30 | to_timezone |
paul@418 | 31 | from imiptools.period import can_schedule, remove_period, \ |
paul@418 | 32 | remove_additional_periods, remove_affected_period, \ |
paul@418 | 33 | update_freebusy |
paul@418 | 34 | from imiptools.profile import Preferences |
paul@418 | 35 | from socket import gethostname |
paul@418 | 36 | import imip_store |
paul@418 | 37 | |
paul@418 | 38 | # References to the Web interface. |
paul@418 | 39 | |
paul@418 | 40 | def get_manager_url(): |
paul@418 | 41 | url_base = MANAGER_URL or "http://%s/" % gethostname() |
paul@418 | 42 | return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/")) |
paul@418 | 43 | |
paul@418 | 44 | def get_object_url(uid, recurrenceid=None): |
paul@418 | 45 | return "%s/%s%s" % ( |
paul@418 | 46 | get_manager_url().rstrip("/"), uid, |
paul@418 | 47 | recurrenceid and "/%s" % recurrenceid or "" |
paul@418 | 48 | ) |
paul@418 | 49 | |
paul@468 | 50 | class Handler(Client): |
paul@418 | 51 | |
paul@418 | 52 | "General handler support." |
paul@418 | 53 | |
paul@418 | 54 | def __init__(self, senders=None, recipient=None, messenger=None): |
paul@418 | 55 | |
paul@418 | 56 | """ |
paul@418 | 57 | Initialise the handler with the calendar 'obj' and the 'senders' and |
paul@418 | 58 | 'recipient' of the object (if specifically indicated). |
paul@418 | 59 | """ |
paul@418 | 60 | |
paul@468 | 61 | Client.__init__(self, recipient and get_uri(recipient)) |
paul@468 | 62 | |
paul@418 | 63 | self.senders = senders and set(map(get_address, senders)) |
paul@418 | 64 | self.recipient = recipient and get_address(recipient) |
paul@418 | 65 | self.messenger = messenger |
paul@418 | 66 | |
paul@418 | 67 | self.results = [] |
paul@418 | 68 | self.outgoing_methods = set() |
paul@418 | 69 | |
paul@418 | 70 | self.obj = None |
paul@418 | 71 | self.uid = None |
paul@418 | 72 | self.recurrenceid = None |
paul@418 | 73 | self.sequence = None |
paul@418 | 74 | self.dtstamp = None |
paul@418 | 75 | |
paul@418 | 76 | self.store = imip_store.FileStore() |
paul@418 | 77 | |
paul@418 | 78 | try: |
paul@418 | 79 | self.publisher = imip_store.FilePublisher() |
paul@418 | 80 | except OSError: |
paul@418 | 81 | self.publisher = None |
paul@418 | 82 | |
paul@418 | 83 | def set_object(self, obj): |
paul@418 | 84 | self.obj = obj |
paul@535 | 85 | self.uid = self.obj.get_uid() |
paul@535 | 86 | self.recurrenceid = self.obj.get_recurrenceid() |
paul@418 | 87 | self.sequence = self.obj.get_value("SEQUENCE") |
paul@418 | 88 | self.dtstamp = self.obj.get_value("DTSTAMP") |
paul@418 | 89 | |
paul@418 | 90 | def wrap(self, text, link=True): |
paul@418 | 91 | |
paul@418 | 92 | "Wrap any valid message for passing to the recipient." |
paul@418 | 93 | |
paul@418 | 94 | texts = [] |
paul@418 | 95 | texts.append(text) |
paul@418 | 96 | if link: |
paul@418 | 97 | texts.append("If your mail program cannot handle this " |
paul@418 | 98 | "message, you may view the details here:\n\n%s" % |
paul@418 | 99 | get_object_url(self.uid, self.recurrenceid)) |
paul@418 | 100 | |
paul@418 | 101 | return self.add_result(None, None, MIMEText("\n".join(texts))) |
paul@418 | 102 | |
paul@418 | 103 | # Result registration. |
paul@418 | 104 | |
paul@418 | 105 | def add_result(self, method, outgoing_recipients, part): |
paul@418 | 106 | |
paul@418 | 107 | """ |
paul@418 | 108 | Record a result having the given 'method', 'outgoing_recipients' and |
paul@418 | 109 | message part. |
paul@418 | 110 | """ |
paul@418 | 111 | |
paul@418 | 112 | if outgoing_recipients: |
paul@418 | 113 | self.outgoing_methods.add(method) |
paul@418 | 114 | self.results.append((outgoing_recipients, part)) |
paul@418 | 115 | |
paul@418 | 116 | def get_results(self): |
paul@418 | 117 | return self.results |
paul@418 | 118 | |
paul@418 | 119 | def get_outgoing_methods(self): |
paul@418 | 120 | return self.outgoing_methods |
paul@418 | 121 | |
paul@418 | 122 | # Convenience methods for modifying free/busy collections. |
paul@418 | 123 | |
paul@561 | 124 | def to_recurrence_start_point(self, recurrenceid): |
paul@511 | 125 | |
paul@511 | 126 | "Get 'recurrenceid' in a form suitable for matching free/busy entries." |
paul@511 | 127 | |
paul@561 | 128 | tzid = self.obj.get_tzid() or self.get_tzid() |
paul@561 | 129 | return to_recurrence_start_point(recurrenceid, tzid) |
paul@511 | 130 | |
paul@418 | 131 | def remove_from_freebusy(self, freebusy): |
paul@418 | 132 | |
paul@418 | 133 | "Remove this event from the given 'freebusy' collection." |
paul@418 | 134 | |
paul@523 | 135 | if not remove_period(freebusy, self.uid, self.recurrenceid) and self.recurrenceid: |
paul@561 | 136 | tzid = self.obj.get_tzid() or self.get_tzid() |
paul@561 | 137 | remove_affected_period(freebusy, self.uid, self.to_recurrence_start_point(self.recurrenceid)) |
paul@418 | 138 | |
paul@418 | 139 | def remove_freebusy_for_recurrences(self, freebusy, recurrenceids=None): |
paul@418 | 140 | |
paul@418 | 141 | """ |
paul@418 | 142 | Remove from 'freebusy' any original recurrence from parent free/busy |
paul@418 | 143 | details for the current object, if the current object is a specific |
paul@418 | 144 | additional recurrence. Otherwise, remove all additional recurrence |
paul@418 | 145 | information corresponding to 'recurrenceids', or if omitted, all |
paul@418 | 146 | recurrences. |
paul@418 | 147 | """ |
paul@418 | 148 | |
paul@418 | 149 | if self.recurrenceid: |
paul@561 | 150 | recurrenceid = self.to_recurrence_start_point(self.recurrenceid) |
paul@511 | 151 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@418 | 152 | else: |
paul@418 | 153 | # Remove obsolete recurrence periods. |
paul@418 | 154 | |
paul@418 | 155 | remove_additional_periods(freebusy, self.uid, recurrenceids) |
paul@418 | 156 | |
paul@418 | 157 | # Remove original periods affected by additional recurrences. |
paul@418 | 158 | |
paul@418 | 159 | if recurrenceids: |
paul@418 | 160 | for recurrenceid in recurrenceids: |
paul@561 | 161 | recurrenceid = self.to_recurrence_start_point(recurrenceid) |
paul@418 | 162 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@418 | 163 | |
paul@418 | 164 | def _update_freebusy(self, freebusy, periods, recurrenceid, transp=None): |
paul@418 | 165 | |
paul@418 | 166 | """ |
paul@418 | 167 | Update the 'freebusy' collection with the given 'periods', indicating an |
paul@418 | 168 | explicit 'recurrenceid' to affect either a recurrence or the parent |
paul@418 | 169 | event. |
paul@418 | 170 | """ |
paul@418 | 171 | |
paul@418 | 172 | update_freebusy(freebusy, periods, |
paul@418 | 173 | transp or self.obj.get_value("TRANSP"), |
paul@418 | 174 | self.uid, recurrenceid, |
paul@418 | 175 | self.obj.get_value("SUMMARY"), |
paul@418 | 176 | self.obj.get_value("ORGANIZER")) |
paul@418 | 177 | |
paul@418 | 178 | def update_freebusy(self, freebusy, periods, transp=None): |
paul@418 | 179 | |
paul@418 | 180 | """ |
paul@418 | 181 | Update the 'freebusy' collection for this event with the given |
paul@418 | 182 | 'periods'. |
paul@418 | 183 | """ |
paul@418 | 184 | |
paul@418 | 185 | self._update_freebusy(freebusy, periods, self.recurrenceid, transp) |
paul@418 | 186 | |
paul@418 | 187 | def update_freebusy_for_participant(self, freebusy, periods, attr, for_organiser=False): |
paul@418 | 188 | |
paul@418 | 189 | """ |
paul@418 | 190 | Update the 'freebusy' collection using the given 'periods', subject to |
paul@418 | 191 | the 'attr' provided for the participant, indicating whether this is |
paul@418 | 192 | being generated 'for_organiser' or not. |
paul@418 | 193 | """ |
paul@418 | 194 | |
paul@491 | 195 | # Organisers employ a special transparency if not attending. |
paul@418 | 196 | |
paul@505 | 197 | if for_organiser or not attr or attr.get("PARTSTAT") != "DECLINED": |
paul@491 | 198 | self.update_freebusy(freebusy, periods, transp=( |
paul@520 | 199 | for_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None)) |
paul@418 | 200 | else: |
paul@418 | 201 | self.remove_from_freebusy(freebusy) |
paul@418 | 202 | |
paul@418 | 203 | # Convenience methods for updating stored free/busy information. |
paul@418 | 204 | |
paul@468 | 205 | def update_freebusy_from_participant(self, participant_item, for_organiser): |
paul@418 | 206 | |
paul@418 | 207 | """ |
paul@468 | 208 | For the calendar user, record the free/busy information for the |
paul@418 | 209 | 'participant_item' (a value plus attributes) representing a different |
paul@418 | 210 | identity, thus maintaining a separate record of their free/busy details. |
paul@418 | 211 | """ |
paul@418 | 212 | |
paul@418 | 213 | participant, participant_attr = participant_item |
paul@418 | 214 | |
paul@468 | 215 | if participant == self.user: |
paul@418 | 216 | return |
paul@418 | 217 | |
paul@468 | 218 | freebusy = self.store.get_freebusy_for_other(self.user, participant) |
paul@419 | 219 | |
paul@419 | 220 | # Obtain the stored object if the current object is not issued by the |
paul@419 | 221 | # organiser. |
paul@419 | 222 | |
paul@468 | 223 | obj = for_organiser and self.obj or self.get_object() |
paul@419 | 224 | if not obj: |
paul@419 | 225 | return |
paul@419 | 226 | |
paul@419 | 227 | # Obtain the affected periods. |
paul@419 | 228 | |
paul@557 | 229 | periods = obj.get_periods(self.get_tzid(), self.get_window_end()) |
paul@418 | 230 | |
paul@418 | 231 | # Record in the free/busy details unless a non-participating attendee. |
paul@505 | 232 | # Use any attendee information for an organiser, not the organiser's own |
paul@505 | 233 | # attributes. |
paul@505 | 234 | |
paul@505 | 235 | if for_organiser: |
paul@505 | 236 | participant_attr = obj.get_value_map("ATTENDEE").get(participant) |
paul@418 | 237 | |
paul@418 | 238 | self.update_freebusy_for_participant(freebusy, periods, participant_attr, |
paul@468 | 239 | for_organiser and not self.is_attendee(participant)) |
paul@418 | 240 | |
paul@505 | 241 | # Tidy up any obsolete recurrences. |
paul@505 | 242 | |
paul@468 | 243 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@468 | 244 | self.store.set_freebusy_for_other(self.user, freebusy, participant) |
paul@418 | 245 | |
paul@468 | 246 | def update_freebusy_from_organiser(self, organiser_item): |
paul@418 | 247 | |
paul@418 | 248 | """ |
paul@468 | 249 | For the current user, record free/busy information from the |
paul@418 | 250 | 'organiser_item' (a value plus attributes). |
paul@418 | 251 | """ |
paul@418 | 252 | |
paul@468 | 253 | self.update_freebusy_from_participant(organiser_item, True) |
paul@418 | 254 | |
paul@468 | 255 | def update_freebusy_from_attendees(self, attendees): |
paul@418 | 256 | |
paul@468 | 257 | "For the current user, record free/busy information from 'attendees'." |
paul@418 | 258 | |
paul@418 | 259 | for attendee_item in attendees.items(): |
paul@468 | 260 | self.update_freebusy_from_participant(attendee_item, False) |
paul@418 | 261 | |
paul@418 | 262 | # Logic, filtering and access to calendar structures and other data. |
paul@418 | 263 | |
paul@468 | 264 | def is_attendee(self, identity, obj=None): |
paul@418 | 265 | |
paul@468 | 266 | """ |
paul@468 | 267 | Return whether 'identity' is an attendee in the current object, or in |
paul@468 | 268 | 'obj' if specified. |
paul@468 | 269 | """ |
paul@418 | 270 | |
paul@468 | 271 | return identity in uri_values((obj or self.obj).get_values("ATTENDEE")) |
paul@418 | 272 | |
paul@418 | 273 | def can_schedule(self, freebusy, periods): |
paul@418 | 274 | return can_schedule(freebusy, periods, self.uid, self.recurrenceid) |
paul@418 | 275 | |
paul@418 | 276 | def filter_by_senders(self, mapping): |
paul@418 | 277 | |
paul@418 | 278 | """ |
paul@418 | 279 | Return a list of items from 'mapping' filtered using sender information. |
paul@418 | 280 | """ |
paul@418 | 281 | |
paul@418 | 282 | if self.senders: |
paul@418 | 283 | |
paul@418 | 284 | # Get a mapping from senders to identities. |
paul@418 | 285 | |
paul@418 | 286 | identities = self.get_sender_identities(mapping) |
paul@418 | 287 | |
paul@418 | 288 | # Find the senders that are valid. |
paul@418 | 289 | |
paul@418 | 290 | senders = map(get_address, identities) |
paul@418 | 291 | valid = self.senders.intersection(senders) |
paul@418 | 292 | |
paul@418 | 293 | # Return the true identities. |
paul@418 | 294 | |
paul@418 | 295 | return [identities[get_uri(address)] for address in valid] |
paul@418 | 296 | else: |
paul@418 | 297 | return mapping |
paul@418 | 298 | |
paul@418 | 299 | def filter_by_recipient(self, mapping): |
paul@418 | 300 | |
paul@418 | 301 | """ |
paul@418 | 302 | Return a list of items from 'mapping' filtered using recipient |
paul@418 | 303 | information. |
paul@418 | 304 | """ |
paul@418 | 305 | |
paul@418 | 306 | if self.recipient: |
paul@418 | 307 | addresses = set(map(get_address, mapping)) |
paul@418 | 308 | return map(get_uri, addresses.intersection([self.recipient])) |
paul@418 | 309 | else: |
paul@418 | 310 | return mapping |
paul@418 | 311 | |
paul@418 | 312 | def require_organiser(self, from_organiser=True): |
paul@418 | 313 | |
paul@418 | 314 | """ |
paul@418 | 315 | Return the organiser for the current object, filtered for the sender or |
paul@418 | 316 | recipient of interest. Return None if no identities are eligible. |
paul@418 | 317 | |
paul@418 | 318 | The organiser identity is normalized. |
paul@418 | 319 | """ |
paul@418 | 320 | |
paul@418 | 321 | organiser_item = uri_item(self.obj.get_item("ORGANIZER")) |
paul@418 | 322 | |
paul@418 | 323 | # Only provide details for an organiser who sent/receives the message. |
paul@418 | 324 | |
paul@418 | 325 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient |
paul@418 | 326 | |
paul@418 | 327 | if not organiser_filter_fn(dict([organiser_item])): |
paul@418 | 328 | return None |
paul@418 | 329 | |
paul@418 | 330 | return organiser_item |
paul@418 | 331 | |
paul@418 | 332 | def require_attendees(self, from_organiser=True): |
paul@418 | 333 | |
paul@418 | 334 | """ |
paul@418 | 335 | Return the attendees for the current object, filtered for the sender or |
paul@418 | 336 | recipient of interest. Return None if no identities are eligible. |
paul@418 | 337 | |
paul@418 | 338 | The attendee identities are normalized. |
paul@418 | 339 | """ |
paul@418 | 340 | |
paul@418 | 341 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@418 | 342 | |
paul@418 | 343 | # Only provide details for attendees who sent/receive the message. |
paul@418 | 344 | |
paul@418 | 345 | attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders |
paul@418 | 346 | |
paul@418 | 347 | attendees = {} |
paul@418 | 348 | for attendee in attendee_filter_fn(attendee_map): |
paul@418 | 349 | attendees[attendee] = attendee_map[attendee] |
paul@418 | 350 | |
paul@418 | 351 | return attendees |
paul@418 | 352 | |
paul@418 | 353 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@418 | 354 | |
paul@418 | 355 | """ |
paul@418 | 356 | Return the organiser and attendees for the current object, filtered for |
paul@418 | 357 | the recipient of interest. Return None if no identities are eligible. |
paul@418 | 358 | |
paul@418 | 359 | Organiser and attendee identities are normalized. |
paul@418 | 360 | """ |
paul@418 | 361 | |
paul@418 | 362 | organiser_item = self.require_organiser(from_organiser) |
paul@418 | 363 | attendees = self.require_attendees(from_organiser) |
paul@418 | 364 | |
paul@418 | 365 | if not attendees or not organiser_item: |
paul@418 | 366 | return None |
paul@418 | 367 | |
paul@418 | 368 | return organiser_item, attendees |
paul@418 | 369 | |
paul@418 | 370 | def get_sender_identities(self, mapping): |
paul@418 | 371 | |
paul@418 | 372 | """ |
paul@418 | 373 | Return a mapping from actual senders to the identities for which they |
paul@418 | 374 | have provided data, extracting this information from the given |
paul@418 | 375 | 'mapping'. |
paul@418 | 376 | """ |
paul@418 | 377 | |
paul@418 | 378 | senders = {} |
paul@418 | 379 | |
paul@418 | 380 | for value, attr in mapping.items(): |
paul@418 | 381 | sent_by = attr.get("SENT-BY") |
paul@418 | 382 | if sent_by: |
paul@418 | 383 | senders[get_uri(sent_by)] = value |
paul@418 | 384 | else: |
paul@418 | 385 | senders[value] = value |
paul@418 | 386 | |
paul@418 | 387 | return senders |
paul@418 | 388 | |
paul@468 | 389 | def _get_object(self, uid, recurrenceid): |
paul@418 | 390 | |
paul@418 | 391 | """ |
paul@468 | 392 | Return the stored object for the current user, with the given 'uid' and |
paul@468 | 393 | 'recurrenceid'. |
paul@418 | 394 | """ |
paul@418 | 395 | |
paul@468 | 396 | fragment = self.store.get_event(self.user, uid, recurrenceid) |
paul@418 | 397 | return fragment and Object(fragment) |
paul@418 | 398 | |
paul@468 | 399 | def get_object(self): |
paul@418 | 400 | |
paul@418 | 401 | """ |
paul@418 | 402 | Return the stored object to which the current object refers for the |
paul@468 | 403 | current user. |
paul@418 | 404 | """ |
paul@418 | 405 | |
paul@468 | 406 | return self._get_object(self.uid, self.recurrenceid) |
paul@418 | 407 | |
paul@468 | 408 | def get_parent_object(self): |
paul@418 | 409 | |
paul@418 | 410 | """ |
paul@418 | 411 | Return the parent object to which the current object refers for the |
paul@468 | 412 | current user. |
paul@418 | 413 | """ |
paul@418 | 414 | |
paul@468 | 415 | return self.recurrenceid and self._get_object(self.uid, None) or None |
paul@418 | 416 | |
paul@468 | 417 | def have_new_object(self, obj=None): |
paul@418 | 418 | |
paul@418 | 419 | """ |
paul@468 | 420 | Return whether the current object is new to the current user (or if the |
paul@418 | 421 | given 'obj' is new). |
paul@418 | 422 | """ |
paul@418 | 423 | |
paul@468 | 424 | obj = obj or self.get_object() |
paul@418 | 425 | |
paul@418 | 426 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@418 | 427 | |
paul@418 | 428 | if obj: |
paul@418 | 429 | sequence = obj.get_value("SEQUENCE") |
paul@418 | 430 | dtstamp = obj.get_value("DTSTAMP") |
paul@418 | 431 | |
paul@418 | 432 | # If the request refers to an older version of the object, ignore |
paul@418 | 433 | # it. |
paul@418 | 434 | |
paul@418 | 435 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, |
paul@418 | 436 | self.is_partstat_updated(obj)) |
paul@418 | 437 | |
paul@418 | 438 | return True |
paul@418 | 439 | |
paul@418 | 440 | def is_partstat_updated(self, obj): |
paul@418 | 441 | |
paul@418 | 442 | """ |
paul@418 | 443 | Return whether the participant status has been updated in the current |
paul@418 | 444 | object in comparison to the given 'obj'. |
paul@418 | 445 | |
paul@418 | 446 | NOTE: Some clients like Claws Mail erase time information from DTSTAMP |
paul@418 | 447 | NOTE: and make it invalid. Thus, such attendance information may also be |
paul@418 | 448 | NOTE: incorporated into any new object assessment. |
paul@418 | 449 | """ |
paul@418 | 450 | |
paul@418 | 451 | old_attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@418 | 452 | new_attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@418 | 453 | |
paul@418 | 454 | for attendee, attr in old_attendees.items(): |
paul@418 | 455 | old_partstat = attr.get("PARTSTAT") |
paul@418 | 456 | new_attr = new_attendees.get(attendee) |
paul@418 | 457 | new_partstat = new_attr and new_attr.get("PARTSTAT") |
paul@418 | 458 | |
paul@418 | 459 | if old_partstat == "NEEDS-ACTION" and new_partstat and \ |
paul@418 | 460 | new_partstat != old_partstat: |
paul@418 | 461 | |
paul@418 | 462 | return True |
paul@418 | 463 | |
paul@418 | 464 | return False |
paul@418 | 465 | |
paul@468 | 466 | def merge_attendance(self, attendees): |
paul@418 | 467 | |
paul@418 | 468 | """ |
paul@418 | 469 | Merge attendance from the current object's 'attendees' into the version |
paul@468 | 470 | stored for the current user. |
paul@418 | 471 | """ |
paul@418 | 472 | |
paul@468 | 473 | obj = self.get_object() |
paul@418 | 474 | |
paul@468 | 475 | if not obj or not self.have_new_object(obj): |
paul@418 | 476 | return False |
paul@418 | 477 | |
paul@418 | 478 | # Get attendee details in a usable form. |
paul@418 | 479 | |
paul@418 | 480 | attendee_map = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@418 | 481 | |
paul@418 | 482 | for attendee, attendee_attr in attendees.items(): |
paul@418 | 483 | |
paul@418 | 484 | # Update attendance in the loaded object. |
paul@418 | 485 | |
paul@418 | 486 | attendee_map[attendee] = attendee_attr |
paul@418 | 487 | |
paul@418 | 488 | # Set the new details and store the object. |
paul@418 | 489 | |
paul@418 | 490 | obj["ATTENDEE"] = attendee_map.items() |
paul@418 | 491 | |
paul@418 | 492 | # Set the complete event if not an additional occurrence. |
paul@418 | 493 | |
paul@418 | 494 | event = obj.to_node() |
paul@468 | 495 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@418 | 496 | |
paul@418 | 497 | return True |
paul@418 | 498 | |
paul@418 | 499 | def update_dtstamp(self): |
paul@418 | 500 | |
paul@418 | 501 | "Update the DTSTAMP in the current object." |
paul@418 | 502 | |
paul@418 | 503 | dtstamp = self.obj.get_utc_datetime("DTSTAMP") |
paul@418 | 504 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@418 | 505 | self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@418 | 506 | |
paul@418 | 507 | def set_sequence(self, increment=False): |
paul@418 | 508 | |
paul@418 | 509 | "Update the SEQUENCE in the current object." |
paul@418 | 510 | |
paul@418 | 511 | sequence = self.obj.get_value("SEQUENCE") or "0" |
paul@418 | 512 | self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@418 | 513 | |
paul@418 | 514 | # vim: tabstop=4 expandtab shiftwidth=4 |