/usr/share/pyshared/schooltool/table/column.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 | #
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2012 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
#
"""
More columns for tables.
"""
import datetime
from zope.app.dependable.interfaces import IDependable
from zope.interface import implementer, implements, classImplements
from zope.i18n.interfaces.locales import ICollator
from zope.i18n import translate
from zope.intid.interfaces import IIntIds
from zope.component import adapter, queryAdapter, queryMultiAdapter
from zope.component import queryUtility
from zope.security.proxy import removeSecurityProxy
from zope.traversing.browser.absoluteurl import absoluteURL
import zc.table
import zc.table.interfaces
import zc.table.column
from schooltool.common import stupid_form_key
from schooltool.table.interfaces import ICheckboxColumn
from schooltool.table.interfaces import IIndexedColumn
#XXX: Misplaced helper
def getResourceURL(library_name, image_name, request):
if not image_name:
return None
if library_name is not None:
library = queryAdapter(request, name=library_name)
image = library.get(image_name)
else:
image = queryAdapter(request, name=image_name)
if image is None:
return None
return absoluteURL(image, request)
class CheckboxColumn(zc.table.column.Column):
"""A columns with a checkbox
The name and id of the checkbox are composed of the prefix keyword
argument and __name__ (or other value if form_id_builder is specified) of
the item being displayed.
"""
implements(ICheckboxColumn)
def __init__(self, prefix, name=None, title=None,
isDisabled=None, id_getter=None):
super(CheckboxColumn, self).__init__(name=name, title=title)
if isDisabled:
self.isDisabled = isDisabled
self.prefix = prefix
if id_getter is None:
self.id_getter = stupid_form_key
else:
self.id_getter = id_getter
def isDisabled(self, item):
return False
def renderCell(self, item, formatter):
if not self.isDisabled(item):
form_id = ".".join(filter(None, [self.prefix, self.id_getter(item)]))
return '<input type="checkbox" name="%s" id="%s" />' % (
form_id, form_id)
else:
return ''
class DependableCheckboxColumn(CheckboxColumn):
"""A column that displays a checkbox that is disabled if item has dependables.
The name and id of the checkbox are composed of the prefix keyword
argument and __name__ of the item being displayed.
"""
def __init__(self, *args, **kw):
kw = dict(kw)
self.show_disabled = kw.pop('show_disabled', True)
super(DependableCheckboxColumn, self).__init__(*args, **kw)
def renderCell(self, item, formatter):
form_id = ".".join(filter(None, [self.prefix, self.id_getter(item)]))
if self.hasDependents(item):
if self.show_disabled:
return '<input type="checkbox" name="%s" id="%s" disabled="disabled" />' % (form_id, form_id)
else:
return ''
else:
checked = form_id in formatter.request and 'checked="checked"' or ''
return '<input type="checkbox" name="%s" id="%s" %s/>' % (
form_id, form_id, checked)
def hasDependents(self, item):
# We cannot adapt security-proxied objects to IDependable. Unwrapping
# is safe since we do not modify anything, and the information whether
# an object can be deleted or not is not classified.
unwrapped_context = removeSecurityProxy(item)
dependable = IDependable(unwrapped_context, None)
if dependable is None:
return False
else:
return bool(dependable.dependents())
class DateColumn(zc.table.column.GetterColumn):
"""Table column that displays dates.
Sortable even when None values are around.
"""
def getSortKey(self, item, formatter):
if self.getter(item, formatter) is None:
return datetime.date.min
else:
return self.getter(item, formatter)
def cell_formatter(self, maybe_date, item, formatter):
view = queryMultiAdapter((maybe_date, formatter.request),
name='mediumDate',
default=lambda: '')
return view()
class LocaleAwareGetterColumn(zc.table.column.GetterColumn):
"""Getter columnt that has locale aware sorting."""
implements(zc.table.interfaces.ISortableColumn)
def getSortKey(self, item, formatter):
collator = ICollator(formatter.request.locale)
s = self.getter(item, formatter)
return s and collator.key(s)
class ImageInputColumn(zc.table.column.Column):
def __init__(self, prefix, title=None, name=None,
alt=None, library=None, image=None, id_getter=None):
super(ImageInputColumn, self).__init__(title=title, name=name)
self.prefix = prefix
self.alt = alt
self.library = library
self.image = image
if id_getter is None:
self.id_getter = stupid_form_key
else:
self.id_getter = id_getter
def params(self, item, formatter):
image_url = getResourceURL(self.library, self.image, formatter.request)
if not image_url:
return None
form_id = ".".join(filter(None, [self.prefix, self.id_getter(item)]))
return {
'title': translate(self.title, context=formatter.request) or '',
'alt': translate(self.alt, context=formatter.request) or '',
'name': form_id,
'src': image_url,
}
def renderCell(self, item, formatter):
params = self.params(item, formatter)
if not params:
return ''
return self.template() % params
def template(self):
return '\n'.join([
'<button class="image" type="submit" name="%(name)s" title="%(title)s" value="1">',
'<img src="%(src)s" alt="%(alt)s" />',
'</button>'
])
class ImageInputValueColumn(ImageInputColumn):
on_click = ""
def __init__(self, *args, **kw):
on_click = kw.pop('on_click', "")
ImageInputColumn.__init__(self, *args, **kw)
self.on_click = on_click
self.form_id = ".".join(filter(None, [self.prefix, self.name]))
def params(self, item, formatter):
image_url = getResourceURL(self.library, self.image, formatter.request)
if not image_url:
return None
value = self.id_getter(item)
return {
'title': translate(self.title, context=formatter.request) or '',
'alt': translate(self.alt, context=formatter.request) or '',
'name': self.form_id,
'value': value,
'src': image_url,
'on_click': self.on_click,
}
def template(self):
return '\n'.join([
'<button class="image" type="submit" name="%(name)s"'
' title="%(title)s" value="%(value)s" onclick="%(on_click)s">',
'<img src="%(src)s" alt="%(alt)s" />',
'</button>'
])
class TableSubmitImageColumn(ImageInputColumn):
def __init__(self, prefix, title=None, name=None, alt=None,
library='schooltool.skin.flourish', image=None,
container_id='', id_getter=None):
super(TableSubmitImageColumn, self).__init__(
prefix, title=title, name=name, alt=alt,
library=library, image=image, id_getter=id_getter)
self.form_id = ".".join(filter(None, [self.prefix, self.name]))
self.container_id = container_id
def on_click(self, formatter):
return "return ST.table.on_form_submit('%s', this)" % self.container_id
def params(self, item, formatter):
image_url = getResourceURL(
self.library, self.image, formatter.request)
if not image_url:
return None
return {
'title': translate(self.title, context=formatter.request) or '',
'alt': translate(self.alt, context=formatter.request) or '',
'name': self.form_id,
'value': self.id_getter(item),
'src': image_url,
'script': self.on_click(formatter), # XXX: should encode this
}
def renderCell(self, item, formatter):
params = self.params(item, formatter)
if not params:
return ''
return self.template() % params
def template(self):
return ' '.join(s.strip() for s in [
'<button class="image" type="submit" name="%(name)s"'
' title="%(title)s" value="%(value)s"',
' onclick="%(script)s">',
' <img src="%(src)s" alt="%(alt)s" />',
'</button>'
])
class MultiStateColumn(zc.table.column.Column):
header_column = None
state_columns = None
def __init__(self, states=(), header=None, state_getter=None):
zc.table.column.Column.__init__(self)
self.header_column = header
self.state_columns = states
if state_getter is None:
state_getter = lambda item: 0
self.getState = state_getter
def renderHeader(self, formatter):
return self.header_column.renderHeader(formatter)
def renderCell(self, item, formatter):
state = self.getState(item, formatter)
column = self.state_columns[state]
cell = column.renderCell(item, formatter)
return cell
class IndexedGetterColumn(zc.table.column.GetterColumn):
implements(IIndexedColumn, zc.table.interfaces.ISortableColumn)
def __init__(self, **kwargs):
self.index = kwargs.pop('index')
super(IndexedGetterColumn, self).__init__(**kwargs)
def _sort(self, items, formatter, start, stop, sorters, multiplier):
if self.subsort and sorters:
items = sorters[0](items, formatter, start, stop, sorters[1:])
else:
items = list(items) # don't mutate original
getSortKey = self.getSortKey
# Patch the SortableColum._sort to use both cmp and key for sorting.
# This reduces usage of getSortKey drastically on large datasets.
items.sort(
cmp=lambda a, b: multiplier*cmp(a, b),
key=lambda item: getSortKey(item, formatter))
return items
def renderCell(self, item, formatter):
item = queryUtility(IIntIds).getObject(item['id'])
return super(IndexedGetterColumn, self).renderCell(item, formatter)
def getSortKey(self, item, formatter):
id = item['id']
index = item['catalog'][self.index]
return index.documents_to_values[id]
class IndexedLocaleAwareGetterColumn(IndexedGetterColumn):
_cached_collator = None
def getSortKey(self, item, formatter):
if not self._cached_collator:
self._cached_collator = ICollator(formatter.request.locale)
s = super(IndexedLocaleAwareGetterColumn, self).getSortKey(item, formatter)
return s and self._cached_collator.key(s)
def makeIndexedColumn(mixins, column, *args, **kw):
class_ = column.__class__
new_class = type(
'_indexed_%s' % class_.__name__,
tuple(mixins) + (class_,),
{})
classImplements(new_class, IIndexedColumn)
new_column = super(class_, new_class).__new__(new_class, *args, **kw)
new_column.__dict__.update(dict(column.__dict__))
return new_column
def unindex(indexed_item):
return queryUtility(IIntIds).getObject(indexed_item['id'])
class RenderUnindexingMixin(object):
def renderCell(self, indexed_item, formatter):
return super(RenderUnindexingMixin, self).renderCell(
unindex(indexed_item), formatter)
class SortUnindexingMixin(object):
def getSortKey(self, indexed_item, formatter):
super(SortUnindexingMixin, self).getSortKey(
unindex(indexed_item), formatter)
@adapter(zc.table.interfaces.IColumn)
@implementer(IIndexedColumn)
def getIndexedColumn(column):
column = makeIndexedColumn(
[RenderUnindexingMixin], column)
return column
@adapter(zc.table.interfaces.ISortableColumn)
@implementer(IIndexedColumn)
def getIndexedSortableColumn(column):
column = makeIndexedColumn(
[RenderUnindexingMixin, SortUnindexingMixin], column)
return column
|