[albatross-users] A pattern for processing a bunch of items at once?

Greg Hamilton gregh at object-craft.com.au
Thu Oct 31 16:53:14 EST 2002


I've had to do something similar a few times. I don't know if this is 
the best solution but it works for me.

On Thursday, October 31, 2002, at 04:15 PM, Gregory Bond wrote:

> I'm looking for a good Albatross pattern for the following problem:
>
> Consider the popview application in the manual.  What if you wanted to 
> have the
> ability to delete several of the messages in the one operation?
>
> My thoughts so far are to include a checkbox on each row with a name 
> like
> "del_<msgnum>" (built using nameexpr), and a delete button.

Create a checkbox for each message. Give each checkbox the same name. 
Set the value to something which uniquely identifies the message.

<al-form method="post">
<al-for iter="element" expr="messages">
<al-input type="checkbox" name="del_msgs" 
valueexpr="element.value().get_message_id()" whitespace>
</al-for>
<al-input type="submit" name="delete" value="Delete">
</al-form>


> But the next bit is odd: the best thing I can think of for the 
> page_process()
> function is something like:
> 	if ctx.req_equals('delete'):
> 		for v in ctx.locals.__dict__:
> 	 	   if v[:4] == 'del_':
> 			mbox.delete_msg(int(v[4:]))
> but this is pretty ugly.
>
> Any better/more elegant ways that ppl can suggest?
>
>

Then put something like this in your page module.

def page_enter(ctx):
	ctx.locals.messages = load_messages_from_somewhere()
	ctx.locals.del_msgs = None
	ctx.add_session_vars('messages', 'del_msgs', )

def page_process(ctx):
	if ctx.req_equals('delete'):
         # if nothing is selected return is None
         # if one item, return is String
         # if >1 items, return is list
		if ctx.local.del_msgs is None:
			# nothing selected
			pass
         elif type(ctx.locals.del_msgs) == types.StringType:
         	# one message to delete
			pass
		elif type(ctx.locals.del_msgs) == types.ListType:
			# a list of messages to delete
			pass


The only trap to watch out for is that 'del_msgs' will be None, a 
String or a List (or more likely a tuple, I don't recall) depending on 
the number of items selected.

Greg Hamilton
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 2222 bytes
Desc: not available
URL: <http://www.object-craft.com.au/pipermail/albatross-users/attachments/20021031/6c5adc59/attachment.bin>


More information about the Albatross-users mailing list