/usr/share/pyshared/schooltool/timetable/app.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 | #
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2011 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
#
"""Timetable integration with SchoolTool app.
XXX: move to schooltool/app/timetable.py or similar.
"""
import zope.lifecycleevent
import zope.lifecycleevent.interfaces
from zope.proxy import sameProxiedObjects
from zope.interface import implements, implementer
from zope.component import adapts, adapter, getUtility, queryAdapter
from zope.annotation.interfaces import IAttributeAnnotatable
from zope.container.interfaces import IContainer
from zope.container.btree import BTreeContainer
from zope.intid.interfaces import IIntIds
from schooltool.app.app import InitBase, StartUpBase
from schooltool.app.interfaces import ISchoolToolApplication
from schooltool.common import DateRange
from schooltool.course.section import InstructorsCrowd, LearnersCrowd
from schooltool.course.interfaces import ISection
from schooltool.schoolyear.subscriber import ObjectEventAdapterSubscriber
from schooltool.schoolyear.interfaces import ISchoolYear
from schooltool.securitypolicy.crowds import Crowd
from schooltool.securitypolicy.crowds import AggregateCrowd
from schooltool.timetable import interfaces
from schooltool.timetable.schedule import ScheduleContainer
from schooltool.timetable.timetable import TimetableContainer
SCHEDULES_KEY = 'schooltool.timetable.schedules'
TIMETABLES_KEY = 'schooltool.timetable.timetables'
class ISchoolToolSchedules(IContainer):
"""A container of schedules."""
class SchoolToolSchedules(BTreeContainer):
implements(ISchoolToolSchedules,
IAttributeAnnotatable)
class TimetableInit(InitBase):
def __call__(self):
self.app[SCHEDULES_KEY] = SchoolToolSchedules()
self.app[TIMETABLES_KEY] = SchoolToolSchedules()
class TimetableStartUp(StartUpBase):
def __call__(self):
if SCHEDULES_KEY not in self.app:
self.app[SCHEDULES_KEY] = SchoolToolSchedules()
if TIMETABLES_KEY not in self.app:
self.app[TIMETABLES_KEY] = SchoolToolSchedules()
@adapter(interfaces.IHaveSchedule)
@implementer(interfaces.IScheduleContainer)
def getScheduleContainer(obj):
int_ids = getUtility(IIntIds)
obj_id = str(int_ids.getId(obj))
app = ISchoolToolApplication(None)
container = app[SCHEDULES_KEY].get(obj_id, None)
if container is None:
container = app[SCHEDULES_KEY][obj_id] = ScheduleContainer()
return container
@implementer(interfaces.IHaveSchedule)
@adapter(interfaces.IScheduleContainer)
def getScheduleContainerOwner(container):
int_ids = getUtility(IIntIds)
obj_id = int(container.__name__)
obj = int_ids.queryObject(obj_id)
return obj
@implementer(interfaces.IHaveSchedule)
@adapter(interfaces.ISchedule)
def getScheduleOwner(schedule):
container = schedule.__parent__
return interfaces.IHaveSchedule(container, None)
@implementer(interfaces.ITimetableContainer)
@adapter(interfaces.ITimetable)
def getTimetableParent(timetable):
return timetable.__parent__
@adapter(interfaces.IHaveTimetables)
@implementer(interfaces.ITimetableContainer)
def getTimetableContainer(obj):
int_ids = getUtility(IIntIds)
obj_id = str(int_ids.getId(obj))
app = ISchoolToolApplication(None)
container = app[TIMETABLES_KEY].get(obj_id, None)
if container is None:
container = app[TIMETABLES_KEY][obj_id] = TimetableContainer()
return container
@adapter(interfaces.ITimetableContainer)
@implementer(interfaces.IHaveTimetables)
def getTimetableContainerOwner(container):
int_ids = getUtility(IIntIds)
obj_id = int(container.__name__)
obj = int_ids.queryObject(obj_id)
return obj
@implementer(interfaces.IHaveTimetables)
@adapter(interfaces.ISchedule)
def getScheduleTimetableOwner(schedule):
container = schedule.__parent__
return interfaces.IHaveTimetables(container, None)
def activityVocabularyFactory():
return lambda context: interfaces.activity_types
class UpdateSelectedPeriodsSchedules(ObjectEventAdapterSubscriber):
adapts(zope.lifecycleevent.interfaces.IObjectModifiedEvent,
interfaces.ITimetable)
def __call__(self):
app = ISchoolToolApplication(None)
# XXX: extremely nasty loop through all schedules.
schedule_containers = app[SCHEDULES_KEY]
for container in schedule_containers.values():
notify_container = False
for schedule in container.values():
if (interfaces.ISelectedPeriodsSchedule.providedBy(schedule) and
sameProxiedObjects(schedule.timetable, self.object)):
notify_container = True
if notify_container:
zope.lifecycleevent.modified(container)
class RemoveRelatedSelectedPeriodsSchedules(ObjectEventAdapterSubscriber):
adapts(zope.lifecycleevent.interfaces.IObjectRemovedEvent,
interfaces.ITimetable)
def __call__(self):
app = ISchoolToolApplication(None)
# XXX: extremely nasty loop through all schedules.
schedule_containers = app[SCHEDULES_KEY]
for container in schedule_containers.values():
for schedule in container.values():
if (interfaces.ISelectedPeriodsSchedule.providedBy(schedule) and
sameProxiedObjects(schedule.timetable, self.object)):
del container[schedule.__name__]
class SchooldaysForSchedule(object):
adapts(interfaces.ISchedule)
implements(interfaces.ISchooldays)
def __init__(self, context):
self.schedule = context
@property
def schoolyear(self):
owner = interfaces.IHaveSchedule(self.schedule)
return ISchoolYear(owner)
def __contains__(self, date):
for term in self.schoolyear.values():
if date in term:
return term.isSchoolday(date)
return False
def __iter__(self):
schedule = self.schedule
dates = DateRange(schedule.first, schedule.last)
return self.iterDates(dates)
def iterDates(self, dates):
for date in dates:
if date in self:
yield date
class SchooldaysForTimetable(SchooldaysForSchedule):
adapts(interfaces.ITimetable)
@property
def schoolyear(self):
owner = interfaces.IHaveTimetables(self.schedule)
return ISchoolYear(owner)
class AdaptingParentCrowdTemplate(Crowd):
"""A crowd that contains principals who are allowed to access the context."""
adapter = None
interface = None
permission = ''
def contains(self, principal):
adapted = self.adapter(self.context)
pcrowd = queryAdapter(adapted, self.interface, self.permission,
default=None)
if pcrowd is not None:
return pcrowd.contains(principal)
else:
return False
class ScheduleViewersCrowd(AdaptingParentCrowdTemplate):
adapter = interfaces.IHaveSchedule
interface = interfaces.IScheduleParentCrowd
permission = "schooltool.view"
class SectionScheduleViewers(AggregateCrowd):
"""Crowd of those who can see the section schedule."""
adapts(ISection)
def crowdFactories(self):
return [InstructorsCrowd, LearnersCrowd]
|