[albatross-users] check boxes again.

Gregory Bond gnb at itga.com.au
Fri Jul 4 11:10:24 EST 2003


> Q: how do I get a complete list of all checkboxes checked and unchecked just 
> like you can with ordinary CGI

Presumably, you get this list from the same place you get the list of all the 
items you are adding to the form.  (i.e. you need exactly this information to 
display the page in the first place, so it can't be that hard!!)

> what is the best way to store a chunk of data (i.e. a list) client side?

If you add a variable to the session (with ctx.add_session_vars()), then it
will be there for you next time the page_process() function is called. You can
add (almost) any Python datatype to the session, so a vector of strings is
easy.  This can be expensive in bandwith if you use client-side sessions,
depending on how much data you add.  We use file-based server sessions with up
to 100k of session data to do this sort of thing with no problems at all.

Perhaps the model you want is something like this (warning: untested):

--------
def page_enter(self, ctx):
	ctx.locals.all_ids = []
	ctx.add_session_vars('all_ids')

def page_display(self, ctx):
	ctx.locals.spam_ids = []
	ctx.locals.not_spam_ids = []
	for i in self.list_of_emails():
		id = i.message_id()
		ctx.locals.all_ids.append(id)
		if i.is_spam():
			ctx.locals.spam_ids.append(id)
		elif i.is_not_spam():
			ctx.locals.not_spam_ids.append(id)
	ctx.run_template('list.htm')

def page_process(self, ctx):
	# Here, ctx.locals.all_ids is a vector of all the IDs
	# ctx.locals.spam_ids is a vector of all the IDs for which the "is spam" checkbox was selected
	# likewise, ctx.locals.not_spam_ids
	for id in ctx.locals.all_ids:
		if id in ctx.locals.spam_ids:
			mark_id_spam(id)
		elif id in ctx.locals.not_spam_ids:
			mark_id_not_spam(id)
		else:
			mark_id_unknown_status(id)

----------

Now depending on how long each vector might be, the "i in smap_ids" might be
too slow (it's O(n) in the number of items) so you could start by converting
the vector into a dict.  But this is unlikely to be an issue for any sensible
web page - your browser will explode trying to render the form well before this
becomes a bottleneck.

And you can avoid storing the all_ids vector in the session by changing your 
process function to something like this:

def page_process(self, ctx):
	for i in self.list_of_emails():
		id = i.message_id()
		if id in ctx.locals.spam_ids:
			mark_id_spam(id)
		elif id in ctx.locals.not_spam_ids:
			mark_id_not_spam(id)
		else:
			mark_id_unknown_status(id)


i.e. use the same for loop in the process function as you used in the al-for 
template that generated the form.





More information about the Albatross-users mailing list