Re: [Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Lorenzo Gil Sanchez
Hi Darryl

El mié, 26-07-2006 a las 00:48 +1200, Darryl Cousins escribió:
> Hi Lorenzo,
> 
> Cool. Where is the `contained` imported from? There is some magic there
> because I couldn't get the test to work when creating the FieldIndex in
> your createIndexes method without it.

from zope.app.container.contained import contained

Regards

Lorenzo

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


[Zope3-Users] Zope3 winXP service not working

2006-07-25 Thread Eusebius

Hello

I installed Zope3 on a Windows XP machine, and configured it as a 
service. The service is properly registered, and seems to be 
starting/stopping normally. Nevertheless it's not working: Zope does not 
listen on 8080 (no python process listening in a netstat output), or any 
other port.


The Zope3 server, started manually (not as a service), works fine.

Any idea?
Thank you very much

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


Re: [Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Darryl Cousins
Hi Lorenzo,

Cool. Where is the `contained` imported from? There is some magic there
because I couldn't get the test to work when creating the FieldIndex in
your createIndexes method without it.

Thanks,
Darryl

On Tue, 2006-07-25 at 13:55 +0200, Lorenzo Gil Sanchez wrote:
> Hi Darryl,
> 
> this is how I ended implementing it:
> 
> 

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


Re: [Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Lorenzo Gil Sanchez
Hi Darryl,

this is how I ended implementing it:


class FilterableContainer(BTreeContainer):

implements(IFilterableContainer)

def __init__(self, catalog=None):
super(FilterableContainer, self).__init__()
self.catalog = contained(LocalCatalog(), self, 'catalog')

def createIndexes(self):
self.catalog['attr1'] = FieldIndex('attr1')
self.catalog['attr2'] = FieldIndex('attr2')


def onFilterableContainerAdded(obj, event):
if IFilterableContainer.providedBy(obj):
obj.createIndexes()


Thanks a lot for your help and suggestions!

Lorenzo

El mar, 25-07-2006 a las 22:38 +1200, Darryl Cousins escribió:
> Hi Lorenzo,
> 
> I got this test to work::
> 
> >>> class TestField(Persistent):
> ... pass
> 
> >>> class TestCase(Persistent):
> ... def __init__(self):
> ... self.fieldkey = KeyReferenceToPersistent(TestField())
> 
> >>> t = TestCase()
> Traceback (most recent call last):
> ...
> NotYet:...
> 
> >>> class TestCaseTwo(Persistent):
> ... def __init__(self):
> ... self.field = TestField()
> ... def initfield(self):
> ... self.fieldkey = KeyReferenceToPersistent(self.field)
> ... del(self.field)
> 
> >>> t = TestCaseTwo()
> >>> tc = root['t'] = t
> 
> >>> transaction.commit()
> >>> tc.initfield()
> >>> print tc.field
> Traceback (most recent call last):
> ...
> AttributeError:...
> 
> (Set up for test is shown in 
> 
> Which would mean that if you created the FieldIndex in __init__ and the
> catalog[key] part in the initialization handler then it should work.
> 
> I put the del(self.field) bit in as a test in case you don't want that
> attribute unecessarily hanging around.
> 
> Hope **that** helps.
> 
> Regards,
> Darryl
> 
> On Tue, 2006-07-25 at 12:24 +0200, Lorenzo Gil Sanchez wrote:
> > No worries, in fact I actually tried the same aproach and hit the same
> > problem
> > 
> > Thanks anyway
> > 
> > Lorenzo
> > 
> > El mar, 25-07-2006 a las 22:16 +1200, Darryl Cousins escribió:
> > > Hi again,
> > > 
> > > I was a too hasty. Because it is the FieldIndex that is missing the
> > > _p_oid attribute, and the fix I offered is for the container. Sorry
> > > about that.
> > > 
> > > I need more thinking.
> > > 
> > > Regards,
> > > Darryl
> > > 
> > > 
> > > On Tue, 2006-07-25 at 22:10 +1200, Darryl Cousins wrote:
> > > > Hi Lorenzo,
> > > > 
> > > > The key reference adapter can only get a key reference **after** the
> > > > object is added to the database (because it uses _p_oid).
> > > > 
> > > > The problem could be fixed with a subscriber to IObjectAddedEvent::
> > > > 
> > > >   def FilterableContainerInitializationHandler(object, event):
> > > >   """Initialize container after its ObjectAddedEvent."""
> > > > 
> > > > # I have checks here too (maybe unecessary)
> > > > if not IObjectAddedEvent.providedBy(event):
> > > > return
> > > > if not IFilterableContainer.providedBy(object):
> > > > return
> > > > # whatever needs to be done
> > > > # self._catalog ...
> > > > 
> > > > This handler is configured::
> > > > 
> > > >   
> > > >> > > for=".IFilterableContainer
> > > >  zope.app.container.interfaces.IObjectAddedEvent"
> > > > handler=".subscribers.FilterableContainerInitializationHandler"
> > > > />
> > > > 
> > > > Hope that helps.
> > > > 
> > > > Best regards,
> > > > Darryl
> > > > 
> > > > On Tue, 2006-07-25 at 11:26 +0200, Lorenzo Gil Sanchez wrote:
> > > > > Hi,
> > > > > 
> > > > > I have a special Folder content. When an instance of this class is
> > > > > created I want to create a catalog and an index in its __init__ 
> > > > > method.
> > > > > Something like:
> > > > > 
> > > > > class FilterableContainer(BTreeContainer):
> > > > > 
> > > > > implements(IFilterableContainer)
> > > > > 
> > > > > def __init__(self):
> > > > > super(FilterableContainer, self).__init__()
> > > > > self._catalog = LocalCatalog()
> > > > > self._catalog['attr1'] = FieldIndex(field_name='attr1')
> > > > > 
> > > > > where LocalCatalog is a subclass of Catalog that only indexes objects
> > > > > that belong to its parent.
> > > > > 
> > > > > The problem arises when I add my FieldIndex to my internal catalog. 
> > > > > Some
> > > > > events are triggered and at the end I got this exception:
> > > > > 
> > > > >   File 
> > > > > "/opt/Zope-3.2.1/lib/python/zope/app/keyreference/persistent.py",
> > > > > line 41, in __init__
> > > > > raise zope.app.keyreference.interfaces.NotYet(object)
> > > > > NotYet: 
> > > > > 
> > > > > 
> > > > > It happens because the key reference adaptor to Persistent thinks my
> > > > > FieldIndex object is already stored in the database, which is not the
> > > > > case.
> > > > > 
> > > > > Anybody knows how should I add an index to my catalog in the __init__
>

Re: [Zope3-Users] Re: Next steps...

2006-07-25 Thread Philipp von Weitershausen
Chris Withers wrote:
> Philipp von Weitershausen wrote:
>> I wouldn't say that. It depends on your taste, really. I personally
>> prefer the package-includes approach for development environments; for
>> actual deployments I find having no package-includes at all, but instead
>> putting everything in site.zcml useful, because it's easier to see in
>> one place what's included and what not.
> 
> I certainly prefer this ;-)
> 
> That said, I do like Apache 2 - on - Debian's a2enmod, a2dismod,
> a2ensite and a2dissite. If Zope had equivalents, I'd probably flip round
> to the other view...

I suggested such a script once,
http://www.z3lab.org/sections/blogs/philipp-weitershausen/2006_01_11_mata-los-productos

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


Re: [Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Darryl Cousins
Hi All,

I was thinking about that del(self.field) that I threw in. Wouldn't that
remove the object too and therefore the key reference?

Apparently not (in the test anyway)::

>>> class TestField(Persistent):
... id = u''
... def __init__(self, id):
...  self.id = id

>>> class TestCaseTwo(Persistent):
... def __init__(self):
... self.field = TestField(id=u'test case two')
... def initfield(self):
... self.fieldkey = KeyReferenceToPersistent(self.field)
... del(self.field)

>>> t = TestCaseTwo()
>>> tc = root['t'] = t
>>> transaction.commit()
>>> tc.initfield()
>>> print tc.field
Traceback (most recent call last):
...
AttributeError:...
>>> print tc.fieldkey().id
test case two

>>> transaction.commit()
>>> print tc.fieldkey().id  # I thought this would fail
test case two

Best regards,
Darryl

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


Re: [Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Darryl Cousins
Hi Lorenzo,

I got this test to work::

>>> class TestField(Persistent):
... pass

>>> class TestCase(Persistent):
... def __init__(self):
... self.fieldkey = KeyReferenceToPersistent(TestField())

>>> t = TestCase()
Traceback (most recent call last):
...
NotYet:...

>>> class TestCaseTwo(Persistent):
... def __init__(self):
... self.field = TestField()
... def initfield(self):
... self.fieldkey = KeyReferenceToPersistent(self.field)
... del(self.field)

>>> t = TestCaseTwo()
>>> tc = root['t'] = t

>>> transaction.commit()
>>> tc.initfield()
>>> print tc.field
Traceback (most recent call last):
...
AttributeError:...

(Set up for test is shown in 

Which would mean that if you created the FieldIndex in __init__ and the
catalog[key] part in the initialization handler then it should work.

I put the del(self.field) bit in as a test in case you don't want that
attribute unecessarily hanging around.

Hope **that** helps.

Regards,
Darryl

On Tue, 2006-07-25 at 12:24 +0200, Lorenzo Gil Sanchez wrote:
> No worries, in fact I actually tried the same aproach and hit the same
> problem
> 
> Thanks anyway
> 
> Lorenzo
> 
> El mar, 25-07-2006 a las 22:16 +1200, Darryl Cousins escribió:
> > Hi again,
> > 
> > I was a too hasty. Because it is the FieldIndex that is missing the
> > _p_oid attribute, and the fix I offered is for the container. Sorry
> > about that.
> > 
> > I need more thinking.
> > 
> > Regards,
> > Darryl
> > 
> > 
> > On Tue, 2006-07-25 at 22:10 +1200, Darryl Cousins wrote:
> > > Hi Lorenzo,
> > > 
> > > The key reference adapter can only get a key reference **after** the
> > > object is added to the database (because it uses _p_oid).
> > > 
> > > The problem could be fixed with a subscriber to IObjectAddedEvent::
> > > 
> > >   def FilterableContainerInitializationHandler(object, event):
> > >   """Initialize container after its ObjectAddedEvent."""
> > > 
> > >   # I have checks here too (maybe unecessary)
> > >   if not IObjectAddedEvent.providedBy(event):
> > >   return
> > >   if not IFilterableContainer.providedBy(object):
> > >   return
> > >   # whatever needs to be done
> > >   # self._catalog ...
> > > 
> > > This handler is configured::
> > > 
> > >   
> > >> > for=".IFilterableContainer
> > >  zope.app.container.interfaces.IObjectAddedEvent"
> > > handler=".subscribers.FilterableContainerInitializationHandler"
> > > />
> > > 
> > > Hope that helps.
> > > 
> > > Best regards,
> > > Darryl
> > > 
> > > On Tue, 2006-07-25 at 11:26 +0200, Lorenzo Gil Sanchez wrote:
> > > > Hi,
> > > > 
> > > > I have a special Folder content. When an instance of this class is
> > > > created I want to create a catalog and an index in its __init__ method.
> > > > Something like:
> > > > 
> > > > class FilterableContainer(BTreeContainer):
> > > > 
> > > > implements(IFilterableContainer)
> > > > 
> > > > def __init__(self):
> > > > super(FilterableContainer, self).__init__()
> > > > self._catalog = LocalCatalog()
> > > > self._catalog['attr1'] = FieldIndex(field_name='attr1')
> > > > 
> > > > where LocalCatalog is a subclass of Catalog that only indexes objects
> > > > that belong to its parent.
> > > > 
> > > > The problem arises when I add my FieldIndex to my internal catalog. Some
> > > > events are triggered and at the end I got this exception:
> > > > 
> > > >   File "/opt/Zope-3.2.1/lib/python/zope/app/keyreference/persistent.py",
> > > > line 41, in __init__
> > > > raise zope.app.keyreference.interfaces.NotYet(object)
> > > > NotYet: 
> > > > 
> > > > 
> > > > It happens because the key reference adaptor to Persistent thinks my
> > > > FieldIndex object is already stored in the database, which is not the
> > > > case.
> > > > 
> > > > Anybody knows how should I add an index to my catalog in the __init__
> > > > method?
> > > > 
> > > > Thanks in advance
> > > > 
> > > > Lorenzo
> > > > 
> > > > ___
> > > > Zope3-users mailing list
> > > > Zope3-users@zope.org
> > > > http://mail.zope.org/mailman/listinfo/zope3-users
> > > 
> > > ___
> > > Zope3-users mailing list
> > > Zope3-users@zope.org
> > > http://mail.zope.org/mailman/listinfo/zope3-users
> > 
> 
> ___
> Zope3-users mailing list
> Zope3-users@zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users

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


Re: [Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Lorenzo Gil Sanchez
No worries, in fact I actually tried the same aproach and hit the same
problem

Thanks anyway

Lorenzo

El mar, 25-07-2006 a las 22:16 +1200, Darryl Cousins escribió:
> Hi again,
> 
> I was a too hasty. Because it is the FieldIndex that is missing the
> _p_oid attribute, and the fix I offered is for the container. Sorry
> about that.
> 
> I need more thinking.
> 
> Regards,
> Darryl
> 
> 
> On Tue, 2006-07-25 at 22:10 +1200, Darryl Cousins wrote:
> > Hi Lorenzo,
> > 
> > The key reference adapter can only get a key reference **after** the
> > object is added to the database (because it uses _p_oid).
> > 
> > The problem could be fixed with a subscriber to IObjectAddedEvent::
> > 
> >   def FilterableContainerInitializationHandler(object, event):
> >   """Initialize container after its ObjectAddedEvent."""
> > 
> > # I have checks here too (maybe unecessary)
> > if not IObjectAddedEvent.providedBy(event):
> > return
> > if not IFilterableContainer.providedBy(object):
> > return
> > # whatever needs to be done
> > # self._catalog ...
> > 
> > This handler is configured::
> > 
> >   
> >> for=".IFilterableContainer
> >  zope.app.container.interfaces.IObjectAddedEvent"
> > handler=".subscribers.FilterableContainerInitializationHandler"
> > />
> > 
> > Hope that helps.
> > 
> > Best regards,
> > Darryl
> > 
> > On Tue, 2006-07-25 at 11:26 +0200, Lorenzo Gil Sanchez wrote:
> > > Hi,
> > > 
> > > I have a special Folder content. When an instance of this class is
> > > created I want to create a catalog and an index in its __init__ method.
> > > Something like:
> > > 
> > > class FilterableContainer(BTreeContainer):
> > > 
> > > implements(IFilterableContainer)
> > > 
> > > def __init__(self):
> > > super(FilterableContainer, self).__init__()
> > > self._catalog = LocalCatalog()
> > > self._catalog['attr1'] = FieldIndex(field_name='attr1')
> > > 
> > > where LocalCatalog is a subclass of Catalog that only indexes objects
> > > that belong to its parent.
> > > 
> > > The problem arises when I add my FieldIndex to my internal catalog. Some
> > > events are triggered and at the end I got this exception:
> > > 
> > >   File "/opt/Zope-3.2.1/lib/python/zope/app/keyreference/persistent.py",
> > > line 41, in __init__
> > > raise zope.app.keyreference.interfaces.NotYet(object)
> > > NotYet: 
> > > 
> > > 
> > > It happens because the key reference adaptor to Persistent thinks my
> > > FieldIndex object is already stored in the database, which is not the
> > > case.
> > > 
> > > Anybody knows how should I add an index to my catalog in the __init__
> > > method?
> > > 
> > > Thanks in advance
> > > 
> > > Lorenzo
> > > 
> > > ___
> > > Zope3-users mailing list
> > > Zope3-users@zope.org
> > > http://mail.zope.org/mailman/listinfo/zope3-users
> > 
> > ___
> > Zope3-users mailing list
> > Zope3-users@zope.org
> > http://mail.zope.org/mailman/listinfo/zope3-users
> 

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


Re: [Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Darryl Cousins
Hi again,

I was a too hasty. Because it is the FieldIndex that is missing the
_p_oid attribute, and the fix I offered is for the container. Sorry
about that.

I need more thinking.

Regards,
Darryl


On Tue, 2006-07-25 at 22:10 +1200, Darryl Cousins wrote:
> Hi Lorenzo,
> 
> The key reference adapter can only get a key reference **after** the
> object is added to the database (because it uses _p_oid).
> 
> The problem could be fixed with a subscriber to IObjectAddedEvent::
> 
>   def FilterableContainerInitializationHandler(object, event):
>   """Initialize container after its ObjectAddedEvent."""
> 
>   # I have checks here too (maybe unecessary)
>   if not IObjectAddedEvent.providedBy(event):
>   return
>   if not IFilterableContainer.providedBy(object):
>   return
>   # whatever needs to be done
>   # self._catalog ...
> 
> This handler is configured::
> 
>   
>for=".IFilterableContainer
>  zope.app.container.interfaces.IObjectAddedEvent"
> handler=".subscribers.FilterableContainerInitializationHandler"
> />
> 
> Hope that helps.
> 
> Best regards,
> Darryl
> 
> On Tue, 2006-07-25 at 11:26 +0200, Lorenzo Gil Sanchez wrote:
> > Hi,
> > 
> > I have a special Folder content. When an instance of this class is
> > created I want to create a catalog and an index in its __init__ method.
> > Something like:
> > 
> > class FilterableContainer(BTreeContainer):
> > 
> > implements(IFilterableContainer)
> > 
> > def __init__(self):
> > super(FilterableContainer, self).__init__()
> > self._catalog = LocalCatalog()
> > self._catalog['attr1'] = FieldIndex(field_name='attr1')
> > 
> > where LocalCatalog is a subclass of Catalog that only indexes objects
> > that belong to its parent.
> > 
> > The problem arises when I add my FieldIndex to my internal catalog. Some
> > events are triggered and at the end I got this exception:
> > 
> >   File "/opt/Zope-3.2.1/lib/python/zope/app/keyreference/persistent.py",
> > line 41, in __init__
> > raise zope.app.keyreference.interfaces.NotYet(object)
> > NotYet: 
> > 
> > 
> > It happens because the key reference adaptor to Persistent thinks my
> > FieldIndex object is already stored in the database, which is not the
> > case.
> > 
> > Anybody knows how should I add an index to my catalog in the __init__
> > method?
> > 
> > Thanks in advance
> > 
> > Lorenzo
> > 
> > ___
> > Zope3-users mailing list
> > Zope3-users@zope.org
> > http://mail.zope.org/mailman/listinfo/zope3-users
> 
> ___
> Zope3-users mailing list
> Zope3-users@zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users

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


Re: [Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Darryl Cousins
Hi Lorenzo,

The key reference adapter can only get a key reference **after** the
object is added to the database (because it uses _p_oid).

The problem could be fixed with a subscriber to IObjectAddedEvent::

  def FilterableContainerInitializationHandler(object, event):
  """Initialize container after its ObjectAddedEvent."""

# I have checks here too (maybe unecessary)
if not IObjectAddedEvent.providedBy(event):
return
if not IFilterableContainer.providedBy(object):
return
# whatever needs to be done
# self._catalog ...

This handler is configured::

  
  

Hope that helps.

Best regards,
Darryl

On Tue, 2006-07-25 at 11:26 +0200, Lorenzo Gil Sanchez wrote:
> Hi,
> 
> I have a special Folder content. When an instance of this class is
> created I want to create a catalog and an index in its __init__ method.
> Something like:
> 
> class FilterableContainer(BTreeContainer):
> 
> implements(IFilterableContainer)
> 
> def __init__(self):
> super(FilterableContainer, self).__init__()
> self._catalog = LocalCatalog()
> self._catalog['attr1'] = FieldIndex(field_name='attr1')
> 
> where LocalCatalog is a subclass of Catalog that only indexes objects
> that belong to its parent.
> 
> The problem arises when I add my FieldIndex to my internal catalog. Some
> events are triggered and at the end I got this exception:
> 
>   File "/opt/Zope-3.2.1/lib/python/zope/app/keyreference/persistent.py",
> line 41, in __init__
> raise zope.app.keyreference.interfaces.NotYet(object)
> NotYet: 
> 
> 
> It happens because the key reference adaptor to Persistent thinks my
> FieldIndex object is already stored in the database, which is not the
> case.
> 
> Anybody knows how should I add an index to my catalog in the __init__
> method?
> 
> Thanks in advance
> 
> Lorenzo
> 
> ___
> Zope3-users mailing list
> Zope3-users@zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users

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


Re: [Zope3-Users] Re: Next steps...

2006-07-25 Thread Chris Withers

Philipp von Weitershausen wrote:

I wouldn't say that. It depends on your taste, really. I personally
prefer the package-includes approach for development environments; for
actual deployments I find having no package-includes at all, but instead
putting everything in site.zcml useful, because it's easier to see in
one place what's included and what not.


I certainly prefer this ;-)

That said, I do like Apache 2 - on - Debian's a2enmod, a2dismod, 
a2ensite and a2dissite. If Zope had equivalents, I'd probably flip round 
to the other view...


Chris

--
Simplistix - Content Management, Zope & Python Consulting
   - http://www.simplistix.co.uk
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


[Zope3-Users] adding indexes in a __init__ method

2006-07-25 Thread Lorenzo Gil Sanchez
Hi,

I have a special Folder content. When an instance of this class is
created I want to create a catalog and an index in its __init__ method.
Something like:

class FilterableContainer(BTreeContainer):

implements(IFilterableContainer)

def __init__(self):
super(FilterableContainer, self).__init__()
self._catalog = LocalCatalog()
self._catalog['attr1'] = FieldIndex(field_name='attr1')

where LocalCatalog is a subclass of Catalog that only indexes objects
that belong to its parent.

The problem arises when I add my FieldIndex to my internal catalog. Some
events are triggered and at the end I got this exception:

  File "/opt/Zope-3.2.1/lib/python/zope/app/keyreference/persistent.py",
line 41, in __init__
raise zope.app.keyreference.interfaces.NotYet(object)
NotYet: 


It happens because the key reference adaptor to Persistent thinks my
FieldIndex object is already stored in the database, which is not the
case.

Anybody knows how should I add an index to my catalog in the __init__
method?

Thanks in advance

Lorenzo

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


Re: [Zope3-Users] Re: Adapters and getters and setters

2006-07-25 Thread Darryl Cousins
Hi Phillip,

Cool. Thanks very much for that!

Best regards,
Darryl

On Tue, 2006-07-25 at 10:16 +0200, Philipp von Weitershausen wrote:
>   # account for everything in the IBuddy interface
>   for field in IBuddy:
>   locals()[field] = property(
>   lambda self: getattr(self.context, field),
>   lambda self, value: setattr(self.context, field, value)
>   ) 

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


[Zope3-Users] Re: Adapters and getters and setters

2006-07-25 Thread Philipp von Weitershausen
Darryl Cousins wrote:
> I only recently began using formlib and I have used a schema to describe
> the form, and provided an adapter to adapt the object to the schema.
> 
> It seems that formlib uses the adapter to access and set attributes on
> the object.

That's correct. That way, for example, you can have an edit form made
from the IZopeDublinCore interface for objects that don't provide this
interface directly. The edit form will instead operate on the adapter,
and therefore actually set the dublin core metadata the right way.

> To allow it to do that I need to provide getters and setters
> for the adapter to access the attributes on the object itself:

That doesn't make sense. Either your adapter implements the full range
of the form schema (in which case you might want to dispatch the
attribute assignment for certain form fields or not...) or it doesn't
(in which case you have a bug).

> Before using formlib I would usually have a getContext() method on the
> adapter to return the adapted object and manually get or set the
> attributes (in the update method for example). Doing the above means I
> don't need to.
> 
> Am I going about this the right way?

The getContext() thing is superfluous. The adaption is much more flexible.

Let's say you have a buddy schema:

  class IBuddy(zope.interface.Interface):
  first = TextLine(title=_("First name"))
  last = TextLine(title=_("Last name"))
  email = TextLine(title=_("Electronic mail address"))
  address = Text(title=_("Postal address"))
  postal_code = TextLine(title=_("Postal code"),
   constraint=re.compile("\d{5,5}(-\d{4,4})?$").match)

Now, the edit form for this could be different:

  class IBuddyForm(IBuddy):
  # also allow city to be entered, we deduce postal_code
  # from that
  city = TextLine(title=_("City"))

Now, buddies don't provide IBuddyForm, they only provide IBuddy. Hence
we need an adapter:

  class BuddyFormAdapter(object):
  adapts(IBuddy)
  implements(IBuddyForm)

  def __init__(self, context):
  self.context = context

  # account for everything in the IBuddy interface
  for field in IBuddy:
  locals()[field] = property(
  lambda self: getattr(self.context, field),
  lambda self, value: setattr(self.context, field, value)
  )

  # account for additional stuff in IBuddyForm
  def getCity(self):
  return getCityFromPostalCode(self.postal_code)
  def setCity(self):
  postal_code = getPostalCodeFromCity(city)
  self.postal_code = postal_code
  city = property(getCity, setCity)

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