This file is indexed.

/usr/share/pyshared/schooltool/schoolyear/schoolyear.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
#
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2008 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
#
"""
School year implementation
"""
import rwproperty
import datetime

from zope.proxy import sameProxiedObjects
from zope.event import notify
from zope.component import queryUtility
from zope.component import adapter
from zope.component import adapts
from zope.interface import implementer
from zope.interface import implements
from zope.container.btree import BTreeContainer

from schooltool.common import DateRange
from schooltool.common import IDateRange
from schooltool.app.interfaces import ISchoolToolApplication
from schooltool.app.app import InitBase
from schooltool.term.interfaces import IDateManager
from schooltool.term.interfaces import ITermContainer
from schooltool.schoolyear.subscriber import EventAdapterSubscriber
from schooltool.schoolyear.interfaces import TermOverflowError
from schooltool.schoolyear.interfaces import TermOverlapError
from schooltool.schoolyear.interfaces import SchoolYearOverlapError
from schooltool.schoolyear.interfaces import ISubscriber
from schooltool.schoolyear.interfaces import ISchoolYear
from schooltool.schoolyear.interfaces import ISchoolYearContainer

SCHOOLYEAR_CONTAINER_KEY = 'schooltool.schoolyear'


class SchoolYearBeforeChangeEvent(object):

    def __init__(self, schoolyear, old_dates, new_dates):
        self.schoolyear = schoolyear
        self.old_dates = old_dates
        self.new_dates = new_dates


class SchoolYearAfterChangeEvent(object):

    def __init__(self, schoolyear, old_dates, new_dates):
        self.schoolyear = schoolyear
        self.old_dates = old_dates
        self.new_dates = new_dates


class SchoolYearContainer(BTreeContainer):
    implements(ISchoolYearContainer)

    _active_id = None

    def _set_active_id(self, new_id):
        if new_id is not None and new_id not in self:
            raise ValueError("School Year %r does not exist" % new_id)
        self._active_id = new_id

    active_id = property(lambda self: self._active_id)

    def __delitem__(self, schoolyear_id):
        if schoolyear_id == self.active_id:
            if len(self.values()) > 1:
                raise ValueError("Can not delete an active schoolyear, unless"
                                 " it is the last school year available!")
            else:
                self._set_active_id(None)
        BTreeContainer.__delitem__(self, schoolyear_id)

    def validateForOverlap(self, schoolyear):
        overlapping_schoolyears = []
        for other_schoolyear in self.values():
            if IDateRange(schoolyear).overlaps(IDateRange(other_schoolyear)):
                overlapping_schoolyears.append(other_schoolyear)

        if overlapping_schoolyears:
            raise SchoolYearOverlapError(schoolyear, overlapping_schoolyears)

    def __setitem__(self, key, schoolyear):
        self.validateForOverlap(schoolyear)
        BTreeContainer.__setitem__(self, key, schoolyear)
        if self.active_id is None:
            self._set_active_id(key)

    def getActiveSchoolYear(self):
        if self.active_id:
            return self[self.active_id]
        return None

    @property
    def sorted_schoolyears(self):
        return sorted(self.values(), key=lambda s: s.last)

    def activateNextSchoolYear(self, year_id=None):
        if year_id is None:
            next = self.getNextSchoolYear()
            if next is not None:
                year_id = next.__name__
        self._set_active_id(year_id)

    def getLastSchoolYearForDate(self, date):
        before, after = [], []
        for year in self.sorted_schoolyears:
            if date in IDateRange(year):
                return year
            if date > year.last:
                before.append((year.last, year))
            if date < year.first:
                after.append((year.first, year))
        if before:
            return max(before)[1]
        if after:
            return min(after)[1]
        return None

    def getSchoolYearForToday(self):
        dtm = queryUtility(IDateManager)
        today = dtm.today
        for year in self.sorted_schoolyears:
            if today in IDateRange(year):
                return year
        return None

    def getNextSchoolYear(self):
        if self.getActiveSchoolYear() is None:
            return None

        this_schoolyear_index = self.sorted_schoolyears.index(self.getActiveSchoolYear())
        next_schoolyear_index = this_schoolyear_index + 1
        if next_schoolyear_index < len(self.sorted_schoolyears):
            return self.sorted_schoolyears[next_schoolyear_index]

        return None


class SchoolYear(BTreeContainer):
    implements(ITermContainer, ISchoolYear)

    def __init__(self, title, first, last):
        self.title = title
        self._first = first
        self._last = last
        if last < first:
            raise ValueError("Last date %r less than first date %r" %
                             (last, first))
        BTreeContainer.__init__(self)

    @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(SchoolYearBeforeChangeEvent(self, old_dates, new_dates))
        self._first = new_first_date
        notify(SchoolYearAfterChangeEvent(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(SchoolYearBeforeChangeEvent(self, old_dates, new_dates))
        self._last = new_last_date
        notify(SchoolYearAfterChangeEvent(self, old_dates, new_dates))

    def validateForOverlap(self, term):
        overlapping_terms = []
        for other_term in self.values():
            if term.overlaps(other_term):
                overlapping_terms.append(other_term)

        if overlapping_terms:
            raise TermOverlapError(term, overlapping_terms)

    def __setitem__(self, key, term):
        self.validateForOverlap(term)
        if term.first < self.first:
            raise ValueError("Term can't start before the school year starts!")
        if term.last > self.last:
            raise ValueError("Term can't end after the school year ends!")
        BTreeContainer.__setitem__(self, key, term)


class SchoolYearDateRangeAdapter(DateRange):
    adapts(ISchoolYear)
    implements(IDateRange)

    def __init__(self, context):
        self.context = context

    @rwproperty.setproperty
    def first(self, new_first_date):
        self.context.first = new_first_date

    @rwproperty.getproperty
    def first(self):
        return self.context.first

    @rwproperty.setproperty
    def last(self, new_last_date):
        self.context.first = new_last_date

    @rwproperty.getproperty
    def last(self):
        return self.context.last


@adapter(ISchoolToolApplication)
@implementer(ISchoolYearContainer)
def getSchoolYearContainer(app):
    return app[SCHOOLYEAR_CONTAINER_KEY]


class SchoolYearInit(InitBase):

    adapts(ISchoolToolApplication)

    def __call__(self):
        self.app[SCHOOLYEAR_CONTAINER_KEY] = SchoolYearContainer()


def validateScholYearsForOverlap(syc, dr, schoolyear):
    overlapping_schoolyears = []
    for other_schoolyear in syc.values():
        if (not sameProxiedObjects(other_schoolyear, schoolyear) and
            dr.overlaps(IDateRange(other_schoolyear))):
            overlapping_schoolyears.append(other_schoolyear)
            if overlapping_schoolyears:
                raise SchoolYearOverlapError(schoolyear,
                                             overlapping_schoolyears)


class SchoolYearOverlapValidationSubscriber(EventAdapterSubscriber):
    adapts(SchoolYearBeforeChangeEvent)
    implements(ISubscriber)

    def __call__(self):
        syc = self.event.schoolyear.__parent__
        dr = DateRange(*self.event.new_dates)
        if syc:
            validateScholYearsForOverlap(syc, dr, self.event.schoolyear)


def validateScholYearForOverflow(dr, schoolyear):
    overflowing_terms = []
    for term in schoolyear.values():
        if (term.first not in dr or
            term.last not in dr):
            overflowing_terms.append(term)
    if overflowing_terms:
        raise TermOverflowError(schoolyear, overflowing_terms)


class SchoolYearTermOverflowValidationSubscriber(EventAdapterSubscriber):
    adapts(SchoolYearBeforeChangeEvent)
    implements(ISubscriber)

    def __call__(self):
        overflowing_terms = []
        sy = self.event.schoolyear
        dr = DateRange(*self.event.new_dates)
        validateScholYearForOverflow(dr, sy)


@adapter(datetime.date)
@implementer(ITermContainer)
def getTermContainerForDate(date):
    app = ISchoolToolApplication(None)
    syc = ISchoolYearContainer(app)
    year = syc.getLastSchoolYearForDate(date)
    term_container = ITermContainer(year, None)
    return term_container