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