/usr/share/pyshared/schooltool/app/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 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 | #
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2005-2010 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
#
"""
SchoolTool application
"""
import calendar
from persistent import Persistent
from persistent.dict import PersistentDict
from zope.location.location import LocationProxy
from zope.component import adapter, adapts
from zope.i18n import translate
from zope.interface import implementer, implements, implementsOnly
from zope.annotation.interfaces import IAttributeAnnotatable, IAnnotations
from zope.app.applicationcontrol.interfaces import IApplicationControl
from zope.app.applicationcontrol.applicationcontrol import applicationController
from zope.component.hooks import getSite
from zope.site import SiteManagerContainer
from zope.container import sample
from zope.container.contained import NameChooser
from zope.container.interfaces import INameChooser
from zope.traversing.interfaces import IContainmentRoot
from schooltool.app.overlay import ICalendarOverlayInfo
from schooltool.app.interfaces import IPluginInit, IPluginStartUp
from schooltool.app.interfaces import ISchoolToolApplication
from schooltool.app.interfaces import IApplicationPreferences, IApplicationTabs
from schooltool.app import relationships
from schooltool.app.interfaces import IAsset
from schooltool.relationship.relationship import RelationshipProperty
from schooltool.common import getRequestFromInteraction
from schooltool.common import SchoolToolMessage as _
class SchoolToolApplication(Persistent, sample.SampleContainer,
SiteManagerContainer):
"""The main application object.
This object can be added as a regular content object to a folder,
or it can be used as the application root object.
"""
implements(ISchoolToolApplication, IAttributeAnnotatable, IContainmentRoot)
def __init__(self):
super(SchoolToolApplication, self).__init__()
def _newContainerData(self):
return PersistentDict()
def _title(self):
"""This is required for the site calendar views to work.
Calendar views expect that their underlying objects (calendar
parents) have an attribute named `title`.
"""
return IApplicationPreferences(self).title
title = property(_title)
@adapter(None)
@implementer(ISchoolToolApplication)
def getSchoolToolApplication(ignore=None):
"""Return the nearest ISchoolToolApplication.
This function is also registered as an adapter, so you
can use it like this:
app = ISchoolToolApplication(None)
and stub it in unit tests.
"""
candidate = getSite()
if ISchoolToolApplication.providedBy(candidate):
return candidate
return None
class SimpleNameChooser(NameChooser):
"""A name chooser that uses object titles as names.
SimpleNameChooser is an adapter for containers
>>> class ContainerStub(dict):
... __name__ = 'resources'
>>> container = ContainerStub()
>>> chooser = SimpleNameChooser(container)
It expects objects to have a `title` attribute, so only register it
as an adapter for containers that limit their contents to objects
with such attribute.
`chooseName` uses the title of the object to be added, converts it to
lowercase, strips punctuation, and replaces spaces with hyphens:
>>> from schooltool.person.person import Person
>>> obj = Person(title='Mr. Smith')
>>> chooser.chooseName('', obj)
u'mr-smith'
If the name is already taken, SimpleNameChooser adds a number at the end
>>> container['mr-smith'] = 42
>>> chooser.chooseName('', obj)
u'mr-smith-2'
If you provide a suggested name, it uses that one.
>>> chooser.chooseName('suggested-name', obj)
'suggested-name'
Even if it has "illegal" characters:
>>> chooser.chooseName('suggested name', obj)
'suggested name'
But it will add a number at the end:
>>> chooser.chooseName('mr-smith', obj)
u'mr-smith-2'
Bad names cause errors
>>> chooser.chooseName('@notallowed', obj)
Traceback (most recent call last):
...
ValueError: Names cannot begin with '+' or '@' or contain '/'
If the name generated from the title gets shortened too much, we
generate a name from the name of the context container instead:
>>> obj.title = "foo-bar-baz-boo"
>>> chooser.chooseName('', obj)
'resource'
So objects with semi empty titles get ids for them too:
>>> obj.title = '---'
>>> chooser.chooseName('', obj)
'resource'
"""
implements(INameChooser)
def chooseName(self, name, obj):
"""See INameChooser."""
if not name:
name = u''.join([c for c in obj.title.lower()
if c.isalnum() or c == ' ']).replace(' ', '-')
if len(name) + 2 < len(obj.title) or name == '':
name = self.context.__name__[:-1]
n = name
i = 1
while n in self.context:
i += 1
n = name + u'-' + unicode(i)
# Make sure the name is valid
self.checkName(n, obj)
return n
class ApplicationPreferences(Persistent):
"""Object for storing any application-wide preferences we have.
See schooltool.app.interfaces.ApplicationPreferences.
"""
implements(IApplicationPreferences)
def getTitle(self):
request = getRequestFromInteraction()
return (self.__dict__.get('title', None) or
translate(_('Your School'), context=request))
def setTitle(self, value):
self.__dict__['title'] = value
title = property(getTitle, setTitle)
timezone = 'UTC'
dateformat = '%Y-%m-%d'
timeformat = '%H:%M'
weekstart = calendar.MONDAY
frontPageCalendar = True
class ApplicationTabs(PersistentDict):
"""Object for storing application tab preferences.
See schooltool.app.interfaces.ApplicationTabs.
"""
implements(IApplicationTabs)
default = 'calendar'
class Asset(object):
"""A mixin for objects that may act as assets."""
implements(IAsset)
leaders = RelationshipProperty(relationships.URILeadership,
relationships.URIAsset,
relationships.URILeader)
def getApplicationPreferences(app):
"""Adapt a SchoolToolApplication to IApplicationPreferences."""
annotations = IAnnotations(app)
key = 'schooltool.app.ApplicationPreferences'
try:
return annotations[key]
except KeyError:
annotations[key] = ApplicationPreferences()
annotations[key].__parent__ = app
return annotations[key]
def getApplicationTabs(app):
"""Adapt a SchoolToolApplication to IApplicationTabs."""
annotations = IAnnotations(app)
key = 'schooltool.app.ApplicationTabs'
try:
return annotations[key]
except KeyError:
annotations[key] = ApplicationTabs()
annotations[key].__parent__ = app
return annotations[key]
class SchoolToolInitializationUtility(object):
def initializeApplication(self, app):
"""Perform school specific initialization.
By default schooltool does not do any specific initialization.
"""
class ActionBase(object):
adapts(ISchoolToolApplication)
implements(IPluginInit)
after = ()
before = ()
def __init__(self, app):
self.app = app
def __call__(self):
raise NotImplementedError("This method should be overriden by"
" inheriting classes")
class InitBase(ActionBase):
implementsOnly(IPluginInit)
class StartUpBase(ActionBase):
implementsOnly(IPluginStartUp)
@adapter(ISchoolToolApplication)
@implementer(IApplicationControl)
def getApplicationControl(app=None):
return LocationProxy(applicationController, app, 'control')
|