Back to index

plone3  3.1.7
Public Member Functions | Private Attributes
Archetypes.fieldproperty.ATFieldProperty Class Reference
Inheritance diagram for Archetypes.fieldproperty.ATFieldProperty:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def __get__
def __set__

Private Attributes

 _name
 _get_transform
 _set_transform

Detailed Description

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 *
    >>> 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.

Definition at line 10 of file fieldproperty.py.


Constructor & Destructor Documentation

def Archetypes.fieldproperty.ATFieldProperty.__init__ (   self,
  name,
  get_transform = None,
  set_transform = None 
)

Reimplemented in Archetypes.fieldproperty.ATToolDependentFieldProperty.

Definition at line 90 of file fieldproperty.py.

00090 
00091     def __init__(self, name, get_transform=None, set_transform=None):
00092         self._name = name
00093         self._get_transform = get_transform
00094         self._set_transform = set_transform
        

Here is the caller graph for this function:


Member Function Documentation

def Archetypes.fieldproperty.ATFieldProperty.__get__ (   self,
  inst,
  klass 
)

Reimplemented in Archetypes.fieldproperty.ATToolDependentFieldProperty.

Definition at line 95 of file fieldproperty.py.

00095 
00096     def __get__(self, inst, klass):
00097         if inst is None:
00098             return self
00099         field = inst.getField(self._name)
00100         if field is None:
00101             raise KeyError("Cannot find field with name %s" % self._name)
00102         value = field.get(inst)
00103         if self._get_transform is not None:
00104             value = self._get_transform(value)
00105         return value

def Archetypes.fieldproperty.ATFieldProperty.__set__ (   self,
  inst,
  value 
)

Reimplemented in Archetypes.fieldproperty.ATToolDependentFieldProperty.

Definition at line 106 of file fieldproperty.py.

00106 
00107     def __set__(self, inst, value):
00108         field = inst.getField(self._name)
00109         if field is None:
00110             raise KeyError("Cannot find field with name %s" % self._name)
00111         if self._set_transform is not None:
00112             value = self._set_transform(value)
00113         field.set(inst, value)
        

Member Data Documentation

Reimplemented in Archetypes.fieldproperty.ATToolDependentFieldProperty.

Definition at line 92 of file fieldproperty.py.

Reimplemented in Archetypes.fieldproperty.ATToolDependentFieldProperty.

Definition at line 91 of file fieldproperty.py.

Reimplemented in Archetypes.fieldproperty.ATToolDependentFieldProperty.

Definition at line 93 of file fieldproperty.py.


The documentation for this class was generated from the following file: