paul@1346 | 1 | #!/usr/bin/env python |
paul@1346 | 2 | |
paul@1346 | 3 | """ |
paul@1346 | 4 | User interface data abstractions. |
paul@1346 | 5 | |
paul@1435 | 6 | Copyright (C) 2014, 2015, 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@1346 | 7 | |
paul@1346 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1346 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1346 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1346 | 11 | version. |
paul@1346 | 12 | |
paul@1346 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1346 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1346 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1346 | 16 | details. |
paul@1346 | 17 | |
paul@1346 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1346 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1346 | 20 | """ |
paul@1346 | 21 | |
paul@1346 | 22 | from collections import OrderedDict |
paul@1346 | 23 | from copy import copy |
paul@1346 | 24 | from datetime import datetime, timedelta |
paul@1346 | 25 | from imiptools.client import ClientForObject |
paul@1352 | 26 | from imiptools.data import get_main_period, uri_items |
paul@1346 | 27 | from imiptools.dates import end_date_from_calendar, end_date_to_calendar, \ |
paul@1346 | 28 | format_datetime, get_datetime, \ |
paul@1346 | 29 | get_datetime_attributes, get_end_of_day, \ |
paul@1346 | 30 | to_date, to_utc_datetime, to_timezone |
paul@1346 | 31 | from imiptools.period import get_overlapping_members, RecurringPeriod |
paul@1346 | 32 | from itertools import chain |
paul@1435 | 33 | import vRecurrence |
paul@1346 | 34 | |
paul@1346 | 35 | # General editing abstractions. |
paul@1346 | 36 | |
paul@1418 | 37 | class CopiableList(list): |
paul@1418 | 38 | |
paul@1418 | 39 | "A list whose elements promise to implement a copy method." |
paul@1418 | 40 | |
paul@1418 | 41 | def copy(self): |
paul@1418 | 42 | copied = [] |
paul@1418 | 43 | for i in self: |
paul@1418 | 44 | copied.append(i.copy()) |
paul@1418 | 45 | return copied |
paul@1418 | 46 | |
paul@1346 | 47 | class State: |
paul@1346 | 48 | |
paul@1346 | 49 | "Manage editing state." |
paul@1346 | 50 | |
paul@1346 | 51 | def __init__(self, callables): |
paul@1346 | 52 | |
paul@1346 | 53 | """ |
paul@1346 | 54 | Define state variable initialisation using the given 'callables', which |
paul@1346 | 55 | is a mapping that defines a callable for each variable name that is |
paul@1346 | 56 | invoked when the variable is first requested. |
paul@1346 | 57 | """ |
paul@1346 | 58 | |
paul@1346 | 59 | self.state = {} |
paul@1346 | 60 | self.original = {} |
paul@1346 | 61 | self.callables = callables |
paul@1346 | 62 | |
paul@1346 | 63 | def get_callable(self, key): |
paul@1346 | 64 | return self.callables.get(key, lambda: None) |
paul@1346 | 65 | |
paul@1346 | 66 | def ensure_original(self, key): |
paul@1346 | 67 | |
paul@1346 | 68 | "Ensure the original state for the given 'key'." |
paul@1346 | 69 | |
paul@1346 | 70 | if not self.original.has_key(key): |
paul@1346 | 71 | self.original[key] = self.get_callable(key)() |
paul@1346 | 72 | |
paul@1346 | 73 | def get_original(self, key): |
paul@1346 | 74 | |
paul@1346 | 75 | "Return the original state for the given 'key'." |
paul@1346 | 76 | |
paul@1346 | 77 | self.ensure_original(key) |
paul@1418 | 78 | data = self.original[key] |
paul@1418 | 79 | if isinstance(data, CopiableList): |
paul@1418 | 80 | copied = data.copy() |
paul@1418 | 81 | else: |
paul@1418 | 82 | copied = copy(self.original[key]) |
paul@1418 | 83 | return copied |
paul@1346 | 84 | |
paul@1346 | 85 | def get(self, key, reset=False): |
paul@1346 | 86 | |
paul@1346 | 87 | """ |
paul@1346 | 88 | Return state for the given 'key', using the configured callable to |
paul@1346 | 89 | compute and set the state if no state is already defined. |
paul@1346 | 90 | |
paul@1346 | 91 | If 'reset' is set to a true value, compute and return the state using |
paul@1346 | 92 | the configured callable regardless of any existing state. |
paul@1346 | 93 | """ |
paul@1346 | 94 | |
paul@1346 | 95 | if reset or not self.state.has_key(key): |
paul@1346 | 96 | self.state[key] = self.get_original(key) |
paul@1346 | 97 | |
paul@1346 | 98 | return self.state[key] |
paul@1346 | 99 | |
paul@1346 | 100 | def set(self, key, value): |
paul@1346 | 101 | |
paul@1346 | 102 | "Set the state of 'key' to 'value'." |
paul@1346 | 103 | |
paul@1346 | 104 | self.ensure_original(key) |
paul@1346 | 105 | self.state[key] = value |
paul@1346 | 106 | |
paul@1418 | 107 | def unset(self, key): |
paul@1418 | 108 | |
paul@1418 | 109 | "Unset 'key' in the state." |
paul@1418 | 110 | |
paul@1418 | 111 | del self.state[key] |
paul@1418 | 112 | del self.original[key] |
paul@1418 | 113 | |
paul@1346 | 114 | def has_changed(self, key): |
paul@1346 | 115 | |
paul@1346 | 116 | "Return whether 'key' has changed during editing." |
paul@1346 | 117 | |
paul@1346 | 118 | return self.get_original(key) != self.get(key) |
paul@1346 | 119 | |
paul@1346 | 120 | # Dictionary emulation methods. |
paul@1346 | 121 | |
paul@1418 | 122 | def __delitem__(self, key): |
paul@1418 | 123 | self.unset(key) |
paul@1418 | 124 | |
paul@1346 | 125 | def __getitem__(self, key): |
paul@1346 | 126 | return self.get(key) |
paul@1346 | 127 | |
paul@1346 | 128 | def __setitem__(self, key, value): |
paul@1346 | 129 | self.set(key, value) |
paul@1346 | 130 | |
paul@1346 | 131 | |
paul@1346 | 132 | |
paul@1346 | 133 | # Object editing abstractions. |
paul@1346 | 134 | |
paul@1346 | 135 | class EditingClient(ClientForObject): |
paul@1346 | 136 | |
paul@1346 | 137 | "A simple calendar client." |
paul@1346 | 138 | |
paul@1348 | 139 | def __init__(self, user, messenger, store=None, journal=None, |
paul@1348 | 140 | preferences_dir=None): |
paul@1348 | 141 | |
paul@1346 | 142 | ClientForObject.__init__(self, None, user, messenger, store, |
paul@1346 | 143 | journal=journal, |
paul@1346 | 144 | preferences_dir=preferences_dir) |
paul@1346 | 145 | self.reset() |
paul@1346 | 146 | |
paul@1346 | 147 | # Editing state. |
paul@1346 | 148 | |
paul@1346 | 149 | def reset(self): |
paul@1346 | 150 | |
paul@1346 | 151 | "Reset the editing state." |
paul@1346 | 152 | |
paul@1346 | 153 | self.state = State({ |
paul@1352 | 154 | "attendees" : lambda: OrderedDict(self.obj.get_items("ATTENDEE") or []), |
paul@1352 | 155 | "organiser" : lambda: self.obj.get_value("ORGANIZER"), |
paul@1418 | 156 | "periods" : lambda: CopiableList(form_periods_from_periods(self.get_unedited_periods())), |
paul@1435 | 157 | "rule" : lambda: CopiableList(vRecurrence.get_selectors_for_rule(self.obj.get_value("RRULE"))), |
paul@1346 | 158 | "suggested_attendees" : self.get_suggested_attendees, |
paul@1352 | 159 | "suggested_periods" : self.get_suggested_periods, |
paul@1352 | 160 | "summary" : lambda: self.obj.get_value("SUMMARY"), |
paul@1346 | 161 | }) |
paul@1346 | 162 | |
paul@1346 | 163 | # Access to stored and current information. |
paul@1346 | 164 | |
paul@1346 | 165 | def get_unedited_periods(self): |
paul@1346 | 166 | |
paul@1346 | 167 | """ |
paul@1346 | 168 | Return the original, unedited periods including revisions from separate |
paul@1346 | 169 | recurrence instances. |
paul@1346 | 170 | """ |
paul@1346 | 171 | |
paul@1346 | 172 | return event_periods_from_updated_periods(self.get_updated_periods()) |
paul@1346 | 173 | |
paul@1346 | 174 | def get_counters(self): |
paul@1346 | 175 | |
paul@1346 | 176 | "Return a counter-proposal mapping from attendees to objects." |
paul@1346 | 177 | |
paul@1346 | 178 | d = {} |
paul@1346 | 179 | |
paul@1346 | 180 | # Get counter-proposals for the specific object. |
paul@1346 | 181 | |
paul@1346 | 182 | recurrenceids = [self.recurrenceid] |
paul@1346 | 183 | |
paul@1346 | 184 | # And for all recurrences associated with a parent object. |
paul@1346 | 185 | |
paul@1346 | 186 | if not self.recurrenceid: |
paul@1346 | 187 | recurrenceids += self.store.get_counter_recurrences(self.user, self.uid) |
paul@1346 | 188 | |
paul@1346 | 189 | # Map attendees to objects. |
paul@1346 | 190 | |
paul@1346 | 191 | for recurrenceid in recurrenceids: |
paul@1346 | 192 | attendees = self.store.get_counters(self.user, self.uid, recurrenceid) |
paul@1346 | 193 | for attendee in attendees: |
paul@1346 | 194 | if not d.has_key(attendee): |
paul@1346 | 195 | d[attendee] = [] |
paul@1346 | 196 | d[attendee].append(self.get_stored_object(self.uid, recurrenceid, "counters", attendee)) |
paul@1346 | 197 | |
paul@1346 | 198 | return d |
paul@1346 | 199 | |
paul@1346 | 200 | def get_suggested_attendees(self): |
paul@1346 | 201 | |
paul@1346 | 202 | "For all counter-proposals, return suggested attendee items." |
paul@1346 | 203 | |
paul@1346 | 204 | existing = self.state.get("attendees") |
paul@1346 | 205 | l = [] |
paul@1352 | 206 | |
paul@1346 | 207 | for attendee, objects in self.get_counters().items(): |
paul@1346 | 208 | for obj in objects: |
paul@1346 | 209 | for suggested, attr in obj.get_items("ATTENDEE"): |
paul@1346 | 210 | if suggested not in existing: |
paul@1346 | 211 | l.append((attendee, (suggested, attr))) |
paul@1352 | 212 | |
paul@1352 | 213 | # Provide a stable ordering. |
paul@1352 | 214 | |
paul@1352 | 215 | l.sort() |
paul@1346 | 216 | return l |
paul@1346 | 217 | |
paul@1346 | 218 | def get_suggested_periods(self): |
paul@1346 | 219 | |
paul@1346 | 220 | "For all counter-proposals, return suggested event periods." |
paul@1346 | 221 | |
paul@1346 | 222 | existing = self.state.get("periods") |
paul@1346 | 223 | |
paul@1346 | 224 | # Get active periods for filtering of suggested periods. |
paul@1346 | 225 | |
paul@1346 | 226 | active = [] |
paul@1346 | 227 | for p in existing: |
paul@1346 | 228 | if not p.cancelled: |
paul@1346 | 229 | active.append(p) |
paul@1346 | 230 | |
paul@1346 | 231 | suggested = [] |
paul@1346 | 232 | |
paul@1346 | 233 | for attendee, objects in self.get_counters().items(): |
paul@1346 | 234 | |
paul@1346 | 235 | # For each object, obtain suggested periods. |
paul@1346 | 236 | |
paul@1346 | 237 | for obj in objects: |
paul@1346 | 238 | |
paul@1346 | 239 | # Obtain the current periods for the object providing the |
paul@1346 | 240 | # suggested periods. |
paul@1346 | 241 | |
paul@1346 | 242 | updated = self.get_updated_periods(obj) |
paul@1346 | 243 | suggestions = event_periods_from_updated_periods(updated) |
paul@1346 | 244 | |
paul@1346 | 245 | # Compare current periods with suggested periods. |
paul@1346 | 246 | |
paul@1346 | 247 | new = set(suggestions).difference(active) |
paul@1346 | 248 | |
paul@1346 | 249 | # Treat each specific recurrence as affecting only the original |
paul@1346 | 250 | # period. |
paul@1346 | 251 | |
paul@1346 | 252 | if obj.get_recurrenceid(): |
paul@1346 | 253 | removed = [] |
paul@1346 | 254 | else: |
paul@1346 | 255 | removed = set(active).difference(suggestions) |
paul@1346 | 256 | |
paul@1346 | 257 | # Associate new and removed periods with the attendee. |
paul@1346 | 258 | |
paul@1346 | 259 | for period in new: |
paul@1346 | 260 | suggested.append((attendee, period, "add")) |
paul@1346 | 261 | |
paul@1346 | 262 | for period in removed: |
paul@1346 | 263 | suggested.append((attendee, period, "remove")) |
paul@1346 | 264 | |
paul@1352 | 265 | # Provide a stable ordering. |
paul@1352 | 266 | |
paul@1352 | 267 | suggested.sort() |
paul@1346 | 268 | return suggested |
paul@1346 | 269 | |
paul@1352 | 270 | def get_conflicting_periods(self): |
paul@1397 | 271 | |
paul@1397 | 272 | "Return a set of periods that conflict with others." |
paul@1397 | 273 | |
paul@1352 | 274 | periods = self.state.get("periods") |
paul@1352 | 275 | attendees = self.state.get("attendees") |
paul@1352 | 276 | conflicts = set() |
paul@1352 | 277 | |
paul@1352 | 278 | for attendee, attr in uri_items(attendees.items()): |
paul@1352 | 279 | if not attendee: |
paul@1352 | 280 | continue |
paul@1352 | 281 | |
paul@1352 | 282 | # If not attending this event, other periods cannot conflict. |
paul@1352 | 283 | |
paul@1352 | 284 | if not attr.get("PARTSTAT") in ("ACCEPTED", "TENTATIVE"): |
paul@1352 | 285 | continue |
paul@1352 | 286 | |
paul@1352 | 287 | # Obtain free/busy details for the attendee. |
paul@1352 | 288 | |
paul@1352 | 289 | if attendee == self.user: |
paul@1352 | 290 | freebusy = self.store.get_freebusy(attendee) |
paul@1352 | 291 | elif attendee: |
paul@1352 | 292 | freebusy = self.store.get_freebusy_for_other(self.user, attendee) |
paul@1352 | 293 | else: |
paul@1352 | 294 | continue |
paul@1352 | 295 | |
paul@1352 | 296 | # Without free/busy information, no conflicts can be determined for |
paul@1352 | 297 | # the user. |
paul@1352 | 298 | |
paul@1352 | 299 | if not freebusy: |
paul@1352 | 300 | continue |
paul@1352 | 301 | |
paul@1352 | 302 | # Compare the origin of each conflicting period. |
paul@1352 | 303 | |
paul@1352 | 304 | for p in freebusy.have_conflict(periods, True): |
paul@1352 | 305 | |
paul@1352 | 306 | # Ignore transparent periods. |
paul@1352 | 307 | |
paul@1352 | 308 | if p.transp == "ORG": |
paul@1352 | 309 | continue |
paul@1352 | 310 | |
paul@1352 | 311 | # Prevent conflicts with this event's own periods. |
paul@1352 | 312 | |
paul@1352 | 313 | if p.uid == self.uid and (not self.recurrenceid or |
paul@1352 | 314 | not p.recurrenceid or |
paul@1352 | 315 | self.recurrenceid == p.recurrenceid): |
paul@1352 | 316 | continue |
paul@1352 | 317 | |
paul@1352 | 318 | conflicts.add(p) |
paul@1352 | 319 | |
paul@1352 | 320 | return conflicts |
paul@1352 | 321 | |
paul@1346 | 322 | # Validation methods. |
paul@1346 | 323 | |
paul@1346 | 324 | def get_checked_periods(self): |
paul@1346 | 325 | |
paul@1346 | 326 | """ |
paul@1346 | 327 | Check the edited periods and return objects representing them, setting |
paul@1346 | 328 | the "periods" state. If errors occur, raise an exception and set the |
paul@1346 | 329 | "errors" state. |
paul@1346 | 330 | """ |
paul@1346 | 331 | |
paul@1346 | 332 | self.state["period_errors"] = errors = {} |
paul@1346 | 333 | |
paul@1346 | 334 | # Basic validation. |
paul@1346 | 335 | |
paul@1346 | 336 | try: |
paul@1346 | 337 | periods = event_periods_from_periods(self.state.get("periods")) |
paul@1346 | 338 | |
paul@1346 | 339 | except PeriodError, exc: |
paul@1346 | 340 | |
paul@1346 | 341 | # Obtain error and period index details from the exception, |
paul@1346 | 342 | # collecting errors for each index position. |
paul@1346 | 343 | |
paul@1346 | 344 | for err, index in exc.args: |
paul@1346 | 345 | l = errors.get(index) |
paul@1346 | 346 | if not l: |
paul@1346 | 347 | l = errors[index] = [] |
paul@1346 | 348 | l.append(err) |
paul@1346 | 349 | raise |
paul@1346 | 350 | |
paul@1346 | 351 | # Check for overlapping periods. |
paul@1346 | 352 | |
paul@1346 | 353 | overlapping = get_overlapping_members(periods) |
paul@1346 | 354 | |
paul@1346 | 355 | for period in overlapping: |
paul@1346 | 356 | for index, p in enumerate(periods): |
paul@1346 | 357 | if period is p: |
paul@1346 | 358 | errors[index] = ["overlap"] |
paul@1346 | 359 | |
paul@1346 | 360 | if overlapping: |
paul@1346 | 361 | raise PeriodError |
paul@1346 | 362 | |
paul@1346 | 363 | self.state["periods"] = form_periods_from_periods(periods) |
paul@1346 | 364 | return periods |
paul@1346 | 365 | |
paul@1346 | 366 | # Update result computation. |
paul@1346 | 367 | |
paul@1346 | 368 | def classify_attendee_changes(self): |
paul@1346 | 369 | |
paul@1346 | 370 | "Classify the attendees in the event." |
paul@1346 | 371 | |
paul@1346 | 372 | original = self.state.get_original("attendees") |
paul@1346 | 373 | current = self.state.get("attendees") |
paul@1346 | 374 | return classify_attendee_changes(original, current) |
paul@1346 | 375 | |
paul@1346 | 376 | def classify_attendee_operations(self): |
paul@1346 | 377 | |
paul@1346 | 378 | "Classify attendee update operations." |
paul@1346 | 379 | |
paul@1346 | 380 | new, modified, unmodified, removed = self.classify_attendee_changes() |
paul@1346 | 381 | |
paul@1346 | 382 | if self.is_organiser(): |
paul@1346 | 383 | to_invite = new |
paul@1346 | 384 | to_cancel = removed |
paul@1346 | 385 | to_modify = modified |
paul@1346 | 386 | else: |
paul@1346 | 387 | to_invite = new |
paul@1346 | 388 | to_cancel = {} |
paul@1346 | 389 | to_modify = modified |
paul@1346 | 390 | |
paul@1346 | 391 | return to_invite, to_cancel, to_modify |
paul@1346 | 392 | |
paul@1346 | 393 | def classify_period_changes(self): |
paul@1346 | 394 | |
paul@1346 | 395 | "Classify changes in the updated periods for the edited event." |
paul@1346 | 396 | |
paul@1421 | 397 | return classify_period_changes(self.combine_periods()) |
paul@1346 | 398 | |
paul@1346 | 399 | def classify_periods(self): |
paul@1346 | 400 | |
paul@1346 | 401 | "Classify the updated periods for the edited event." |
paul@1346 | 402 | |
paul@1421 | 403 | return classify_periods(self.combine_periods()) |
paul@1346 | 404 | |
paul@1346 | 405 | def combine_periods(self): |
paul@1346 | 406 | |
paul@1346 | 407 | "Combine unedited and checked edited periods to make updated periods." |
paul@1346 | 408 | |
paul@1418 | 409 | original = self.state.get_original("periods") |
paul@1346 | 410 | current = self.get_checked_periods() |
paul@1346 | 411 | return combine_periods(original, current) |
paul@1346 | 412 | |
paul@1346 | 413 | def classify_period_operations(self, is_changed=False): |
paul@1346 | 414 | |
paul@1346 | 415 | "Classify period update operations." |
paul@1346 | 416 | |
paul@1346 | 417 | new, replaced, retained, cancelled, obsolete = self.classify_periods() |
paul@1346 | 418 | |
paul@1346 | 419 | modified, unmodified, removed = self.classify_period_changes() |
paul@1346 | 420 | |
paul@1346 | 421 | is_organiser = self.is_organiser() |
paul@1346 | 422 | is_shared = self.obj.is_shared() |
paul@1346 | 423 | |
paul@1346 | 424 | return classify_period_operations(new, replaced, retained, cancelled, |
paul@1346 | 425 | obsolete, modified, removed, |
paul@1346 | 426 | is_organiser, is_shared, is_changed) |
paul@1346 | 427 | |
paul@1346 | 428 | def properties_changed(self): |
paul@1346 | 429 | |
paul@1346 | 430 | "Test for changes in event details." |
paul@1346 | 431 | |
paul@1346 | 432 | is_changed = [] |
paul@1346 | 433 | |
paul@1435 | 434 | for name in ["rule", "summary"]: |
paul@1346 | 435 | if self.state.has_changed(name): |
paul@1346 | 436 | is_changed.append(name) |
paul@1346 | 437 | |
paul@1346 | 438 | return is_changed |
paul@1346 | 439 | |
paul@1346 | 440 | def finish(self): |
paul@1346 | 441 | |
paul@1346 | 442 | "Finish editing, writing edited details to the object." |
paul@1346 | 443 | |
paul@1346 | 444 | if self.state.get("finished"): |
paul@1346 | 445 | return |
paul@1346 | 446 | |
paul@1346 | 447 | is_changed = self.properties_changed() |
paul@1346 | 448 | |
paul@1346 | 449 | # Determine attendee modifications. |
paul@1346 | 450 | |
paul@1346 | 451 | self.state["attendee_operations"] = \ |
paul@1346 | 452 | to_invite, to_cancel, to_modify = \ |
paul@1346 | 453 | self.classify_attendee_operations() |
paul@1346 | 454 | |
paul@1346 | 455 | self.state["attendees_to_cancel"] = to_cancel |
paul@1346 | 456 | |
paul@1346 | 457 | # Determine period modification operations. |
paul@1346 | 458 | # Use property changes and attendee suggestions to affect the result for |
paul@1346 | 459 | # attendee responses. |
paul@1346 | 460 | |
paul@1346 | 461 | is_changed = is_changed or to_invite |
paul@1346 | 462 | |
paul@1346 | 463 | self.state["period_operations"] = \ |
paul@1346 | 464 | to_unschedule, to_reschedule, to_add, to_exclude, to_set, \ |
paul@1346 | 465 | all_unscheduled, all_rescheduled = \ |
paul@1346 | 466 | self.classify_period_operations(is_changed) |
paul@1346 | 467 | |
paul@1346 | 468 | # Determine whole event update status. |
paul@1346 | 469 | |
paul@1346 | 470 | is_changed = is_changed or to_set |
paul@1346 | 471 | |
paul@1346 | 472 | # Update event details. |
paul@1346 | 473 | |
paul@1346 | 474 | if self.can_edit_properties(): |
paul@1346 | 475 | self.obj.set_value("SUMMARY", self.state.get("summary")) |
paul@1346 | 476 | |
paul@1346 | 477 | self.update_attendees(to_invite, to_cancel, to_modify) |
paul@1346 | 478 | self.update_event_from_periods(to_set, to_exclude) |
paul@1346 | 479 | |
paul@1435 | 480 | if self.state.has_changed("rule"): |
paul@1435 | 481 | rule = vRecurrence.to_property(self.state.get("rule")) |
paul@1435 | 482 | self.obj.set_rule((rule, {})) |
paul@1435 | 483 | |
paul@1346 | 484 | # Classify the nature of any update. |
paul@1346 | 485 | |
paul@1346 | 486 | if is_changed: |
paul@1346 | 487 | self.state["changed"] = "complete" |
paul@1346 | 488 | elif to_reschedule or to_unschedule or to_add: |
paul@1346 | 489 | self.state["changed"] = "incremental" |
paul@1346 | 490 | |
paul@1346 | 491 | self.state["finished"] = self.update_event_version(is_changed) |
paul@1346 | 492 | |
paul@1346 | 493 | # Update preparation. |
paul@1346 | 494 | |
paul@1346 | 495 | def have_update(self): |
paul@1346 | 496 | |
paul@1346 | 497 | "Return whether an update can be prepared and sent." |
paul@1346 | 498 | |
paul@1346 | 499 | return not self.is_organiser() or \ |
paul@1346 | 500 | not self.obj.is_shared() or \ |
paul@1346 | 501 | self.obj.is_shared() and self.state.get("changed") and \ |
paul@1346 | 502 | self.have_other_attendees() |
paul@1346 | 503 | |
paul@1346 | 504 | def have_other_attendees(self): |
paul@1346 | 505 | |
paul@1346 | 506 | "Return whether any attendees other than the user are present." |
paul@1346 | 507 | |
paul@1346 | 508 | attendees = self.state.get("attendees") |
paul@1346 | 509 | return attendees and (not attendees.has_key(self.user) or len(attendees.keys()) > 1) |
paul@1346 | 510 | |
paul@1346 | 511 | def prepare_cancel_message(self): |
paul@1346 | 512 | |
paul@1346 | 513 | "Prepare the cancel message for uninvited attendees." |
paul@1346 | 514 | |
paul@1346 | 515 | to_cancel = self.state.get("attendees_to_cancel") |
paul@1346 | 516 | return self.make_cancel_message(to_cancel) |
paul@1346 | 517 | |
paul@1346 | 518 | def prepare_publish_message(self): |
paul@1346 | 519 | |
paul@1346 | 520 | "Prepare the publishing message for the updated event." |
paul@1346 | 521 | |
paul@1346 | 522 | to_unschedule, to_reschedule, to_add, to_exclude, to_set, \ |
paul@1346 | 523 | all_unscheduled, all_rescheduled = self.state.get("period_operations") |
paul@1346 | 524 | |
paul@1346 | 525 | return self.make_self_update_message(all_unscheduled, all_rescheduled, to_add) |
paul@1346 | 526 | |
paul@1346 | 527 | def prepare_update_message(self): |
paul@1346 | 528 | |
paul@1346 | 529 | "Prepare the update message for the updated event." |
paul@1346 | 530 | |
paul@1346 | 531 | if not self.have_update(): |
paul@1346 | 532 | return None |
paul@1346 | 533 | |
paul@1346 | 534 | # Obtain operation details. |
paul@1346 | 535 | |
paul@1346 | 536 | to_unschedule, to_reschedule, to_add, to_exclude, to_set, \ |
paul@1346 | 537 | all_unscheduled, all_rescheduled = self.state.get("period_operations") |
paul@1346 | 538 | |
paul@1346 | 539 | # Prepare the message. |
paul@1346 | 540 | |
paul@1346 | 541 | recipients = self.get_recipients() |
paul@1346 | 542 | update_parent = self.state["changed"] == "complete" |
paul@1346 | 543 | |
paul@1346 | 544 | if self.is_organiser(): |
paul@1346 | 545 | return self.make_update_message(recipients, update_parent, |
paul@1346 | 546 | to_unschedule, to_reschedule, |
paul@1346 | 547 | all_unscheduled, all_rescheduled, |
paul@1346 | 548 | to_add) |
paul@1346 | 549 | else: |
paul@1346 | 550 | return self.make_response_message(recipients, update_parent, |
paul@1346 | 551 | all_rescheduled, to_reschedule) |
paul@1346 | 552 | |
paul@1386 | 553 | def get_publish_objects(self): |
paul@1386 | 554 | |
paul@1386 | 555 | "Return details of unscheduled, rescheduled and added objects." |
paul@1386 | 556 | |
paul@1386 | 557 | to_unschedule, to_reschedule, to_add, to_exclude, to_set, \ |
paul@1386 | 558 | all_unscheduled, all_rescheduled = self.state.get("period_operations") |
paul@1386 | 559 | |
paul@1386 | 560 | return self.get_rescheduled_objects(all_unscheduled), \ |
paul@1386 | 561 | self.get_rescheduled_objects(all_rescheduled), \ |
paul@1386 | 562 | self.get_rescheduled_objects(to_add) |
paul@1386 | 563 | |
paul@1378 | 564 | # Access methods. |
paul@1378 | 565 | |
paul@1378 | 566 | def find_attendee(self, attendee): |
paul@1378 | 567 | |
paul@1378 | 568 | "Return the index of 'attendee' or None if not present." |
paul@1378 | 569 | |
paul@1378 | 570 | attendees = self.state.get("attendees") |
paul@1378 | 571 | try: |
paul@1378 | 572 | return attendees.keys().index(attendee) |
paul@1378 | 573 | except ValueError: |
paul@1378 | 574 | return None |
paul@1378 | 575 | |
paul@1346 | 576 | # Modification methods. |
paul@1346 | 577 | |
paul@1346 | 578 | def add_attendee(self, uri=None): |
paul@1346 | 579 | |
paul@1346 | 580 | "Add a blank attendee." |
paul@1346 | 581 | |
paul@1346 | 582 | attendees = self.state.get("attendees") |
paul@1346 | 583 | attendees[uri or ""] = {"PARTSTAT" : "NEEDS-ACTION"} |
paul@1346 | 584 | |
paul@1346 | 585 | def add_suggested_attendee(self, index): |
paul@1346 | 586 | |
paul@1346 | 587 | "Add the suggested attendee at 'index' to the event." |
paul@1346 | 588 | |
paul@1346 | 589 | attendees = self.state.get("attendees") |
paul@1346 | 590 | suggested_attendees = self.state.get("suggested_attendees") |
paul@1346 | 591 | try: |
paul@1346 | 592 | attendee, (suggested, attr) = suggested_attendees[index] |
paul@1346 | 593 | self.add_attendee(suggested) |
paul@1346 | 594 | except IndexError: |
paul@1346 | 595 | pass |
paul@1346 | 596 | |
paul@1346 | 597 | def add_period(self): |
paul@1346 | 598 | |
paul@1346 | 599 | "Add a copy of the main period as a new recurrence." |
paul@1346 | 600 | |
paul@1346 | 601 | current = self.state.get("periods") |
paul@1346 | 602 | new = get_main_period(current).copy() |
paul@1346 | 603 | new.origin = "RDATE" |
paul@1346 | 604 | new.replacement = False |
paul@1346 | 605 | new.recurrenceid = False |
paul@1346 | 606 | new.cancelled = False |
paul@1346 | 607 | current.append(new) |
paul@1346 | 608 | |
paul@1346 | 609 | def apply_suggested_period(self, index): |
paul@1346 | 610 | |
paul@1346 | 611 | "Apply the suggested period at 'index' to the event." |
paul@1346 | 612 | |
paul@1346 | 613 | current = self.state.get("periods") |
paul@1346 | 614 | suggested = self.state.get("suggested_periods") |
paul@1346 | 615 | |
paul@1346 | 616 | try: |
paul@1346 | 617 | attendee, period, operation = suggested[index] |
paul@1346 | 618 | period = form_period_from_period(period) |
paul@1346 | 619 | |
paul@1346 | 620 | # Cancel any removed periods. |
paul@1346 | 621 | |
paul@1346 | 622 | if operation == "remove": |
paul@1346 | 623 | for index, p in enumerate(current): |
paul@1346 | 624 | if p == period: |
paul@1346 | 625 | self.cancel_periods([index]) |
paul@1346 | 626 | break |
paul@1346 | 627 | |
paul@1346 | 628 | # Add or replace any other suggestions. |
paul@1346 | 629 | |
paul@1346 | 630 | elif operation == "add": |
paul@1346 | 631 | |
paul@1346 | 632 | # Make the status of the period compatible. |
paul@1346 | 633 | |
paul@1346 | 634 | period.cancelled = False |
paul@1422 | 635 | period.origin = "RDATE" |
paul@1346 | 636 | |
paul@1346 | 637 | # Either replace or add the period. |
paul@1346 | 638 | |
paul@1346 | 639 | recurrenceid = period.get_recurrenceid() |
paul@1346 | 640 | |
paul@1346 | 641 | for i, p in enumerate(current): |
paul@1346 | 642 | if p.get_recurrenceid() == recurrenceid: |
paul@1346 | 643 | current[i] = period |
paul@1346 | 644 | break |
paul@1346 | 645 | |
paul@1346 | 646 | # Add as a new period. |
paul@1346 | 647 | |
paul@1346 | 648 | else: |
paul@1346 | 649 | period.recurrenceid = None |
paul@1346 | 650 | current.append(period) |
paul@1346 | 651 | |
paul@1346 | 652 | except IndexError: |
paul@1346 | 653 | pass |
paul@1346 | 654 | |
paul@1346 | 655 | def cancel_periods(self, indexes, cancelled=True): |
paul@1346 | 656 | |
paul@1346 | 657 | """ |
paul@1346 | 658 | Set cancellation state for periods with the given 'indexes', indicating |
paul@1346 | 659 | 'cancelled' as a true or false value. New periods will be removed if |
paul@1346 | 660 | cancelled. |
paul@1346 | 661 | """ |
paul@1346 | 662 | |
paul@1346 | 663 | periods = self.state.get("periods") |
paul@1346 | 664 | to_remove = [] |
paul@1346 | 665 | removed = 0 |
paul@1346 | 666 | |
paul@1346 | 667 | for index in indexes: |
paul@1346 | 668 | p = periods[index] |
paul@1346 | 669 | |
paul@1346 | 670 | # Make replacements from existing periods and cancel them. |
paul@1346 | 671 | |
paul@1346 | 672 | if p.recurrenceid: |
paul@1346 | 673 | p.cancelled = cancelled |
paul@1346 | 674 | |
paul@1346 | 675 | # Remove new periods completely. |
paul@1346 | 676 | |
paul@1346 | 677 | elif cancelled: |
paul@1346 | 678 | to_remove.append(index - removed) |
paul@1346 | 679 | removed += 1 |
paul@1346 | 680 | |
paul@1346 | 681 | for index in to_remove: |
paul@1346 | 682 | del periods[index] |
paul@1346 | 683 | |
paul@1346 | 684 | def can_edit_attendance(self): |
paul@1346 | 685 | |
paul@1346 | 686 | "Return whether the organiser's attendance can be edited." |
paul@1346 | 687 | |
paul@1346 | 688 | return self.state.get("attendees").has_key(self.user) |
paul@1346 | 689 | |
paul@1346 | 690 | def edit_attendance(self, partstat): |
paul@1346 | 691 | |
paul@1346 | 692 | "Set the 'partstat' of the current user, if attending." |
paul@1346 | 693 | |
paul@1346 | 694 | attendees = self.state.get("attendees") |
paul@1346 | 695 | attr = attendees.get(self.user) |
paul@1346 | 696 | |
paul@1346 | 697 | # Set the attendance for the user, if attending. |
paul@1346 | 698 | |
paul@1346 | 699 | if attr is not None: |
paul@1346 | 700 | new_attr = {} |
paul@1346 | 701 | new_attr.update(attr) |
paul@1346 | 702 | new_attr["PARTSTAT"] = partstat |
paul@1346 | 703 | attendees[self.user] = new_attr |
paul@1346 | 704 | |
paul@1346 | 705 | def can_edit_attendee(self, index): |
paul@1346 | 706 | |
paul@1346 | 707 | """ |
paul@1346 | 708 | Return whether the attendee at 'index' can be edited, requiring either |
paul@1346 | 709 | the organiser and an unshared event, or a new attendee. |
paul@1346 | 710 | """ |
paul@1346 | 711 | |
paul@1346 | 712 | attendees = self.state.get("attendees") |
paul@1346 | 713 | attendee = attendees.keys()[index] |
paul@1346 | 714 | |
paul@1346 | 715 | try: |
paul@1346 | 716 | attr = attendees[attendee] |
paul@1352 | 717 | if self.is_organiser() or not attr: |
paul@1346 | 718 | return (attendee, attr) |
paul@1346 | 719 | except IndexError: |
paul@1346 | 720 | pass |
paul@1346 | 721 | |
paul@1346 | 722 | return None |
paul@1346 | 723 | |
paul@1346 | 724 | def can_remove_attendee(self, index): |
paul@1346 | 725 | |
paul@1346 | 726 | """ |
paul@1346 | 727 | Return whether the attendee at 'index' can be removed, requiring either |
paul@1346 | 728 | the organiser or a new attendee. |
paul@1346 | 729 | """ |
paul@1346 | 730 | |
paul@1346 | 731 | attendees = self.state.get("attendees") |
paul@1346 | 732 | attendee = attendees.keys()[index] |
paul@1346 | 733 | |
paul@1346 | 734 | try: |
paul@1346 | 735 | attr = attendees[attendee] |
paul@1346 | 736 | if self.is_organiser() or not attr: |
paul@1346 | 737 | return (attendee, attr) |
paul@1346 | 738 | except IndexError: |
paul@1346 | 739 | pass |
paul@1346 | 740 | |
paul@1346 | 741 | return None |
paul@1346 | 742 | |
paul@1346 | 743 | def remove_attendees(self, indexes): |
paul@1346 | 744 | |
paul@1435 | 745 | "Remove attendee at 'indexes'." |
paul@1346 | 746 | |
paul@1346 | 747 | attendees = self.state.get("attendees") |
paul@1346 | 748 | to_remove = [] |
paul@1346 | 749 | |
paul@1346 | 750 | for index in indexes: |
paul@1346 | 751 | attendee_item = self.can_remove_attendee(index) |
paul@1346 | 752 | if attendee_item: |
paul@1346 | 753 | attendee, attr = attendee_item |
paul@1346 | 754 | to_remove.append(attendee) |
paul@1346 | 755 | |
paul@1346 | 756 | for key in to_remove: |
paul@1346 | 757 | del attendees[key] |
paul@1346 | 758 | |
paul@1346 | 759 | def can_edit_period(self, index): |
paul@1346 | 760 | |
paul@1346 | 761 | """ |
paul@1346 | 762 | Return the period at 'index' for editing or None if it cannot be edited. |
paul@1346 | 763 | """ |
paul@1346 | 764 | |
paul@1346 | 765 | try: |
paul@1346 | 766 | return self.state.get("periods")[index] |
paul@1346 | 767 | except IndexError: |
paul@1346 | 768 | return None |
paul@1346 | 769 | |
paul@1346 | 770 | def can_edit_properties(self): |
paul@1346 | 771 | |
paul@1346 | 772 | "Return whether general event properties can be edited." |
paul@1346 | 773 | |
paul@1346 | 774 | return True |
paul@1346 | 775 | |
paul@1435 | 776 | def can_edit_rule_selector(self, index): |
paul@1346 | 777 | |
paul@1435 | 778 | "Return whether the recurrence rule selector at 'index' can be edited." |
paul@1435 | 779 | |
paul@1435 | 780 | try: |
paul@1435 | 781 | rule = self.state.get("rule") |
paul@1435 | 782 | return rule and rule[index] or None |
paul@1435 | 783 | except IndexError: |
paul@1435 | 784 | return None |
paul@1435 | 785 | |
paul@1435 | 786 | def remove_rule_selectors(self, indexes): |
paul@1435 | 787 | |
paul@1435 | 788 | "Remove rule selectors at 'indexes'." |
paul@1435 | 789 | |
paul@1435 | 790 | rule = self.state.get("rule") |
paul@1435 | 791 | to_remove = [] |
paul@1435 | 792 | removed = 0 |
paul@1435 | 793 | |
paul@1435 | 794 | for index in indexes: |
paul@1435 | 795 | if self.can_edit_rule_selector(index): |
paul@1435 | 796 | to_remove.append(index - removed) |
paul@1435 | 797 | removed += 1 |
paul@1435 | 798 | |
paul@1435 | 799 | for index in to_remove: |
paul@1435 | 800 | del rule[index] |
paul@1346 | 801 | |
paul@1346 | 802 | # Period-related abstractions. |
paul@1346 | 803 | |
paul@1346 | 804 | class PeriodError(Exception): |
paul@1346 | 805 | pass |
paul@1346 | 806 | |
paul@1346 | 807 | class EditablePeriod(RecurringPeriod): |
paul@1346 | 808 | |
paul@1346 | 809 | "An editable period tracking the identity of any original period." |
paul@1346 | 810 | |
paul@1346 | 811 | def _get_recurrenceid_item(self): |
paul@1346 | 812 | |
paul@1346 | 813 | # Convert any stored identifier to the current time zone. |
paul@1346 | 814 | # NOTE: This should not be necessary, but is done for consistency with |
paul@1346 | 815 | # NOTE: the datetime properties. |
paul@1346 | 816 | |
paul@1346 | 817 | dt = get_datetime(self.recurrenceid) |
paul@1346 | 818 | dt = to_timezone(dt, self.tzid) |
paul@1346 | 819 | return dt, get_datetime_attributes(dt) |
paul@1346 | 820 | |
paul@1346 | 821 | def get_recurrenceid(self): |
paul@1346 | 822 | |
paul@1346 | 823 | """ |
paul@1346 | 824 | Return a recurrence identity to be used to associate stored periods with |
paul@1346 | 825 | edited periods. |
paul@1346 | 826 | """ |
paul@1346 | 827 | |
paul@1346 | 828 | if not self.recurrenceid: |
paul@1346 | 829 | return RecurringPeriod.get_recurrenceid(self) |
paul@1346 | 830 | return self.recurrenceid |
paul@1346 | 831 | |
paul@1346 | 832 | def get_recurrenceid_item(self): |
paul@1346 | 833 | |
paul@1346 | 834 | """ |
paul@1346 | 835 | Return a recurrence identifier value and datetime properties for use in |
paul@1346 | 836 | specifying the RECURRENCE-ID property. |
paul@1346 | 837 | """ |
paul@1346 | 838 | |
paul@1346 | 839 | if not self.recurrenceid: |
paul@1346 | 840 | return RecurringPeriod.get_recurrenceid_item(self) |
paul@1346 | 841 | return self._get_recurrenceid_item() |
paul@1346 | 842 | |
paul@1346 | 843 | class EventPeriod(EditablePeriod): |
paul@1346 | 844 | |
paul@1346 | 845 | """ |
paul@1346 | 846 | A simple period plus attribute details, compatible with RecurringPeriod, and |
paul@1346 | 847 | intended to represent information obtained from an iCalendar resource. |
paul@1346 | 848 | """ |
paul@1346 | 849 | |
paul@1346 | 850 | def __init__(self, start, end, tzid=None, origin=None, start_attr=None, |
paul@1346 | 851 | end_attr=None, form_start=None, form_end=None, |
paul@1346 | 852 | replacement=False, cancelled=False, recurrenceid=None): |
paul@1346 | 853 | |
paul@1346 | 854 | """ |
paul@1346 | 855 | Initialise a period with the given 'start' and 'end' datetimes. |
paul@1346 | 856 | |
paul@1346 | 857 | The optional 'tzid' provides time zone information, and the optional |
paul@1346 | 858 | 'origin' indicates the kind of period this object describes. |
paul@1346 | 859 | |
paul@1346 | 860 | The optional 'start_attr' and 'end_attr' provide metadata for the start |
paul@1346 | 861 | and end datetimes respectively, and 'form_start' and 'form_end' are |
paul@1346 | 862 | values provided as textual input. |
paul@1346 | 863 | |
paul@1346 | 864 | The 'replacement' flag indicates whether the period is provided by a |
paul@1346 | 865 | separate recurrence instance. |
paul@1346 | 866 | |
paul@1346 | 867 | The 'cancelled' flag indicates whether a separate recurrence is |
paul@1346 | 868 | cancelled. |
paul@1346 | 869 | |
paul@1346 | 870 | The 'recurrenceid' describes the original identity of the period, |
paul@1346 | 871 | regardless of whether it is separate or not. |
paul@1346 | 872 | """ |
paul@1346 | 873 | |
paul@1346 | 874 | EditablePeriod.__init__(self, start, end, tzid, origin, start_attr, end_attr) |
paul@1346 | 875 | self.form_start = form_start |
paul@1346 | 876 | self.form_end = form_end |
paul@1346 | 877 | |
paul@1346 | 878 | # Information about whether a separate recurrence provides this period |
paul@1346 | 879 | # and the original period identity. |
paul@1346 | 880 | |
paul@1346 | 881 | self.replacement = replacement |
paul@1346 | 882 | self.cancelled = cancelled |
paul@1346 | 883 | self.recurrenceid = recurrenceid |
paul@1346 | 884 | |
paul@1346 | 885 | # Additional editing state. |
paul@1346 | 886 | |
paul@1346 | 887 | self.new_replacement = False |
paul@1346 | 888 | |
paul@1346 | 889 | def as_tuple(self): |
paul@1346 | 890 | return self.start, self.end, self.tzid, self.origin, self.start_attr, \ |
paul@1346 | 891 | self.end_attr, self.form_start, self.form_end, self.replacement, \ |
paul@1346 | 892 | self.cancelled, self.recurrenceid |
paul@1346 | 893 | |
paul@1346 | 894 | def __repr__(self): |
paul@1346 | 895 | return "EventPeriod%r" % (self.as_tuple(),) |
paul@1346 | 896 | |
paul@1346 | 897 | def copy(self): |
paul@1346 | 898 | return EventPeriod(*self.as_tuple()) |
paul@1346 | 899 | |
paul@1346 | 900 | def as_event_period(self, index=None): |
paul@1346 | 901 | return self |
paul@1346 | 902 | |
paul@1346 | 903 | def get_start_item(self): |
paul@1346 | 904 | return self.get_start(), self.get_start_attr() |
paul@1346 | 905 | |
paul@1346 | 906 | def get_end_item(self): |
paul@1346 | 907 | return self.get_end(), self.get_end_attr() |
paul@1346 | 908 | |
paul@1346 | 909 | # Form data compatibility methods. |
paul@1346 | 910 | |
paul@1346 | 911 | def get_form_start(self): |
paul@1346 | 912 | if not self.form_start: |
paul@1346 | 913 | self.form_start = self.get_form_date(self.get_start(), self.start_attr) |
paul@1346 | 914 | return self.form_start |
paul@1346 | 915 | |
paul@1346 | 916 | def get_form_end(self): |
paul@1346 | 917 | if not self.form_end: |
paul@1346 | 918 | self.form_end = self.get_form_date(end_date_from_calendar(self.get_end()), self.end_attr) |
paul@1346 | 919 | return self.form_end |
paul@1346 | 920 | |
paul@1346 | 921 | def as_form_period(self): |
paul@1346 | 922 | return FormPeriod( |
paul@1346 | 923 | self.get_form_start(), |
paul@1346 | 924 | self.get_form_end(), |
paul@1346 | 925 | isinstance(self.end, datetime) or self.get_start() != self.get_end() - timedelta(1), |
paul@1346 | 926 | isinstance(self.start, datetime) or isinstance(self.end, datetime), |
paul@1346 | 927 | self.tzid, |
paul@1346 | 928 | self.origin, |
paul@1346 | 929 | self.replacement, |
paul@1346 | 930 | self.cancelled, |
paul@1346 | 931 | self.recurrenceid |
paul@1346 | 932 | ) |
paul@1346 | 933 | |
paul@1346 | 934 | def get_form_date(self, dt, attr=None): |
paul@1346 | 935 | return FormDate( |
paul@1346 | 936 | format_datetime(to_date(dt)), |
paul@1346 | 937 | isinstance(dt, datetime) and str(dt.hour) or None, |
paul@1346 | 938 | isinstance(dt, datetime) and str(dt.minute) or None, |
paul@1346 | 939 | isinstance(dt, datetime) and str(dt.second) or None, |
paul@1346 | 940 | attr and attr.get("TZID") or None, |
paul@1346 | 941 | dt, attr |
paul@1346 | 942 | ) |
paul@1346 | 943 | |
paul@1346 | 944 | class FormPeriod(EditablePeriod): |
paul@1346 | 945 | |
paul@1346 | 946 | "A period whose information originates from a form." |
paul@1346 | 947 | |
paul@1346 | 948 | def __init__(self, start, end, end_enabled=True, times_enabled=True, |
paul@1346 | 949 | tzid=None, origin=None, replacement=False, cancelled=False, |
paul@1346 | 950 | recurrenceid=None): |
paul@1346 | 951 | self.start = start |
paul@1346 | 952 | self.end = end |
paul@1346 | 953 | self.end_enabled = end_enabled |
paul@1346 | 954 | self.times_enabled = times_enabled |
paul@1346 | 955 | self.tzid = tzid |
paul@1346 | 956 | self.origin = origin |
paul@1346 | 957 | self.replacement = replacement |
paul@1346 | 958 | self.cancelled = cancelled |
paul@1346 | 959 | self.recurrenceid = recurrenceid |
paul@1346 | 960 | self.new_replacement = False |
paul@1346 | 961 | |
paul@1346 | 962 | def as_tuple(self): |
paul@1346 | 963 | return self.start, self.end, self.end_enabled, self.times_enabled, \ |
paul@1346 | 964 | self.tzid, self.origin, self.replacement, self.cancelled, \ |
paul@1346 | 965 | self.recurrenceid |
paul@1346 | 966 | |
paul@1346 | 967 | def __repr__(self): |
paul@1346 | 968 | return "FormPeriod%r" % (self.as_tuple(),) |
paul@1346 | 969 | |
paul@1346 | 970 | def copy(self): |
paul@1346 | 971 | args = (self.start.copy(), self.end.copy()) + self.as_tuple()[2:] |
paul@1346 | 972 | return FormPeriod(*args) |
paul@1346 | 973 | |
paul@1346 | 974 | def as_event_period(self, index=None): |
paul@1346 | 975 | |
paul@1346 | 976 | """ |
paul@1346 | 977 | Return a converted version of this object as an event period suitable |
paul@1346 | 978 | for iCalendar usage. If 'index' is indicated, include it in any error |
paul@1346 | 979 | raised in the conversion process. |
paul@1346 | 980 | """ |
paul@1346 | 981 | |
paul@1346 | 982 | dtstart, dtstart_attr = self.get_start_item() |
paul@1346 | 983 | if not dtstart: |
paul@1346 | 984 | if index is not None: |
paul@1346 | 985 | raise PeriodError(("dtstart", index)) |
paul@1346 | 986 | else: |
paul@1346 | 987 | raise PeriodError("dtstart") |
paul@1346 | 988 | |
paul@1346 | 989 | dtend, dtend_attr = self.get_end_item() |
paul@1346 | 990 | if not dtend: |
paul@1346 | 991 | if index is not None: |
paul@1346 | 992 | raise PeriodError(("dtend", index)) |
paul@1346 | 993 | else: |
paul@1346 | 994 | raise PeriodError("dtend") |
paul@1346 | 995 | |
paul@1346 | 996 | if dtstart > dtend: |
paul@1346 | 997 | if index is not None: |
paul@1346 | 998 | raise PeriodError(("dtstart", index), ("dtend", index)) |
paul@1346 | 999 | else: |
paul@1346 | 1000 | raise PeriodError("dtstart", "dtend") |
paul@1346 | 1001 | |
paul@1346 | 1002 | return EventPeriod(dtstart, end_date_to_calendar(dtend), self.tzid, |
paul@1346 | 1003 | self.origin, dtstart_attr, dtend_attr, |
paul@1346 | 1004 | self.start, self.end, self.replacement, |
paul@1346 | 1005 | self.cancelled, self.recurrenceid) |
paul@1346 | 1006 | |
paul@1346 | 1007 | # Period data methods. |
paul@1346 | 1008 | |
paul@1346 | 1009 | def get_start(self): |
paul@1346 | 1010 | return self.start and self.start.as_datetime(self.times_enabled) or None |
paul@1346 | 1011 | |
paul@1346 | 1012 | def get_end(self): |
paul@1346 | 1013 | |
paul@1346 | 1014 | # Handle specified end datetimes. |
paul@1346 | 1015 | |
paul@1346 | 1016 | if self.end_enabled: |
paul@1346 | 1017 | dtend = self.end.as_datetime(self.times_enabled) |
paul@1346 | 1018 | if not dtend: |
paul@1346 | 1019 | return None |
paul@1346 | 1020 | |
paul@1346 | 1021 | # Handle same day times. |
paul@1346 | 1022 | |
paul@1346 | 1023 | elif self.times_enabled: |
paul@1346 | 1024 | formdate = FormDate(self.start.date, self.end.hour, self.end.minute, self.end.second, self.end.tzid) |
paul@1346 | 1025 | dtend = formdate.as_datetime(self.times_enabled) |
paul@1346 | 1026 | if not dtend: |
paul@1346 | 1027 | return None |
paul@1346 | 1028 | |
paul@1346 | 1029 | # Otherwise, treat the end date as the start date. Datetimes are |
paul@1346 | 1030 | # handled by making the event occupy the rest of the day. |
paul@1346 | 1031 | |
paul@1346 | 1032 | else: |
paul@1346 | 1033 | dtstart, dtstart_attr = self.get_start_item() |
paul@1346 | 1034 | if dtstart: |
paul@1346 | 1035 | if isinstance(dtstart, datetime): |
paul@1346 | 1036 | dtend = get_end_of_day(dtstart, dtstart_attr["TZID"]) |
paul@1346 | 1037 | else: |
paul@1346 | 1038 | dtend = dtstart |
paul@1346 | 1039 | else: |
paul@1346 | 1040 | return None |
paul@1346 | 1041 | |
paul@1346 | 1042 | return dtend |
paul@1346 | 1043 | |
paul@1346 | 1044 | def get_start_attr(self): |
paul@1346 | 1045 | return self.start and self.start.get_attributes(self.times_enabled) or {} |
paul@1346 | 1046 | |
paul@1346 | 1047 | def get_end_attr(self): |
paul@1346 | 1048 | return self.end and self.end.get_attributes(self.times_enabled) or {} |
paul@1346 | 1049 | |
paul@1346 | 1050 | # Form data methods. |
paul@1346 | 1051 | |
paul@1346 | 1052 | def get_form_start(self): |
paul@1346 | 1053 | return self.start |
paul@1346 | 1054 | |
paul@1346 | 1055 | def get_form_end(self): |
paul@1346 | 1056 | return self.end |
paul@1346 | 1057 | |
paul@1346 | 1058 | def as_form_period(self): |
paul@1346 | 1059 | return self |
paul@1346 | 1060 | |
paul@1346 | 1061 | class FormDate: |
paul@1346 | 1062 | |
paul@1346 | 1063 | "Date information originating from form information." |
paul@1346 | 1064 | |
paul@1346 | 1065 | def __init__(self, date=None, hour=None, minute=None, second=None, tzid=None, dt=None, attr=None): |
paul@1346 | 1066 | self.date = date |
paul@1346 | 1067 | self.hour = hour |
paul@1346 | 1068 | self.minute = minute |
paul@1346 | 1069 | self.second = second |
paul@1346 | 1070 | self.tzid = tzid |
paul@1346 | 1071 | self.dt = dt |
paul@1346 | 1072 | self.attr = attr |
paul@1346 | 1073 | |
paul@1346 | 1074 | def as_tuple(self): |
paul@1346 | 1075 | return self.date, self.hour, self.minute, self.second, self.tzid, self.dt, self.attr |
paul@1346 | 1076 | |
paul@1346 | 1077 | def copy(self): |
paul@1346 | 1078 | return FormDate(*self.as_tuple()) |
paul@1346 | 1079 | |
paul@1346 | 1080 | def reset(self): |
paul@1346 | 1081 | self.dt = None |
paul@1346 | 1082 | |
paul@1346 | 1083 | def __repr__(self): |
paul@1346 | 1084 | return "FormDate%r" % (self.as_tuple(),) |
paul@1346 | 1085 | |
paul@1346 | 1086 | def get_component(self, value): |
paul@1346 | 1087 | return (value or "").rjust(2, "0")[:2] |
paul@1346 | 1088 | |
paul@1346 | 1089 | def get_hour(self): |
paul@1346 | 1090 | return self.get_component(self.hour) |
paul@1346 | 1091 | |
paul@1346 | 1092 | def get_minute(self): |
paul@1346 | 1093 | return self.get_component(self.minute) |
paul@1346 | 1094 | |
paul@1346 | 1095 | def get_second(self): |
paul@1346 | 1096 | return self.get_component(self.second) |
paul@1346 | 1097 | |
paul@1346 | 1098 | def get_date_string(self): |
paul@1346 | 1099 | return self.date or "" |
paul@1346 | 1100 | |
paul@1346 | 1101 | def get_datetime_string(self): |
paul@1346 | 1102 | if not self.date: |
paul@1346 | 1103 | return "" |
paul@1346 | 1104 | |
paul@1346 | 1105 | hour = self.hour; minute = self.minute; second = self.second |
paul@1346 | 1106 | |
paul@1346 | 1107 | if hour or minute or second: |
paul@1346 | 1108 | time = "T%s%s%s" % tuple(map(self.get_component, (hour, minute, second))) |
paul@1346 | 1109 | else: |
paul@1346 | 1110 | time = "" |
paul@1346 | 1111 | |
paul@1346 | 1112 | return "%s%s" % (self.date, time) |
paul@1346 | 1113 | |
paul@1346 | 1114 | def get_tzid(self): |
paul@1346 | 1115 | return self.tzid |
paul@1346 | 1116 | |
paul@1346 | 1117 | def as_datetime(self, with_time=True): |
paul@1346 | 1118 | |
paul@1346 | 1119 | """ |
paul@1346 | 1120 | Return a datetime for this object if one is provided or can be produced. |
paul@1346 | 1121 | """ |
paul@1346 | 1122 | |
paul@1346 | 1123 | # Return any original datetime details. |
paul@1346 | 1124 | |
paul@1346 | 1125 | if self.dt: |
paul@1346 | 1126 | return self.dt |
paul@1346 | 1127 | |
paul@1346 | 1128 | # Otherwise, construct a datetime. |
paul@1346 | 1129 | |
paul@1346 | 1130 | s, attr = self.as_datetime_item(with_time) |
paul@1346 | 1131 | if not s: |
paul@1346 | 1132 | return None |
paul@1346 | 1133 | |
paul@1346 | 1134 | # An erroneous datetime will yield None as result. |
paul@1346 | 1135 | |
paul@1346 | 1136 | try: |
paul@1346 | 1137 | return get_datetime(s, attr) |
paul@1346 | 1138 | except ValueError: |
paul@1346 | 1139 | return None |
paul@1346 | 1140 | |
paul@1346 | 1141 | def as_datetime_item(self, with_time=True): |
paul@1346 | 1142 | |
paul@1346 | 1143 | """ |
paul@1346 | 1144 | Return a (datetime string, attr) tuple for the datetime information |
paul@1346 | 1145 | provided by this object, where both tuple elements will be None if no |
paul@1346 | 1146 | suitable date or datetime information exists. |
paul@1346 | 1147 | """ |
paul@1346 | 1148 | |
paul@1346 | 1149 | s = None |
paul@1346 | 1150 | if with_time: |
paul@1346 | 1151 | s = self.get_datetime_string() |
paul@1346 | 1152 | attr = self.get_attributes(True) |
paul@1346 | 1153 | if not s: |
paul@1346 | 1154 | s = self.get_date_string() |
paul@1346 | 1155 | attr = self.get_attributes(False) |
paul@1346 | 1156 | if not s: |
paul@1346 | 1157 | return None, None |
paul@1346 | 1158 | return s, attr |
paul@1346 | 1159 | |
paul@1346 | 1160 | def get_attributes(self, with_time=True): |
paul@1346 | 1161 | |
paul@1346 | 1162 | "Return attributes for the date or datetime represented by this object." |
paul@1346 | 1163 | |
paul@1346 | 1164 | if with_time: |
paul@1346 | 1165 | return {"TZID" : self.get_tzid(), "VALUE" : "DATE-TIME"} |
paul@1346 | 1166 | else: |
paul@1346 | 1167 | return {"VALUE" : "DATE"} |
paul@1346 | 1168 | |
paul@1346 | 1169 | def event_period_from_period(period, index=None): |
paul@1346 | 1170 | |
paul@1346 | 1171 | """ |
paul@1346 | 1172 | Convert a 'period' to one suitable for use in an iCalendar representation. |
paul@1346 | 1173 | In an "event period" representation, the end day of any date-level event is |
paul@1346 | 1174 | encoded as the "day after" the last day actually involved in the event. |
paul@1346 | 1175 | """ |
paul@1346 | 1176 | |
paul@1346 | 1177 | if isinstance(period, EventPeriod): |
paul@1346 | 1178 | return period |
paul@1346 | 1179 | elif isinstance(period, FormPeriod): |
paul@1346 | 1180 | return period.as_event_period(index) |
paul@1346 | 1181 | else: |
paul@1346 | 1182 | dtstart, dtstart_attr = period.get_start_item() |
paul@1346 | 1183 | dtend, dtend_attr = period.get_end_item() |
paul@1346 | 1184 | |
paul@1346 | 1185 | if not isinstance(period, RecurringPeriod): |
paul@1346 | 1186 | dtend = end_date_to_calendar(dtend) |
paul@1346 | 1187 | |
paul@1346 | 1188 | return EventPeriod(dtstart, dtend, period.tzid, period.origin, |
paul@1346 | 1189 | dtstart_attr, dtend_attr, |
paul@1346 | 1190 | recurrenceid=format_datetime(to_utc_datetime(dtstart))) |
paul@1346 | 1191 | |
paul@1346 | 1192 | def event_periods_from_periods(periods): |
paul@1346 | 1193 | return map(event_period_from_period, periods, range(0, len(periods))) |
paul@1346 | 1194 | |
paul@1346 | 1195 | def form_period_from_period(period): |
paul@1346 | 1196 | |
paul@1346 | 1197 | """ |
paul@1346 | 1198 | Convert a 'period' into a representation usable in a user-editable form. |
paul@1346 | 1199 | In a "form period" representation, the end day of any date-level event is |
paul@1346 | 1200 | presented in a "natural" form, not the iCalendar "day after" form. |
paul@1346 | 1201 | """ |
paul@1346 | 1202 | |
paul@1346 | 1203 | if isinstance(period, EventPeriod): |
paul@1346 | 1204 | return period.as_form_period() |
paul@1346 | 1205 | elif isinstance(period, FormPeriod): |
paul@1346 | 1206 | return period |
paul@1346 | 1207 | else: |
paul@1346 | 1208 | return event_period_from_period(period).as_form_period() |
paul@1346 | 1209 | |
paul@1346 | 1210 | def form_periods_from_periods(periods): |
paul@1346 | 1211 | return map(form_period_from_period, periods) |
paul@1346 | 1212 | |
paul@1346 | 1213 | |
paul@1346 | 1214 | |
paul@1346 | 1215 | # Event period processing. |
paul@1346 | 1216 | |
paul@1346 | 1217 | def periods_from_updated_periods(updated_periods, fn): |
paul@1346 | 1218 | |
paul@1346 | 1219 | """ |
paul@1421 | 1220 | Return periods from the given 'updated_periods' having the form (stored, |
paul@1421 | 1221 | unedited), creating them using 'fn', and setting replacement, cancelled and |
paul@1421 | 1222 | recurrence identifier details. |
paul@1346 | 1223 | |
paul@1346 | 1224 | This function should be used to produce editing-related periods from the |
paul@1346 | 1225 | general updated periods provided by the client abstractions. |
paul@1346 | 1226 | """ |
paul@1346 | 1227 | |
paul@1346 | 1228 | periods = [] |
paul@1346 | 1229 | |
paul@1346 | 1230 | for sp, p in updated_periods: |
paul@1346 | 1231 | |
paul@1346 | 1232 | # Stored periods with corresponding current periods. |
paul@1346 | 1233 | |
paul@1346 | 1234 | if p: |
paul@1346 | 1235 | period = fn(p) |
paul@1346 | 1236 | |
paul@1346 | 1237 | # Replacements are identified by comparing object identities, since |
paul@1346 | 1238 | # a replacement will not be provided by the same object. |
paul@1346 | 1239 | |
paul@1346 | 1240 | if sp is not p: |
paul@1346 | 1241 | period.replacement = True |
paul@1346 | 1242 | |
paul@1346 | 1243 | # Stored periods without corresponding current periods. |
paul@1346 | 1244 | |
paul@1346 | 1245 | else: |
paul@1346 | 1246 | period = fn(sp) |
paul@1346 | 1247 | period.replacement = True |
paul@1346 | 1248 | period.cancelled = True |
paul@1346 | 1249 | |
paul@1346 | 1250 | # Replace the recurrence identifier with that of the original period. |
paul@1346 | 1251 | |
paul@1346 | 1252 | period.recurrenceid = sp.get_recurrenceid() |
paul@1346 | 1253 | periods.append(period) |
paul@1346 | 1254 | |
paul@1346 | 1255 | return periods |
paul@1346 | 1256 | |
paul@1346 | 1257 | def event_periods_from_updated_periods(updated_periods): |
paul@1421 | 1258 | |
paul@1421 | 1259 | """ |
paul@1421 | 1260 | Return event periods from the 'updated_periods' having the form (stored, |
paul@1421 | 1261 | unedited). |
paul@1421 | 1262 | """ |
paul@1421 | 1263 | |
paul@1346 | 1264 | return periods_from_updated_periods(updated_periods, event_period_from_period) |
paul@1346 | 1265 | |
paul@1346 | 1266 | def form_periods_from_updated_periods(updated_periods): |
paul@1421 | 1267 | |
paul@1421 | 1268 | """ |
paul@1421 | 1269 | Return form periods from the 'updated_periods' having the form (stored, |
paul@1421 | 1270 | unedited). |
paul@1421 | 1271 | """ |
paul@1421 | 1272 | |
paul@1346 | 1273 | return periods_from_updated_periods(updated_periods, form_period_from_period) |
paul@1346 | 1274 | |
paul@1346 | 1275 | def periods_by_recurrence(periods): |
paul@1346 | 1276 | |
paul@1346 | 1277 | """ |
paul@1346 | 1278 | Return a mapping from recurrence identifier to period for 'periods' along |
paul@1346 | 1279 | with a collection of unmapped periods. |
paul@1346 | 1280 | """ |
paul@1346 | 1281 | |
paul@1346 | 1282 | d = {} |
paul@1346 | 1283 | new = [] |
paul@1346 | 1284 | |
paul@1346 | 1285 | for p in periods: |
paul@1346 | 1286 | if not p.recurrenceid: |
paul@1346 | 1287 | new.append(p) |
paul@1346 | 1288 | else: |
paul@1346 | 1289 | d[p.recurrenceid] = p |
paul@1346 | 1290 | |
paul@1346 | 1291 | return d, new |
paul@1346 | 1292 | |
paul@1346 | 1293 | def combine_periods(old, new): |
paul@1346 | 1294 | |
paul@1346 | 1295 | """ |
paul@1346 | 1296 | Combine 'old' and 'new' periods for comparison, making a list of (old, new) |
paul@1421 | 1297 | updated period tuples. Such tuples encode correspondences between periods |
paul@1421 | 1298 | representing the same, potentially-edited data. |
paul@1346 | 1299 | """ |
paul@1346 | 1300 | |
paul@1346 | 1301 | old_by_recurrenceid, _new_periods = periods_by_recurrence(old) |
paul@1346 | 1302 | new_by_recurrenceid, new_periods = periods_by_recurrence(new) |
paul@1346 | 1303 | |
paul@1346 | 1304 | combined = [] |
paul@1346 | 1305 | |
paul@1346 | 1306 | for recurrenceid, op in old_by_recurrenceid.items(): |
paul@1346 | 1307 | np = new_by_recurrenceid.get(recurrenceid) |
paul@1346 | 1308 | |
paul@1346 | 1309 | # Old period has corresponding new period that is not cancelled. |
paul@1346 | 1310 | |
paul@1346 | 1311 | if np and not (np.cancelled and not op.cancelled): |
paul@1346 | 1312 | combined.append((op, np)) |
paul@1346 | 1313 | |
paul@1346 | 1314 | # No corresponding new, uncancelled period. |
paul@1346 | 1315 | |
paul@1346 | 1316 | else: |
paul@1346 | 1317 | combined.append((op, None)) |
paul@1346 | 1318 | |
paul@1346 | 1319 | # New periods without corresponding old periods are genuinely new. |
paul@1346 | 1320 | |
paul@1346 | 1321 | for np in new_periods: |
paul@1346 | 1322 | combined.append((None, np)) |
paul@1346 | 1323 | |
paul@1346 | 1324 | # Note that new periods should not have recurrence identifiers, and if |
paul@1346 | 1325 | # imported from other events, they should have such identifiers removed. |
paul@1346 | 1326 | |
paul@1346 | 1327 | return combined |
paul@1346 | 1328 | |
paul@1421 | 1329 | def classify_periods(edited_periods): |
paul@1346 | 1330 | |
paul@1346 | 1331 | """ |
paul@1421 | 1332 | Using the 'edited_periods', being a list of (unedited, edited) periods, |
paul@1346 | 1333 | return a tuple containing collections of new, replaced, retained, cancelled |
paul@1346 | 1334 | and obsolete periods. |
paul@1346 | 1335 | |
paul@1346 | 1336 | Note that replaced and retained indicate the presence or absence of |
paul@1346 | 1337 | differences between the original event periods and the current periods that |
paul@1346 | 1338 | would need to be represented using separate recurrence instances, not |
paul@1346 | 1339 | whether any editing operations have changed the periods. |
paul@1346 | 1340 | |
paul@1346 | 1341 | Obsolete periods are those that have been replaced but not cancelled. |
paul@1346 | 1342 | """ |
paul@1346 | 1343 | |
paul@1346 | 1344 | new = [] |
paul@1346 | 1345 | replaced = [] |
paul@1346 | 1346 | retained = [] |
paul@1346 | 1347 | cancelled = [] |
paul@1346 | 1348 | obsolete = [] |
paul@1346 | 1349 | |
paul@1421 | 1350 | for op, p in edited_periods: |
paul@1346 | 1351 | |
paul@1421 | 1352 | # Unedited periods that are not cancelled. |
paul@1346 | 1353 | |
paul@1421 | 1354 | if op and not op.cancelled: |
paul@1346 | 1355 | |
paul@1346 | 1356 | # With cancelled or absent current periods. |
paul@1346 | 1357 | |
paul@1346 | 1358 | if not p or p.cancelled: |
paul@1421 | 1359 | cancelled.append(op) |
paul@1346 | 1360 | |
paul@1346 | 1361 | # With differing or replacement current periods. |
paul@1346 | 1362 | |
paul@1421 | 1363 | elif p != op or p.replacement: |
paul@1346 | 1364 | replaced.append(p) |
paul@1346 | 1365 | if not p.replacement: |
paul@1346 | 1366 | p.new_replacement = True |
paul@1421 | 1367 | obsolete.append(op) |
paul@1346 | 1368 | |
paul@1346 | 1369 | # With retained, not differing current periods. |
paul@1346 | 1370 | |
paul@1346 | 1371 | else: |
paul@1346 | 1372 | retained.append(p) |
paul@1346 | 1373 | if p.new_replacement: |
paul@1346 | 1374 | p.new_replacement = False |
paul@1346 | 1375 | |
paul@1422 | 1376 | # Cancelled unedited periods. |
paul@1422 | 1377 | |
paul@1422 | 1378 | elif op: |
paul@1422 | 1379 | replaced.append(p) |
paul@1422 | 1380 | |
paul@1421 | 1381 | # New periods without corresponding unedited periods. |
paul@1346 | 1382 | |
paul@1346 | 1383 | elif p: |
paul@1346 | 1384 | new.append(p) |
paul@1346 | 1385 | |
paul@1346 | 1386 | return new, replaced, retained, cancelled, obsolete |
paul@1346 | 1387 | |
paul@1421 | 1388 | def classify_period_changes(edited_periods): |
paul@1346 | 1389 | |
paul@1346 | 1390 | """ |
paul@1421 | 1391 | Using the 'edited_periods', being a list of (unedited, edited) periods, |
paul@1346 | 1392 | return a tuple containing collections of modified, unmodified and removed |
paul@1346 | 1393 | periods. |
paul@1346 | 1394 | """ |
paul@1346 | 1395 | |
paul@1346 | 1396 | modified = [] |
paul@1346 | 1397 | unmodified = [] |
paul@1346 | 1398 | removed = [] |
paul@1346 | 1399 | |
paul@1421 | 1400 | for op, p in edited_periods: |
paul@1346 | 1401 | |
paul@1346 | 1402 | # Test for periods cancelled, reinstated or changed, or left unmodified |
paul@1346 | 1403 | # during editing. |
paul@1346 | 1404 | |
paul@1346 | 1405 | if op: |
paul@1346 | 1406 | if not op.cancelled and (not p or p.cancelled): |
paul@1346 | 1407 | removed.append(op) |
paul@1346 | 1408 | elif op.cancelled and not p.cancelled or p != op: |
paul@1346 | 1409 | modified.append(p) |
paul@1346 | 1410 | else: |
paul@1346 | 1411 | unmodified.append(p) |
paul@1346 | 1412 | |
paul@1346 | 1413 | # New periods are always modifications. |
paul@1346 | 1414 | |
paul@1346 | 1415 | elif p: |
paul@1346 | 1416 | modified.append(p) |
paul@1346 | 1417 | |
paul@1346 | 1418 | return modified, unmodified, removed |
paul@1346 | 1419 | |
paul@1346 | 1420 | def classify_period_operations(new, replaced, retained, cancelled, |
paul@1346 | 1421 | obsolete, modified, removed, |
paul@1346 | 1422 | is_organiser, is_shared, is_changed): |
paul@1346 | 1423 | |
paul@1346 | 1424 | """ |
paul@1346 | 1425 | Classify the operations for the update of an event. For updates modifying |
paul@1346 | 1426 | shared events, return periods for descheduling and rescheduling (where these |
paul@1346 | 1427 | operations can modify the event), and periods for exclusion and application |
paul@1346 | 1428 | (where these operations redefine the event). |
paul@1346 | 1429 | |
paul@1346 | 1430 | To define the new state of the event, details of the complete set of |
paul@1346 | 1431 | unscheduled and rescheduled periods are also provided. |
paul@1346 | 1432 | """ |
paul@1346 | 1433 | |
paul@1346 | 1434 | active_periods = new + replaced + retained |
paul@1397 | 1435 | active_non_rule = filter(lambda p: p.origin != "RRULE", active_periods) |
paul@1346 | 1436 | |
paul@1437 | 1437 | # Modified replaced recurrences are used for incremental updates. |
paul@1346 | 1438 | |
paul@1346 | 1439 | replaced_modified = select_recurrences(replaced, modified).values() |
paul@1346 | 1440 | |
paul@1437 | 1441 | # Unmodified replaced recurrences are used in the complete event summary. |
paul@1346 | 1442 | |
paul@1346 | 1443 | replaced_unmodified = subtract_recurrences(replaced, modified).values() |
paul@1437 | 1444 | |
paul@1437 | 1445 | # Determine any new replacement periods. These are edited periods that were |
paul@1437 | 1446 | # not previously replacements. |
paul@1437 | 1447 | |
paul@1437 | 1448 | replaced_new = filter(lambda p: p.new_replacement, replaced) |
paul@1346 | 1449 | |
paul@1346 | 1450 | # Obtain the removed periods in terms of existing periods. These are used in |
paul@1346 | 1451 | # incremental updates. |
paul@1346 | 1452 | |
paul@1346 | 1453 | cancelled_removed = select_recurrences(cancelled, removed).values() |
paul@1420 | 1454 | cancelled_main = get_main_period(cancelled_removed) |
paul@1420 | 1455 | cancelled_main = cancelled_main and [cancelled_main] or [] |
paul@1346 | 1456 | |
paul@1346 | 1457 | # Reinstated periods are previously-cancelled periods that are now modified |
paul@1346 | 1458 | # periods, and they appear in updates. |
paul@1346 | 1459 | |
paul@1346 | 1460 | reinstated = select_recurrences(modified, cancelled).values() |
paul@1346 | 1461 | |
paul@1346 | 1462 | # Get cancelled periods without reinstated periods. These appear in complete |
paul@1346 | 1463 | # event summaries. |
paul@1346 | 1464 | |
paul@1346 | 1465 | cancelled_unmodified = subtract_recurrences(cancelled, modified).values() |
paul@1346 | 1466 | |
paul@1346 | 1467 | # Cancelled periods originating from rules must be excluded since there are |
paul@1346 | 1468 | # no explicit instances to be deleted. |
paul@1346 | 1469 | |
paul@1437 | 1470 | cancelled_rule = filter(lambda p: p.origin == "RRULE", cancelled_removed) |
paul@1346 | 1471 | |
paul@1346 | 1472 | # Obsolete periods (replaced by other periods) originating from rules must |
paul@1346 | 1473 | # be excluded if no explicit instance will be used to replace them. |
paul@1346 | 1474 | |
paul@1437 | 1475 | obsolete_rule = filter(lambda p: p.origin == "RRULE", obsolete) |
paul@1346 | 1476 | |
paul@1346 | 1477 | # As organiser... |
paul@1346 | 1478 | |
paul@1346 | 1479 | if is_organiser: |
paul@1346 | 1480 | |
paul@1346 | 1481 | # For unshared events... |
paul@1346 | 1482 | # All modifications redefine the event. |
paul@1346 | 1483 | |
paul@1346 | 1484 | # For shared events... |
paul@1435 | 1485 | # Property changes should cause event redefinition. |
paul@1346 | 1486 | # Cancelled rule-originating periods must be excluded. |
paul@1435 | 1487 | # NOTE: New periods where no replacement periods exist might also cause |
paul@1435 | 1488 | # NOTE: complete redefinition, especially if ADD requests are not |
paul@1435 | 1489 | # NOTE: desired. |
paul@1346 | 1490 | |
paul@1435 | 1491 | if not is_shared or is_changed: |
paul@1397 | 1492 | to_set = active_non_rule |
paul@1420 | 1493 | to_exclude = list(chain(cancelled_rule, obsolete_rule, cancelled_main)) |
paul@1346 | 1494 | to_unschedule = [] |
paul@1346 | 1495 | to_reschedule = [] |
paul@1346 | 1496 | to_add = [] |
paul@1346 | 1497 | all_unscheduled = [] |
paul@1346 | 1498 | all_rescheduled = [] |
paul@1346 | 1499 | |
paul@1346 | 1500 | # Changed periods should be rescheduled separately. |
paul@1346 | 1501 | # Removed periods should be cancelled separately. |
paul@1346 | 1502 | |
paul@1346 | 1503 | else: |
paul@1346 | 1504 | to_set = [] |
paul@1346 | 1505 | to_exclude = [] |
paul@1346 | 1506 | to_unschedule = cancelled_removed |
paul@1437 | 1507 | to_reschedule = list(chain(replaced_modified, reinstated)) |
paul@1346 | 1508 | to_add = new |
paul@1346 | 1509 | all_unscheduled = cancelled_unmodified |
paul@1346 | 1510 | all_rescheduled = list(chain(replaced_unmodified, to_reschedule)) |
paul@1346 | 1511 | |
paul@1346 | 1512 | # As attendee... |
paul@1346 | 1513 | |
paul@1346 | 1514 | else: |
paul@1346 | 1515 | to_unschedule = [] |
paul@1346 | 1516 | to_add = [] |
paul@1346 | 1517 | |
paul@1437 | 1518 | # Parent event changes cause redefinition of the entire event. |
paul@1437 | 1519 | # The event is defined in terms of new periods and exceptions for |
paul@1437 | 1520 | # removed periods or obsolete rule periods. |
paul@1346 | 1521 | |
paul@1437 | 1522 | if new or removed or is_changed or reinstated or replaced_new: |
paul@1397 | 1523 | to_set = active_non_rule |
paul@1420 | 1524 | to_exclude = list(chain(cancelled, obsolete_rule, cancelled_main)) |
paul@1346 | 1525 | to_reschedule = [] |
paul@1346 | 1526 | all_unscheduled = [] |
paul@1346 | 1527 | all_rescheduled = [] |
paul@1346 | 1528 | |
paul@1437 | 1529 | # Changed periods without new or removed periods are proposed as |
paul@1437 | 1530 | # separate changes. |
paul@1437 | 1531 | |
paul@1437 | 1532 | else: |
paul@1437 | 1533 | to_set = [] |
paul@1437 | 1534 | to_exclude = [] |
paul@1437 | 1535 | to_reschedule = replaced_modified |
paul@1437 | 1536 | all_unscheduled = cancelled_unmodified |
paul@1437 | 1537 | all_rescheduled = list(chain(replaced_unmodified, to_reschedule)) |
paul@1437 | 1538 | |
paul@1346 | 1539 | return to_unschedule, to_reschedule, to_add, to_exclude, to_set, all_unscheduled, all_rescheduled |
paul@1346 | 1540 | |
paul@1346 | 1541 | def get_period_mapping(periods): |
paul@1346 | 1542 | |
paul@1346 | 1543 | "Return a mapping of recurrence identifiers to the given 'periods." |
paul@1346 | 1544 | |
paul@1346 | 1545 | d, new = periods_by_recurrence(periods) |
paul@1346 | 1546 | return d |
paul@1346 | 1547 | |
paul@1346 | 1548 | def select_recurrences(source, selected): |
paul@1346 | 1549 | |
paul@1346 | 1550 | "Restrict 'source' to the recurrences referenced by 'selected'." |
paul@1346 | 1551 | |
paul@1346 | 1552 | mapping = get_period_mapping(source) |
paul@1346 | 1553 | |
paul@1346 | 1554 | recurrenceids = get_recurrenceids(selected) |
paul@1346 | 1555 | for recurrenceid in mapping.keys(): |
paul@1346 | 1556 | if not recurrenceid in recurrenceids: |
paul@1346 | 1557 | del mapping[recurrenceid] |
paul@1346 | 1558 | return mapping |
paul@1346 | 1559 | |
paul@1346 | 1560 | def subtract_recurrences(source, selected): |
paul@1346 | 1561 | |
paul@1346 | 1562 | "Remove from 'source' the recurrences referenced by 'selected'." |
paul@1346 | 1563 | |
paul@1346 | 1564 | mapping = get_period_mapping(source) |
paul@1346 | 1565 | |
paul@1346 | 1566 | for recurrenceid in get_recurrenceids(selected): |
paul@1346 | 1567 | if mapping.has_key(recurrenceid): |
paul@1346 | 1568 | del mapping[recurrenceid] |
paul@1346 | 1569 | return mapping |
paul@1346 | 1570 | |
paul@1346 | 1571 | def get_recurrenceids(periods): |
paul@1346 | 1572 | |
paul@1346 | 1573 | "Return the recurrence identifiers employed by 'periods'." |
paul@1346 | 1574 | |
paul@1346 | 1575 | return map(lambda p: p.get_recurrenceid(), periods) |
paul@1346 | 1576 | |
paul@1346 | 1577 | |
paul@1346 | 1578 | |
paul@1346 | 1579 | # Attendee processing. |
paul@1346 | 1580 | |
paul@1346 | 1581 | def classify_attendee_changes(original, current): |
paul@1346 | 1582 | |
paul@1346 | 1583 | """ |
paul@1346 | 1584 | Return categories of attendees given the 'original' and 'current' |
paul@1346 | 1585 | collections of attendees. |
paul@1346 | 1586 | """ |
paul@1346 | 1587 | |
paul@1346 | 1588 | new = {} |
paul@1346 | 1589 | modified = {} |
paul@1346 | 1590 | unmodified = {} |
paul@1346 | 1591 | |
paul@1346 | 1592 | # Check current attendees against the original ones. |
paul@1346 | 1593 | |
paul@1346 | 1594 | for attendee, attendee_attr in current.items(): |
paul@1346 | 1595 | original_attr = original.get(attendee) |
paul@1346 | 1596 | |
paul@1346 | 1597 | # New attendee if missing original details. |
paul@1346 | 1598 | |
paul@1346 | 1599 | if not original_attr: |
paul@1346 | 1600 | new[attendee] = attendee_attr |
paul@1346 | 1601 | |
paul@1346 | 1602 | # Details unchanged for existing attendee. |
paul@1346 | 1603 | |
paul@1346 | 1604 | elif attendee_attr == original_attr: |
paul@1346 | 1605 | unmodified[attendee] = attendee_attr |
paul@1346 | 1606 | |
paul@1346 | 1607 | # Details changed for existing attendee. |
paul@1346 | 1608 | |
paul@1346 | 1609 | else: |
paul@1346 | 1610 | modified[attendee] = attendee_attr |
paul@1346 | 1611 | |
paul@1346 | 1612 | removed = {} |
paul@1346 | 1613 | |
paul@1346 | 1614 | # Check for removed attendees. |
paul@1346 | 1615 | |
paul@1346 | 1616 | for attendee, attendee_attr in original.items(): |
paul@1346 | 1617 | if not current.has_key(attendee): |
paul@1346 | 1618 | removed[attendee] = attendee_attr |
paul@1346 | 1619 | |
paul@1346 | 1620 | return new, modified, unmodified, removed |
paul@1346 | 1621 | |
paul@1346 | 1622 | # vim: tabstop=4 expandtab shiftwidth=4 |