1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/imipweb/data.py Mon Apr 06 16:39:52 2015 +0200
1.3 @@ -0,0 +1,112 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Web interface data abstractions.
1.8 +
1.9 +Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from datetime import datetime, timedelta
1.26 +from imiptools.dates import get_datetime, get_start_of_day
1.27 +from imiptools.period import Period
1.28 +
1.29 +class EventPeriod(Period):
1.30 +
1.31 + "A simple period plus attribute details, compatible with RecurringPeriod."
1.32 +
1.33 + def __init__(self, start, end, start_attr=None, end_attr=None):
1.34 + Period.__init__(self, start, end)
1.35 + self.start_attr = start_attr
1.36 + self.end_attr = end_attr
1.37 +
1.38 + def as_tuple(self):
1.39 + return self.start, self.end, self.start_attr, self.end_attr
1.40 +
1.41 + def __repr__(self):
1.42 + return "EventPeriod(%r, %r, %r, %r)" % self.as_tuple()
1.43 +
1.44 +def handle_date_control_values(values, with_time=True):
1.45 +
1.46 + """
1.47 + Handle date control information for the given 'values', returning a
1.48 + (datetime, attr) tuple, or None if the fields cannot be used to
1.49 + construct a datetime object.
1.50 + """
1.51 +
1.52 + if not values or not values["date"]:
1.53 + return None
1.54 + elif with_time:
1.55 + value = "%s%s" % (values["date"], values["time"])
1.56 + attr = {"TZID" : values["tzid"], "VALUE" : "DATE-TIME"}
1.57 + dt = get_datetime(value, attr)
1.58 + else:
1.59 + attr = {"VALUE" : "DATE"}
1.60 + dt = get_datetime(values["date"])
1.61 +
1.62 + if dt:
1.63 + return dt, attr
1.64 +
1.65 + return None
1.66 +
1.67 +def handle_period_controls(start_values, end_values, dtend_enabled, dttimes_enabled, index=None):
1.68 +
1.69 + """
1.70 + Handle datetime controls for a particular period, described by the given
1.71 + 'start_values' and 'end_values', with 'dtend_enabled' and
1.72 + 'dttimes_enabled' affecting the usage of the provided values.
1.73 +
1.74 + If 'index' is specified, incorporate it into any error indicator.
1.75 + """
1.76 +
1.77 + t = handle_date_control_values(start_values, dttimes_enabled)
1.78 + if t:
1.79 + dtstart, dtstart_attr = t
1.80 + else:
1.81 + return None, [index is not None and ("dtstart", index) or "dtstart"]
1.82 +
1.83 + # Handle specified end datetimes.
1.84 +
1.85 + if dtend_enabled:
1.86 + t = handle_date_control_values(end_values, dttimes_enabled)
1.87 + if t:
1.88 + dtend, dtend_attr = t
1.89 +
1.90 + # Convert end dates to iCalendar "next day" dates.
1.91 +
1.92 + if not isinstance(dtend, datetime):
1.93 + dtend += timedelta(1)
1.94 + else:
1.95 + return None, [index is not None and ("dtend", index) or "dtend"]
1.96 +
1.97 + # Otherwise, treat the end date as the start date. Datetimes are
1.98 + # handled by making the event occupy the rest of the day.
1.99 +
1.100 + else:
1.101 + dtend = dtstart + timedelta(1)
1.102 + dtend_attr = dtstart_attr
1.103 +
1.104 + if isinstance(dtstart, datetime):
1.105 + dtend = get_start_of_day(dtend, attr["TZID"])
1.106 +
1.107 + if dtstart > dtend:
1.108 + return None, [
1.109 + index is not None and ("dtstart", index) or "dtstart",
1.110 + index is not None and ("dtend", index) or "dtend"
1.111 + ]
1.112 +
1.113 + return EventPeriod(dtstart, dtend, dtstart_attr, dtend_attr), None
1.114 +
1.115 +# vim: tabstop=4 expandtab shiftwidth=4