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