/usr/share/pyshared/schooltool/table/batch.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 | #
# 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
#
"""
Batching for schooltool.
$Id$
"""
from zope.interface import implements
from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
from schooltool.table.interfaces import IBatch
class Batch(object):
"""Batching mechanism for Tables"""
implements(IBatch)
template = ViewPageTemplateFile("templates/batch.pt")
def __init__(self, formatter, batch_size=25):
self.formatter = formatter
if formatter.prefix:
self.name = "." + formatter.prefix
else:
self.name = ""
self.request = self.formatter.request
self.context = self.formatter
item_list = list(self.context._items)
self.full_size = len(item_list)
self.extra_url = self.context.extra_url()
self.base_url = self.request.URL
self.start = int(self.request.get('batch_start' + self.name, 0))
self.size = int(self.request.get('batch_size' + self.name, batch_size))
if self.start >= self.full_size:
self.start = max(0, self.full_size-self.size)
self.length = len(item_list[self.start:self.start + self.size])
def render(self):
if self.size < self.full_size or self.needsBatch:
return self.template()
else:
return ''
@property
def needsBatch(self):
there_are_more = self.full_size > self.size + self.start
there_were_before = self.start > 0
return there_are_more or there_were_before
def __len__(self):
return self.length
def previous_url(self):
start = self.start - self.size
if start >= 0:
return '%s?batch_start%s=%s&batch_size%s=%s%s' % (
self.base_url, self.name, start, self.name, self.size, self.extra_url)
return None
def next_url(self):
start = self.size + self.start
if self.full_size > start:
return '%s?batch_start%s=%s&batch_size%s=%s%s' % (
self.base_url, self.name, start, self.name, self.size, self.extra_url)
def num(self):
return self.start / self.size + 1
def numBatches(self):
num = self.full_size / self.size
if self.full_size % self.size:
num += 1
return num
def batch_urls(self):
urls = []
start = 0
num = 1
while self.full_size > start:
css_class = 'batch_page'
if (self.start == start):
css_class = 'batch_page current'
href = '%s?batch_start%s=%s&batch_size%s=%s%s' % (
self.base_url, self.name, start, self.name, self.size, self.extra_url)
urls.append({'href': href,
'num': num,
'class': css_class})
num += 1
start += self.size
return urls
class IterableBatch(Batch):
def __init__(self, items, request, extra_url=None, batch_size=25,
sort_by=None, name=None):
self.context = None
self.items = items
self.request = request
if name:
self.name = "." + name
else:
self.name = ""
def key(obj):
try:
return obj.get(sort_by)
except AttributeError:
return getattr(obj, sort_by)
if sort_by is not None:
item_list = sorted(items, key=key)
else:
item_list = items
self.full_size = len(item_list)
self.extra_url = extra_url or ""
self.base_url = self.request.URL
self.start = int(self.request.get('batch_start' + self.name, 0))
self.size = int(self.request.get('batch_size' + self.name, batch_size))
if self.start >= self.full_size:
self.start = max(0, self.full_size-self.size)
self.list = item_list[self.start:self.start + self.size]
self.length = len(self.list)
def __iter__(self):
return iter(self.list)
class TokenBatch(object):
"""Another batching mechanism for Tables"""
def __init__(self, items, start=0, size=0):
self.items = list(items)
self.full_size = len(self.items)
self.start = start
self.size = size
self.length = len(self.items[self.start:self.start + self.size])
@property
def needsBatch(self):
there_are_more = self.full_size > self.size + self.start
there_were_before = self.start > 0
return there_are_more or there_were_before
def __len__(self):
return self.length
def __iter__(self):
return iter(self.items[self.start:self.start + self.size])
def previous(self):
start = self.start - self.size
if start >= 0:
return {'start': start,
'size': self.size,
'items': self.items[start:start+self.size]}
if start > -self.size:
return {'start': 0,
'size': self.size,
'items': self.items[0:self.size]}
return None
def next(self):
start = self.start + self.size
if self.full_size > start:
return {'start': start,
'size': self.size,
'items': self.items[start:start+self.size]}
return None
def current(self):
return self.start / self.size + 1
def numBatches(self):
num = self.full_size / self.size
if self.full_size % self.size:
num += 1
return num
def tokens(self):
tokens = []
start = 0
num = 1
while self.full_size > start:
tokens.append({
'num': num,
'start': start,
'size': self.size,
'current': bool(self.start == start),
'items': self.items[start:start+self.size]})
num += 1
start += self.size
return tokens
|