Package platecom :: Package utils :: Module fieldproperty :: Class ATFieldProperty
[hide private]
[frames] | no frames]

Class ATFieldProperty

source code

object --+
         |
        ATFieldProperty

Field properties based on Archetypes schema

These properties can only be used on Archetypes objects. They delegate to schema.getField(fieldname).get() and set().

You can use it in your type as follows. The name of the field does not need to conincide with the field-property name, but this is probably sensible. However, AttributeStorage will interfere here, so we explicitly use annoation storage.

>>> import string
>>> from Products.Archetypes.atapi import *
>>> from iccommunity.core.fieldproperty import ATFieldProperty
>>> class MyContent(BaseObject):
...     portal_type = meta_type = 'MyContent'
...     schema = Schema((
...         StringField('some_field', storage=AnnotationStorage()),
...         StringField('_other_field'),
...         ))
...
...     some_field = ATFieldProperty('some_field')
...     other_field = ATFieldProperty('_other_field')
...     upper_lower = ATFieldProperty('_other_field',
...         get_transform=string.upper, set_transform=string.lower)
>>> registerType(MyContent, 'Archetypes')

Now, get and set operations on the fieldproperty behave the same way as the mutator and accessor.

>>> foo = MyContent('foo')
>>> foo.some_field
''
>>> foo.some_field = "Bar"
>>> foo.some_field
'Bar'
>>> foo.getField('some_field').get(foo)
'Bar'

The old-style mutator and accessors still work, of course

>>> foo.getSome_field()
'Bar'
>>> foo.setSome_field("Baz")
>>> foo.some_field
'Baz'

Here is an example using the default AttributeStorage. In this case, we need different names for the AT field name and the properity, because AttributeStorage will use the field name as the attribute name. If you don't do this, you may get infinite recursion!

>>> foo.other_field = "Hello"
>>> foo.other_field
'Hello'
>>> foo.get_other_field()
'Hello'
>>> foo.set_other_field("Good bye")
>>> foo.other_field
'Good bye'

Finally, the get_transform and set_transform arguments can be used to perform transformations on the retrieved value and the value before it is set, respectively. The field upper_lower uses string.upper() on the way out and string.lower() on the way in.

>>> foo.upper_lower = "MiXeD"
>>> foo.upper_lower
'MIXED'
>>> foo.get_other_field()
'mixed'
>>> foo.set_other_field('UpPeRaNdLoWeR')
>>> foo.upper_lower
'UPPERANDLOWER'

A less frivolous example of this functionality can be seen in the ATDateTimeFieldProperty class below.

Instance Methods [hide private]
 
__init__(self, name, get_transform=None, set_transform=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__get__(self, inst, klass) source code
 
__set__(self, inst, value) source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, name, get_transform=None, set_transform=None)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)