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@1257 | 346 | def get_active_periods(periods): |
paul@1257 | 347 | |
paul@1257 | 348 | "Return a mapping of non-replaced periods to counts, given 'periods'." |
paul@1257 | 349 | |
paul@1257 | 350 | active_periods = {} |
paul@1257 | 351 | for p in periods: |
paul@1257 | 352 | if not p.replaced: |
paul@1257 | 353 | if not active_periods.has_key(p): |
paul@1257 | 354 | active_periods[p] = 1 |
paul@1257 | 355 | else: |
paul@1257 | 356 | active_periods[p] += 1 |
paul@1257 | 357 | return active_periods |
paul@1257 | 358 | |
paul@1261 | 359 | def get_removed_periods(periods, still_to_remove): |
paul@1261 | 360 | |
paul@1261 | 361 | """ |
paul@1261 | 362 | From the recurrence 'periods', given details of those 'still_to_remove', |
paul@1261 | 363 | return the remaining active periods and the periods to unschedule or |
paul@1261 | 364 | exclude, using a tuple of the form (active, unscheduled, excluded). |
paul@1261 | 365 | """ |
paul@1261 | 366 | |
paul@1261 | 367 | to_remove = set() |
paul@1261 | 368 | |
paul@1261 | 369 | # Get all periods that are not replaced. |
paul@1261 | 370 | |
paul@1261 | 371 | active_periods = get_active_periods(periods) |
paul@1261 | 372 | |
paul@1262 | 373 | for period in still_to_remove: |
paul@1261 | 374 | active_periods[period] -= 1 |
paul@1261 | 375 | to_remove.add(period) |
paul@1261 | 376 | |
paul@1261 | 377 | # Determine whether some periods are both removed and added. |
paul@1261 | 378 | |
paul@1261 | 379 | remaining = [] |
paul@1261 | 380 | for period, n in active_periods.items(): |
paul@1261 | 381 | if n > 0: |
paul@1261 | 382 | remaining.append(period) |
paul@1261 | 383 | |
paul@1261 | 384 | to_remove.difference_update(remaining) |
paul@1261 | 385 | return remaining, to_remove |
paul@1261 | 386 | |
paul@1257 | 387 | |
paul@1257 | 388 | |
paul@1244 | 389 | # Form field extraction and serialisation. |
paul@1244 | 390 | |
paul@1246 | 391 | def get_date_control_inputs(args, name, tzid_name=None): |
paul@1246 | 392 | |
paul@1246 | 393 | """ |
paul@1246 | 394 | Return a tuple of date control inputs taken from 'args' for field names |
paul@1246 | 395 | starting with 'name'. |
paul@1246 | 396 | |
paul@1246 | 397 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1246 | 398 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1246 | 399 | """ |
paul@1246 | 400 | |
paul@1246 | 401 | return args.get("%s-date" % name, []), \ |
paul@1246 | 402 | args.get("%s-hour" % name, []), \ |
paul@1246 | 403 | args.get("%s-minute" % name, []), \ |
paul@1246 | 404 | args.get("%s-second" % name, []), \ |
paul@1246 | 405 | args.get("%s-tzid" % (tzid_name or name), []) |
paul@1246 | 406 | |
paul@1244 | 407 | def get_date_control_values(args, name, multiple=False, tzid_name=None, tzid=None): |
paul@1244 | 408 | |
paul@1244 | 409 | """ |
paul@1244 | 410 | Return a form date object representing fields taken from 'args' starting |
paul@1244 | 411 | with 'name'. |
paul@1244 | 412 | |
paul@1244 | 413 | If 'multiple' is set to a true value, many date objects will be returned |
paul@1244 | 414 | corresponding to a collection of datetimes. |
paul@1244 | 415 | |
paul@1244 | 416 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1244 | 417 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 418 | |
paul@1244 | 419 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 420 | time zone information is indicated in the field data. |
paul@1244 | 421 | """ |
paul@1244 | 422 | |
paul@1246 | 423 | dates, hours, minutes, seconds, tzids = get_date_control_inputs(args, name, tzid_name) |
paul@1244 | 424 | |
paul@1244 | 425 | # Handle absent values by employing None values. |
paul@1244 | 426 | |
paul@1244 | 427 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@1244 | 428 | |
paul@1244 | 429 | if not field_values and not multiple: |
paul@1244 | 430 | all_values = FormDate() |
paul@1244 | 431 | else: |
paul@1244 | 432 | all_values = [] |
paul@1244 | 433 | for date, hour, minute, second, tzid_field in field_values: |
paul@1244 | 434 | value = FormDate(date, hour, minute, second, tzid_field or tzid) |
paul@1244 | 435 | |
paul@1244 | 436 | # Return a single value or append to a collection of all values. |
paul@1244 | 437 | |
paul@1244 | 438 | if not multiple: |
paul@1244 | 439 | return value |
paul@1244 | 440 | else: |
paul@1244 | 441 | all_values.append(value) |
paul@1244 | 442 | |
paul@1244 | 443 | return all_values |
paul@1244 | 444 | |
paul@1245 | 445 | def set_date_control_values(formdates, args, name, tzid_name=None): |
paul@1244 | 446 | |
paul@1244 | 447 | """ |
paul@1245 | 448 | Using the values of the given 'formdates', replace form fields in 'args' |
paul@1245 | 449 | starting with 'name'. |
paul@1244 | 450 | |
paul@1244 | 451 | If 'tzid_name' is specified, the time zone information will be stored in |
paul@1244 | 452 | fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 453 | """ |
paul@1244 | 454 | |
paul@1244 | 455 | args["%s-date" % name] = [] |
paul@1244 | 456 | args["%s-hour" % name] = [] |
paul@1244 | 457 | args["%s-minute" % name] = [] |
paul@1244 | 458 | args["%s-second" % name] = [] |
paul@1244 | 459 | args["%s-tzid" % (tzid_name or name)] = [] |
paul@1244 | 460 | |
paul@1244 | 461 | for d in formdates: |
paul@1246 | 462 | args["%s-date" % name].append(d and d.date or "") |
paul@1246 | 463 | args["%s-hour" % name].append(d and d.hour or "") |
paul@1246 | 464 | args["%s-minute" % name].append(d and d.minute or "") |
paul@1246 | 465 | args["%s-second" % name].append(d and d.second or "") |
paul@1246 | 466 | args["%s-tzid" % (tzid_name or name)].append(d and d.tzid or "") |
paul@1244 | 467 | |
paul@1244 | 468 | def get_period_control_values(args, start_name, end_name, |
paul@1244 | 469 | end_enabled_name, times_enabled_name, |
paul@1244 | 470 | origin=None, origin_name=None, |
paul@1266 | 471 | replaced_name=None, recurrenceid_name=None, |
paul@1266 | 472 | tzid=None): |
paul@1244 | 473 | |
paul@1244 | 474 | """ |
paul@1245 | 475 | Return period values from fields found in 'args' prefixed with the given |
paul@1244 | 476 | 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1244 | 477 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1244 | 478 | (to enable times for periods). |
paul@1244 | 479 | |
paul@1244 | 480 | If 'origin' is specified, a single period with the given origin is |
paul@1244 | 481 | returned. If 'origin_name' is specified, fields containing the name will |
paul@1266 | 482 | provide origin information, fields containing 'replaced_name' will indicate |
paul@1266 | 483 | periods that are replaced, and fields containing 'recurrenceid_name' will |
paul@1266 | 484 | indicate periods that have existing recurrence details from an event. |
paul@1244 | 485 | |
paul@1244 | 486 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 487 | time zone information is indicated in the field data. |
paul@1244 | 488 | """ |
paul@1244 | 489 | |
paul@1244 | 490 | # Get the end datetime and time presence settings. |
paul@1244 | 491 | |
paul@1244 | 492 | all_end_enabled = args.get(end_enabled_name, []) |
paul@1244 | 493 | all_times_enabled = args.get(times_enabled_name, []) |
paul@1244 | 494 | |
paul@1244 | 495 | # Get the origins of period data and whether the periods are replaced. |
paul@1244 | 496 | |
paul@1244 | 497 | if origin: |
paul@1244 | 498 | all_origins = [origin] |
paul@1244 | 499 | else: |
paul@1244 | 500 | all_origins = origin_name and args.get(origin_name, []) or [] |
paul@1244 | 501 | |
paul@1244 | 502 | all_replaced = replaced_name and args.get(replaced_name, []) or [] |
paul@1266 | 503 | all_recurrenceids = recurrenceid_name and args.get(recurrenceid_name, []) or [] |
paul@1244 | 504 | |
paul@1244 | 505 | # Get the start and end datetimes. |
paul@1244 | 506 | |
paul@1244 | 507 | all_starts = get_date_control_values(args, start_name, True, tzid=tzid) |
paul@1244 | 508 | all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid) |
paul@1244 | 509 | |
paul@1245 | 510 | # Construct period objects for each start, end, origin combination. |
paul@1245 | 511 | |
paul@1244 | 512 | periods = [] |
paul@1244 | 513 | |
paul@1266 | 514 | for index, (start, end, found_origin, recurrenceid) in \ |
paul@1266 | 515 | enumerate(map(None, all_starts, all_ends, all_origins, all_recurrenceids)): |
paul@1244 | 516 | |
paul@1245 | 517 | # Obtain period settings from separate controls. |
paul@1245 | 518 | |
paul@1244 | 519 | end_enabled = str(index) in all_end_enabled |
paul@1244 | 520 | times_enabled = str(index) in all_times_enabled |
paul@1244 | 521 | replaced = str(index) in all_replaced |
paul@1244 | 522 | |
paul@1244 | 523 | period = FormPeriod(start, end, end_enabled, times_enabled, tzid, |
paul@1266 | 524 | found_origin or origin, replaced, recurrenceid) |
paul@1244 | 525 | periods.append(period) |
paul@1244 | 526 | |
paul@1244 | 527 | # Return a single period if a single origin was specified. |
paul@1244 | 528 | |
paul@1244 | 529 | if origin: |
paul@1244 | 530 | return periods[0] |
paul@1244 | 531 | else: |
paul@1244 | 532 | return periods |
paul@1244 | 533 | |
paul@1245 | 534 | def set_period_control_values(periods, args, start_name, end_name, |
paul@1245 | 535 | end_enabled_name, times_enabled_name, |
paul@1266 | 536 | origin_name=None, replaced_name=None, |
paul@1266 | 537 | recurrenceid_name=None): |
paul@1245 | 538 | |
paul@1245 | 539 | """ |
paul@1245 | 540 | Using the given 'periods', replace form fields in 'args' prefixed with the |
paul@1245 | 541 | given 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1245 | 542 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1245 | 543 | (to enable times for periods). |
paul@1245 | 544 | |
paul@1245 | 545 | If 'origin_name' is specified, fields containing the name will provide |
paul@1266 | 546 | origin information, fields containing 'replaced_name' will indicate periods |
paul@1266 | 547 | that are replaced, and fields containing 'recurrenceid_name' will indicate |
paul@1266 | 548 | periods that have existing recurrence details from an event. |
paul@1245 | 549 | """ |
paul@1245 | 550 | |
paul@1245 | 551 | # Record period settings separately. |
paul@1245 | 552 | |
paul@1245 | 553 | args[end_enabled_name] = [] |
paul@1245 | 554 | args[times_enabled_name] = [] |
paul@1245 | 555 | |
paul@1245 | 556 | # Record origin and replacement information if naming is defined. |
paul@1245 | 557 | |
paul@1245 | 558 | if origin_name: |
paul@1245 | 559 | args[origin_name] = [] |
paul@1245 | 560 | |
paul@1245 | 561 | if replaced_name: |
paul@1245 | 562 | args[replaced_name] = [] |
paul@1245 | 563 | |
paul@1266 | 564 | if recurrenceid_name: |
paul@1266 | 565 | args[recurrenceid_name] = [] |
paul@1266 | 566 | |
paul@1245 | 567 | all_starts = [] |
paul@1245 | 568 | all_ends = [] |
paul@1245 | 569 | |
paul@1245 | 570 | for index, period in enumerate(periods): |
paul@1245 | 571 | |
paul@1245 | 572 | # Encode period settings in controls. |
paul@1245 | 573 | |
paul@1245 | 574 | if period.end_enabled: |
paul@1245 | 575 | args[end_enabled_name].append(str(index)) |
paul@1245 | 576 | if period.times_enabled: |
paul@1245 | 577 | args[times_enabled_name].append(str(index)) |
paul@1245 | 578 | |
paul@1245 | 579 | # Add origin information where controls are present to record it. |
paul@1245 | 580 | |
paul@1245 | 581 | if origin_name: |
paul@1245 | 582 | args[origin_name].append(period.origin or "") |
paul@1245 | 583 | |
paul@1245 | 584 | # Add replacement information where controls are present to record it. |
paul@1245 | 585 | |
paul@1245 | 586 | if replaced_name and period.replaced: |
paul@1245 | 587 | args[replaced_name].append(str(index)) |
paul@1245 | 588 | |
paul@1266 | 589 | # Add recurrence identifiers where controls are present to record it. |
paul@1266 | 590 | |
paul@1266 | 591 | if recurrenceid_name: |
paul@1266 | 592 | args[recurrenceid_name].append(period.recurrenceid or "") |
paul@1266 | 593 | |
paul@1245 | 594 | # Collect form date information for addition below. |
paul@1245 | 595 | |
paul@1245 | 596 | all_starts.append(period.get_form_start()) |
paul@1245 | 597 | all_ends.append(period.get_form_end()) |
paul@1245 | 598 | |
paul@1245 | 599 | # Set the controls for the dates. |
paul@1245 | 600 | |
paul@1245 | 601 | set_date_control_values(all_starts, args, start_name) |
paul@1245 | 602 | set_date_control_values(all_ends, args, end_name, tzid_name=start_name) |
paul@1245 | 603 | |
paul@1257 | 604 | |
paul@1257 | 605 | |
paul@1257 | 606 | # Utilities. |
paul@1257 | 607 | |
paul@1257 | 608 | def filter_duplicates(l): |
paul@1257 | 609 | |
paul@1257 | 610 | """ |
paul@1257 | 611 | Return collection 'l' filtered for duplicate values, retaining the given |
paul@1257 | 612 | element ordering. |
paul@1257 | 613 | """ |
paul@1257 | 614 | |
paul@1257 | 615 | s = set() |
paul@1257 | 616 | f = [] |
paul@1257 | 617 | |
paul@1257 | 618 | for value in l: |
paul@1257 | 619 | if value not in s: |
paul@1257 | 620 | s.add(value) |
paul@1257 | 621 | f.append(value) |
paul@1257 | 622 | |
paul@1257 | 623 | return f |
paul@1257 | 624 | |
paul@1257 | 625 | def remove_from_collection(l, indexes, fn): |
paul@1257 | 626 | |
paul@1257 | 627 | """ |
paul@1257 | 628 | Remove from collection 'l' all values present at the given 'indexes' where |
paul@1257 | 629 | 'fn' applied to each referenced value returns a true value. Values where |
paul@1257 | 630 | 'fn' returns a false value are added to a list of deferred removals which is |
paul@1257 | 631 | returned. |
paul@1257 | 632 | """ |
paul@1257 | 633 | |
paul@1257 | 634 | still_to_remove = [] |
paul@1257 | 635 | correction = 0 |
paul@1257 | 636 | |
paul@1257 | 637 | for i in indexes: |
paul@1257 | 638 | try: |
paul@1257 | 639 | i = int(i) - correction |
paul@1257 | 640 | value = l[i] |
paul@1257 | 641 | except (IndexError, ValueError): |
paul@1257 | 642 | continue |
paul@1257 | 643 | |
paul@1257 | 644 | if fn(value): |
paul@1257 | 645 | del l[i] |
paul@1257 | 646 | correction += 1 |
paul@1257 | 647 | else: |
paul@1258 | 648 | still_to_remove.append(value) |
paul@1257 | 649 | |
paul@1257 | 650 | return still_to_remove |
paul@1257 | 651 | |
paul@497 | 652 | # vim: tabstop=4 expandtab shiftwidth=4 |