Perhaps this already exists somewhere, but I couldn't find it. Feed this 
function an object and it returns a dictionary of the associated schema 
key/value pairs including those that are PersistentList's of objects. Handy for 
view classes. Pointers and improvements welcome.
 
Kevin Smith

def mapSchemasToDict( context ):
    """ Map schemas to dict

    Provide data for demonstration::

        >>> from zope import interface, schema
        >>> from persistent import Persistent
        >>> from persistent.list import PersistentList
        >>> class IPerson( interface.Interface ):
        ...     first = schema.TextLine( title=u'First name' )
        ...     last = schema.TextLine( title=u'Last name' )
        ...
        >>> class Person:
        ...     interface.implements(IPerson)
        ...     def __init__( self, first, last ):
        ...         self.first = first
        ...         self.last = last
        ...
        >>> p1 = Person( "Jim", "Fulton" )
        >>> p1.first
        'Jim'

    create addressbook content::

        >>> class IAddressBook( interface.Interface ):
        ...     title = schema.TextLine( title=u'Title' )
        ...     persons = schema.Object( IPerson, title=u'Persons')
        ...
        >>> class AddressBook(Persistent):
        ...     interface.implements(IAddressBook)
        ...     def __init__(self, title=''):
        ...          self.title = title
        ...     title = ''
        ...     persons = PersistentList()
        ...
        >>> a1 = AddressBook( title="My Address Book" )
        >>> a1.title
        'My Address Book'
        >>> a1.persons.append( p1 )
        >>> a1.persons.append( p1 )
        >>> a1.persons
        [<...Person instance at ...>, <...Person instance at ...>]
        >>> d = mapSchemasToDict( a1 )
        >>> d
        {'persons': [{'last': 'Fulton', 'first': 'Jim'}, {'last': 'Fulton', 
'first':
        'Jim'}], 'title': 'My Address Book'}
    """

    listofinterfaces = list( interface.providedBy( context ) )
    results = {}
    for oneinterface in listofinterfaces:
        fields = schema.getFieldNames( oneinterface )
        if fields:
            for field in fields:
                fieldvalue = getattr( oneinterface( context ), field )
                if isinstance( fieldvalue, PersistentList ):
                    subfieldvalue = []
                    for subfield in fieldvalue:
                        subfieldvalue.append( mapSchemasToDict( subfield ) )
                    fieldvalue = subfieldvalue
                results[field] = fieldvalue
    return results


USAGE:

class MyView( object ):
    ...
    @property
    def fields( self ):
        return mapSchemasToDict( self.context )


template.pt
    ...
    <span tal:replace="view/fields" />

      ...or as in the doctest example...

    <span tal:replace="view/fields/title" /> .... 
   
       returns...

            My Address Book



_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to