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@868 | 89 | self.replaced |
paul@498 | 90 | ) |
paul@498 | 91 | |
paul@498 | 92 | def get_form_date(self, dt, attr=None): |
paul@498 | 93 | return FormDate( |
paul@498 | 94 | format_datetime(to_date(dt)), |
paul@498 | 95 | isinstance(dt, datetime) and str(dt.hour) or None, |
paul@498 | 96 | isinstance(dt, datetime) and str(dt.minute) or None, |
paul@498 | 97 | isinstance(dt, datetime) and str(dt.second) or None, |
paul@498 | 98 | attr and attr.get("TZID") or None, |
paul@498 | 99 | dt, attr |
paul@498 | 100 | ) |
paul@498 | 101 | |
paul@620 | 102 | class FormPeriod(RecurringPeriod): |
paul@498 | 103 | |
paul@498 | 104 | "A period whose information originates from a form." |
paul@498 | 105 | |
paul@1246 | 106 | def __init__(self, start, end, end_enabled=True, times_enabled=True, |
paul@1246 | 107 | tzid=None, origin=None, replaced=False): |
paul@498 | 108 | self.start = start |
paul@498 | 109 | self.end = end |
paul@498 | 110 | self.end_enabled = end_enabled |
paul@498 | 111 | self.times_enabled = times_enabled |
paul@541 | 112 | self.tzid = tzid |
paul@499 | 113 | self.origin = origin |
paul@868 | 114 | self.replaced = replaced |
paul@497 | 115 | |
paul@497 | 116 | def as_tuple(self): |
paul@868 | 117 | return self.start, self.end, self.end_enabled, self.times_enabled, self.tzid, self.origin, self.replaced |
paul@497 | 118 | |
paul@497 | 119 | def __repr__(self): |
paul@833 | 120 | return "FormPeriod%r" % (self.as_tuple(),) |
paul@498 | 121 | |
paul@499 | 122 | def as_event_period(self, index=None): |
paul@528 | 123 | |
paul@528 | 124 | """ |
paul@528 | 125 | Return a converted version of this object as an event period suitable |
paul@528 | 126 | for iCalendar usage. If 'index' is indicated, include it in any error |
paul@528 | 127 | raised in the conversion process. |
paul@528 | 128 | """ |
paul@528 | 129 | |
paul@700 | 130 | dtstart, dtstart_attr = self.get_start_item() |
paul@528 | 131 | if not dtstart: |
paul@1251 | 132 | if index is not None: |
paul@1251 | 133 | raise PeriodError(("dtstart", index)) |
paul@1251 | 134 | else: |
paul@1251 | 135 | raise PeriodError("dtstart") |
paul@499 | 136 | |
paul@700 | 137 | dtend, dtend_attr = self.get_end_item() |
paul@528 | 138 | if not dtend: |
paul@1251 | 139 | if index is not None: |
paul@1251 | 140 | raise PeriodError(("dtend", index)) |
paul@1251 | 141 | else: |
paul@1251 | 142 | raise PeriodError("dtend") |
paul@499 | 143 | |
paul@499 | 144 | if dtstart > dtend: |
paul@1251 | 145 | if index is not None: |
paul@1251 | 146 | raise PeriodError(("dtstart", index), ("dtend", index)) |
paul@1251 | 147 | else: |
paul@1251 | 148 | raise PeriodError("dtstart", "dtend") |
paul@499 | 149 | |
paul@1246 | 150 | return EventPeriod(dtstart, end_date_to_calendar(dtend), self.tzid, |
paul@1246 | 151 | self.origin, dtstart_attr, dtend_attr, |
paul@1246 | 152 | self.start, self.end, self.replaced) |
paul@499 | 153 | |
paul@499 | 154 | # Period data methods. |
paul@499 | 155 | |
paul@498 | 156 | def get_start(self): |
paul@1246 | 157 | return self.start and self.start.as_datetime(self.times_enabled) or None |
paul@498 | 158 | |
paul@498 | 159 | def get_end(self): |
paul@620 | 160 | |
paul@620 | 161 | # Handle specified end datetimes. |
paul@620 | 162 | |
paul@620 | 163 | if self.end_enabled: |
paul@620 | 164 | dtend = self.end.as_datetime(self.times_enabled) |
paul@620 | 165 | if not dtend: |
paul@620 | 166 | return None |
paul@620 | 167 | |
paul@886 | 168 | # Handle same day times. |
paul@886 | 169 | |
paul@886 | 170 | elif self.times_enabled: |
paul@886 | 171 | formdate = FormDate(self.start.date, self.end.hour, self.end.minute, self.end.second, self.end.tzid) |
paul@886 | 172 | dtend = formdate.as_datetime(self.times_enabled) |
paul@886 | 173 | if not dtend: |
paul@886 | 174 | return None |
paul@886 | 175 | |
paul@620 | 176 | # Otherwise, treat the end date as the start date. Datetimes are |
paul@620 | 177 | # handled by making the event occupy the rest of the day. |
paul@620 | 178 | |
paul@620 | 179 | else: |
paul@620 | 180 | dtstart, dtstart_attr = self.get_start_item() |
paul@620 | 181 | if dtstart: |
paul@620 | 182 | if isinstance(dtstart, datetime): |
paul@620 | 183 | dtend = get_end_of_day(dtstart, dtstart_attr["TZID"]) |
paul@620 | 184 | else: |
paul@620 | 185 | dtend = dtstart |
paul@620 | 186 | else: |
paul@620 | 187 | return None |
paul@620 | 188 | |
paul@528 | 189 | return dtend |
paul@528 | 190 | |
paul@620 | 191 | def get_start_attr(self): |
paul@1246 | 192 | return self.start and self.start.get_attributes(self.times_enabled) or {} |
paul@528 | 193 | |
paul@620 | 194 | def get_end_attr(self): |
paul@1246 | 195 | return self.end and self.end.get_attributes(self.times_enabled) or {} |
paul@498 | 196 | |
paul@499 | 197 | # Form data methods. |
paul@498 | 198 | |
paul@498 | 199 | def get_form_start(self): |
paul@498 | 200 | return self.start |
paul@498 | 201 | |
paul@498 | 202 | def get_form_end(self): |
paul@498 | 203 | return self.end |
paul@498 | 204 | |
paul@498 | 205 | def as_form_period(self): |
paul@498 | 206 | return self |
paul@497 | 207 | |
paul@498 | 208 | class FormDate: |
paul@498 | 209 | |
paul@498 | 210 | "Date information originating from form information." |
paul@498 | 211 | |
paul@498 | 212 | def __init__(self, date=None, hour=None, minute=None, second=None, tzid=None, dt=None, attr=None): |
paul@498 | 213 | self.date = date |
paul@498 | 214 | self.hour = hour |
paul@498 | 215 | self.minute = minute |
paul@498 | 216 | self.second = second |
paul@498 | 217 | self.tzid = tzid |
paul@498 | 218 | self.dt = dt |
paul@498 | 219 | self.attr = attr |
paul@498 | 220 | |
paul@498 | 221 | def as_tuple(self): |
paul@498 | 222 | return self.date, self.hour, self.minute, self.second, self.tzid, self.dt, self.attr |
paul@498 | 223 | |
paul@498 | 224 | def __repr__(self): |
paul@833 | 225 | return "FormDate%r" % (self.as_tuple(),) |
paul@498 | 226 | |
paul@498 | 227 | def get_component(self, value): |
paul@498 | 228 | return (value or "").rjust(2, "0")[:2] |
paul@498 | 229 | |
paul@498 | 230 | def get_hour(self): |
paul@498 | 231 | return self.get_component(self.hour) |
paul@498 | 232 | |
paul@498 | 233 | def get_minute(self): |
paul@498 | 234 | return self.get_component(self.minute) |
paul@498 | 235 | |
paul@498 | 236 | def get_second(self): |
paul@498 | 237 | return self.get_component(self.second) |
paul@498 | 238 | |
paul@498 | 239 | def get_date_string(self): |
paul@498 | 240 | return self.date or "" |
paul@498 | 241 | |
paul@498 | 242 | def get_datetime_string(self): |
paul@498 | 243 | if not self.date: |
paul@498 | 244 | return "" |
paul@498 | 245 | |
paul@498 | 246 | hour = self.hour; minute = self.minute; second = self.second |
paul@498 | 247 | |
paul@498 | 248 | if hour or minute or second: |
paul@498 | 249 | time = "T%s%s%s" % tuple(map(self.get_component, (hour, minute, second))) |
paul@498 | 250 | else: |
paul@498 | 251 | time = "" |
paul@498 | 252 | |
paul@498 | 253 | return "%s%s" % (self.date, time) |
paul@498 | 254 | |
paul@498 | 255 | def get_tzid(self): |
paul@498 | 256 | return self.tzid |
paul@498 | 257 | |
paul@528 | 258 | def as_datetime(self, with_time=True): |
paul@498 | 259 | |
paul@528 | 260 | "Return a datetime for this object." |
paul@498 | 261 | |
paul@498 | 262 | # Return any original datetime details. |
paul@498 | 263 | |
paul@498 | 264 | if self.dt: |
paul@528 | 265 | return self.dt |
paul@498 | 266 | |
paul@528 | 267 | # Otherwise, construct a datetime. |
paul@498 | 268 | |
paul@528 | 269 | s, attr = self.as_datetime_item(with_time) |
paul@528 | 270 | if s: |
paul@528 | 271 | return get_datetime(s, attr) |
paul@498 | 272 | else: |
paul@528 | 273 | return None |
paul@528 | 274 | |
paul@528 | 275 | def as_datetime_item(self, with_time=True): |
paul@498 | 276 | |
paul@528 | 277 | """ |
paul@528 | 278 | Return a (datetime string, attr) tuple for the datetime information |
paul@528 | 279 | provided by this object, where both tuple elements will be None if no |
paul@528 | 280 | suitable date or datetime information exists. |
paul@528 | 281 | """ |
paul@498 | 282 | |
paul@528 | 283 | s = None |
paul@528 | 284 | if with_time: |
paul@528 | 285 | s = self.get_datetime_string() |
paul@528 | 286 | attr = self.get_attributes(True) |
paul@528 | 287 | if not s: |
paul@528 | 288 | s = self.get_date_string() |
paul@528 | 289 | attr = self.get_attributes(False) |
paul@528 | 290 | if not s: |
paul@528 | 291 | return None, None |
paul@528 | 292 | return s, attr |
paul@498 | 293 | |
paul@528 | 294 | def get_attributes(self, with_time=True): |
paul@528 | 295 | |
paul@528 | 296 | "Return attributes for the date or datetime represented by this object." |
paul@498 | 297 | |
paul@528 | 298 | if with_time: |
paul@528 | 299 | return {"TZID" : self.get_tzid(), "VALUE" : "DATE-TIME"} |
paul@528 | 300 | else: |
paul@528 | 301 | return {"VALUE" : "DATE"} |
paul@498 | 302 | |
paul@499 | 303 | def event_period_from_period(period): |
paul@624 | 304 | |
paul@624 | 305 | """ |
paul@624 | 306 | Convert a 'period' to one suitable for use in an iCalendar representation. |
paul@624 | 307 | In an "event period" representation, the end day of any date-level event is |
paul@624 | 308 | encoded as the "day after" the last day actually involved in the event. |
paul@624 | 309 | """ |
paul@624 | 310 | |
paul@499 | 311 | if isinstance(period, EventPeriod): |
paul@499 | 312 | return period |
paul@499 | 313 | elif isinstance(period, FormPeriod): |
paul@499 | 314 | return period.as_event_period() |
paul@499 | 315 | else: |
paul@528 | 316 | dtstart, dtstart_attr = period.get_start_item() |
paul@528 | 317 | dtend, dtend_attr = period.get_end_item() |
paul@539 | 318 | if not isinstance(period, RecurringPeriod): |
paul@539 | 319 | dtend = end_date_to_calendar(dtend) |
paul@541 | 320 | return EventPeriod(dtstart, dtend, period.tzid, period.origin, dtstart_attr, dtend_attr) |
paul@499 | 321 | |
paul@499 | 322 | def form_period_from_period(period): |
paul@624 | 323 | |
paul@624 | 324 | """ |
paul@624 | 325 | Convert a 'period' into a representation usable in a user-editable form. |
paul@624 | 326 | In a "form period" representation, the end day of any date-level event is |
paul@624 | 327 | presented in a "natural" form, not the iCalendar "day after" form. |
paul@624 | 328 | """ |
paul@624 | 329 | |
paul@499 | 330 | if isinstance(period, EventPeriod): |
paul@499 | 331 | return period.as_form_period() |
paul@499 | 332 | elif isinstance(period, FormPeriod): |
paul@499 | 333 | return period |
paul@499 | 334 | else: |
paul@499 | 335 | return event_period_from_period(period).as_form_period() |
paul@499 | 336 | |
paul@1246 | 337 | |
paul@1246 | 338 | |
paul@1244 | 339 | # Form field extraction and serialisation. |
paul@1244 | 340 | |
paul@1246 | 341 | def get_date_control_inputs(args, name, tzid_name=None): |
paul@1246 | 342 | |
paul@1246 | 343 | """ |
paul@1246 | 344 | Return a tuple of date control inputs taken from 'args' for field names |
paul@1246 | 345 | starting with 'name'. |
paul@1246 | 346 | |
paul@1246 | 347 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1246 | 348 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1246 | 349 | """ |
paul@1246 | 350 | |
paul@1246 | 351 | return args.get("%s-date" % name, []), \ |
paul@1246 | 352 | args.get("%s-hour" % name, []), \ |
paul@1246 | 353 | args.get("%s-minute" % name, []), \ |
paul@1246 | 354 | args.get("%s-second" % name, []), \ |
paul@1246 | 355 | args.get("%s-tzid" % (tzid_name or name), []) |
paul@1246 | 356 | |
paul@1244 | 357 | def get_date_control_values(args, name, multiple=False, tzid_name=None, tzid=None): |
paul@1244 | 358 | |
paul@1244 | 359 | """ |
paul@1244 | 360 | Return a form date object representing fields taken from 'args' starting |
paul@1244 | 361 | with 'name'. |
paul@1244 | 362 | |
paul@1244 | 363 | If 'multiple' is set to a true value, many date objects will be returned |
paul@1244 | 364 | corresponding to a collection of datetimes. |
paul@1244 | 365 | |
paul@1244 | 366 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1244 | 367 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 368 | |
paul@1244 | 369 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 370 | time zone information is indicated in the field data. |
paul@1244 | 371 | """ |
paul@1244 | 372 | |
paul@1246 | 373 | dates, hours, minutes, seconds, tzids = get_date_control_inputs(args, name, tzid_name) |
paul@1244 | 374 | |
paul@1244 | 375 | # Handle absent values by employing None values. |
paul@1244 | 376 | |
paul@1244 | 377 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@1244 | 378 | |
paul@1244 | 379 | if not field_values and not multiple: |
paul@1244 | 380 | all_values = FormDate() |
paul@1244 | 381 | else: |
paul@1244 | 382 | all_values = [] |
paul@1244 | 383 | for date, hour, minute, second, tzid_field in field_values: |
paul@1244 | 384 | value = FormDate(date, hour, minute, second, tzid_field or tzid) |
paul@1244 | 385 | |
paul@1244 | 386 | # Return a single value or append to a collection of all values. |
paul@1244 | 387 | |
paul@1244 | 388 | if not multiple: |
paul@1244 | 389 | return value |
paul@1244 | 390 | else: |
paul@1244 | 391 | all_values.append(value) |
paul@1244 | 392 | |
paul@1244 | 393 | return all_values |
paul@1244 | 394 | |
paul@1245 | 395 | def set_date_control_values(formdates, args, name, tzid_name=None): |
paul@1244 | 396 | |
paul@1244 | 397 | """ |
paul@1245 | 398 | Using the values of the given 'formdates', replace form fields in 'args' |
paul@1245 | 399 | starting with 'name'. |
paul@1244 | 400 | |
paul@1244 | 401 | If 'tzid_name' is specified, the time zone information will be stored in |
paul@1244 | 402 | fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 403 | """ |
paul@1244 | 404 | |
paul@1244 | 405 | args["%s-date" % name] = [] |
paul@1244 | 406 | args["%s-hour" % name] = [] |
paul@1244 | 407 | args["%s-minute" % name] = [] |
paul@1244 | 408 | args["%s-second" % name] = [] |
paul@1244 | 409 | args["%s-tzid" % (tzid_name or name)] = [] |
paul@1244 | 410 | |
paul@1244 | 411 | for d in formdates: |
paul@1246 | 412 | args["%s-date" % name].append(d and d.date or "") |
paul@1246 | 413 | args["%s-hour" % name].append(d and d.hour or "") |
paul@1246 | 414 | args["%s-minute" % name].append(d and d.minute or "") |
paul@1246 | 415 | args["%s-second" % name].append(d and d.second or "") |
paul@1246 | 416 | args["%s-tzid" % (tzid_name or name)].append(d and d.tzid or "") |
paul@1244 | 417 | |
paul@1244 | 418 | def get_period_control_values(args, start_name, end_name, |
paul@1244 | 419 | end_enabled_name, times_enabled_name, |
paul@1244 | 420 | origin=None, origin_name=None, |
paul@1244 | 421 | replaced_name=None, tzid=None): |
paul@1244 | 422 | |
paul@1244 | 423 | """ |
paul@1245 | 424 | Return period values from fields found in 'args' prefixed with the given |
paul@1244 | 425 | 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1244 | 426 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1244 | 427 | (to enable times for periods). |
paul@1244 | 428 | |
paul@1244 | 429 | If 'origin' is specified, a single period with the given origin is |
paul@1244 | 430 | returned. If 'origin_name' is specified, fields containing the name will |
paul@1244 | 431 | provide origin information, and fields containing 'replaced_name' will |
paul@1244 | 432 | indicate periods that are replaced. |
paul@1244 | 433 | |
paul@1244 | 434 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 435 | time zone information is indicated in the field data. |
paul@1244 | 436 | """ |
paul@1244 | 437 | |
paul@1244 | 438 | # Get the end datetime and time presence settings. |
paul@1244 | 439 | |
paul@1244 | 440 | all_end_enabled = args.get(end_enabled_name, []) |
paul@1244 | 441 | all_times_enabled = args.get(times_enabled_name, []) |
paul@1244 | 442 | |
paul@1244 | 443 | # Get the origins of period data and whether the periods are replaced. |
paul@1244 | 444 | |
paul@1244 | 445 | if origin: |
paul@1244 | 446 | all_origins = [origin] |
paul@1244 | 447 | else: |
paul@1244 | 448 | all_origins = origin_name and args.get(origin_name, []) or [] |
paul@1244 | 449 | |
paul@1244 | 450 | all_replaced = replaced_name and args.get(replaced_name, []) or [] |
paul@1244 | 451 | |
paul@1244 | 452 | # Get the start and end datetimes. |
paul@1244 | 453 | |
paul@1244 | 454 | all_starts = get_date_control_values(args, start_name, True, tzid=tzid) |
paul@1244 | 455 | all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid) |
paul@1244 | 456 | |
paul@1245 | 457 | # Construct period objects for each start, end, origin combination. |
paul@1245 | 458 | |
paul@1244 | 459 | periods = [] |
paul@1244 | 460 | |
paul@1244 | 461 | for index, (start, end, found_origin) in \ |
paul@1244 | 462 | enumerate(map(None, all_starts, all_ends, all_origins)): |
paul@1244 | 463 | |
paul@1245 | 464 | # Obtain period settings from separate controls. |
paul@1245 | 465 | |
paul@1244 | 466 | end_enabled = str(index) in all_end_enabled |
paul@1244 | 467 | times_enabled = str(index) in all_times_enabled |
paul@1244 | 468 | replaced = str(index) in all_replaced |
paul@1244 | 469 | |
paul@1244 | 470 | period = FormPeriod(start, end, end_enabled, times_enabled, tzid, |
paul@1244 | 471 | found_origin or origin, replaced) |
paul@1244 | 472 | periods.append(period) |
paul@1244 | 473 | |
paul@1244 | 474 | # Return a single period if a single origin was specified. |
paul@1244 | 475 | |
paul@1244 | 476 | if origin: |
paul@1244 | 477 | return periods[0] |
paul@1244 | 478 | else: |
paul@1244 | 479 | return periods |
paul@1244 | 480 | |
paul@1245 | 481 | def set_period_control_values(periods, args, start_name, end_name, |
paul@1245 | 482 | end_enabled_name, times_enabled_name, |
paul@1245 | 483 | origin_name=None, replaced_name=None): |
paul@1245 | 484 | |
paul@1245 | 485 | """ |
paul@1245 | 486 | Using the given 'periods', replace form fields in 'args' prefixed with the |
paul@1245 | 487 | given 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1245 | 488 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1245 | 489 | (to enable times for periods). |
paul@1245 | 490 | |
paul@1245 | 491 | If 'origin_name' is specified, fields containing the name will provide |
paul@1245 | 492 | origin information, and fields containing 'replaced_name' will indicate |
paul@1245 | 493 | periods that are replaced. |
paul@1245 | 494 | """ |
paul@1245 | 495 | |
paul@1245 | 496 | # Record period settings separately. |
paul@1245 | 497 | |
paul@1245 | 498 | args[end_enabled_name] = [] |
paul@1245 | 499 | args[times_enabled_name] = [] |
paul@1245 | 500 | |
paul@1245 | 501 | # Record origin and replacement information if naming is defined. |
paul@1245 | 502 | |
paul@1245 | 503 | if origin_name: |
paul@1245 | 504 | args[origin_name] = [] |
paul@1245 | 505 | |
paul@1245 | 506 | if replaced_name: |
paul@1245 | 507 | args[replaced_name] = [] |
paul@1245 | 508 | |
paul@1245 | 509 | all_starts = [] |
paul@1245 | 510 | all_ends = [] |
paul@1245 | 511 | |
paul@1245 | 512 | for index, period in enumerate(periods): |
paul@1245 | 513 | |
paul@1245 | 514 | # Encode period settings in controls. |
paul@1245 | 515 | |
paul@1245 | 516 | if period.end_enabled: |
paul@1245 | 517 | args[end_enabled_name].append(str(index)) |
paul@1245 | 518 | if period.times_enabled: |
paul@1245 | 519 | args[times_enabled_name].append(str(index)) |
paul@1245 | 520 | |
paul@1245 | 521 | # Add origin information where controls are present to record it. |
paul@1245 | 522 | |
paul@1245 | 523 | if origin_name: |
paul@1245 | 524 | args[origin_name].append(period.origin or "") |
paul@1245 | 525 | |
paul@1245 | 526 | # Add replacement information where controls are present to record it. |
paul@1245 | 527 | |
paul@1245 | 528 | if replaced_name and period.replaced: |
paul@1245 | 529 | args[replaced_name].append(str(index)) |
paul@1245 | 530 | |
paul@1245 | 531 | # Collect form date information for addition below. |
paul@1245 | 532 | |
paul@1245 | 533 | all_starts.append(period.get_form_start()) |
paul@1245 | 534 | all_ends.append(period.get_form_end()) |
paul@1245 | 535 | |
paul@1245 | 536 | # Set the controls for the dates. |
paul@1245 | 537 | |
paul@1245 | 538 | set_date_control_values(all_starts, args, start_name) |
paul@1245 | 539 | set_date_control_values(all_ends, args, end_name, tzid_name=start_name) |
paul@1245 | 540 | |
paul@497 | 541 | # vim: tabstop=4 expandtab shiftwidth=4 |