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