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@556 | 22 | from datetime import datetime, timedelta |
paul@539 | 23 | from imiptools.dates import end_date_from_calendar, end_date_to_calendar, \ |
paul@1268 | 24 | format_datetime, get_datetime, \ |
paul@1268 | 25 | get_datetime_attributes, get_end_of_day, \ |
paul@1268 | 26 | to_date, to_utc_datetime, to_timezone |
paul@621 | 27 | from imiptools.period import RecurringPeriod |
paul@497 | 28 | |
paul@1299 | 29 | # General editing abstractions. |
paul@1299 | 30 | |
paul@1299 | 31 | class State: |
paul@1299 | 32 | |
paul@1299 | 33 | "Manage computed state." |
paul@1299 | 34 | |
paul@1299 | 35 | def __init__(self, callables): |
paul@1299 | 36 | |
paul@1299 | 37 | """ |
paul@1299 | 38 | Define state variable initialisation using the given 'callables', which |
paul@1299 | 39 | is a mapping that defines a callable for each variable name that is |
paul@1299 | 40 | invoked when the variable is first requested. |
paul@1299 | 41 | """ |
paul@1299 | 42 | |
paul@1299 | 43 | self.state = {} |
paul@1299 | 44 | self.callables = callables |
paul@1299 | 45 | |
paul@1299 | 46 | def get_callable(self, key): |
paul@1299 | 47 | return self.callables.get(key, lambda: None) |
paul@1299 | 48 | |
paul@1299 | 49 | def get(self, key, reset=False): |
paul@1299 | 50 | |
paul@1299 | 51 | """ |
paul@1299 | 52 | Return state for the given 'key', using the configured callable to |
paul@1299 | 53 | compute and set the state if no state is already defined. |
paul@1299 | 54 | |
paul@1299 | 55 | If 'reset' is set to a true value, compute and return the state using |
paul@1299 | 56 | the configured callable regardless of any existing state. |
paul@1299 | 57 | """ |
paul@1299 | 58 | |
paul@1299 | 59 | if reset or not self.state.has_key(key): |
paul@1299 | 60 | self.state[key] = self.get_callable(key)() |
paul@1299 | 61 | |
paul@1299 | 62 | return self.state[key] |
paul@1299 | 63 | |
paul@1299 | 64 | def set(self, key, value): |
paul@1299 | 65 | self.state[key] = value |
paul@1299 | 66 | |
paul@1299 | 67 | def __getitem__(self, key): |
paul@1299 | 68 | return self.get(key) |
paul@1299 | 69 | |
paul@1299 | 70 | def __setitem__(self, key, value): |
paul@1299 | 71 | self.set(key, value) |
paul@1299 | 72 | |
paul@1299 | 73 | def has_changed(self, key): |
paul@1299 | 74 | return self.get_callable(key)() != self.get(key) |
paul@1299 | 75 | |
paul@1299 | 76 | |
paul@1299 | 77 | |
paul@1299 | 78 | # Period-related abstractions. |
paul@1299 | 79 | |
paul@498 | 80 | class PeriodError(Exception): |
paul@498 | 81 | pass |
paul@498 | 82 | |
paul@1280 | 83 | class EditablePeriod(RecurringPeriod): |
paul@1280 | 84 | |
paul@1280 | 85 | "An editable period tracking the identity of any original period." |
paul@1280 | 86 | |
paul@1280 | 87 | def _get_recurrenceid_item(self): |
paul@1280 | 88 | |
paul@1280 | 89 | # Convert any stored identifier to the current time zone. |
paul@1280 | 90 | # NOTE: This should not be necessary, but is done for consistency with |
paul@1280 | 91 | # NOTE: the datetime properties. |
paul@1280 | 92 | |
paul@1280 | 93 | dt = get_datetime(self.recurrenceid) |
paul@1280 | 94 | dt = to_timezone(dt, self.tzid) |
paul@1280 | 95 | return dt, get_datetime_attributes(dt) |
paul@1280 | 96 | |
paul@1280 | 97 | def get_recurrenceid(self): |
paul@1280 | 98 | if not self.recurrenceid: |
paul@1290 | 99 | return RecurringPeriod.get_recurrenceid(self) |
paul@1280 | 100 | return self.recurrenceid |
paul@1280 | 101 | |
paul@1280 | 102 | def get_recurrenceid_item(self): |
paul@1280 | 103 | if not self.recurrenceid: |
paul@1290 | 104 | return RecurringPeriod.get_recurrenceid_item(self) |
paul@1280 | 105 | return self._get_recurrenceid_item() |
paul@1280 | 106 | |
paul@1280 | 107 | class EventPeriod(EditablePeriod): |
paul@497 | 108 | |
paul@498 | 109 | """ |
paul@498 | 110 | A simple period plus attribute details, compatible with RecurringPeriod, and |
paul@498 | 111 | intended to represent information obtained from an iCalendar resource. |
paul@498 | 112 | """ |
paul@497 | 113 | |
paul@1246 | 114 | def __init__(self, start, end, tzid=None, origin=None, start_attr=None, |
paul@1268 | 115 | end_attr=None, form_start=None, form_end=None, |
paul@1268 | 116 | replacement=False, cancelled=False, recurrenceid=None): |
paul@528 | 117 | |
paul@528 | 118 | """ |
paul@1268 | 119 | Initialise a period with the given 'start' and 'end' datetimes. |
paul@1268 | 120 | |
paul@1268 | 121 | The optional 'tzid' provides time zone information, and the optional |
paul@1268 | 122 | 'origin' indicates the kind of period this object describes. |
paul@1268 | 123 | |
paul@1268 | 124 | The optional 'start_attr' and 'end_attr' provide metadata for the start |
paul@1268 | 125 | and end datetimes respectively, and 'form_start' and 'form_end' are |
paul@1268 | 126 | values provided as textual input. |
paul@1268 | 127 | |
paul@1268 | 128 | The 'replacement' flag indicates whether the period is provided by a |
paul@1268 | 129 | separate recurrence instance. |
paul@1268 | 130 | |
paul@1268 | 131 | The 'cancelled' flag indicates whether a separate recurrence is |
paul@1268 | 132 | cancelled. |
paul@1268 | 133 | |
paul@1268 | 134 | The 'recurrenceid' describes the original identity of the period, |
paul@1268 | 135 | regardless of whether it is separate or not. |
paul@528 | 136 | """ |
paul@528 | 137 | |
paul@1280 | 138 | EditablePeriod.__init__(self, start, end, tzid, origin, start_attr, end_attr) |
paul@498 | 139 | self.form_start = form_start |
paul@498 | 140 | self.form_end = form_end |
paul@1268 | 141 | |
paul@1268 | 142 | # Information about whether a separate recurrence provides this period |
paul@1268 | 143 | # and the original period identity. |
paul@1268 | 144 | |
paul@1268 | 145 | self.replacement = replacement |
paul@1268 | 146 | self.cancelled = cancelled |
paul@1268 | 147 | self.recurrenceid = recurrenceid |
paul@498 | 148 | |
paul@1277 | 149 | # Additional editing state. |
paul@1277 | 150 | |
paul@1277 | 151 | self.new_replacement = False |
paul@498 | 152 | |
paul@498 | 153 | def as_tuple(self): |
paul@1246 | 154 | return self.start, self.end, self.tzid, self.origin, self.start_attr, \ |
paul@1268 | 155 | self.end_attr, self.form_start, self.form_end, self.replacement, \ |
paul@1268 | 156 | self.cancelled, self.recurrenceid |
paul@498 | 157 | |
paul@498 | 158 | def __repr__(self): |
paul@833 | 159 | return "EventPeriod%r" % (self.as_tuple(),) |
paul@499 | 160 | |
paul@1268 | 161 | def copy(self): |
paul@1268 | 162 | return EventPeriod(*self.as_tuple()) |
paul@1268 | 163 | |
paul@1280 | 164 | def as_event_period(self, index=None): |
paul@499 | 165 | return self |
paul@499 | 166 | |
paul@700 | 167 | def get_start_item(self): |
paul@700 | 168 | return self.get_start(), self.get_start_attr() |
paul@700 | 169 | |
paul@700 | 170 | def get_end_item(self): |
paul@700 | 171 | return self.get_end(), self.get_end_attr() |
paul@700 | 172 | |
paul@499 | 173 | # Form data compatibility methods. |
paul@498 | 174 | |
paul@498 | 175 | def get_form_start(self): |
paul@498 | 176 | if not self.form_start: |
paul@499 | 177 | self.form_start = self.get_form_date(self.get_start(), self.start_attr) |
paul@498 | 178 | return self.form_start |
paul@498 | 179 | |
paul@498 | 180 | def get_form_end(self): |
paul@498 | 181 | if not self.form_end: |
paul@539 | 182 | self.form_end = self.get_form_date(end_date_from_calendar(self.get_end()), self.end_attr) |
paul@498 | 183 | return self.form_end |
paul@498 | 184 | |
paul@498 | 185 | def as_form_period(self): |
paul@498 | 186 | return FormPeriod( |
paul@499 | 187 | self.get_form_start(), |
paul@499 | 188 | self.get_form_end(), |
paul@556 | 189 | isinstance(self.end, datetime) or self.get_start() != self.get_end() - timedelta(1), |
paul@532 | 190 | isinstance(self.start, datetime) or isinstance(self.end, datetime), |
paul@541 | 191 | self.tzid, |
paul@868 | 192 | self.origin, |
paul@1268 | 193 | self.replacement, |
paul@1268 | 194 | self.cancelled, |
paul@1268 | 195 | self.recurrenceid |
paul@498 | 196 | ) |
paul@498 | 197 | |
paul@498 | 198 | def get_form_date(self, dt, attr=None): |
paul@498 | 199 | return FormDate( |
paul@498 | 200 | format_datetime(to_date(dt)), |
paul@498 | 201 | isinstance(dt, datetime) and str(dt.hour) or None, |
paul@498 | 202 | isinstance(dt, datetime) and str(dt.minute) or None, |
paul@498 | 203 | isinstance(dt, datetime) and str(dt.second) or None, |
paul@498 | 204 | attr and attr.get("TZID") or None, |
paul@498 | 205 | dt, attr |
paul@498 | 206 | ) |
paul@498 | 207 | |
paul@1280 | 208 | class FormPeriod(EditablePeriod): |
paul@498 | 209 | |
paul@498 | 210 | "A period whose information originates from a form." |
paul@498 | 211 | |
paul@1246 | 212 | def __init__(self, start, end, end_enabled=True, times_enabled=True, |
paul@1268 | 213 | tzid=None, origin=None, replacement=False, cancelled=False, |
paul@1268 | 214 | recurrenceid=None): |
paul@498 | 215 | self.start = start |
paul@498 | 216 | self.end = end |
paul@498 | 217 | self.end_enabled = end_enabled |
paul@498 | 218 | self.times_enabled = times_enabled |
paul@541 | 219 | self.tzid = tzid |
paul@499 | 220 | self.origin = origin |
paul@1268 | 221 | self.replacement = replacement |
paul@1268 | 222 | self.cancelled = cancelled |
paul@1266 | 223 | self.recurrenceid = recurrenceid |
paul@1277 | 224 | self.new_replacement = False |
paul@497 | 225 | |
paul@497 | 226 | def as_tuple(self): |
paul@1268 | 227 | return self.start, self.end, self.end_enabled, self.times_enabled, \ |
paul@1268 | 228 | self.tzid, self.origin, self.replacement, self.cancelled, \ |
paul@1268 | 229 | self.recurrenceid |
paul@497 | 230 | |
paul@497 | 231 | def __repr__(self): |
paul@833 | 232 | return "FormPeriod%r" % (self.as_tuple(),) |
paul@498 | 233 | |
paul@1268 | 234 | def copy(self): |
paul@1268 | 235 | return FormPeriod(*self.as_tuple()) |
paul@1253 | 236 | |
paul@499 | 237 | def as_event_period(self, index=None): |
paul@528 | 238 | |
paul@528 | 239 | """ |
paul@528 | 240 | Return a converted version of this object as an event period suitable |
paul@528 | 241 | for iCalendar usage. If 'index' is indicated, include it in any error |
paul@528 | 242 | raised in the conversion process. |
paul@528 | 243 | """ |
paul@528 | 244 | |
paul@700 | 245 | dtstart, dtstart_attr = self.get_start_item() |
paul@528 | 246 | if not dtstart: |
paul@1251 | 247 | if index is not None: |
paul@1251 | 248 | raise PeriodError(("dtstart", index)) |
paul@1251 | 249 | else: |
paul@1251 | 250 | raise PeriodError("dtstart") |
paul@499 | 251 | |
paul@700 | 252 | dtend, dtend_attr = self.get_end_item() |
paul@528 | 253 | if not dtend: |
paul@1251 | 254 | if index is not None: |
paul@1251 | 255 | raise PeriodError(("dtend", index)) |
paul@1251 | 256 | else: |
paul@1251 | 257 | raise PeriodError("dtend") |
paul@499 | 258 | |
paul@499 | 259 | if dtstart > dtend: |
paul@1251 | 260 | if index is not None: |
paul@1251 | 261 | raise PeriodError(("dtstart", index), ("dtend", index)) |
paul@1251 | 262 | else: |
paul@1251 | 263 | raise PeriodError("dtstart", "dtend") |
paul@499 | 264 | |
paul@1246 | 265 | return EventPeriod(dtstart, end_date_to_calendar(dtend), self.tzid, |
paul@1246 | 266 | self.origin, dtstart_attr, dtend_attr, |
paul@1268 | 267 | self.start, self.end, self.replacement, |
paul@1268 | 268 | self.cancelled, self.recurrenceid) |
paul@499 | 269 | |
paul@499 | 270 | # Period data methods. |
paul@499 | 271 | |
paul@498 | 272 | def get_start(self): |
paul@1246 | 273 | return self.start and self.start.as_datetime(self.times_enabled) or None |
paul@498 | 274 | |
paul@498 | 275 | def get_end(self): |
paul@620 | 276 | |
paul@620 | 277 | # Handle specified end datetimes. |
paul@620 | 278 | |
paul@620 | 279 | if self.end_enabled: |
paul@620 | 280 | dtend = self.end.as_datetime(self.times_enabled) |
paul@620 | 281 | if not dtend: |
paul@620 | 282 | return None |
paul@620 | 283 | |
paul@886 | 284 | # Handle same day times. |
paul@886 | 285 | |
paul@886 | 286 | elif self.times_enabled: |
paul@886 | 287 | formdate = FormDate(self.start.date, self.end.hour, self.end.minute, self.end.second, self.end.tzid) |
paul@886 | 288 | dtend = formdate.as_datetime(self.times_enabled) |
paul@886 | 289 | if not dtend: |
paul@886 | 290 | return None |
paul@886 | 291 | |
paul@620 | 292 | # Otherwise, treat the end date as the start date. Datetimes are |
paul@620 | 293 | # handled by making the event occupy the rest of the day. |
paul@620 | 294 | |
paul@620 | 295 | else: |
paul@620 | 296 | dtstart, dtstart_attr = self.get_start_item() |
paul@620 | 297 | if dtstart: |
paul@620 | 298 | if isinstance(dtstart, datetime): |
paul@620 | 299 | dtend = get_end_of_day(dtstart, dtstart_attr["TZID"]) |
paul@620 | 300 | else: |
paul@620 | 301 | dtend = dtstart |
paul@620 | 302 | else: |
paul@620 | 303 | return None |
paul@620 | 304 | |
paul@528 | 305 | return dtend |
paul@528 | 306 | |
paul@620 | 307 | def get_start_attr(self): |
paul@1246 | 308 | return self.start and self.start.get_attributes(self.times_enabled) or {} |
paul@528 | 309 | |
paul@620 | 310 | def get_end_attr(self): |
paul@1246 | 311 | return self.end and self.end.get_attributes(self.times_enabled) or {} |
paul@498 | 312 | |
paul@499 | 313 | # Form data methods. |
paul@498 | 314 | |
paul@498 | 315 | def get_form_start(self): |
paul@498 | 316 | return self.start |
paul@498 | 317 | |
paul@498 | 318 | def get_form_end(self): |
paul@498 | 319 | return self.end |
paul@498 | 320 | |
paul@498 | 321 | def as_form_period(self): |
paul@498 | 322 | return self |
paul@497 | 323 | |
paul@498 | 324 | class FormDate: |
paul@498 | 325 | |
paul@498 | 326 | "Date information originating from form information." |
paul@498 | 327 | |
paul@498 | 328 | def __init__(self, date=None, hour=None, minute=None, second=None, tzid=None, dt=None, attr=None): |
paul@498 | 329 | self.date = date |
paul@498 | 330 | self.hour = hour |
paul@498 | 331 | self.minute = minute |
paul@498 | 332 | self.second = second |
paul@498 | 333 | self.tzid = tzid |
paul@498 | 334 | self.dt = dt |
paul@498 | 335 | self.attr = attr |
paul@498 | 336 | |
paul@498 | 337 | def as_tuple(self): |
paul@498 | 338 | return self.date, self.hour, self.minute, self.second, self.tzid, self.dt, self.attr |
paul@498 | 339 | |
paul@1268 | 340 | def reset(self): |
paul@1268 | 341 | self.dt = None |
paul@1268 | 342 | |
paul@498 | 343 | def __repr__(self): |
paul@833 | 344 | return "FormDate%r" % (self.as_tuple(),) |
paul@498 | 345 | |
paul@498 | 346 | def get_component(self, value): |
paul@498 | 347 | return (value or "").rjust(2, "0")[:2] |
paul@498 | 348 | |
paul@498 | 349 | def get_hour(self): |
paul@498 | 350 | return self.get_component(self.hour) |
paul@498 | 351 | |
paul@498 | 352 | def get_minute(self): |
paul@498 | 353 | return self.get_component(self.minute) |
paul@498 | 354 | |
paul@498 | 355 | def get_second(self): |
paul@498 | 356 | return self.get_component(self.second) |
paul@498 | 357 | |
paul@498 | 358 | def get_date_string(self): |
paul@498 | 359 | return self.date or "" |
paul@498 | 360 | |
paul@498 | 361 | def get_datetime_string(self): |
paul@498 | 362 | if not self.date: |
paul@498 | 363 | return "" |
paul@498 | 364 | |
paul@498 | 365 | hour = self.hour; minute = self.minute; second = self.second |
paul@498 | 366 | |
paul@498 | 367 | if hour or minute or second: |
paul@498 | 368 | time = "T%s%s%s" % tuple(map(self.get_component, (hour, minute, second))) |
paul@498 | 369 | else: |
paul@498 | 370 | time = "" |
paul@498 | 371 | |
paul@498 | 372 | return "%s%s" % (self.date, time) |
paul@498 | 373 | |
paul@498 | 374 | def get_tzid(self): |
paul@498 | 375 | return self.tzid |
paul@498 | 376 | |
paul@528 | 377 | def as_datetime(self, with_time=True): |
paul@498 | 378 | |
paul@528 | 379 | "Return a datetime for this object." |
paul@498 | 380 | |
paul@498 | 381 | # Return any original datetime details. |
paul@498 | 382 | |
paul@498 | 383 | if self.dt: |
paul@528 | 384 | return self.dt |
paul@498 | 385 | |
paul@528 | 386 | # Otherwise, construct a datetime. |
paul@498 | 387 | |
paul@528 | 388 | s, attr = self.as_datetime_item(with_time) |
paul@528 | 389 | if s: |
paul@528 | 390 | return get_datetime(s, attr) |
paul@498 | 391 | else: |
paul@528 | 392 | return None |
paul@528 | 393 | |
paul@528 | 394 | def as_datetime_item(self, with_time=True): |
paul@498 | 395 | |
paul@528 | 396 | """ |
paul@528 | 397 | Return a (datetime string, attr) tuple for the datetime information |
paul@528 | 398 | provided by this object, where both tuple elements will be None if no |
paul@528 | 399 | suitable date or datetime information exists. |
paul@528 | 400 | """ |
paul@498 | 401 | |
paul@528 | 402 | s = None |
paul@528 | 403 | if with_time: |
paul@528 | 404 | s = self.get_datetime_string() |
paul@528 | 405 | attr = self.get_attributes(True) |
paul@528 | 406 | if not s: |
paul@528 | 407 | s = self.get_date_string() |
paul@528 | 408 | attr = self.get_attributes(False) |
paul@528 | 409 | if not s: |
paul@528 | 410 | return None, None |
paul@528 | 411 | return s, attr |
paul@498 | 412 | |
paul@528 | 413 | def get_attributes(self, with_time=True): |
paul@528 | 414 | |
paul@528 | 415 | "Return attributes for the date or datetime represented by this object." |
paul@498 | 416 | |
paul@528 | 417 | if with_time: |
paul@528 | 418 | return {"TZID" : self.get_tzid(), "VALUE" : "DATE-TIME"} |
paul@528 | 419 | else: |
paul@528 | 420 | return {"VALUE" : "DATE"} |
paul@498 | 421 | |
paul@1280 | 422 | def event_period_from_period(period, index=None): |
paul@624 | 423 | |
paul@624 | 424 | """ |
paul@624 | 425 | Convert a 'period' to one suitable for use in an iCalendar representation. |
paul@624 | 426 | In an "event period" representation, the end day of any date-level event is |
paul@624 | 427 | encoded as the "day after" the last day actually involved in the event. |
paul@624 | 428 | """ |
paul@624 | 429 | |
paul@499 | 430 | if isinstance(period, EventPeriod): |
paul@499 | 431 | return period |
paul@499 | 432 | elif isinstance(period, FormPeriod): |
paul@1280 | 433 | return period.as_event_period(index) |
paul@499 | 434 | else: |
paul@528 | 435 | dtstart, dtstart_attr = period.get_start_item() |
paul@528 | 436 | dtend, dtend_attr = period.get_end_item() |
paul@1268 | 437 | |
paul@539 | 438 | if not isinstance(period, RecurringPeriod): |
paul@539 | 439 | dtend = end_date_to_calendar(dtend) |
paul@1268 | 440 | |
paul@1268 | 441 | return EventPeriod(dtstart, dtend, period.tzid, period.origin, |
paul@1268 | 442 | dtstart_attr, dtend_attr, |
paul@1268 | 443 | recurrenceid=format_datetime(to_utc_datetime(dtstart))) |
paul@1268 | 444 | |
paul@1268 | 445 | def event_periods_from_periods(periods): |
paul@1280 | 446 | return map(event_period_from_period, periods, range(0, len(periods))) |
paul@499 | 447 | |
paul@499 | 448 | def form_period_from_period(period): |
paul@624 | 449 | |
paul@624 | 450 | """ |
paul@624 | 451 | Convert a 'period' into a representation usable in a user-editable form. |
paul@624 | 452 | In a "form period" representation, the end day of any date-level event is |
paul@624 | 453 | presented in a "natural" form, not the iCalendar "day after" form. |
paul@624 | 454 | """ |
paul@624 | 455 | |
paul@499 | 456 | if isinstance(period, EventPeriod): |
paul@499 | 457 | return period.as_form_period() |
paul@499 | 458 | elif isinstance(period, FormPeriod): |
paul@499 | 459 | return period |
paul@499 | 460 | else: |
paul@499 | 461 | return event_period_from_period(period).as_form_period() |
paul@499 | 462 | |
paul@1268 | 463 | def form_periods_from_periods(periods): |
paul@1268 | 464 | return map(form_period_from_period, periods) |
paul@1268 | 465 | |
paul@1246 | 466 | |
paul@1246 | 467 | |
paul@1268 | 468 | # Event period processing. |
paul@1257 | 469 | |
paul@1268 | 470 | def periods_from_updated_periods(updated_periods, fn): |
paul@1257 | 471 | |
paul@1267 | 472 | """ |
paul@1268 | 473 | Return periods from the given 'updated_periods' created using 'fn, setting |
paul@1268 | 474 | replacement, cancelled and recurrence identifier details. |
paul@1267 | 475 | """ |
paul@1257 | 476 | |
paul@1268 | 477 | periods = [] |
paul@1268 | 478 | |
paul@1268 | 479 | for sp, p in updated_periods: |
paul@1268 | 480 | if p: |
paul@1268 | 481 | period = fn(p) |
paul@1268 | 482 | if sp != p: |
paul@1268 | 483 | period.replacement = True |
paul@1268 | 484 | else: |
paul@1268 | 485 | period = fn(sp) |
paul@1268 | 486 | period.replacement = True |
paul@1268 | 487 | period.cancelled = True |
paul@1268 | 488 | |
paul@1268 | 489 | # Replace the recurrence identifier with that of the original period. |
paul@1268 | 490 | |
paul@1268 | 491 | period.recurrenceid = sp.get_recurrenceid() |
paul@1268 | 492 | periods.append(period) |
paul@1268 | 493 | |
paul@1268 | 494 | return periods |
paul@1268 | 495 | |
paul@1268 | 496 | def event_periods_from_updated_periods(updated_periods): |
paul@1268 | 497 | return periods_from_updated_periods(updated_periods, event_period_from_period) |
paul@1268 | 498 | |
paul@1268 | 499 | def form_periods_from_updated_periods(updated_periods): |
paul@1268 | 500 | return periods_from_updated_periods(updated_periods, form_period_from_period) |
paul@1268 | 501 | |
paul@1268 | 502 | def periods_by_recurrence(periods): |
paul@1268 | 503 | |
paul@1268 | 504 | """ |
paul@1268 | 505 | Return a mapping from recurrence identifier to period for 'periods' along |
paul@1268 | 506 | with a collection of unmapped periods. |
paul@1267 | 507 | """ |
paul@1257 | 508 | |
paul@1268 | 509 | d = {} |
paul@1268 | 510 | new = [] |
paul@1267 | 511 | |
paul@1257 | 512 | for p in periods: |
paul@1268 | 513 | if not p.recurrenceid: |
paul@1268 | 514 | new.append(p) |
paul@1268 | 515 | else: |
paul@1268 | 516 | d[p.recurrenceid] = p |
paul@1268 | 517 | |
paul@1268 | 518 | return d, new |
paul@1268 | 519 | |
paul@1268 | 520 | def combine_periods(old, new): |
paul@1268 | 521 | |
paul@1268 | 522 | "Combine 'old' and 'new' periods for comparison." |
paul@1268 | 523 | |
paul@1268 | 524 | old_by_recurrenceid, _new_periods = periods_by_recurrence(old) |
paul@1268 | 525 | new_by_recurrenceid, new_periods = periods_by_recurrence(new) |
paul@1268 | 526 | |
paul@1268 | 527 | combined = [] |
paul@1267 | 528 | |
paul@1268 | 529 | for recurrenceid, op in old_by_recurrenceid.items(): |
paul@1268 | 530 | np = new_by_recurrenceid.get(recurrenceid) |
paul@1268 | 531 | if np and not np.cancelled: |
paul@1268 | 532 | combined.append((op, np)) |
paul@1268 | 533 | else: |
paul@1268 | 534 | combined.append((op, None)) |
paul@1268 | 535 | |
paul@1268 | 536 | for np in new_periods: |
paul@1268 | 537 | combined.append((None, np)) |
paul@1267 | 538 | |
paul@1268 | 539 | return combined |
paul@1267 | 540 | |
paul@1268 | 541 | def classify_periods(updated_periods): |
paul@1267 | 542 | |
paul@1268 | 543 | """ |
paul@1268 | 544 | Using the 'updated_periods', being a list of (stored, current) periods, |
paul@1268 | 545 | return a tuple containing collections of new, changed, unchanged and removed |
paul@1268 | 546 | periods. |
paul@1268 | 547 | |
paul@1268 | 548 | Note that changed and unchanged indicate the presence or absence of |
paul@1268 | 549 | differences between the original event periods and the current periods, not |
paul@1268 | 550 | whether any editing operations have changed the periods. |
paul@1268 | 551 | """ |
paul@1267 | 552 | |
paul@1267 | 553 | new = [] |
paul@1267 | 554 | changed = [] |
paul@1267 | 555 | unchanged = [] |
paul@1268 | 556 | removed = [] |
paul@1267 | 557 | |
paul@1268 | 558 | for sp, p in updated_periods: |
paul@1268 | 559 | if sp: |
paul@1268 | 560 | if not p or p.cancelled: |
paul@1268 | 561 | removed.append(sp) |
paul@1268 | 562 | elif p != sp or p.replacement: |
paul@1268 | 563 | changed.append(p) |
paul@1277 | 564 | if not p.replacement: |
paul@1277 | 565 | p.new_replacement = True |
paul@1268 | 566 | else: |
paul@1268 | 567 | unchanged.append(p) |
paul@1277 | 568 | if p.new_replacement: |
paul@1277 | 569 | p.new_replacement = False |
paul@1268 | 570 | elif p: |
paul@1268 | 571 | new.append(p) |
paul@1267 | 572 | |
paul@1268 | 573 | return new, changed, unchanged, removed |
paul@1267 | 574 | |
paul@1268 | 575 | def classify_operations(new, changed, unchanged, removed, is_organiser, is_shared): |
paul@1261 | 576 | |
paul@1261 | 577 | """ |
paul@1268 | 578 | Classify the operations for the update of an event. Return the unscheduled |
paul@1268 | 579 | periods, rescheduled periods, excluded periods, and the periods to be set in |
paul@1268 | 580 | the object to replace the existing stored periods. |
paul@1261 | 581 | """ |
paul@1261 | 582 | |
paul@1268 | 583 | active_periods = new + unchanged + changed |
paul@1268 | 584 | |
paul@1268 | 585 | # As organiser... |
paul@1268 | 586 | |
paul@1268 | 587 | if is_organiser: |
paul@1268 | 588 | to_exclude = [] |
paul@1268 | 589 | |
paul@1268 | 590 | # For unshared events... |
paul@1268 | 591 | # All modifications redefine the event. |
paul@1261 | 592 | |
paul@1268 | 593 | # For shared events... |
paul@1268 | 594 | # New periods should cause the event to be redefined. |
paul@1261 | 595 | |
paul@1268 | 596 | if not is_shared or new: |
paul@1268 | 597 | to_unschedule = [] |
paul@1268 | 598 | to_reschedule = [] |
paul@1268 | 599 | to_set = active_periods |
paul@1268 | 600 | |
paul@1268 | 601 | # Changed periods should be rescheduled separately. |
paul@1268 | 602 | # Removed periods should be cancelled separately. |
paul@1261 | 603 | |
paul@1268 | 604 | else: |
paul@1268 | 605 | to_unschedule = removed |
paul@1268 | 606 | to_reschedule = changed |
paul@1268 | 607 | to_set = [] |
paul@1268 | 608 | |
paul@1268 | 609 | # As attendee... |
paul@1268 | 610 | |
paul@1268 | 611 | else: |
paul@1268 | 612 | to_unschedule = [] |
paul@1268 | 613 | |
paul@1268 | 614 | # Changed periods without new or removed periods are proposed as |
paul@1268 | 615 | # separate changes. |
paul@1261 | 616 | |
paul@1268 | 617 | if not new and not removed: |
paul@1268 | 618 | to_exclude = [] |
paul@1268 | 619 | to_reschedule = changed |
paul@1268 | 620 | to_set = [] |
paul@1261 | 621 | |
paul@1268 | 622 | # Otherwise, the event is defined in terms of new periods and |
paul@1268 | 623 | # exceptions for removed periods. |
paul@1267 | 624 | |
paul@1268 | 625 | else: |
paul@1268 | 626 | to_exclude = removed |
paul@1268 | 627 | to_reschedule = [] |
paul@1268 | 628 | to_set = active_periods |
paul@1268 | 629 | |
paul@1268 | 630 | return to_unschedule, to_reschedule, to_exclude, to_set |
paul@1261 | 631 | |
paul@1257 | 632 | |
paul@1257 | 633 | |
paul@1244 | 634 | # Form field extraction and serialisation. |
paul@1244 | 635 | |
paul@1246 | 636 | def get_date_control_inputs(args, name, tzid_name=None): |
paul@1246 | 637 | |
paul@1246 | 638 | """ |
paul@1246 | 639 | Return a tuple of date control inputs taken from 'args' for field names |
paul@1246 | 640 | starting with 'name'. |
paul@1246 | 641 | |
paul@1246 | 642 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1246 | 643 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1246 | 644 | """ |
paul@1246 | 645 | |
paul@1246 | 646 | return args.get("%s-date" % name, []), \ |
paul@1246 | 647 | args.get("%s-hour" % name, []), \ |
paul@1246 | 648 | args.get("%s-minute" % name, []), \ |
paul@1246 | 649 | args.get("%s-second" % name, []), \ |
paul@1246 | 650 | args.get("%s-tzid" % (tzid_name or name), []) |
paul@1246 | 651 | |
paul@1244 | 652 | def get_date_control_values(args, name, multiple=False, tzid_name=None, tzid=None): |
paul@1244 | 653 | |
paul@1244 | 654 | """ |
paul@1244 | 655 | Return a form date object representing fields taken from 'args' starting |
paul@1244 | 656 | with 'name'. |
paul@1244 | 657 | |
paul@1244 | 658 | If 'multiple' is set to a true value, many date objects will be returned |
paul@1244 | 659 | corresponding to a collection of datetimes. |
paul@1244 | 660 | |
paul@1244 | 661 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1244 | 662 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 663 | |
paul@1244 | 664 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 665 | time zone information is indicated in the field data. |
paul@1244 | 666 | """ |
paul@1244 | 667 | |
paul@1246 | 668 | dates, hours, minutes, seconds, tzids = get_date_control_inputs(args, name, tzid_name) |
paul@1244 | 669 | |
paul@1244 | 670 | # Handle absent values by employing None values. |
paul@1244 | 671 | |
paul@1244 | 672 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@1244 | 673 | |
paul@1244 | 674 | if not field_values and not multiple: |
paul@1244 | 675 | all_values = FormDate() |
paul@1244 | 676 | else: |
paul@1244 | 677 | all_values = [] |
paul@1244 | 678 | for date, hour, minute, second, tzid_field in field_values: |
paul@1244 | 679 | value = FormDate(date, hour, minute, second, tzid_field or tzid) |
paul@1244 | 680 | |
paul@1244 | 681 | # Return a single value or append to a collection of all values. |
paul@1244 | 682 | |
paul@1244 | 683 | if not multiple: |
paul@1244 | 684 | return value |
paul@1244 | 685 | else: |
paul@1244 | 686 | all_values.append(value) |
paul@1244 | 687 | |
paul@1244 | 688 | return all_values |
paul@1244 | 689 | |
paul@1245 | 690 | def set_date_control_values(formdates, args, name, tzid_name=None): |
paul@1244 | 691 | |
paul@1244 | 692 | """ |
paul@1245 | 693 | Using the values of the given 'formdates', replace form fields in 'args' |
paul@1245 | 694 | starting with 'name'. |
paul@1244 | 695 | |
paul@1244 | 696 | If 'tzid_name' is specified, the time zone information will be stored in |
paul@1244 | 697 | fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 698 | """ |
paul@1244 | 699 | |
paul@1244 | 700 | args["%s-date" % name] = [] |
paul@1244 | 701 | args["%s-hour" % name] = [] |
paul@1244 | 702 | args["%s-minute" % name] = [] |
paul@1244 | 703 | args["%s-second" % name] = [] |
paul@1244 | 704 | args["%s-tzid" % (tzid_name or name)] = [] |
paul@1244 | 705 | |
paul@1244 | 706 | for d in formdates: |
paul@1246 | 707 | args["%s-date" % name].append(d and d.date or "") |
paul@1246 | 708 | args["%s-hour" % name].append(d and d.hour or "") |
paul@1246 | 709 | args["%s-minute" % name].append(d and d.minute or "") |
paul@1246 | 710 | args["%s-second" % name].append(d and d.second or "") |
paul@1246 | 711 | args["%s-tzid" % (tzid_name or name)].append(d and d.tzid or "") |
paul@1244 | 712 | |
paul@1244 | 713 | def get_period_control_values(args, start_name, end_name, |
paul@1244 | 714 | end_enabled_name, times_enabled_name, |
paul@1244 | 715 | origin=None, origin_name=None, |
paul@1268 | 716 | replacement_name=None, cancelled_name=None, |
paul@1268 | 717 | recurrenceid_name=None, tzid=None): |
paul@1244 | 718 | |
paul@1244 | 719 | """ |
paul@1245 | 720 | Return period values from fields found in 'args' prefixed with the given |
paul@1244 | 721 | 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1244 | 722 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1244 | 723 | (to enable times for periods). |
paul@1244 | 724 | |
paul@1244 | 725 | If 'origin' is specified, a single period with the given origin is |
paul@1244 | 726 | returned. If 'origin_name' is specified, fields containing the name will |
paul@1268 | 727 | provide origin information. |
paul@1268 | 728 | |
paul@1268 | 729 | If specified, fields containing 'replacement_name' will indicate periods |
paul@1268 | 730 | provided by separate recurrences, fields containing 'cancelled_name' |
paul@1268 | 731 | will indicate periods that are replacements and cancelled, and fields |
paul@1268 | 732 | containing 'recurrenceid_name' will indicate periods that have existing |
paul@1268 | 733 | recurrence details from an event. |
paul@1244 | 734 | |
paul@1244 | 735 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 736 | time zone information is indicated in the field data. |
paul@1244 | 737 | """ |
paul@1244 | 738 | |
paul@1244 | 739 | # Get the end datetime and time presence settings. |
paul@1244 | 740 | |
paul@1244 | 741 | all_end_enabled = args.get(end_enabled_name, []) |
paul@1244 | 742 | all_times_enabled = args.get(times_enabled_name, []) |
paul@1244 | 743 | |
paul@1268 | 744 | # Get the origins of period data and whether the periods are replacements. |
paul@1244 | 745 | |
paul@1244 | 746 | if origin: |
paul@1244 | 747 | all_origins = [origin] |
paul@1244 | 748 | else: |
paul@1244 | 749 | all_origins = origin_name and args.get(origin_name, []) or [] |
paul@1244 | 750 | |
paul@1268 | 751 | all_replacements = replacement_name and args.get(replacement_name, []) or [] |
paul@1268 | 752 | all_cancelled = cancelled_name and args.get(cancelled_name, []) or [] |
paul@1266 | 753 | all_recurrenceids = recurrenceid_name and args.get(recurrenceid_name, []) or [] |
paul@1244 | 754 | |
paul@1244 | 755 | # Get the start and end datetimes. |
paul@1244 | 756 | |
paul@1244 | 757 | all_starts = get_date_control_values(args, start_name, True, tzid=tzid) |
paul@1244 | 758 | all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid) |
paul@1244 | 759 | |
paul@1245 | 760 | # Construct period objects for each start, end, origin combination. |
paul@1245 | 761 | |
paul@1244 | 762 | periods = [] |
paul@1244 | 763 | |
paul@1266 | 764 | for index, (start, end, found_origin, recurrenceid) in \ |
paul@1266 | 765 | enumerate(map(None, all_starts, all_ends, all_origins, all_recurrenceids)): |
paul@1244 | 766 | |
paul@1245 | 767 | # Obtain period settings from separate controls. |
paul@1245 | 768 | |
paul@1244 | 769 | end_enabled = str(index) in all_end_enabled |
paul@1244 | 770 | times_enabled = str(index) in all_times_enabled |
paul@1268 | 771 | replacement = str(index) in all_replacements |
paul@1268 | 772 | cancelled = str(index) in all_cancelled |
paul@1244 | 773 | |
paul@1244 | 774 | period = FormPeriod(start, end, end_enabled, times_enabled, tzid, |
paul@1268 | 775 | found_origin or origin, replacement, cancelled, |
paul@1268 | 776 | recurrenceid) |
paul@1244 | 777 | periods.append(period) |
paul@1244 | 778 | |
paul@1244 | 779 | # Return a single period if a single origin was specified. |
paul@1244 | 780 | |
paul@1244 | 781 | if origin: |
paul@1244 | 782 | return periods[0] |
paul@1244 | 783 | else: |
paul@1244 | 784 | return periods |
paul@1244 | 785 | |
paul@1245 | 786 | def set_period_control_values(periods, args, start_name, end_name, |
paul@1245 | 787 | end_enabled_name, times_enabled_name, |
paul@1268 | 788 | origin_name=None, replacement_name=None, |
paul@1268 | 789 | cancelled_name=None, recurrenceid_name=None): |
paul@1245 | 790 | |
paul@1245 | 791 | """ |
paul@1245 | 792 | Using the given 'periods', replace form fields in 'args' prefixed with the |
paul@1245 | 793 | given 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1245 | 794 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1245 | 795 | (to enable times for periods). |
paul@1245 | 796 | |
paul@1245 | 797 | If 'origin_name' is specified, fields containing the name will provide |
paul@1268 | 798 | origin information, fields containing 'replacement_name' will indicate |
paul@1268 | 799 | periods provided by separate recurrences, fields containing 'cancelled_name' |
paul@1268 | 800 | will indicate periods that are replacements and cancelled, and fields |
paul@1268 | 801 | containing 'recurrenceid_name' will indicate periods that have existing |
paul@1268 | 802 | recurrence details from an event. |
paul@1245 | 803 | """ |
paul@1245 | 804 | |
paul@1245 | 805 | # Record period settings separately. |
paul@1245 | 806 | |
paul@1245 | 807 | args[end_enabled_name] = [] |
paul@1245 | 808 | args[times_enabled_name] = [] |
paul@1245 | 809 | |
paul@1245 | 810 | # Record origin and replacement information if naming is defined. |
paul@1245 | 811 | |
paul@1245 | 812 | if origin_name: |
paul@1245 | 813 | args[origin_name] = [] |
paul@1245 | 814 | |
paul@1268 | 815 | if replacement_name: |
paul@1268 | 816 | args[replacement_name] = [] |
paul@1268 | 817 | |
paul@1268 | 818 | if cancelled_name: |
paul@1268 | 819 | args[cancelled_name] = [] |
paul@1245 | 820 | |
paul@1266 | 821 | if recurrenceid_name: |
paul@1266 | 822 | args[recurrenceid_name] = [] |
paul@1266 | 823 | |
paul@1245 | 824 | all_starts = [] |
paul@1245 | 825 | all_ends = [] |
paul@1245 | 826 | |
paul@1245 | 827 | for index, period in enumerate(periods): |
paul@1245 | 828 | |
paul@1245 | 829 | # Encode period settings in controls. |
paul@1245 | 830 | |
paul@1245 | 831 | if period.end_enabled: |
paul@1245 | 832 | args[end_enabled_name].append(str(index)) |
paul@1245 | 833 | if period.times_enabled: |
paul@1245 | 834 | args[times_enabled_name].append(str(index)) |
paul@1245 | 835 | |
paul@1245 | 836 | # Add origin information where controls are present to record it. |
paul@1245 | 837 | |
paul@1245 | 838 | if origin_name: |
paul@1245 | 839 | args[origin_name].append(period.origin or "") |
paul@1245 | 840 | |
paul@1245 | 841 | # Add replacement information where controls are present to record it. |
paul@1245 | 842 | |
paul@1268 | 843 | if replacement_name and period.replacement: |
paul@1268 | 844 | args[replacement_name].append(str(index)) |
paul@1268 | 845 | |
paul@1268 | 846 | # Add cancelled recurrence information where controls are present to |
paul@1268 | 847 | # record it. |
paul@1268 | 848 | |
paul@1268 | 849 | if cancelled_name and period.cancelled: |
paul@1268 | 850 | args[cancelled_name].append(str(index)) |
paul@1245 | 851 | |
paul@1266 | 852 | # Add recurrence identifiers where controls are present to record it. |
paul@1266 | 853 | |
paul@1266 | 854 | if recurrenceid_name: |
paul@1266 | 855 | args[recurrenceid_name].append(period.recurrenceid or "") |
paul@1266 | 856 | |
paul@1245 | 857 | # Collect form date information for addition below. |
paul@1245 | 858 | |
paul@1245 | 859 | all_starts.append(period.get_form_start()) |
paul@1245 | 860 | all_ends.append(period.get_form_end()) |
paul@1245 | 861 | |
paul@1245 | 862 | # Set the controls for the dates. |
paul@1245 | 863 | |
paul@1245 | 864 | set_date_control_values(all_starts, args, start_name) |
paul@1245 | 865 | set_date_control_values(all_ends, args, end_name, tzid_name=start_name) |
paul@1245 | 866 | |
paul@1257 | 867 | |
paul@1257 | 868 | |
paul@1257 | 869 | # Utilities. |
paul@1257 | 870 | |
paul@1257 | 871 | def filter_duplicates(l): |
paul@1257 | 872 | |
paul@1257 | 873 | """ |
paul@1257 | 874 | Return collection 'l' filtered for duplicate values, retaining the given |
paul@1257 | 875 | element ordering. |
paul@1257 | 876 | """ |
paul@1257 | 877 | |
paul@1257 | 878 | s = set() |
paul@1257 | 879 | f = [] |
paul@1257 | 880 | |
paul@1257 | 881 | for value in l: |
paul@1257 | 882 | if value not in s: |
paul@1257 | 883 | s.add(value) |
paul@1257 | 884 | f.append(value) |
paul@1257 | 885 | |
paul@1257 | 886 | return f |
paul@1257 | 887 | |
paul@1257 | 888 | def remove_from_collection(l, indexes, fn): |
paul@1257 | 889 | |
paul@1257 | 890 | """ |
paul@1257 | 891 | Remove from collection 'l' all values present at the given 'indexes' where |
paul@1257 | 892 | 'fn' applied to each referenced value returns a true value. Values where |
paul@1257 | 893 | 'fn' returns a false value are added to a list of deferred removals which is |
paul@1257 | 894 | returned. |
paul@1257 | 895 | """ |
paul@1257 | 896 | |
paul@1257 | 897 | still_to_remove = [] |
paul@1257 | 898 | correction = 0 |
paul@1257 | 899 | |
paul@1257 | 900 | for i in indexes: |
paul@1257 | 901 | try: |
paul@1257 | 902 | i = int(i) - correction |
paul@1257 | 903 | value = l[i] |
paul@1257 | 904 | except (IndexError, ValueError): |
paul@1257 | 905 | continue |
paul@1257 | 906 | |
paul@1257 | 907 | if fn(value): |
paul@1257 | 908 | del l[i] |
paul@1257 | 909 | correction += 1 |
paul@1257 | 910 | else: |
paul@1258 | 911 | still_to_remove.append(value) |
paul@1257 | 912 | |
paul@1257 | 913 | return still_to_remove |
paul@1257 | 914 | |
paul@497 | 915 | # vim: tabstop=4 expandtab shiftwidth=4 |