[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',
                                                       sort_on='Date',
                                                       
sort_order='reverse');">
  <table cellspacing="0" cellpadding="2" border="0" width="100%">
   <tr>
    <th class="infobox">Quote</th>
   </tr>
  </table>
  <table cellspacing="0" cellpadding="0" border="0" width="100%">
   <tr>
    <td><img src="/images/trans_1px.gif" width="1"
             height="2" border="0"
             tal:attributes="src 
string:${portal_url}/images/trans_1px.gif"></td>
   </tr>
  </table>
  <table cellspacing="0" cellpadding="1" border="0" width="100%">
   <tr valign="top">
    <td bgcolor="#666666">
     <table border="0" cellpadding="3" cellspacing="0" width="100%" 
bgcolor="#ffffff">
      <tr tal:condition="qitems">
       <td class="content"
           tal:define="wRandom modules/whrandom;qsitems 
python:wRandom.choice(qitems);
                       drid qsitems/data_record_id_;">
        <p style="text-align:left">
        "<span 
tal:replace="python:qsitems.getobject(drid).text">Description</span>"
        </p>
        <p style="text-align:right">
        -<span 
tal:replace="python:qsitems.getobject(drid).quote_author">Quote 
Author</span>
        &nbsp;&nbsp;&nbsp;
        </p>
        <p style="text-align:center">submitted by<br>
        <span 
tal:replace="python:qsitems.getobject(drid).submitted_by">Submitted 
By</span>
        </p>
       </td>
      </tr>
      <tr tal:condition="python:not(qitems)">
       <td class="content">No current
       <span tal:replace="string:${portal_title}">Title</span>
       quotes</td>
      </tr>
     </table>
    </td>
   </tr>
  </table>
</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.



~C

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