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