/usr/share/pyshared/schooltool/timetable/schedule.py is in python-schooltool 1:2.1.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | #
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2005 Shuttleworth Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Scheduling of meetings.
"""
import pytz
import datetime
from persistent import Persistent
from persistent.dict import PersistentDict
from zope.component import getUtility
from zope.container.contained import Contained
from zope.container.btree import BTreeContainer
from zope.intid.interfaces import IIntIds
from zope.interface import implements
from zope.proxy import sameProxiedObjects
from schooltool.common import DateRange
from schooltool.timetable import interfaces
class Period(Persistent, Contained):
implements(interfaces.IPeriod)
title = None
activity_type = None
def __init__(self, title=None, activity_type=None):
Contained.__init__(self)
self.title = title
self.activity_type = activity_type
class Meeting(object):
implements(interfaces.IMeeting)
def __init__(self, dtstart, duration, period=None, meeting_id=None):
self.dtstart = dtstart
self.duration = duration
self.period = period
self.meeting_id = meeting_id
def clone(self, **kw):
dtstart = kw.get('dtstart', self.dtstart)
duration = kw.get('duration', self.duration)
period = kw.get('period', self.period)
meeting_id = kw.get('meeting_id', self.meeting_id)
return self.__class__(dtstart, duration,
period=period, meeting_id=meeting_id)
def __repr__(self):
parts = []
if self.period is not None and self.period.title:
parts.append(self.period.title)
if self.dtstart is not None:
parts.append('on %s' % self.dtstart.strftime('%Y-%m-%d %H:%M %Z'))
return '<%s%s>' % (
self.__class__.__name__, parts and ' '+' '.join(parts) or '')
class MeetingException(Persistent, Meeting):
implements(interfaces.IMeetingException)
_period_id = None
@property
def period(self):
if self._period_id is None:
return None
int_ids = getUtility(IIntIds)
return int_ids.queryObject(self._period_id)
@period.setter
def period(self, value):
if value is None:
self._period_id = None
else:
int_ids = getUtility(IIntIds)
self._period_id = int_ids.getId(value)
class Schedule(Contained):
"""A non-persistent abstract schedule."""
implements(interfaces.ISchedule)
first = None
last = None
title = None
timezone = None
def __init__(self, first, last, title=None, timezone='UTC'):
Contained.__init__(self)
self.title = title
self.first = first
self.last = last
self.timezone = timezone
def iterMeetings(date, until_date=None):
return iter([])
def date_timespan(date, tzinfo=pytz.UTC):
starts = datetime.datetime.combine(date, datetime.time.min)
starts = tzinfo.localize(starts)
ends = datetime.datetime.combine(date, datetime.time.max)
ends = tzinfo.localize(ends)
return starts, ends
def iterMeetingsInTimezone(schedule, other_timezone, date, until_date=None):
if until_date == None:
until_date = date
other_timezone = pytz.timezone(other_timezone)
schedule_timezone = pytz.timezone(schedule.timezone)
start_time = date_timespan(date, tzinfo=other_timezone)[0]
end_time = date_timespan(until_date, tzinfo=other_timezone)[1]
tt_start_date = start_time.astimezone(schedule_timezone).date()
tt_end_date = end_time.astimezone(schedule_timezone).date()
if tt_start_date == tt_end_date:
tt_end_date = None
meetings = schedule.iterMeetings(tt_start_date, until_date=tt_end_date)
for meeting in meetings:
if (meeting.dtstart >= start_time and
meeting.dtstart <= end_time):
yield meeting
def iterMeetingsWithExceptions(meetings, exceptions, timezone,
date, until_date=None):
if until_date is None:
until_date = date
by_date = {}
for d in DateRange(date, until_date):
if d in exceptions:
by_date[d] = list(exceptions[d])
tz = pytz.timezone(timezone)
for original_meeting in meetings:
meeting_date = original_meeting.dtstart.astimezone(tz).date()
if meeting_date in exceptions:
continue
if meeting_date not in by_date:
by_date[meeting_date] = list()
by_date[meeting_date].append(original_meeting)
for d in sorted(by_date):
for meeting in sorted(by_date[d], key=lambda m: m.dtstart):
yield meeting
class ScheduleContainer(BTreeContainer):
implements(interfaces.IScheduleContainer)
timezone = None
exceptions = None
def __init__(self, timezone='UTC'):
BTreeContainer.__init__(self)
self.timezone = timezone
self.exceptions = PersistentDict()
@property
def first(self):
dates = [schedule.first for schedule in self.values()
if schedule.first is not None]
return dates and min(dates) or None
@property
def last(self):
dates = [schedule.last for schedule in self.values()
if schedule.last is not None]
return dates and max(dates) or None
def iterOriginalMeetings(self, date, until_date=None):
if until_date is None:
until_date = date
meetings = []
for schedule in self.values():
if not sameProxiedObjects(schedule.__parent__, self):
# We are likely in the process of deleting/moving
# this schedule. Ignore.
continue
tt_meetings = iterMeetingsInTimezone(
schedule, self.timezone, date, until_date=until_date)
meetings.extend(list(tt_meetings))
return sorted(meetings, key=lambda m: m.dtstart)
def iterMeetings(self, date, until_date=None):
meetings = self.iterOriginalMeetings(date, until_date=until_date)
return iterMeetingsWithExceptions(
meetings, self.exceptions, self.timezone,
date, until_date=until_date)
|