paul@497 | 1 | #!/usr/bin/env python |
paul@497 | 2 | |
paul@497 | 3 | """ |
paul@497 | 4 | Web interface data abstractions. |
paul@497 | 5 | |
paul@497 | 6 | Copyright (C) 2014, 2015 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@497 | 22 | from datetime import datetime, timedelta |
paul@497 | 23 | from imiptools.dates import get_datetime, get_start_of_day |
paul@497 | 24 | from imiptools.period import Period |
paul@497 | 25 | |
paul@497 | 26 | class EventPeriod(Period): |
paul@497 | 27 | |
paul@497 | 28 | "A simple period plus attribute details, compatible with RecurringPeriod." |
paul@497 | 29 | |
paul@497 | 30 | def __init__(self, start, end, start_attr=None, end_attr=None): |
paul@497 | 31 | Period.__init__(self, start, end) |
paul@497 | 32 | self.start_attr = start_attr |
paul@497 | 33 | self.end_attr = end_attr |
paul@497 | 34 | |
paul@497 | 35 | def as_tuple(self): |
paul@497 | 36 | return self.start, self.end, self.start_attr, self.end_attr |
paul@497 | 37 | |
paul@497 | 38 | def __repr__(self): |
paul@497 | 39 | return "EventPeriod(%r, %r, %r, %r)" % self.as_tuple() |
paul@497 | 40 | |
paul@497 | 41 | def handle_date_control_values(values, with_time=True): |
paul@497 | 42 | |
paul@497 | 43 | """ |
paul@497 | 44 | Handle date control information for the given 'values', returning a |
paul@497 | 45 | (datetime, attr) tuple, or None if the fields cannot be used to |
paul@497 | 46 | construct a datetime object. |
paul@497 | 47 | """ |
paul@497 | 48 | |
paul@497 | 49 | if not values or not values["date"]: |
paul@497 | 50 | return None |
paul@497 | 51 | elif with_time: |
paul@497 | 52 | value = "%s%s" % (values["date"], values["time"]) |
paul@497 | 53 | attr = {"TZID" : values["tzid"], "VALUE" : "DATE-TIME"} |
paul@497 | 54 | dt = get_datetime(value, attr) |
paul@497 | 55 | else: |
paul@497 | 56 | attr = {"VALUE" : "DATE"} |
paul@497 | 57 | dt = get_datetime(values["date"]) |
paul@497 | 58 | |
paul@497 | 59 | if dt: |
paul@497 | 60 | return dt, attr |
paul@497 | 61 | |
paul@497 | 62 | return None |
paul@497 | 63 | |
paul@497 | 64 | def handle_period_controls(start_values, end_values, dtend_enabled, dttimes_enabled, index=None): |
paul@497 | 65 | |
paul@497 | 66 | """ |
paul@497 | 67 | Handle datetime controls for a particular period, described by the given |
paul@497 | 68 | 'start_values' and 'end_values', with 'dtend_enabled' and |
paul@497 | 69 | 'dttimes_enabled' affecting the usage of the provided values. |
paul@497 | 70 | |
paul@497 | 71 | If 'index' is specified, incorporate it into any error indicator. |
paul@497 | 72 | """ |
paul@497 | 73 | |
paul@497 | 74 | t = handle_date_control_values(start_values, dttimes_enabled) |
paul@497 | 75 | if t: |
paul@497 | 76 | dtstart, dtstart_attr = t |
paul@497 | 77 | else: |
paul@497 | 78 | return None, [index is not None and ("dtstart", index) or "dtstart"] |
paul@497 | 79 | |
paul@497 | 80 | # Handle specified end datetimes. |
paul@497 | 81 | |
paul@497 | 82 | if dtend_enabled: |
paul@497 | 83 | t = handle_date_control_values(end_values, dttimes_enabled) |
paul@497 | 84 | if t: |
paul@497 | 85 | dtend, dtend_attr = t |
paul@497 | 86 | |
paul@497 | 87 | # Convert end dates to iCalendar "next day" dates. |
paul@497 | 88 | |
paul@497 | 89 | if not isinstance(dtend, datetime): |
paul@497 | 90 | dtend += timedelta(1) |
paul@497 | 91 | else: |
paul@497 | 92 | return None, [index is not None and ("dtend", index) or "dtend"] |
paul@497 | 93 | |
paul@497 | 94 | # Otherwise, treat the end date as the start date. Datetimes are |
paul@497 | 95 | # handled by making the event occupy the rest of the day. |
paul@497 | 96 | |
paul@497 | 97 | else: |
paul@497 | 98 | dtend = dtstart + timedelta(1) |
paul@497 | 99 | dtend_attr = dtstart_attr |
paul@497 | 100 | |
paul@497 | 101 | if isinstance(dtstart, datetime): |
paul@497 | 102 | dtend = get_start_of_day(dtend, attr["TZID"]) |
paul@497 | 103 | |
paul@497 | 104 | if dtstart > dtend: |
paul@497 | 105 | return None, [ |
paul@497 | 106 | index is not None and ("dtstart", index) or "dtstart", |
paul@497 | 107 | index is not None and ("dtend", index) or "dtend" |
paul@497 | 108 | ] |
paul@497 | 109 | |
paul@497 | 110 | return EventPeriod(dtstart, dtend, dtstart_attr, dtend_attr), None |
paul@497 | 111 | |
paul@497 | 112 | # vim: tabstop=4 expandtab shiftwidth=4 |