Skip to content

Commit fe766e3

Browse files
author
Richard Jones
committed
document and make easier the actions-returning-content idiom
1 parent edeae19 commit fe766e3

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Feature:
3030
- HTMLLinkProperty field() method renders as a field now (thanks darryl)
3131
- all HTML templating methods now automatically check for permissions
3232
(either view or edit as appropriate), greatly simplifying templates
33+
- cgi Action handlers may now return the actual content to be sent back to
34+
the user (rather than using some template)
3335

3436
Fixed:
3537
- mysql documentation fixed to note requirement of 4.0+ and InnoDB

doc/customizing.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Customising Roundup
33
===================
44

5-
:Version: $Revision: 1.116 $
5+
:Version: $Revision: 1.117 $
66

77
.. This document borrows from the ZopeBook section on ZPT. The original is at:
88
http://www.zope.org/Documentation/Books/ZopeBook/current/ZPT.stx
@@ -2148,6 +2148,23 @@ In your HTML form, add a hidden form element like so::
21482148

21492149
where "myaction" is the name you registered in the previous step.
21502150

2151+
Actions may return content to the user
2152+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2153+
2154+
Actions generally perform some database manipulation and then pass control
2155+
on to the rendering of a template in the current context (see `Determining
2156+
web context`_ for how that works.) Some actions will want to generate the
2157+
actual content returned to the user. Action methods may return their own
2158+
content string to be displayed to the user, overriding the templating step.
2159+
In this situation, we assume that the content is HTML by default. You may
2160+
override the content type indicated to the user by calling ``setHeader``::
2161+
2162+
self.client.setHeader('Content-Type', 'text/csv')
2163+
2164+
This example indicates that the value sent back to the user is actually
2165+
comma-separated value content (eg. something to be loaded into a
2166+
spreadsheet or database).
2167+
21512168

21522169
Examples
21532170
========

roundup/cgi/client.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: client.py,v 1.163 2004-02-25 03:24:43 richard Exp $
1+
# $Id: client.py,v 1.164 2004-02-25 03:39:53 richard Exp $
22

33
"""WWW request handler (also used in the stand-alone server).
44
"""
@@ -249,24 +249,18 @@ def clean_sessions(self):
249249
Note: also cleans One Time Keys, and other "session" based stuff.
250250
"""
251251
sessions = self.db.sessions
252-
last_clean = sessions.get('last_clean', 'last_use') or 0
252+
last_clean = self.db.sessions.get('last_clean', 'last_use') or 0
253253

254+
# time to clean?
254255
week = 60*60*24*7
255256
hour = 60*60
256257
now = time.time()
257-
if now - last_clean > hour:
258-
# remove aged sessions
259-
for sessid in sessions.list():
260-
interval = now - sessions.get(sessid, 'last_use')
261-
if interval > week:
262-
sessions.destroy(sessid)
263-
# remove aged otks
264-
otks = self.db.otks
265-
for sessid in otks.list():
266-
interval = now - otks.get(sessid, '__time')
267-
if interval > week:
268-
otks.destroy(sessid)
269-
sessions.set('last_clean', last_use=time.time())
258+
if now - last_clean < hour:
259+
return
260+
261+
self.db.sessions.clean(now)
262+
self.db.otks.clean(now)
263+
self.db.sessions.set('last_clean', last_use=time.time())
270264

271265
def determine_user(self):
272266
''' Determine who the user is
@@ -296,7 +290,7 @@ def determine_user(self):
296290
# get the user from the session
297291
try:
298292
# update the lifetime datestamp
299-
sessions.set(self.session, last_use=time.time())
293+
sessions.updateTimestamp(self.session)
300294
sessions.commit()
301295
user = sessions.get(self.session, 'user')
302296
except KeyError:
@@ -575,6 +569,11 @@ def write(self, content):
575569
self.header()
576570
self.request.wfile.write(content)
577571

572+
def setHeader(self, header, value):
573+
'''Override a header to be returned to the user's browser.
574+
'''
575+
self.additional_headers[header] = value
576+
578577
def header(self, headers=None, response=None):
579578
'''Put up the appropriate header.
580579
'''

0 commit comments

Comments
 (0)