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