/usr/share/pyshared/schooltool/term/term.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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | #
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2003 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
#
"""
Term implementation
"""
import persistent
import rwproperty
import pytz
from datetime import datetime
import zope.interface
from zope.event import notify
from zope.proxy import sameProxiedObjects
from zope.component import adapts
from zope.component import adapter
from zope.interface import implements
from zope.interface import implementer
from zope.lifecycleevent.interfaces import IObjectRemovedEvent
from zope.container import contained, btree
from schooltool.schoolyear.subscriber import EventAdapterSubscriber
from schooltool.schoolyear.subscriber import ObjectEventAdapterSubscriber
from schooltool.schoolyear.interfaces import TermOverlapError
from schooltool.schoolyear.interfaces import ISubscriber
from schooltool.schoolyear.interfaces import ISchoolYear
from schooltool.schoolyear.interfaces import ISchoolYearContainer
from schooltool.app.interfaces import IApplicationPreferences
from schooltool.app.interfaces import ISchoolToolApplication
from schooltool.common import IDateRange
from schooltool.common import DateRange
from schooltool.term.interfaces import TermDateNotInSchoolYear
from schooltool.term import interfaces
from schooltool.common import SchoolToolMessage as _
class TermBeforeChangeEvent(object):
def __init__(self, term, old_dates, new_dates):
self.term = term
self.old_dates = old_dates
self.new_dates = new_dates
class TermAfterChangeEvent(object):
def __init__(self, term, old_dates, new_dates):
self.term = term
self.old_dates = old_dates
self.new_dates = new_dates
class EmergencyDayEvent(object):
def __init__(self, date, replacement_date):
self.date = date
self.replacement_date = replacement_date
class Term(DateRange, contained.Contained, persistent.Persistent):
zope.interface.implements(interfaces.ITerm, interfaces.ITermWrite)
def __init__(self, title, first, last):
self.title = title
self._first = first
self._last = last
self._schooldays = set()
if last < first:
raise ValueError("Last date %r less than first date %r" %
(last, first))
@rwproperty.getproperty
def first(self):
return self._first
@rwproperty.setproperty
def first(self, new_first_date):
old_dates = (self._first, self._last)
new_dates = (new_first_date, self._last)
if self._last < new_first_date:
raise ValueError("Last date %r less than first date %r" %
(self._last, new_first_date))
notify(TermBeforeChangeEvent(self, old_dates, new_dates))
self._first = new_first_date
notify(TermAfterChangeEvent(self, old_dates, new_dates))
@rwproperty.getproperty
def last(self):
return self._last
@rwproperty.setproperty
def last(self, new_last_date):
old_dates = (self._first, self._last)
new_dates = (self._first, new_last_date)
if new_last_date < self._first:
raise ValueError("Last date %r less than first date %r" %
(new_last_date, self._first))
notify(TermBeforeChangeEvent(self, old_dates, new_dates))
self._last = new_last_date
notify(TermAfterChangeEvent(self, old_dates, new_dates))
def _validate(self, date):
if not date in self:
raise ValueError("Date %r not in term [%r, %r]" %
(date, self.first, self.last))
def isSchoolday(self, date):
self._validate(date)
if date in self._schooldays:
return True
return False
def add(self, date):
self._validate(date)
self._schooldays.add(date)
self._schooldays = self._schooldays # persistence
def remove(self, date):
self._validate(date)
self._schooldays.remove(date)
self._schooldays = self._schooldays # persistence
def addWeekdays(self, *weekdays):
for date in self:
if date.weekday() in weekdays:
self.add(date)
def removeWeekdays(self, *weekdays):
for date in self:
if date.weekday() in weekdays and self.isSchoolday(date):
self.remove(date)
def toggleWeekdays(self, *weekdays):
for date in self:
if date.weekday() in weekdays:
if self.isSchoolday(date):
self.remove(date)
else:
self.add(date)
def reset(self, first, last):
if last < first:
# import timemachine
raise ValueError("Last date %r less than first date %r" %
(last, first))
self.first = first
self.last = last
self._schooldays.clear()
class TermContainer(btree.BTreeContainer):
"""BBB: only there for backwards compatibility."""
def getTermForDate(date):
"""Find the term that contains `date`.
Returns None if `date` falls outside all terms.
"""
terms = interfaces.ITermContainer(date, {})
for term in terms.values():
if date in term:
return term
else:
return None
def getNextTermForDate(date):
"""Find the term that contains `date`, or the next one.
If there is a term that contains `date`, it is returned. Otherwise, the
first term that starts after `date` is returned. If there are none,
the last term that ended before `date` is returned.
Returns None if there are no terms.
"""
terms = interfaces.ITermContainer(date, {})
before, after = [], []
for term in terms.values():
if date in term:
return term
if date > term.last:
before.append((term.last, term))
if date < term.first:
after.append((term.first, term))
if after:
return min(after)[1]
if before:
return max(before)[1]
return None
def listTerms(context):
"""List terms of a schoolyear in a chronological order."""
terms = interfaces.ITermContainer(context, {})
return sorted(terms.values(), key=lambda t: t.first)
def getPreviousTerm(term):
"""Get the next term in the same SchoolYear.
The limitation is imposed as SchoolYears are strictly separated
entities (for archival purposes).
"""
prev_terms = [t for t in listTerms(term)
if t.last < term.first]
if prev_terms:
return prev_terms[-1]
return None
def getNextTerm(term):
"""Get the next term in the same SchoolYear.
The limitation is imposed as SchoolYears are strictly separated
entities (for archival purposes).
"""
next_terms = [t for t in listTerms(term)
if t.first > term.last]
if next_terms:
return next_terms[0]
return None
class DateManagerUtility(object):
zope.interface.implements(interfaces.IDateManager)
@property
def today(self):
app = ISchoolToolApplication(None)
tzinfo = pytz.timezone(IApplicationPreferences(app).timezone)
dt = pytz.utc.localize(datetime.utcnow())
return dt.astimezone(tzinfo).date()
@property
def current_term(self):
return getNextTermForDate(self.today)
@implementer(interfaces.ITermContainer)
def getTermContainer(context):
app = ISchoolToolApplication(None)
syc = ISchoolYearContainer(app)
return syc.getActiveSchoolYear()
@adapter(interfaces.ITerm)
@implementer(ISchoolYear)
def getSchoolYearForTerm(term):
return term.__parent__
class RemoveTermsWhenSchoolYearIsDeleted(ObjectEventAdapterSubscriber):
adapts(IObjectRemovedEvent, ISchoolYear)
def __call__(self):
for term_id in list(self.object.keys()):
del self.object[term_id]
def validateTermsForOverlap(sy, dr, term):
overlapping_terms = []
for other_term in sy.values():
if (not sameProxiedObjects(other_term, term) and
dr.overlaps(IDateRange(other_term))):
overlapping_terms.append(other_term)
if overlapping_terms:
raise TermOverlapError(term,
overlapping_terms)
class TermOverlapValidationSubscriber(EventAdapterSubscriber):
adapts(TermBeforeChangeEvent)
implements(ISubscriber)
def __call__(self):
sy = self.event.term.__parent__
dr = DateRange(*self.event.new_dates)
if sy:
validateTermsForOverlap(sy, dr, self.event.term)
class TermOverflowValidationSubscriber(EventAdapterSubscriber):
adapts(TermBeforeChangeEvent)
implements(ISubscriber)
def __call__(self):
if self.event.term.__parent__ is None:
return
dr = IDateRange(self.event.term.__parent__)
if (self.event.new_dates[0] not in dr or
self.event.new_dates[1] not in dr):
raise TermDateNotInSchoolYear(_("Term date is not in the school year."))
|