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