/usr/share/pyshared/schooltool/timetable/daytemplates.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 | #
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2010 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
#
"""
Period and time slot schedule templates.
Template scheduling over dates.
"""
from persistent import Persistent
from zope.interface import implements, implementer
from zope.component import adapter
from zope.container.contained import Contained
from zope.container.ordered import OrderedContainer
from zope.container.contained import containedEvent
from zope.event import notify
from schooltool.common import DateRange
from schooltool.timetable import interfaces
class DayTemplate(OrderedContainer):
implements(interfaces.IDayTemplate)
title = None
def __init__(self, title=u''):
OrderedContainer.__init__(self)
self.title = title
class DayTemplateContainer(OrderedContainer):
implements(interfaces.IDayTemplateContainer)
class TimeSlot(Persistent, Contained):
implements(interfaces.ITimeSlot)
tstart = None
duration = None
activity_type = None
def __init__(self, tstart, duration, activity_type=None):
Contained.__init__(self)
self.tstart = tstart
self.duration = duration
self.activity_type = activity_type
def __cmp__(self, other):
return cmp((self.tstart, self.duration),
(other.tstart, other.duration))
class DayTemplateSchedule(Persistent, Contained):
"""Day templates scheduled by date."""
implements(interfaces.IDayTemplateSchedule)
templates = None
def initTemplates(self):
self.templates, event = containedEvent(
DayTemplateContainer(), self, 'templates')
notify(event)
def iterDates(self, dates):
for date in dates:
yield None
class CalendarDayTemplates(DayTemplateSchedule):
implements(interfaces.ICalendarDayTemplates)
starting_index = 0
def getDay(self, schedule, date):
assert self.templates
days_passed = (date - schedule.first).days
keys = self.templates.keys()
n = (self.templates.starting_index + days_passed) % len(keys)
return self.templates[keys[n]]
def iterDates(self, dates):
if not self.templates:
for date in dates:
yield None
return
schedule = interfaces.ISchedule(self)
scheduled_dates = DateRange(schedule.first, schedule.last)
for date in dates:
if date not in scheduled_dates:
yield None
else:
day = self.getDay(schedule, date)
yield day
class WeekDayTemplates(DayTemplateSchedule):
implements(interfaces.IWeekDayTemplates)
def getWeekDayKey(self, weekday):
return u'%d' % weekday
def getWeekDay(self, weekday):
assert self.templates is not None
key = self.getWeekDayKey(weekday)
return self.templates.get(key, None)
def iterDates(self, dates):
if not self.templates:
for date in dates:
yield None
return
schedule = interfaces.ISchedule(self)
schooldays = interfaces.ISchooldays(schedule)
scheduled_dates = DateRange(schedule.first, schedule.last)
for date in dates:
if (date not in scheduled_dates or
date not in schooldays):
yield None
else:
day = self.getWeekDay(date.weekday())
yield day
class SchoolDayTemplates(DayTemplateSchedule):
implements(interfaces.ISchoolDayTemplates)
starting_index = 0
def getDayIndex(self, schedule, schooldays, date):
assert self.templates
day_index = self.starting_index
if date == schedule.first:
return day_index
if date > schedule.first:
skip_dates = DateRange(schedule.first, date - date.resolution)
else:
skip_dates = DateRange(date + date.resolution, schedule.first)
skipped_schooldays = len(list(schooldays.iterDates(skip_dates)))
if date < schedule.first:
skipped_schooldays = -skipped_schooldays
day_index = (day_index + skipped_schooldays) % len(self.templates)
return day_index
def iterDates(self, dates):
if not self.templates:
for date in dates:
yield None
return
schedule = interfaces.ISchedule(self)
scheduled_dates = DateRange(schedule.first, schedule.last)
schooldays = interfaces.ISchooldays(schedule)
prev_index = None
prev_date = None
for date in dates:
if (prev_date is not None and
date - prev_date != date.resolution):
# This is not the next day, reset day index
prev_index = None
if (date not in scheduled_dates or
date not in schooldays):
# Not a scheduled schoolday
yield None
else:
if prev_index is None:
day_index = self.getDayIndex(schedule, schooldays, date)
else:
day_index = (prev_index + 1) % len(self.templates)
keys = self.templates.keys()
yield self.templates[keys[day_index]]
prev_index = day_index
prev_date = date
@adapter(interfaces.IDayTemplateSchedule)
@implementer(interfaces.ISchedule)
def getScheduledTemplatesSchedule(day_templates):
return day_templates.__parent__
|