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