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