Skip to content

Commit 5070dfd

Browse files
author
Richard Jones
committed
Exposed the Batch mechanism through the top-level "utils" variable.
See the keyword.item template for an example of how it can be used.
1 parent 19b607f commit 5070dfd

File tree

5 files changed

+86
-48
lines changed

5 files changed

+86
-48
lines changed

roundup/cgi/ZTUtils/Batch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
##############################################################################
1313
__doc__='''Batch class, for iterating over a sequence in batches
1414
15-
$Id: Batch.py,v 1.1 2002-09-05 00:37:09 richard Exp $'''
16-
__version__='$Revision: 1.1 $'[11:-2]
15+
$Id: Batch.py,v 1.2 2002-09-11 23:54:26 richard Exp $'''
16+
__version__='$Revision: 1.2 $'[11:-2]
1717

1818
class LazyPrevBatch:
1919
def __of__(self, parent):
@@ -84,7 +84,7 @@ def __getitem__(self, index):
8484
return self._sequence[index + self.end]
8585

8686
if index >= self.length: raise IndexError, index
87-
return self._sequence[index+self.first]
87+
return self._sequence[index + self.first]
8888

8989
def __len__(self):
9090
return self.length

roundup/cgi/client.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: client.py,v 1.27 2002-09-10 12:44:42 richard Exp $
1+
# $Id: client.py,v 1.28 2002-09-11 23:54:25 richard Exp $
22

33
__doc__ = """
44
WWW request handler (also used in the stand-alone server).
@@ -99,6 +99,11 @@ def __init__(self, instance, request, env, form=None):
9999
# someone gave us a non-int debug level, turn it off
100100
self.debug = 0
101101

102+
# additional headers to send with the request - must be registered
103+
# before the first write
104+
self.additional_headers = {}
105+
self.response_code = 200
106+
102107
def main(self):
103108
''' Process a request.
104109
@@ -149,7 +154,9 @@ def main(self):
149154
# the headers, otherwise the headers have been set before the
150155
# exception was raised
151156
if url:
152-
self.header({'Location': url}, response=302)
157+
self.additional_headers['Location'] = url
158+
self.response_code = 302
159+
self.write('Redirecting to <a href="%s">%s</a>'%(url, url))
153160
except SendFile, designator:
154161
self.serve_file(designator)
155162
except SendStaticFile, file:
@@ -303,13 +310,13 @@ def serve_file(self, designator, dre=re.compile(r'([^\d]+)(\d+)')):
303310

304311
# we just want to serve up the file named
305312
file = self.db.file
306-
self.header({'Content-Type': file.get(nodeid, 'type')})
313+
self.additional_headers['Content-Type'] = file.get(nodeid, 'type')
307314
self.write(file.get(nodeid, 'content'))
308315

309316
def serve_static_file(self, file):
310317
# we just want to serve up the file named
311318
mt = mimetypes.guess_type(str(file))[0]
312-
self.header({'Content-Type': mt})
319+
self.additional_headers['Content-Type'] = mt
313320
self.write(open(os.path.join(self.instance.config.TEMPLATES,
314321
file)).read())
315322

@@ -395,11 +402,14 @@ def write(self, content):
395402
self.header()
396403
self.request.wfile.write(content)
397404

398-
def header(self, headers=None, response=200):
405+
def header(self, headers=None, response=None):
399406
'''Put up the appropriate header.
400407
'''
401408
if headers is None:
402409
headers = {'Content-Type':'text/html'}
410+
if response is None:
411+
response = self.response_code
412+
headers.update(self.additional_headers)
403413
if not headers.has_key('Content-Type'):
404414
headers['Content-Type'] = 'text/html'
405415
self.request.send_response(response)
@@ -433,8 +443,8 @@ def set_cookie(self, user, password):
433443
# generate the cookie path - make sure it has a trailing '/'
434444
path = '/'.join((self.env['SCRIPT_NAME'], self.env['TRACKER_NAME'],
435445
''))
436-
self.header({'Set-Cookie': 'roundup_user_2=%s; expires=%s; Path=%s;'%(
437-
self.session, expire, path)})
446+
self.additional_headers['Set-Cookie'] = \
447+
'roundup_user_2=%s; expires=%s; Path=%s;'%(self.session, expire, path)
438448

439449
def make_user_anonymous(self):
440450
''' Make us anonymous
@@ -454,9 +464,8 @@ def logout(self):
454464
now = Cookie._getdate()
455465
path = '/'.join((self.env['SCRIPT_NAME'], self.env['TRACKER_NAME'],
456466
''))
457-
self.header({'Set-Cookie':
458-
'roundup_user_2=deleted; Max-Age=0; expires=%s; Path=%s;'%(now,
459-
path)})
467+
self.additional_headers['Set-Cookie'] = \
468+
'roundup_user_2=deleted; Max-Age=0; expires=%s; Path=%s;'%(now, path)
460469
self.login()
461470

462471
def opendb(self, user):
@@ -530,8 +539,8 @@ def logout_action(self):
530539
now = Cookie._getdate()
531540
path = '/'.join((self.env['SCRIPT_NAME'], self.env['TRACKER_NAME'],
532541
''))
533-
self.header(headers={'Set-Cookie':
534-
'roundup_user_2=deleted; Max-Age=0; expires=%s; Path=%s;'%(now, path)})
542+
self.additional_headers['Set-Cookie'] = \
543+
'roundup_user_2=deleted; Max-Age=0; expires=%s; Path=%s;'%(now, path)
535544

536545
# Let the user know what's going on
537546
self.ok_message.append(_('You are logged out'))
@@ -578,7 +587,11 @@ def registerAction(self):
578587
self.set_cookie(self.user, password)
579588

580589
# nice message
581-
self.ok_message.append(_('You are now registered, welcome!'))
590+
message = _('You are now registered, welcome!')
591+
592+
# redirect to the item's edit page
593+
raise Redirect, '%s/%s%s?:ok_message=%s'%(
594+
self.base, self.classname, self.userid, urllib.quote(message))
582595

583596
def registerPermission(self, props):
584597
''' Determine whether the user has permission to register

roundup/cgi/templating.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def getContext(self, client, classname, request):
150150
'request': request,
151151
'content': client.content,
152152
'db': HTMLDatabase(client),
153-
'instance': client.instance
153+
'instance': client.instance,
154+
'utils': TemplatingUtils(client),
154155
}
155156
# add in the item if there is one
156157
if client.nodeid:
@@ -263,7 +264,6 @@ def __getitem__(self, item):
263264
value = []
264265
else:
265266
value = None
266-
print (prop, value)
267267
return htmlklass(self._client, '', prop, item, value)
268268

269269
# no good
@@ -1316,21 +1316,25 @@ def __getitem__(self, index):
13161316
if index + self.end < self.first: raise IndexError, index
13171317
return self._sequence[index + self.end]
13181318

1319-
if index >= self.length: raise IndexError, index
1319+
if index >= self.length:
1320+
raise IndexError, index
13201321

13211322
# move the last_item along - but only if the fetched index changes
13221323
# (for some reason, index 0 is fetched twice)
13231324
if index != self.last_index:
13241325
self.last_item = self.current_item
13251326
self.last_index = index
13261327

1327-
# wrap the return in an HTMLItem
1328-
if self.classname == 'user':
1329-
klass = HTMLUser
1330-
else:
1331-
klass = HTMLItem
1332-
self.current_item = klass(self.client, self.classname,
1333-
self._sequence[index+self.first])
1328+
item = self._sequence[index + self.first]
1329+
if not isinstance(item, HTMLItem):
1330+
# "item" is actually just an id - wrap the return in an HTMLItem
1331+
if self.classname == 'user':
1332+
klass = HTMLUser
1333+
else:
1334+
klass = HTMLItem
1335+
item = klass(self.client, self.classname, item)
1336+
1337+
self.current_item = item
13341338
return self.current_item
13351339

13361340
def propchanged(self, property):
@@ -1362,3 +1366,12 @@ def length(self):
13621366
self.sequence_length = l = len(self._sequence)
13631367
return l
13641368

1369+
class TemplatingUtils:
1370+
''' Utilities for templating
1371+
'''
1372+
def __init__(self, client):
1373+
self.client = client
1374+
def Batch(self, classname, l, size, start, end=0, orphan=0, overlap=0):
1375+
return Batch(self.client, classname, l, size, start, end, orphan,
1376+
overlap)
1377+

roundup/templates/classic/html/keyword.item

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
<!-- dollarId: keyword.item,v 1.3 2002/05/22 00:32:34 richard Exp dollar-->
22

3+
<table class="otherinfo">
4+
<tr><th colspan="4" class="header">Existing Keywords</th></tr>
5+
<tal:block tal:define="keywords db/keyword/list"
6+
tal:repeat="start python:range(0, len(keywords), 4)">
7+
<tr tal:define="batch python:utils.Batch('keyword', keywords, 4, start)">
8+
<td tal:repeat="keyword batch" tal:content="keyword/name">
9+
keyword goes here
10+
</td>
11+
</tr>
12+
</tal:block>
13+
<tr><td colspan="4" style="border-top: 1px solid gray">&nbsp;</td></tr>
14+
</table>
15+
316
<p class="help" tal:condition="not:context/id">
4-
Use this form to create a new keyword.
17+
To create a new keyword, enter it below and click "Submit New Entry".
518
</p>
619

720
<form method="POST" onSubmit="return submit_once()"

roundup/templates/classic/html/style.css

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,6 @@ table.history td {
240240
empty-cells: show;
241241
}
242242

243-
/* style for "other" displays */
244-
table.otherinfo {
245-
border-spacing: 0px;
246-
border-collapse: separate;
247-
width: 100%;
248-
}
249-
250-
table.otherinfo th.header{
251-
padding-top: 10px;
252-
border-bottom: 1px solid gray;
253-
font-weight: bold;
254-
background-color: white;
255-
color: #707040;
256-
}
257-
258-
table.otherinfo th {
259-
border-bottom: 1px solid #afafaf;
260-
font-weight: bold;
261-
text-align: left;
262-
}
263-
264243

265244
/* style for class list */
266245
table.classlist {
@@ -305,3 +284,23 @@ table.classhelp td {
305284
}
306285

307286

287+
/* style for "other" displays */
288+
table.otherinfo {
289+
border-spacing: 0px;
290+
border-collapse: separate;
291+
width: 100%;
292+
}
293+
294+
table.otherinfo th.header{
295+
padding-top: 10px;
296+
border-bottom: 1px solid gray;
297+
font-weight: bold;
298+
background-color: white;
299+
color: #707040;
300+
}
301+
302+
table.otherinfo th {
303+
border-bottom: 1px solid #afafaf;
304+
font-weight: bold;
305+
text-align: left;
306+
}

0 commit comments

Comments
 (0)