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