/usr/share/pyshared/schooltool/course/section.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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | #
# 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
#
"""
Section implementation
"""
from persistent import Persistent
import rwproperty
from zope.annotation.interfaces import IAttributeAnnotatable
from zope.intid.interfaces import IIntIds
from zope.interface import implements
from zope.interface import implementer
from zope.event import notify
from zope.component import getUtility
from zope.component import adapter
from zope.component import adapts
from zope.container.interfaces import INameChooser
from zope.container.btree import BTreeContainer
from zope.container.contained import Contained
from zope.lifecycleevent.interfaces import IObjectRemovedEvent
from zope.proxy import sameProxiedObjects
from zope.security.proxy import removeSecurityProxy
from schooltool.app import membership
from schooltool.app.relationships import URIInstruction, URISection
from schooltool.app.app import InitBase
from schooltool.app import relationships
from schooltool.app.security import ConfigurableCrowd
from schooltool.app.interfaces import ISchoolToolApplication
from schooltool.course import interfaces, booking
from schooltool.group.interfaces import IBaseGroup as IGroup
from schooltool.person.interfaces import IPerson
from schooltool.relationship.relationship import getRelatedObjects
from schooltool.relationship import RelationshipProperty
from schooltool.schoolyear.subscriber import EventAdapterSubscriber
from schooltool.schoolyear.subscriber import ObjectEventAdapterSubscriber
from schooltool.schoolyear.interfaces import ISubscriber
from schooltool.schoolyear.interfaces import ISchoolYear
from schooltool.securitypolicy.crowds import Crowd, AggregateCrowd
from schooltool.term.term import getNextTerm
from schooltool.term.interfaces import ITerm
from schooltool.common import SchoolToolMessage as _
class InvalidSectionLinkException(Exception):
pass
class SectionBeforeLinkingEvent(object):
def __init__(self, first, second):
self.first = first
self.second = second
class Section(Persistent, Contained):
implements(interfaces.ISectionContained,
IAttributeAnnotatable)
_location = None
_previous = None
_next = None
def __init__(self, title="Section", description=None, schedule=None):
self.title = title
self.description = description
@property
def label(self):
instructors = "; ".join([i.title for i in self.instructors])
msg = _('${instructors} -- ${section_title}',
mapping={'instructors': instructors, 'section_title': self.title})
return msg
@property
def size(self):
size = 0
for member in self.members:
if IPerson.providedBy(member):
size = size + 1
elif IGroup.providedBy(member):
size = size + len(member.members)
return size
def _unlinkRangeTo(self, other):
"""Unlink sections between self and the other in self.linked_sections."""
linked = self.linked_sections
if other not in linked or self is other:
return
idx_first, idx_last = sorted([linked.index(self), linked.index(other)])
linked[idx_first]._next = None
for section in linked[idx_first+1:idx_last]:
section._previous = None
section._next = None
linked[idx_last]._previous = None
@rwproperty.getproperty
def previous(self):
return self._previous
@rwproperty.setproperty
def previous(self, new):
new = removeSecurityProxy(new)
if new is self._previous:
return
if new is self:
raise InvalidSectionLinkException(
_('Cannot assign section as previous to itself'))
notify(SectionBeforeLinkingEvent(new, self))
if new is not None:
self._unlinkRangeTo(new)
old_prev = self._previous
self._previous = None
if old_prev is not None:
old_prev.next = None
self._previous = new
if new is not None:
new.next = self
@rwproperty.getproperty
def next(self):
return self._next
@rwproperty.setproperty
def next(self, new):
new = removeSecurityProxy(new)
if new is self._next:
return
if new is self:
raise InvalidSectionLinkException(
_('Cannot assign section as next to itself'))
notify(SectionBeforeLinkingEvent(self, new))
if new is not None:
self._unlinkRangeTo(new)
old_next = self._next
self._next = None
if old_next is not None:
old_next.previous = None
self._next = new
if new is not None:
new.previous = self
@property
def linked_sections(self):
sections = [self]
pit = self.previous
while pit:
sections.insert(0, pit)
pit = pit.previous
nit = self.next
while nit:
sections.append(nit)
nit = nit.next
return sections
instructors = RelationshipProperty(relationships.URIInstruction,
relationships.URISection,
relationships.URIInstructor)
courses = RelationshipProperty(relationships.URICourseSections,
relationships.URISectionOfCourse,
relationships.URICourse)
members = RelationshipProperty(membership.URIMembership,
membership.URIGroup,
membership.URIMember)
resources = RelationshipProperty(booking.URISectionBooking,
booking.URISection,
booking.URIResource)
@adapter(ITerm)
@implementer(interfaces.ISectionContainer)
def getSectionContainer(term):
int_ids = getUtility(IIntIds)
term_id = str(int_ids.getId(term))
app = ISchoolToolApplication(None)
sc = app['schooltool.course.section'].get(term_id, None)
if sc is None:
sc = app['schooltool.course.section'][term_id] = SectionContainer()
return sc
@adapter(interfaces.ISectionContainer)
@implementer(ISchoolYear)
def getSchoolYearForSectionContainer(section_container):
return ISchoolYear(ITerm(section_container))
@adapter(interfaces.ISection)
@implementer(ISchoolYear)
def getSchoolYearForSection(section):
return ISchoolYear(ITerm(section.__parent__))
@adapter(interfaces.ISectionContainer)
@implementer(ITerm)
def getTermForSectionContainer(section_container):
container_id = int(section_container.__name__)
int_ids = getUtility(IIntIds)
container = int_ids.getObject(container_id)
return container
@adapter(interfaces.ISection)
@implementer(ITerm)
def getTermForSection(section):
return ITerm(section.__parent__)
@adapter(interfaces.ISectionContainer)
@implementer(interfaces.ICourseContainer)
def getCourseContainerForSectionContainer(section_container):
return interfaces.ICourseContainer(ISchoolYear(section_container))
@adapter(interfaces.ISection)
@implementer(interfaces.ICourseContainer)
def getCourseContainerForSection(section):
return interfaces.ICourseContainer(ISchoolYear(section))
class SectionContainerContainer(BTreeContainer):
"""Container of Section containers."""
implements(interfaces.ISectionContainerContainer)
class SectionContainer(BTreeContainer):
"""Container of Sections."""
implements(interfaces.ISectionContainer, IAttributeAnnotatable)
class SectionInit(InitBase):
def __call__(self):
self.app['schooltool.course.section'] = SectionContainerContainer()
class InstructorsCrowd(Crowd):
"""Crowd of instructors of a section."""
title = _(u'Instructors')
description = _(u'Instructors of the section.')
def contains(self, principal):
instructors = interfaces.ISection(self.context).instructors
return IPerson(principal, None) in instructors
class PersonInstructorsCrowd(Crowd):
"""Crowd of instructors of a person."""
title = _(u'Instructors')
description = _(u'Instructors of a person in any of his sections.')
def _getSections(self, ob):
return [section for section in getRelatedObjects(ob, membership.URIGroup)
if interfaces.ISection.providedBy(section)]
def contains(self, principal):
user = IPerson(principal, None)
person = IPerson(self.context)
for section in self._getSections(person):
if user in section.instructors:
return True
return False
class LearnersCrowd(Crowd):
"""Crowd of learners of a section.
At the moment only direct members of a section are considered as
learners.
"""
adapts(interfaces.ISection)
title = _(u'Learners')
description = _(u'Students of the section.')
def contains(self, principal):
return IPerson(principal, None) in self.context.members
class SectionCalendarSettingCrowd(ConfigurableCrowd):
adapts(interfaces.ISection)
setting_key = 'everyone_can_view_section_info'
class SectionCalendarViewers(AggregateCrowd):
"""Crowd of those who can see the section calendar."""
adapts(interfaces.ISection)
def crowdFactories(self):
return [InstructorsCrowd, LearnersCrowd, SectionCalendarSettingCrowd]
class PersonLearnerAdapter(object):
implements(interfaces.ILearner)
adapts(IPerson)
def __init__(self, person):
self.person = person
def _getSections(self, ob):
return [section for section in getRelatedObjects(ob, membership.URIGroup)
if interfaces.ISection.providedBy(section)]
def sections(self):
# First check the the sections a pupil is in directly
for section in self._getSections(self.person):
yield section
class PersonInstructorAdapter(object):
implements(interfaces.IInstructor)
adapts(IPerson)
def __init__(self, person):
self.person = person
def sections(self):
return getRelatedObjects(self.person, URISection,
rel_type=URIInstruction)
class RemoveSectionsWhenTermIsDeleted(ObjectEventAdapterSubscriber):
adapts(IObjectRemovedEvent, ITerm)
def __call__(self):
section_container = interfaces.ISectionContainer(self.object)
for section_id in list(section_container.keys()):
del section_container[section_id]
class UnlinkSectionWhenDeleted(ObjectEventAdapterSubscriber):
adapts(IObjectRemovedEvent, interfaces.ISection)
def __call__(self):
self.object.previous = None
self.object.next = None
class SectionLinkContinuinityValidationSubscriber(EventAdapterSubscriber):
adapts(SectionBeforeLinkingEvent)
implements(ISubscriber)
def __call__(self):
if (self.event.first is None or
self.event.second is None):
return # unlinking sections
first_term = ITerm(self.event.first)
second_term = ITerm(self.event.second)
if sameProxiedObjects(first_term, second_term):
raise InvalidSectionLinkException(
_("Cannot link sections in same term"))
if not sameProxiedObjects(ISchoolYear(first_term),
ISchoolYear(second_term)):
raise InvalidSectionLinkException(
_("Cannot link sections in different school years"))
if not sameProxiedObjects(getNextTerm(first_term), second_term):
raise InvalidSectionLinkException(
_("Sections must be in consecutive terms"))
def getSectionRosterEventParticipants(event, rel_type):
if rel_type != event.rel_type:
return None, None
if interfaces.ISection.providedBy(event.participant1):
return event.participant1, event.participant2
elif interfaces.ISection.providedBy(event.participant2):
return event.participant2, event.participant1
else:
return None, None
def propagateSectionInstructorAdded(event):
section, teacher = getSectionRosterEventParticipants(event,
relationships.URIInstruction)
if section is None:
return
if section.next and teacher not in section.next.instructors:
section.next.instructors.add(teacher)
def propagateSectionInstructorRemoved(event):
section, teacher = getSectionRosterEventParticipants(event,
relationships.URIInstruction)
if section is None:
return
if section.next and teacher in section.next.instructors:
section.next.instructors.remove(teacher)
def propagateSectionStudentAdded(event):
section, student = getSectionRosterEventParticipants(event,
relationships.URIMembership)
if section is None:
return
if section.next and student not in section.next.members:
section.next.members.add(student)
def propagateSectionStudentRemoved(event):
section, student = getSectionRosterEventParticipants(event,
relationships.URIMembership)
if section is None:
return
if section.next and student in section.next.members:
section.next.members.remove(student)
def copySection(section, target_term):
"""Create a copy of a section in a desired term."""
section_copy = Section(section.title, section.description)
sections = interfaces.ISectionContainer(target_term)
name = section.__name__
if name in sections:
name = INameChooser(sections).chooseName(name, section_copy)
sections[name] = section_copy
for course in section.courses:
section_copy.courses.add(course)
for instructor in section.instructors:
section_copy.instructors.add(instructor)
for member in section.members:
section_copy.members.add(member)
return section_copy
|