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@538 | 24 | format_datetime, get_datetime, get_end_of_day, \ |
paul@532 | 25 | to_date |
paul@621 | 26 | from imiptools.period import RecurringPeriod |
paul@497 | 27 | |
paul@1299 | 28 | # General editing abstractions. |
paul@1299 | 29 | |
paul@1299 | 30 | class State: |
paul@1299 | 31 | |
paul@1299 | 32 | "Manage computed state." |
paul@1299 | 33 | |
paul@1299 | 34 | def __init__(self, callables): |
paul@1299 | 35 | |
paul@1299 | 36 | """ |
paul@1299 | 37 | Define state variable initialisation using the given 'callables', which |
paul@1299 | 38 | is a mapping that defines a callable for each variable name that is |
paul@1299 | 39 | invoked when the variable is first requested. |
paul@1299 | 40 | """ |
paul@1299 | 41 | |
paul@1299 | 42 | self.state = {} |
paul@1299 | 43 | self.callables = callables |
paul@1299 | 44 | |
paul@1299 | 45 | def get_callable(self, key): |
paul@1299 | 46 | return self.callables.get(key, lambda: None) |
paul@1299 | 47 | |
paul@1299 | 48 | def get(self, key, reset=False): |
paul@1299 | 49 | |
paul@1299 | 50 | """ |
paul@1299 | 51 | Return state for the given 'key', using the configured callable to |
paul@1299 | 52 | compute and set the state if no state is already defined. |
paul@1299 | 53 | |
paul@1299 | 54 | If 'reset' is set to a true value, compute and return the state using |
paul@1299 | 55 | the configured callable regardless of any existing state. |
paul@1299 | 56 | """ |
paul@1299 | 57 | |
paul@1299 | 58 | if reset or not self.state.has_key(key): |
paul@1299 | 59 | self.state[key] = self.get_callable(key)() |
paul@1299 | 60 | |
paul@1299 | 61 | return self.state[key] |
paul@1299 | 62 | |
paul@1299 | 63 | def set(self, key, value): |
paul@1299 | 64 | self.state[key] = value |
paul@1299 | 65 | |
paul@1299 | 66 | def __getitem__(self, key): |
paul@1299 | 67 | return self.get(key) |
paul@1299 | 68 | |
paul@1299 | 69 | def __setitem__(self, key, value): |
paul@1299 | 70 | self.set(key, value) |
paul@1299 | 71 | |
paul@1299 | 72 | def has_changed(self, key): |
paul@1299 | 73 | return self.get_callable(key)() != self.get(key) |
paul@1299 | 74 | |
paul@1299 | 75 | |
paul@1299 | 76 | |
paul@1299 | 77 | # Period-related abstractions. |
paul@1299 | 78 | |
paul@498 | 79 | class PeriodError(Exception): |
paul@498 | 80 | pass |
paul@498 | 81 | |
paul@539 | 82 | class EventPeriod(RecurringPeriod): |
paul@497 | 83 | |
paul@498 | 84 | """ |
paul@498 | 85 | A simple period plus attribute details, compatible with RecurringPeriod, and |
paul@498 | 86 | intended to represent information obtained from an iCalendar resource. |
paul@498 | 87 | """ |
paul@497 | 88 | |
paul@1246 | 89 | def __init__(self, start, end, tzid=None, origin=None, start_attr=None, |
paul@1246 | 90 | end_attr=None, form_start=None, form_end=None, replaced=False): |
paul@528 | 91 | |
paul@528 | 92 | """ |
paul@528 | 93 | Initialise a period with the given 'start' and 'end' datetimes, together |
paul@528 | 94 | with optional 'start_attr' and 'end_attr' metadata, 'form_start' and |
paul@528 | 95 | 'form_end' values provided as textual input, and with an optional |
paul@528 | 96 | 'origin' indicating the kind of period this object describes. |
paul@528 | 97 | """ |
paul@528 | 98 | |
paul@541 | 99 | RecurringPeriod.__init__(self, start, end, tzid, origin, start_attr, end_attr) |
paul@498 | 100 | self.form_start = form_start |
paul@498 | 101 | self.form_end = form_end |
paul@868 | 102 | self.replaced = replaced |
paul@498 | 103 | |
paul@498 | 104 | def as_tuple(self): |
paul@1246 | 105 | return self.start, self.end, self.tzid, self.origin, self.start_attr, \ |
paul@1246 | 106 | self.end_attr, self.form_start, self.form_end, self.replaced |
paul@498 | 107 | |
paul@498 | 108 | def __repr__(self): |
paul@833 | 109 | return "EventPeriod%r" % (self.as_tuple(),) |
paul@499 | 110 | |
paul@499 | 111 | def as_event_period(self): |
paul@499 | 112 | return self |
paul@499 | 113 | |
paul@700 | 114 | def get_start_item(self): |
paul@700 | 115 | return self.get_start(), self.get_start_attr() |
paul@700 | 116 | |
paul@700 | 117 | def get_end_item(self): |
paul@700 | 118 | return self.get_end(), self.get_end_attr() |
paul@700 | 119 | |
paul@499 | 120 | # Form data compatibility methods. |
paul@498 | 121 | |
paul@498 | 122 | def get_form_start(self): |
paul@498 | 123 | if not self.form_start: |
paul@499 | 124 | self.form_start = self.get_form_date(self.get_start(), self.start_attr) |
paul@498 | 125 | return self.form_start |
paul@498 | 126 | |
paul@498 | 127 | def get_form_end(self): |
paul@498 | 128 | if not self.form_end: |
paul@539 | 129 | self.form_end = self.get_form_date(end_date_from_calendar(self.get_end()), self.end_attr) |
paul@498 | 130 | return self.form_end |
paul@498 | 131 | |
paul@498 | 132 | def as_form_period(self): |
paul@498 | 133 | return FormPeriod( |
paul@499 | 134 | self.get_form_start(), |
paul@499 | 135 | self.get_form_end(), |
paul@556 | 136 | isinstance(self.end, datetime) or self.get_start() != self.get_end() - timedelta(1), |
paul@532 | 137 | isinstance(self.start, datetime) or isinstance(self.end, datetime), |
paul@541 | 138 | self.tzid, |
paul@868 | 139 | self.origin, |
paul@1266 | 140 | self.replaced and True or False, |
paul@1266 | 141 | format_datetime(self.get_start_point()) |
paul@498 | 142 | ) |
paul@498 | 143 | |
paul@498 | 144 | def get_form_date(self, dt, attr=None): |
paul@498 | 145 | return FormDate( |
paul@498 | 146 | format_datetime(to_date(dt)), |
paul@498 | 147 | isinstance(dt, datetime) and str(dt.hour) or None, |
paul@498 | 148 | isinstance(dt, datetime) and str(dt.minute) or None, |
paul@498 | 149 | isinstance(dt, datetime) and str(dt.second) or None, |
paul@498 | 150 | attr and attr.get("TZID") or None, |
paul@498 | 151 | dt, attr |
paul@498 | 152 | ) |
paul@498 | 153 | |
paul@620 | 154 | class FormPeriod(RecurringPeriod): |
paul@498 | 155 | |
paul@498 | 156 | "A period whose information originates from a form." |
paul@498 | 157 | |
paul@1246 | 158 | def __init__(self, start, end, end_enabled=True, times_enabled=True, |
paul@1266 | 159 | tzid=None, origin=None, replaced=False, recurrenceid=None): |
paul@498 | 160 | self.start = start |
paul@498 | 161 | self.end = end |
paul@498 | 162 | self.end_enabled = end_enabled |
paul@498 | 163 | self.times_enabled = times_enabled |
paul@541 | 164 | self.tzid = tzid |
paul@499 | 165 | self.origin = origin |
paul@868 | 166 | self.replaced = replaced |
paul@1266 | 167 | self.recurrenceid = recurrenceid |
paul@497 | 168 | |
paul@497 | 169 | def as_tuple(self): |
paul@1266 | 170 | return self.start, self.end, self.end_enabled, self.times_enabled, self.tzid, self.origin, self.replaced, self.recurrenceid |
paul@497 | 171 | |
paul@497 | 172 | def __repr__(self): |
paul@833 | 173 | return "FormPeriod%r" % (self.as_tuple(),) |
paul@498 | 174 | |
paul@1266 | 175 | def is_changed(self): |
paul@1266 | 176 | return not self.recurrenceid or format_datetime(self.get_start_point()) != self.recurrenceid |
paul@1253 | 177 | |
paul@499 | 178 | def as_event_period(self, index=None): |
paul@528 | 179 | |
paul@528 | 180 | """ |
paul@528 | 181 | Return a converted version of this object as an event period suitable |
paul@528 | 182 | for iCalendar usage. If 'index' is indicated, include it in any error |
paul@528 | 183 | raised in the conversion process. |
paul@528 | 184 | """ |
paul@528 | 185 | |
paul@700 | 186 | dtstart, dtstart_attr = self.get_start_item() |
paul@528 | 187 | if not dtstart: |
paul@1251 | 188 | if index is not None: |
paul@1251 | 189 | raise PeriodError(("dtstart", index)) |
paul@1251 | 190 | else: |
paul@1251 | 191 | raise PeriodError("dtstart") |
paul@499 | 192 | |
paul@700 | 193 | dtend, dtend_attr = self.get_end_item() |
paul@528 | 194 | if not dtend: |
paul@1251 | 195 | if index is not None: |
paul@1251 | 196 | raise PeriodError(("dtend", index)) |
paul@1251 | 197 | else: |
paul@1251 | 198 | raise PeriodError("dtend") |
paul@499 | 199 | |
paul@499 | 200 | if dtstart > dtend: |
paul@1251 | 201 | if index is not None: |
paul@1251 | 202 | raise PeriodError(("dtstart", index), ("dtend", index)) |
paul@1251 | 203 | else: |
paul@1251 | 204 | raise PeriodError("dtstart", "dtend") |
paul@499 | 205 | |
paul@1246 | 206 | return EventPeriod(dtstart, end_date_to_calendar(dtend), self.tzid, |
paul@1246 | 207 | self.origin, dtstart_attr, dtend_attr, |
paul@1246 | 208 | self.start, self.end, self.replaced) |
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@498 | 280 | def __repr__(self): |
paul@833 | 281 | return "FormDate%r" % (self.as_tuple(),) |
paul@498 | 282 | |
paul@498 | 283 | def get_component(self, value): |
paul@498 | 284 | return (value or "").rjust(2, "0")[:2] |
paul@498 | 285 | |
paul@498 | 286 | def get_hour(self): |
paul@498 | 287 | return self.get_component(self.hour) |
paul@498 | 288 | |
paul@498 | 289 | def get_minute(self): |
paul@498 | 290 | return self.get_component(self.minute) |
paul@498 | 291 | |
paul@498 | 292 | def get_second(self): |
paul@498 | 293 | return self.get_component(self.second) |
paul@498 | 294 | |
paul@498 | 295 | def get_date_string(self): |
paul@498 | 296 | return self.date or "" |
paul@498 | 297 | |
paul@498 | 298 | def get_datetime_string(self): |
paul@498 | 299 | if not self.date: |
paul@498 | 300 | return "" |
paul@498 | 301 | |
paul@498 | 302 | hour = self.hour; minute = self.minute; second = self.second |
paul@498 | 303 | |
paul@498 | 304 | if hour or minute or second: |
paul@498 | 305 | time = "T%s%s%s" % tuple(map(self.get_component, (hour, minute, second))) |
paul@498 | 306 | else: |
paul@498 | 307 | time = "" |
paul@498 | 308 | |
paul@498 | 309 | return "%s%s" % (self.date, time) |
paul@498 | 310 | |
paul@498 | 311 | def get_tzid(self): |
paul@498 | 312 | return self.tzid |
paul@498 | 313 | |
paul@528 | 314 | def as_datetime(self, with_time=True): |
paul@498 | 315 | |
paul@1313 | 316 | """ |
paul@1313 | 317 | Return a datetime for this object if one is provided or can be produced. |
paul@1313 | 318 | """ |
paul@498 | 319 | |
paul@498 | 320 | # Return any original datetime details. |
paul@498 | 321 | |
paul@498 | 322 | if self.dt: |
paul@528 | 323 | return self.dt |
paul@498 | 324 | |
paul@528 | 325 | # Otherwise, construct a datetime. |
paul@498 | 326 | |
paul@528 | 327 | s, attr = self.as_datetime_item(with_time) |
paul@1313 | 328 | if not s: |
paul@1313 | 329 | return None |
paul@1313 | 330 | |
paul@1313 | 331 | # An erroneous datetime will yield None as result. |
paul@1313 | 332 | |
paul@1313 | 333 | try: |
paul@528 | 334 | return get_datetime(s, attr) |
paul@1313 | 335 | except ValueError: |
paul@528 | 336 | return None |
paul@528 | 337 | |
paul@528 | 338 | def as_datetime_item(self, with_time=True): |
paul@498 | 339 | |
paul@528 | 340 | """ |
paul@528 | 341 | Return a (datetime string, attr) tuple for the datetime information |
paul@528 | 342 | provided by this object, where both tuple elements will be None if no |
paul@528 | 343 | suitable date or datetime information exists. |
paul@528 | 344 | """ |
paul@498 | 345 | |
paul@528 | 346 | s = None |
paul@528 | 347 | if with_time: |
paul@528 | 348 | s = self.get_datetime_string() |
paul@528 | 349 | attr = self.get_attributes(True) |
paul@528 | 350 | if not s: |
paul@528 | 351 | s = self.get_date_string() |
paul@528 | 352 | attr = self.get_attributes(False) |
paul@528 | 353 | if not s: |
paul@528 | 354 | return None, None |
paul@528 | 355 | return s, attr |
paul@498 | 356 | |
paul@528 | 357 | def get_attributes(self, with_time=True): |
paul@528 | 358 | |
paul@528 | 359 | "Return attributes for the date or datetime represented by this object." |
paul@498 | 360 | |
paul@528 | 361 | if with_time: |
paul@528 | 362 | return {"TZID" : self.get_tzid(), "VALUE" : "DATE-TIME"} |
paul@528 | 363 | else: |
paul@528 | 364 | return {"VALUE" : "DATE"} |
paul@498 | 365 | |
paul@499 | 366 | def event_period_from_period(period): |
paul@624 | 367 | |
paul@624 | 368 | """ |
paul@624 | 369 | Convert a 'period' to one suitable for use in an iCalendar representation. |
paul@624 | 370 | In an "event period" representation, the end day of any date-level event is |
paul@624 | 371 | encoded as the "day after" the last day actually involved in the event. |
paul@624 | 372 | """ |
paul@624 | 373 | |
paul@499 | 374 | if isinstance(period, EventPeriod): |
paul@499 | 375 | return period |
paul@499 | 376 | elif isinstance(period, FormPeriod): |
paul@499 | 377 | return period.as_event_period() |
paul@499 | 378 | else: |
paul@528 | 379 | dtstart, dtstart_attr = period.get_start_item() |
paul@528 | 380 | dtend, dtend_attr = period.get_end_item() |
paul@539 | 381 | if not isinstance(period, RecurringPeriod): |
paul@539 | 382 | dtend = end_date_to_calendar(dtend) |
paul@541 | 383 | return EventPeriod(dtstart, dtend, period.tzid, period.origin, dtstart_attr, dtend_attr) |
paul@499 | 384 | |
paul@499 | 385 | def form_period_from_period(period): |
paul@624 | 386 | |
paul@624 | 387 | """ |
paul@624 | 388 | Convert a 'period' into a representation usable in a user-editable form. |
paul@624 | 389 | In a "form period" representation, the end day of any date-level event is |
paul@624 | 390 | presented in a "natural" form, not the iCalendar "day after" form. |
paul@624 | 391 | """ |
paul@624 | 392 | |
paul@499 | 393 | if isinstance(period, EventPeriod): |
paul@499 | 394 | return period.as_form_period() |
paul@499 | 395 | elif isinstance(period, FormPeriod): |
paul@499 | 396 | return period |
paul@499 | 397 | else: |
paul@499 | 398 | return event_period_from_period(period).as_form_period() |
paul@499 | 399 | |
paul@1246 | 400 | |
paul@1246 | 401 | |
paul@1257 | 402 | # Form period processing. |
paul@1257 | 403 | |
paul@1267 | 404 | def get_existing_periods(periods, still_to_remove): |
paul@1257 | 405 | |
paul@1267 | 406 | """ |
paul@1267 | 407 | Find all periods that existed before editing, given 'periods', applying |
paul@1267 | 408 | the periods in 'still_to_remove' and producing retained, replaced and |
paul@1267 | 409 | to-remove collections containing these existing periods. |
paul@1267 | 410 | """ |
paul@1257 | 411 | |
paul@1267 | 412 | retained = [] |
paul@1267 | 413 | replaced = [] |
paul@1267 | 414 | to_remove = [] |
paul@1267 | 415 | |
paul@1257 | 416 | for p in periods: |
paul@1267 | 417 | p = form_period_from_period(p) |
paul@1267 | 418 | if p.recurrenceid: |
paul@1267 | 419 | if p.replaced: |
paul@1267 | 420 | replaced.append(p) |
paul@1267 | 421 | elif p in still_to_remove: |
paul@1267 | 422 | to_remove.append(p) |
paul@1257 | 423 | else: |
paul@1267 | 424 | retained.append(p) |
paul@1267 | 425 | |
paul@1267 | 426 | return retained, replaced, to_remove |
paul@1267 | 427 | |
paul@1267 | 428 | def get_new_periods(periods): |
paul@1267 | 429 | |
paul@1267 | 430 | "Return all periods introduced during editing, given 'periods'." |
paul@1267 | 431 | |
paul@1267 | 432 | new = [] |
paul@1267 | 433 | for p in periods: |
paul@1267 | 434 | fp = form_period_from_period(p) |
paul@1267 | 435 | if not fp.recurrenceid: |
paul@1267 | 436 | new.append(p) |
paul@1267 | 437 | return new |
paul@1257 | 438 | |
paul@1267 | 439 | def get_changed_periods(periods): |
paul@1267 | 440 | |
paul@1267 | 441 | "Return changed and unchanged periods, given 'periods'." |
paul@1267 | 442 | |
paul@1267 | 443 | changed = [] |
paul@1267 | 444 | unchanged = [] |
paul@1267 | 445 | |
paul@1267 | 446 | for p in periods: |
paul@1267 | 447 | fp = form_period_from_period(p) |
paul@1267 | 448 | if fp.is_changed(): |
paul@1267 | 449 | changed.append(p) |
paul@1267 | 450 | else: |
paul@1267 | 451 | unchanged.append(p) |
paul@1267 | 452 | |
paul@1267 | 453 | return changed, unchanged |
paul@1267 | 454 | |
paul@1267 | 455 | def classify_periods(periods, still_to_remove): |
paul@1261 | 456 | |
paul@1261 | 457 | """ |
paul@1261 | 458 | From the recurrence 'periods', given details of those 'still_to_remove', |
paul@1267 | 459 | return a tuple containing collections of new, changed, unchanged, replaced |
paul@1267 | 460 | and to-be-removed periods. |
paul@1261 | 461 | """ |
paul@1261 | 462 | |
paul@1267 | 463 | retained, replaced, to_remove = get_existing_periods(periods, still_to_remove) |
paul@1261 | 464 | |
paul@1267 | 465 | # Filter new periods with the existing period information. |
paul@1261 | 466 | |
paul@1267 | 467 | new = set(get_new_periods(periods)) |
paul@1261 | 468 | |
paul@1267 | 469 | new.difference_update(retained) |
paul@1267 | 470 | new.difference_update(replaced) |
paul@1267 | 471 | new.difference_update(to_remove) |
paul@1261 | 472 | |
paul@1267 | 473 | # Divide retained periods into changed and unchanged collections. |
paul@1261 | 474 | |
paul@1267 | 475 | changed, unchanged = get_changed_periods(retained) |
paul@1267 | 476 | |
paul@1267 | 477 | return list(new), changed, unchanged, replaced, to_remove |
paul@1261 | 478 | |
paul@1257 | 479 | |
paul@1257 | 480 | |
paul@1244 | 481 | # Form field extraction and serialisation. |
paul@1244 | 482 | |
paul@1246 | 483 | def get_date_control_inputs(args, name, tzid_name=None): |
paul@1246 | 484 | |
paul@1246 | 485 | """ |
paul@1246 | 486 | Return a tuple of date control inputs taken from 'args' for field names |
paul@1246 | 487 | starting with 'name'. |
paul@1246 | 488 | |
paul@1246 | 489 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1246 | 490 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1246 | 491 | """ |
paul@1246 | 492 | |
paul@1246 | 493 | return args.get("%s-date" % name, []), \ |
paul@1246 | 494 | args.get("%s-hour" % name, []), \ |
paul@1246 | 495 | args.get("%s-minute" % name, []), \ |
paul@1246 | 496 | args.get("%s-second" % name, []), \ |
paul@1246 | 497 | args.get("%s-tzid" % (tzid_name or name), []) |
paul@1246 | 498 | |
paul@1244 | 499 | def get_date_control_values(args, name, multiple=False, tzid_name=None, tzid=None): |
paul@1244 | 500 | |
paul@1244 | 501 | """ |
paul@1244 | 502 | Return a form date object representing fields taken from 'args' starting |
paul@1244 | 503 | with 'name'. |
paul@1244 | 504 | |
paul@1244 | 505 | If 'multiple' is set to a true value, many date objects will be returned |
paul@1244 | 506 | corresponding to a collection of datetimes. |
paul@1244 | 507 | |
paul@1244 | 508 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1244 | 509 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 510 | |
paul@1244 | 511 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 512 | time zone information is indicated in the field data. |
paul@1244 | 513 | """ |
paul@1244 | 514 | |
paul@1246 | 515 | dates, hours, minutes, seconds, tzids = get_date_control_inputs(args, name, tzid_name) |
paul@1244 | 516 | |
paul@1244 | 517 | # Handle absent values by employing None values. |
paul@1244 | 518 | |
paul@1244 | 519 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@1244 | 520 | |
paul@1244 | 521 | if not field_values and not multiple: |
paul@1244 | 522 | all_values = FormDate() |
paul@1244 | 523 | else: |
paul@1244 | 524 | all_values = [] |
paul@1244 | 525 | for date, hour, minute, second, tzid_field in field_values: |
paul@1244 | 526 | value = FormDate(date, hour, minute, second, tzid_field or tzid) |
paul@1244 | 527 | |
paul@1244 | 528 | # Return a single value or append to a collection of all values. |
paul@1244 | 529 | |
paul@1244 | 530 | if not multiple: |
paul@1244 | 531 | return value |
paul@1244 | 532 | else: |
paul@1244 | 533 | all_values.append(value) |
paul@1244 | 534 | |
paul@1244 | 535 | return all_values |
paul@1244 | 536 | |
paul@1245 | 537 | def set_date_control_values(formdates, args, name, tzid_name=None): |
paul@1244 | 538 | |
paul@1244 | 539 | """ |
paul@1245 | 540 | Using the values of the given 'formdates', replace form fields in 'args' |
paul@1245 | 541 | starting with 'name'. |
paul@1244 | 542 | |
paul@1244 | 543 | If 'tzid_name' is specified, the time zone information will be stored in |
paul@1244 | 544 | fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 545 | """ |
paul@1244 | 546 | |
paul@1244 | 547 | args["%s-date" % name] = [] |
paul@1244 | 548 | args["%s-hour" % name] = [] |
paul@1244 | 549 | args["%s-minute" % name] = [] |
paul@1244 | 550 | args["%s-second" % name] = [] |
paul@1244 | 551 | args["%s-tzid" % (tzid_name or name)] = [] |
paul@1244 | 552 | |
paul@1244 | 553 | for d in formdates: |
paul@1246 | 554 | args["%s-date" % name].append(d and d.date or "") |
paul@1246 | 555 | args["%s-hour" % name].append(d and d.hour or "") |
paul@1246 | 556 | args["%s-minute" % name].append(d and d.minute or "") |
paul@1246 | 557 | args["%s-second" % name].append(d and d.second or "") |
paul@1246 | 558 | args["%s-tzid" % (tzid_name or name)].append(d and d.tzid or "") |
paul@1244 | 559 | |
paul@1244 | 560 | def get_period_control_values(args, start_name, end_name, |
paul@1244 | 561 | end_enabled_name, times_enabled_name, |
paul@1244 | 562 | origin=None, origin_name=None, |
paul@1266 | 563 | replaced_name=None, recurrenceid_name=None, |
paul@1266 | 564 | tzid=None): |
paul@1244 | 565 | |
paul@1244 | 566 | """ |
paul@1245 | 567 | Return period values from fields found in 'args' prefixed with the given |
paul@1244 | 568 | 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1244 | 569 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1244 | 570 | (to enable times for periods). |
paul@1244 | 571 | |
paul@1244 | 572 | If 'origin' is specified, a single period with the given origin is |
paul@1244 | 573 | returned. If 'origin_name' is specified, fields containing the name will |
paul@1266 | 574 | provide origin information, fields containing 'replaced_name' will indicate |
paul@1266 | 575 | periods that are replaced, and fields containing 'recurrenceid_name' will |
paul@1266 | 576 | indicate periods that have existing recurrence details from an event. |
paul@1244 | 577 | |
paul@1244 | 578 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 579 | time zone information is indicated in the field data. |
paul@1244 | 580 | """ |
paul@1244 | 581 | |
paul@1244 | 582 | # Get the end datetime and time presence settings. |
paul@1244 | 583 | |
paul@1244 | 584 | all_end_enabled = args.get(end_enabled_name, []) |
paul@1244 | 585 | all_times_enabled = args.get(times_enabled_name, []) |
paul@1244 | 586 | |
paul@1244 | 587 | # Get the origins of period data and whether the periods are replaced. |
paul@1244 | 588 | |
paul@1244 | 589 | if origin: |
paul@1244 | 590 | all_origins = [origin] |
paul@1244 | 591 | else: |
paul@1244 | 592 | all_origins = origin_name and args.get(origin_name, []) or [] |
paul@1244 | 593 | |
paul@1244 | 594 | all_replaced = replaced_name and args.get(replaced_name, []) or [] |
paul@1266 | 595 | all_recurrenceids = recurrenceid_name and args.get(recurrenceid_name, []) or [] |
paul@1244 | 596 | |
paul@1244 | 597 | # Get the start and end datetimes. |
paul@1244 | 598 | |
paul@1244 | 599 | all_starts = get_date_control_values(args, start_name, True, tzid=tzid) |
paul@1244 | 600 | all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid) |
paul@1244 | 601 | |
paul@1245 | 602 | # Construct period objects for each start, end, origin combination. |
paul@1245 | 603 | |
paul@1244 | 604 | periods = [] |
paul@1244 | 605 | |
paul@1266 | 606 | for index, (start, end, found_origin, recurrenceid) in \ |
paul@1266 | 607 | enumerate(map(None, all_starts, all_ends, all_origins, all_recurrenceids)): |
paul@1244 | 608 | |
paul@1245 | 609 | # Obtain period settings from separate controls. |
paul@1245 | 610 | |
paul@1244 | 611 | end_enabled = str(index) in all_end_enabled |
paul@1244 | 612 | times_enabled = str(index) in all_times_enabled |
paul@1244 | 613 | replaced = str(index) in all_replaced |
paul@1244 | 614 | |
paul@1244 | 615 | period = FormPeriod(start, end, end_enabled, times_enabled, tzid, |
paul@1266 | 616 | found_origin or origin, replaced, recurrenceid) |
paul@1244 | 617 | periods.append(period) |
paul@1244 | 618 | |
paul@1244 | 619 | # Return a single period if a single origin was specified. |
paul@1244 | 620 | |
paul@1244 | 621 | if origin: |
paul@1244 | 622 | return periods[0] |
paul@1244 | 623 | else: |
paul@1244 | 624 | return periods |
paul@1244 | 625 | |
paul@1245 | 626 | def set_period_control_values(periods, args, start_name, end_name, |
paul@1245 | 627 | end_enabled_name, times_enabled_name, |
paul@1266 | 628 | origin_name=None, replaced_name=None, |
paul@1266 | 629 | recurrenceid_name=None): |
paul@1245 | 630 | |
paul@1245 | 631 | """ |
paul@1245 | 632 | Using the given 'periods', replace form fields in 'args' prefixed with the |
paul@1245 | 633 | given 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1245 | 634 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1245 | 635 | (to enable times for periods). |
paul@1245 | 636 | |
paul@1245 | 637 | If 'origin_name' is specified, fields containing the name will provide |
paul@1266 | 638 | origin information, fields containing 'replaced_name' will indicate periods |
paul@1266 | 639 | that are replaced, and fields containing 'recurrenceid_name' will indicate |
paul@1266 | 640 | periods that have existing recurrence details from an event. |
paul@1245 | 641 | """ |
paul@1245 | 642 | |
paul@1245 | 643 | # Record period settings separately. |
paul@1245 | 644 | |
paul@1245 | 645 | args[end_enabled_name] = [] |
paul@1245 | 646 | args[times_enabled_name] = [] |
paul@1245 | 647 | |
paul@1245 | 648 | # Record origin and replacement information if naming is defined. |
paul@1245 | 649 | |
paul@1245 | 650 | if origin_name: |
paul@1245 | 651 | args[origin_name] = [] |
paul@1245 | 652 | |
paul@1245 | 653 | if replaced_name: |
paul@1245 | 654 | args[replaced_name] = [] |
paul@1245 | 655 | |
paul@1266 | 656 | if recurrenceid_name: |
paul@1266 | 657 | args[recurrenceid_name] = [] |
paul@1266 | 658 | |
paul@1245 | 659 | all_starts = [] |
paul@1245 | 660 | all_ends = [] |
paul@1245 | 661 | |
paul@1245 | 662 | for index, period in enumerate(periods): |
paul@1245 | 663 | |
paul@1245 | 664 | # Encode period settings in controls. |
paul@1245 | 665 | |
paul@1245 | 666 | if period.end_enabled: |
paul@1245 | 667 | args[end_enabled_name].append(str(index)) |
paul@1245 | 668 | if period.times_enabled: |
paul@1245 | 669 | args[times_enabled_name].append(str(index)) |
paul@1245 | 670 | |
paul@1245 | 671 | # Add origin information where controls are present to record it. |
paul@1245 | 672 | |
paul@1245 | 673 | if origin_name: |
paul@1245 | 674 | args[origin_name].append(period.origin or "") |
paul@1245 | 675 | |
paul@1245 | 676 | # Add replacement information where controls are present to record it. |
paul@1245 | 677 | |
paul@1245 | 678 | if replaced_name and period.replaced: |
paul@1245 | 679 | args[replaced_name].append(str(index)) |
paul@1245 | 680 | |
paul@1266 | 681 | # Add recurrence identifiers where controls are present to record it. |
paul@1266 | 682 | |
paul@1266 | 683 | if recurrenceid_name: |
paul@1266 | 684 | args[recurrenceid_name].append(period.recurrenceid or "") |
paul@1266 | 685 | |
paul@1245 | 686 | # Collect form date information for addition below. |
paul@1245 | 687 | |
paul@1245 | 688 | all_starts.append(period.get_form_start()) |
paul@1245 | 689 | all_ends.append(period.get_form_end()) |
paul@1245 | 690 | |
paul@1245 | 691 | # Set the controls for the dates. |
paul@1245 | 692 | |
paul@1245 | 693 | set_date_control_values(all_starts, args, start_name) |
paul@1245 | 694 | set_date_control_values(all_ends, args, end_name, tzid_name=start_name) |
paul@1245 | 695 | |
paul@1257 | 696 | |
paul@1257 | 697 | |
paul@1257 | 698 | # Utilities. |
paul@1257 | 699 | |
paul@1257 | 700 | def filter_duplicates(l): |
paul@1257 | 701 | |
paul@1257 | 702 | """ |
paul@1257 | 703 | Return collection 'l' filtered for duplicate values, retaining the given |
paul@1257 | 704 | element ordering. |
paul@1257 | 705 | """ |
paul@1257 | 706 | |
paul@1257 | 707 | s = set() |
paul@1257 | 708 | f = [] |
paul@1257 | 709 | |
paul@1257 | 710 | for value in l: |
paul@1257 | 711 | if value not in s: |
paul@1257 | 712 | s.add(value) |
paul@1257 | 713 | f.append(value) |
paul@1257 | 714 | |
paul@1257 | 715 | return f |
paul@1257 | 716 | |
paul@1257 | 717 | def remove_from_collection(l, indexes, fn): |
paul@1257 | 718 | |
paul@1257 | 719 | """ |
paul@1257 | 720 | Remove from collection 'l' all values present at the given 'indexes' where |
paul@1257 | 721 | 'fn' applied to each referenced value returns a true value. Values where |
paul@1257 | 722 | 'fn' returns a false value are added to a list of deferred removals which is |
paul@1257 | 723 | returned. |
paul@1257 | 724 | """ |
paul@1257 | 725 | |
paul@1257 | 726 | still_to_remove = [] |
paul@1257 | 727 | correction = 0 |
paul@1257 | 728 | |
paul@1257 | 729 | for i in indexes: |
paul@1257 | 730 | try: |
paul@1257 | 731 | i = int(i) - correction |
paul@1257 | 732 | value = l[i] |
paul@1257 | 733 | except (IndexError, ValueError): |
paul@1257 | 734 | continue |
paul@1257 | 735 | |
paul@1257 | 736 | if fn(value): |
paul@1257 | 737 | del l[i] |
paul@1257 | 738 | correction += 1 |
paul@1257 | 739 | else: |
paul@1258 | 740 | still_to_remove.append(value) |
paul@1257 | 741 | |
paul@1257 | 742 | return still_to_remove |
paul@1257 | 743 | |
paul@497 | 744 | # vim: tabstop=4 expandtab shiftwidth=4 |