[Zope-CMF] Random Results from portal_catalog in ZPT

Carl Rendell cer@sol43.com
Fri, 12 Apr 2002 16:21:06 -0700

On Friday, April 12, 2002, at 12:56  PM, Dieter Maurer wrote:

> Carl Rendell writes:
>> Anyone have a standard way they obtain random content from a
>> portal_catalog search in ZPT?
> What a strange question. Usually, I want my search results be 
> deterministic,
> not random ;-)

With things like 'tips' or 'quotes' on an intranet site, the users 
want some variety and random selections from a catalog search do 
the trick.

> Anyway. Catalog searches return sequences.
> The "random" (maybe "whrandom") module contains a "choice" function
> that allows you to randomly select an element from a sequence.
> Both modules are directly available to both Python Scripts and ZPT.
> Combine the above and you get a randomly selected catalog hit.
> When you want more than one, repeat it in a script and append the
> results.

Yep, the return sequences.. the question was how to handle them? I 
came up with this for use in a table for random quotes, anyone else 
have thoughts on another implementation?

<span tal:define="
       qitems python:here.portal_catalog.searchResults(meta_type='Quote',
  <table cellspacing="0" cellpadding="2" border="0" width="100%">
    <th class="infobox">Quote</th>
  <table cellspacing="0" cellpadding="0" border="0" width="100%">
    <td><img src="/images/trans_1px.gif" width="1"
             height="2" border="0"
  <table cellspacing="0" cellpadding="1" border="0" width="100%">
   <tr valign="top">
    <td bgcolor="#666666">
     <table border="0" cellpadding="3" cellspacing="0" width="100%" 
      <tr tal:condition="qitems">
       <td class="content"
           tal:define="wRandom modules/whrandom;qsitems 
                       drid qsitems/data_record_id_;">
        <p style="text-align:left">
        <p style="text-align:right">
        <p style="text-align:center">submitted by<br>
      <tr tal:condition="python:not(qitems)">
       <td class="content">No current
       <span tal:replace="string:${portal_title}">Title</span>

For me, this is a more explicit way to produce the results 
requested and keep as much of the scripting in one place.. Could 
have been done with a python script of course.


Carl E. Rendell
Information Distribution Consulting        |   "Ahhhh the power of
cer@sol43.com                              |    acquisition"  - Chef Z