[Zope-CMF] Adding date fields

Tres Seaver tseaver@zope.com
Wed, 17 Oct 2001 17:40:49 -0400


Meilicke, Scott wrote:

> Within a new product (python based), what is the best way to add date fields
> similar to effective_date in the CMF?
> 
> I've mucked around with copying various bits of code from dublincore.py into
> my product, but without much success.

You probably want to guarantee that the value which gets stored in your attribute

is always an instance of DateTime.  To do this, DublinCore.DefaultDublinCoreImpl
uses a helper method, '_datify', which tries hard to convert whatever it is
handed into a DateTime, including special handling for the string 'None':

   def _datify( value ):
       if value == 'None':
           value = None
       elif not isinstance( value, DateTime ):
           if value is not None:
               value = DateTime( value )
       return value

Your class would hold an attribute (let's say, '_birthdate'):

   class Person:
       """ Hold PIM data about a person."""
       _birthdate = None

       def getBirthdate( self ):
           """ Return birthdate as a string, 'YYYY/MM/DD'. """
           return self._birthdate and self.birthdate.Date() or 'None'

       def setBirtdate( self, newValue ):

           self._birthdate = _datify( newValue )

Does that help any?


Tres.
-- 
===============================================================
Tres Seaver                                tseaver@zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com