[Zope] ZCatalog / Zsearch dates?

Kuraiken arashi1@pd.jaring.my
Thu, 30 Sep 1999 18:53:55 +0800


Michel Pelletier wrote:
> Then you will get no results.  This is because the 'date' index is
> storing DateTime objects, NOT strings.  To get a result, you will need
> to pass the Catalog a query that contains an instanciated DateTime
> object:
> 
>   <dtml-in "Catalog(date=DateTime(date))">
>     ...
>   </dtml-in>
> 

Ah, that makes sense! Thanks. (although, I believe it's ZopeTime not DateTime?
At least...ZopeTime works but not DateTime - I get a keyError otherwise)

> A very useful example of using dates can be seen on the zope.org
> advanced search page:
> 
<snip>

Yes indeed. It's now saved for later reference :-)

(The following is not a problem, it's a solution for others who might have had a
similar problem)

However, I came across another "problem". Now that I'm passing in a ZopeTime
object, Zope will insist on _getting_ a ZopeTime object. Let me clarify. I have
a search form such:

Title: [textfield]
Date: [textfield]
Text/keyword: [textfield]

Now, if I leave the date field blank, I get an error because it's expecting a
ZopeTime object. There are cases where you'll want to make a search by one or
more items that you know. You might not know the date of the item, for instance.
So how do I get around this error? (This problem did not surface if only strings
were used.)

You need to do:
<dtml-if date>
  <dtml-in "Catalog(date=ZopeTime(date))">
   ...
  </dtml-in>
<dtml-else>
  <dtml-in Catalog>
   ...
  </dtml-in>
<dtml-if>

The first one is the "special" case where a date is specified and the second one
for when it's not. This avoids the Error message for not passing in the ZopeTime
object. Simple but it got me stumped for a bit...

-- 
-----------------------------------------
Kuraiken - Python fanatic.
-----------------------------------------
Python. Try it. It'll swallow you whole!
-----------------------------------------