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