~ubuntu-branches/ubuntu/raring/schooltool.intervention/raring

« back to all changes in this revision

Viewing changes to src/schooltool/intervention/adapters.py

  • Committer: Gediminas Paulauskas
  • Date: 2011-09-19 16:56:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: menesis@pov.lt-20110919165645-718diuud5tc4mjsx
Tags: 0.5.0-0ubuntu1
* New upstream release.
* debian/rules: move gradebook to Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# SchoolTool - common information systems platform for school administration
 
3
# Copyright (c) 2005 Shuttleworth Foundation
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
#
 
19
 
 
20
import pytz
 
21
from persistent import Persistent
 
22
from datetime import datetime
 
23
import rwproperty
 
24
 
 
25
from zope.container.contained import NameChooser
 
26
from zope.container.interfaces import INameChooser
 
27
from zope.component import queryUtility, getMultiAdapter
 
28
from zope.component import adapter, adapts
 
29
from zope.interface import implementer, implements
 
30
from zope.lifecycleevent.interfaces import IObjectRemovedEvent
 
31
from zope.security.proxy import removeSecurityProxy
 
32
from zope.traversing.api import getParent
 
33
 
 
34
from schooltool.app.app import InitBase, StartUpBase
 
35
from schooltool.app.interfaces import ISchoolToolApplication
 
36
from schooltool.contact.interfaces import IContactable, IContact
 
37
from schooltool.person.interfaces import IPerson
 
38
from schooltool.schoolyear.interfaces import ISchoolYear, ISchoolYearContainer
 
39
from schooltool.schoolyear.subscriber import ObjectEventAdapterSubscriber
 
40
from schooltool.term.interfaces import IDateManager
 
41
 
 
42
import interfaces, intervention
 
43
 
 
44
 
 
45
def setUpInterventions(app):
 
46
    app[u'schooltool.interventions'] = intervention.InterventionRoot()
 
47
 
 
48
 
 
49
class InterventionStartup(StartUpBase):
 
50
    def __call__(self):
 
51
        if u'schooltool.interventions' not in self.app:
 
52
            setUpInterventions(self.app)
 
53
 
 
54
 
 
55
class InterventionInit(InitBase):
 
56
    """Create the InterventionRoot object."""
 
57
 
 
58
    def __call__(self):
 
59
        setUpInterventions(self.app)
 
60
 
 
61
 
 
62
@adapter(ISchoolToolApplication)
 
63
@implementer(interfaces.IInterventionRoot)
 
64
def getInterventionRoot(app):
 
65
    return app[u'schooltool.interventions']
 
66
 
 
67
 
 
68
@adapter(ISchoolYear)
 
69
@implementer(interfaces.IInterventionSchoolYear)
 
70
def getSchoolYearInterventionSchoolYear(schoolyear):
 
71
    app = ISchoolToolApplication(None)
 
72
    interventionRoot = interfaces.IInterventionRoot(app)
 
73
    try:
 
74
        interventionSchoolYear = interventionRoot[schoolyear.__name__]
 
75
    except KeyError:
 
76
        interventionSchoolYear = intervention.InterventionSchoolYear()
 
77
        interventionRoot[schoolyear.__name__] = interventionSchoolYear
 
78
    return interventionSchoolYear
 
79
 
 
80
 
 
81
@adapter(ISchoolToolApplication)
 
82
@implementer(interfaces.IInterventionSchoolYear)
 
83
def getSchoolToolApplicationInterventionSchoolYear(app):
 
84
    term = queryUtility(IDateManager).current_term
 
85
    schoolyear = ISchoolYear(term)
 
86
    return interfaces.IInterventionSchoolYear(schoolyear)
 
87
 
 
88
 
 
89
@adapter(interfaces.IInterventionSchoolYear)
 
90
@implementer(ISchoolYear)
 
91
def getInterventionSchoolYearSchoolYear(schoolyear):
 
92
    app = ISchoolToolApplication(None)
 
93
    return ISchoolYearContainer(app)[schoolyear.__name__]
 
94
 
 
95
 
 
96
@adapter(interfaces.IInterventionMarker)
 
97
@implementer(interfaces.IInterventionStudent)
 
98
def getMarkerInterventionStudent(marker):
 
99
    return interfaces.IInterventionStudent(marker.__parent__.__parent__)
 
100
 
 
101
 
 
102
@adapter(IPerson, ISchoolYear)
 
103
@implementer(interfaces.IInterventionStudent)
 
104
def getStudentYearInterventionStudent(student, schoolyear):
 
105
    interventionSchoolYear = interfaces.IInterventionSchoolYear(schoolyear)
 
106
    try:
 
107
        interventionStudent = interventionSchoolYear[student.__name__]
 
108
    except KeyError:
 
109
        interventionStudent = intervention.InterventionStudent()
 
110
        interventionSchoolYear[student.__name__] = interventionStudent
 
111
        interventionStudent[u'messages'] = intervention.InterventionMessages()
 
112
        interventionStudent[u'goals'] = intervention.InterventionGoals()
 
113
    return interventionStudent
 
114
 
 
115
 
 
116
@adapter(IPerson)
 
117
@implementer(interfaces.IInterventionStudent)
 
118
def getStudentInterventionStudent(student):
 
119
    app = ISchoolToolApplication(None)
 
120
    interventionSchoolYear = interfaces.IInterventionSchoolYear(app)
 
121
    schoolyear = ISchoolYear(interventionSchoolYear)
 
122
    interventionStudent = getMultiAdapter((student, schoolyear), 
 
123
        interfaces.IInterventionStudent)
 
124
    return interventionStudent
 
125
 
 
126
 
 
127
@adapter(interfaces.IStudentSchoolYearProxy)
 
128
@implementer(interfaces.IInterventionStudent)
 
129
def getSchoolYearProxyInterventionStudent(proxy):
 
130
    student = proxy.__parent__.__parent__
 
131
    app = ISchoolToolApplication(None)
 
132
    schoolyear = ISchoolYearContainer(app)[proxy.__name__]
 
133
    interventionStudent = getMultiAdapter((student, schoolyear), 
 
134
        interfaces.IInterventionStudent)
 
135
    return interventionStudent
 
136
 
 
137
 
 
138
@adapter(interfaces.IInterventionStudent)
 
139
@implementer(IPerson)
 
140
def getInterventionStudentStudent(interventionStudent):
 
141
    persons = ISchoolToolApplication(None)['persons']
 
142
    return persons[interventionStudent.__name__]
 
143
 
 
144
 
 
145
@adapter(interfaces.IInterventionMessages)
 
146
@implementer(IPerson)
 
147
def getInterventionMessagesStudent(interventionMessages):
 
148
    return IPerson(interventionMessages.__parent__)
 
149
 
 
150
 
 
151
@adapter(interfaces.IInterventionMessage)
 
152
@implementer(IPerson)
 
153
def getInterventionMessageStudent(interventionMessage):
 
154
    return IPerson(interventionMessage.__parent__)
 
155
 
 
156
 
 
157
@adapter(interfaces.IInterventionGoals)
 
158
@implementer(IPerson)
 
159
def getInterventionGoalsStudent(interventionGoals):
 
160
    return IPerson(interventionGoals.__parent__)
 
161
 
 
162
 
 
163
@adapter(interfaces.IInterventionGoal)
 
164
@implementer(IPerson)
 
165
def getInterventionGoalStudent(interventionGoal):
 
166
    return IPerson(interventionGoal.__parent__)
 
167
 
 
168
 
 
169
@adapter(interfaces.ISectionMessagesProxy)
 
170
@implementer(IPerson)
 
171
def getSectionMessagesProxyStudent(proxy):
 
172
    return removeSecurityProxy(proxy).student
 
173
 
 
174
 
 
175
@adapter(interfaces.IStudentMessagesGoalsProxy)
 
176
@implementer(IPerson)
 
177
def getSectionMessagesGoalsProxyStudent(proxy):
 
178
    return removeSecurityProxy(proxy.__parent__.__parent__).student
 
179
 
 
180
 
 
181
def getInterventionSchoolYearFromObj(obj):
 
182
    while not interfaces.IInterventionSchoolYear.providedBy(obj):
 
183
        if interfaces.IStudentSchoolYearProxy.providedBy(obj):
 
184
            return obj.year
 
185
        obj = getParent(obj)
 
186
    return obj
 
187
 
 
188
 
 
189
class SequenceNumberNameChooser(NameChooser):
 
190
    """A name chooser that returns a sequence number."""
 
191
 
 
192
    implements(INameChooser)
 
193
 
 
194
    def chooseName(self, name, obj):
 
195
        """See INameChooser."""
 
196
        numbers = [int(v.__name__) for v in self.context.values()
 
197
                   if v.__name__.isdigit()]
 
198
        if numbers:
 
199
            n = str(max(numbers) + 1)
 
200
        else:
 
201
            n = '1'
 
202
        self.checkName(n, obj)
 
203
        return n
 
204
 
 
205
 
 
206
class PersonRemovedSubsciber(ObjectEventAdapterSubscriber):
 
207
    adapts(IObjectRemovedEvent, IPerson)
 
208
 
 
209
    def __call__(self):
 
210
        app = ISchoolToolApplication(None)
 
211
        interventionRoot = app[u'schooltool.interventions']
 
212
        for schoolYear in interventionRoot.values():
 
213
            if self.object.username in schoolYear:
 
214
                del schoolYear[self.object.username]
 
215
 
 
216
 
 
217
class SchoolYearRemovedSubsciber(ObjectEventAdapterSubscriber):
 
218
    adapts(IObjectRemovedEvent, ISchoolYear)
 
219
 
 
220
    def __call__(self):
 
221
        app = ISchoolToolApplication(None)
 
222
        interventionRoot = app[u'schooltool.interventions']
 
223
        if self.object.__name__ in interventionRoot:
 
224
            del interventionRoot[self.object.__name__]
 
225