[Zope3-Users] "has a" relationships

Jachin Rupe jachin at voltzsoftware.com
Tue May 9 18:01:54 EDT 2006


hi there

Alright... I'm working on implementing IPerson in the class Person  
and I'm not sure exactly what has to happen.  I kinda have two ideas  
but I suspect I need to some how combine them but I don't know how to  
do that.  A big part of the problem is that I do not know what  
zope.schema.fieldproperty.FieldProperty does.  The only reason I'm  
working it in, is because it appears to be necessary to get this  
whole thing to work:

http://svn.zope.org/Zope3/trunk/src/zope/app/form/browser/ 
objectwidget.txt

Here are my ideas:

--------------

from persistent import Persistent
from zope.interface import implements

from abook.interfaces import IPerson

class Person(Persistent):
	
	implements(IPerson)
	
	firstName = u""
	lastName = u""
	phoneNumbers = persistent.dict.PersistentDict()
	emails = persistent.dict.PersistentDict()
	addresses = persistent.dict.PersistentDict()

--------------

from persistent import Persistent
from zope.interface import implements
from zope.schema.fieldproperty import FieldProperty

from abook.interfaces import IStreetAddress
from abook.interfaces import IPerson

class Person(Persistent):
	
	implements(IPerson)
	
	phoneNumbers = FieldProperty(IPerson['phoneNumbers'])
	emails = FieldProperty(IPerson['emails'])
	addresses = FieldProperty(IPerson['addresses'])
	
	def __init__(self,
					firstName = u"", lastName = u"",
					phoneNumbers = None, emails = None,
					addresses = None):
		
		self.firstName = firstName
		self.lastName = lastName
		self.phoneNumbers = phoneNumbers
		self.emails = emails
		self.addresses = addresses
	

--------------

I guess my question is, how should I be implementing Person?  Is one  
of those right or are they both wrong?

thanks

-jachin


>
> #------------abook/interfaces.py------------
>
> from zope.interface import Interface
> import zope.schema
>
> class IStreetAddress(Interface):
> 	"""A vine street address"""
> 	
> 	street = zope.schema.Text (
> 		title=u"Street 1",
> 		description=u"The street address",
> 		required = False
> 	)
> 	
> 	city = zope.schema.TextLine (
> 		title=u"City",
> 		description=u"The city.",
> 		required = False
> 	)
> 	
> 	state = zope.schema.TextLine (
> 		title=u"State",
> 		description=u"The state.",
> 		required = False
> 	)
> 	
> 	zipcode = zope.schema.TextLine (
> 		title=u"Zip Code",
> 		description=u"The zip code",
> 		required = False,
> 		min_length = 5
> 	)
>
> class IABookEntry(Interface):
> 	phoneNumbers = zope.schema.Dict(
> 		title=u"Phone Numbers",
> 		description=u"The phone numbers for this entry",
> 		required=False,
> 		key_type=zope.schema.TextLine (
> 				title=u"Type",
> 				description=u"The type of phone number",
> 				required=True
> 			),
> 		value_type=zope.schema.TextLine (
> 				title=u"Number",
> 				description=u"The phone number.",
> 				required=True
> 			)
> 		)
> 	
> 	emails = zope.schema.Dict(
> 		title=u"Email Addresses",
> 		description=u"The email addresses for this entry",
> 		required=False,
> 		key_type=zope.schema.TextLine (
> 				title=u"Type",
> 				description=u"The type of email address",
> 				required=True
> 			),
> 		value_type=zope.schema.TextLine (
> 				title=u"Email Address",
> 				description=u"The email address.",
> 				required=True
> 			)
> 		)
> 	
> 	addresses = zope.schema.Dict(
> 		title=u"Addresses",
> 		description=u"Street address",
> 		required=False,
> 		key_type=zope.schema.TextLine(
> 				title=u"Type",
> 				description=u"The type of street address",
> 				required=True
> 			),
> 		value_type=zope.schema.Object (
> 				title=u"Street Address",
> 				description=u"A street address",
> 				required=True,
> 				schema=IStreetAddress
> 			)
> 		)
>
>
> class IPerson(IABookEntry):
> 	firstName = zope.schema.TextLine (
> 		title=u"First Name",
> 		description=u"The person's first name",
> 		required=False
> 	)
> 	
> 	lastName = zope.schema.TextLine (
> 		title=u"Last Name",
> 		description=u"The person's last name",
> 		required=False
> 	)
> 	
> 	
> class IBusiness(IABookEntry):
> 	
> 	businessName = zope.schema.TextLine (
> 		title=u"Business Name",
> 		description=u"The business' last name",
> 		required=False
> 	)
>
>
>
> #------------abook/streetAddress.py------------
>
>
> from persistent import Persistent
> from zope.interface import implements
>
> from abook.interfaces import IStreetAddress
>
> class StreetAddress(Persistent):
> 	"""This is a class for holding street addresses
> 	
> 	Make sure that StreetAddress implements the
> 	correct interface interface:
> 	
> 	>>> from zope.interface.verify import verifyClass
> 	>>> verifyClass(IStreetAddress, StreetAddress)
> 	True
> 	
> 	Now lets expierment with changing around some of
> 	the attributes:
> 	
> 	>>> a = StreetAddress()
> 	>>> a.street
> 	u''
> 	>>> a.street = u"123 Some St."
> 	>>> a.street
> 	u'123 Some St.'
> 	
> 	>>> a.city
> 	u''
> 	
> 	>>> a.state
> 	u''
> 	
> 	>>> a.zipcode
> 	u''
> 	
> 	"""
> 	implements(IStreetAddress)
> 	
> 	street = u""
> 	city = u""
> 	state = u""
> 	zipcode = u""
>
>
> #------------abook/tests/test_StreetAddress.py------------
>
>
> import unittest
> from zope.testing.doctestunit import DocTestSuite
> 	
>
> def test_suite():
> 	return unittest.TestSuite((
> 		DocTestSuite('abook.streetAddress'),
> 		))
> 	
>
> if __name__ == '__main__':
> 	unittest.main(defaultTest='test_suite')
>
>




More information about the Zope3-users mailing list