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