/usr/share/pyshared/schooltool/app/utils.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 | #
#
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2009 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 views.
"""
import zope.schema
from zope.proxy import sameProxiedObjects
from zope.interface import implements
from schooltool.common import simple_form_key
def vocabulary(choices):
"""Create a SimpleVocabulary from a list of values and titles.
>>> v = vocabulary([('value1', u"Title for value1"),
... ('value2', u"Title for value2")])
>>> for term in v:
... print term.value, '|', term.token, '|', term.title
value1 | value1 | Title for value1
value2 | value2 | Title for value2
"""
return zope.schema.vocabulary.SimpleVocabulary(
[zope.schema.vocabulary.SimpleTerm(v, title=t) for v, t in choices])
def vocabulary_titled(items):
"""Create a SimpleVocabulary from a list of objects having __name__ and
title attributes. Such items are common in many containers in SchoolTool.
>>> class Item(object):
... def __init__(self, name, title):
... self.__name__ = name
... self.title = title
... def __repr__(self):
... return '<Item "%s">' % self.title
>>> v = vocabulary_titled([Item(u'thevalue1', u"Title one"),
... Item(u'\xc5\xa0amas', u"Title two")])
>>> for term in v:
... print term.value, '|', term.token, '|', term.title
<Item "Title one"> | thevalue1- | Title one
<Item "Title two"> | amas-uea5t | Title two
"""
return zope.schema.vocabulary.SimpleVocabulary(
[zope.schema.vocabulary.SimpleTerm(
item,
token=unicode(item.__name__).encode('punycode'),
title=item.title)
for item in items])
class TitledContainerItemVocabulary(object):
"""Vocabulary of titled items in a container."""
implements(zope.schema.interfaces.IVocabularyTokenized)
def __init__(self, context):
self.context = context
@property
def container(self):
raise NotImplementedError("Don't know how to get the container from context")
def __len__(self):
return len(self.container)
def __contains__(self, item):
return sameProxiedObjects(item, self.container.get(item.__name__))
def __iter__(self):
for item in self.container.values():
yield self.getTerm(item)
def getTermByToken(self, token):
terms = [self.getTerm(item) for item in self.container.values()]
by_token = dict([(term.token, term) for term in terms])
if token not in by_token:
raise LookupError(token)
return by_token[token]
def getTerm(self, item):
return zope.schema.vocabulary.SimpleTerm(
item,
token=simple_form_key(item),
title=self.getTitle(item))
def getTitle(self, item):
return item.title
|