[Checkins] SVN: z3c.relationfield/trunk/src/z3c/relationfield/
Refactoring: we had a Relation field and a Relation value.
Rename Relation
Martijn Faassen
faassen at infrae.com
Tue Apr 15 08:53:45 EDT 2008
Log message for revision 85382:
Refactoring: we had a Relation field and a Relation value. Rename Relation
value class to RelationValue.
All relevant imports now come from __init__.py and interfaces.py, not from
sub-modules.
Changed:
U z3c.relationfield/trunk/src/z3c/relationfield/README.txt
U z3c.relationfield/trunk/src/z3c/relationfield/__init__.py
U z3c.relationfield/trunk/src/z3c/relationfield/event.py
U z3c.relationfield/trunk/src/z3c/relationfield/ftests.py
U z3c.relationfield/trunk/src/z3c/relationfield/index.py
U z3c.relationfield/trunk/src/z3c/relationfield/interfaces.py
U z3c.relationfield/trunk/src/z3c/relationfield/relation.py
U z3c.relationfield/trunk/src/z3c/relationfield/schema.py
-=-
Modified: z3c.relationfield/trunk/src/z3c/relationfield/README.txt
===================================================================
--- z3c.relationfield/trunk/src/z3c/relationfield/README.txt 2008-04-15 11:31:07 UTC (rev 85381)
+++ z3c.relationfield/trunk/src/z3c/relationfield/README.txt 2008-04-15 12:53:41 UTC (rev 85382)
@@ -21,11 +21,12 @@
We previously defined an interface ``IItem`` with a relation field in
it. We also defined a class ``Item`` that implements both ``IItem``
and the special ``z3c.relationfield.interfaces.IHasRelations``
-interface. This marker interface is needed to let the relations be
-cataloged.
+interface. The ``IHasRelation`` marker interface is needed to let the
+relations be cataloged. Unfortunately we cannot define ``Item`` and
+``IItem`` in the doctest here, as these objects need to be stored in
+the ZODB cleanly and therefore need to be in a module. Let's set up a
+test application in a container::
-Let's set up a test application in a container::
-
>>> root = getRootFolder()['root'] = TestApp()
We make sure that this is the current site, so we can look up local
@@ -55,9 +56,9 @@
Now we can create an item ``b`` that links to item ``a``::
- >>> from z3c.relationfield.relation import Relation
+ >>> from z3c.relationfield import RelationValue
>>> b = Item()
- >>> b.rel = Relation(a_id)
+ >>> b.rel = RelationValue(a_id)
We now store the ``b`` object, which will also set up its relation::
@@ -75,15 +76,16 @@
>>> to_object.__name__
u'a'
-We can also get the object that is doing the pointing; the RelationProperty
-took care of setting this::
+We can also get the object that is doing the pointing; since we
+supplied the ``IHasRelations`` interface, the event system took care
+of setting this::
>>> from_object = root['b'].rel.from_object
>>> from_object.__name__
u'b'
-This object is also known as the ``__parent__``; again the RelationProperty
-took care of setting this::
+This object is also known as the ``__parent__``; again the event
+sytem took care of setting this::
>>> parent_object = root['b'].rel.__parent__
>>> parent_object is from_object
@@ -177,7 +179,7 @@
>>> l = sorted(catalog.findRelations({'to_id': intids.getId(root['a'])}))
>>> l
- [<z3c.relationfield.relation.Relation object at ...>]
+ [<z3c.relationfield.relation.RelationValue object at ...>]
We look at this relation object again. We indeed go the right one::
@@ -209,7 +211,7 @@
... 'from_attribute': 'rel',
... 'from_interfaces_flattened': IItem,
... 'to_interfaces_flattened': IItem}))
- [<z3c.relationfield.relation.Relation object at ...>]
+ [<z3c.relationfield.relation.RelationValue object at ...>]
There are no relations stored for another attribute::
@@ -246,11 +248,11 @@
We currently have a relation from ``b`` to ``a``::
>>> sorted(catalog.findRelations({'to_id': intids.getId(root['a'])}))
- [<z3c.relationfield.relation.Relation object at ...>]
+ [<z3c.relationfield.relation.RelationValue object at ...>]
We can change the relation to point at a new object ``c``::
- >>> root['b'].rel = Relation(c_id)
+ >>> root['b'].rel = RelationValue(c_id)
We need to send an ``IObjectModifiedEvent`` to let the catalog know we
have changed the relations::
@@ -261,7 +263,7 @@
We should find now a single relation from ``b`` to ``c``::
>>> sorted(catalog.findRelations({'to_id': c_id}))
- [<z3c.relationfield.relation.Relation object at ...>]
+ [<z3c.relationfield.relation.RelationValue object at ...>]
The relation to ``a`` should now be gone::
@@ -274,7 +276,7 @@
We have a relation from ``b`` to ``c`` right now::
>>> sorted(catalog.findRelations({'to_id': c_id}))
- [<z3c.relationfield.relation.Relation object at ...>]
+ [<z3c.relationfield.relation.RelationValue object at ...>]
We can clean up an existing relation from ``b`` to ``c`` by setting it
to ``None``::
@@ -294,11 +296,11 @@
Let's reestablish the removed relation::
- >>> root['b'].rel = Relation(c_id)
+ >>> root['b'].rel = RelationValue(c_id)
>>> notify(grok.ObjectModifiedEvent(root['b']))
>>> sorted(catalog.findRelations({'to_id': c_id}))
- [<z3c.relationfield.relation.Relation object at ...>]
+ [<z3c.relationfield.relation.RelationValue object at ...>]
Copying an object with relations
--------------------------------
@@ -341,14 +343,15 @@
If we have an import procedure where we import relations from some
external source such as an XML file, it may be that we read a relation
that points to an object that does not yet exist as it is yet to be
-imported. We provide a special ``TemporaryRelation`` for this case. A
-``TemporaryRelation`` just contains the path of what it is pointing
-to, but does not resolve it yet. Let's use ``TemporaryRelation`` in a new
-object, creating a relation to ``a``::
+imported. We provide a special ``TemporaryRelationValue`` for this
+case. A ``TemporaryRelationValue`` just contains the path of what it
+is pointing to, but does not resolve it yet. Let's use
+``TemporaryRelationValue`` in a new object, creating a relation to
+``a``::
- >>> from z3c.relationfield import TemporaryRelation
+ >>> from z3c.relationfield import TemporaryRelationValue
>>> root['d'] = Item()
- >>> root['d'].rel = TemporaryRelation('a')
+ >>> root['d'].rel = TemporaryRelationValue('a')
A modification event does not actually get this relation cataloged::
@@ -401,7 +404,7 @@
Let's take a look at the relation widget now::
>>> from zope.publisher.browser import TestRequest
- >>> from z3c.relationfield.widget import RelationWidget
+ >>> from z3c.relationfield import RelationWidget
>>> request = TestRequest()
>>> widget = RelationWidget(IItem['rel'], request)
>>> print widget()
@@ -415,7 +418,7 @@
on the object called "relationurl". Without such a view, the display
widget will link directly to the object::
- >>> from z3c.relationfield.widget import RelationDisplayWidget
+ >>> from z3c.relationfield import RelationDisplayWidget
>>> widget = RelationDisplayWidget(IItem['rel'], request)
We have to set the widget up with some data::
Modified: z3c.relationfield/trunk/src/z3c/relationfield/__init__.py
===================================================================
--- z3c.relationfield/trunk/src/z3c/relationfield/__init__.py 2008-04-15 11:31:07 UTC (rev 85381)
+++ z3c.relationfield/trunk/src/z3c/relationfield/__init__.py 2008-04-15 12:53:41 UTC (rev 85382)
@@ -1,4 +1,5 @@
-from relation import Relation, TemporaryRelation
-from schema import Relation as RelationField
+from relation import RelationValue, TemporaryRelationValue
+from index import RelationCatalog
+from schema import Relation
from event import realize_relations
-
+from widget import RelationWidget, RelationDisplayWidget
Modified: z3c.relationfield/trunk/src/z3c/relationfield/event.py
===================================================================
--- z3c.relationfield/trunk/src/z3c/relationfield/event.py 2008-04-15 11:31:07 UTC (rev 85381)
+++ z3c.relationfield/trunk/src/z3c/relationfield/event.py 2008-04-15 12:53:41 UTC (rev 85382)
@@ -6,10 +6,10 @@
from zope.app.intid.interfaces import IIntIds
from zc.relation.interfaces import ICatalog
-from z3c.relationfield.schema import Relation as RelationField
-from z3c.relationfield.relation import Relation
from z3c.relationfield.interfaces import (IHasRelations,
- IRelation, ITemporaryRelation)
+ IRelation,
+ IRelationValue,
+ ITemporaryRelationValue)
@grok.subscribe(IHasRelations, grok.IObjectAddedEvent)
def addRelations(obj, event):
@@ -50,7 +50,7 @@
"""Given an object, convert any temporary relatiosn on it to real ones.
"""
for name, relation in _potential_relations(obj):
- if ITemporaryRelation.providedBy(relation):
+ if ITemporaryRelationValue.providedBy(relation):
setattr(obj, name, relation.convert())
def _setRelation(obj, name, value):
@@ -75,22 +75,22 @@
catalog.index_doc(id, value)
def _relations(obj):
- """Given an object, return tuples of name, relation.
+ """Given an object, return tuples of name, relation value.
Only real relations are returned, not temporary relations.
"""
for name, relation in _potential_relations(obj):
- if IRelation.providedBy(relation):
+ if IRelationValue.providedBy(relation):
yield name, relation
def _potential_relations(obj):
- """Given an object return tuples of name, relation.
+ """Given an object return tuples of name, relation value.
- Returns both IRelation attributes as well as ITemporaryRelation
+ Returns both IRelationValue attributes as well as ITemporaryRelationValue
attributes.
"""
for iface in providedBy(obj).flattened():
for name, field in getFields(iface).items():
- if isinstance(field, RelationField):
+ if IRelation.providedBy(field):
relation = getattr(obj, name)
yield name, relation
Modified: z3c.relationfield/trunk/src/z3c/relationfield/ftests.py
===================================================================
--- z3c.relationfield/trunk/src/z3c/relationfield/ftests.py 2008-04-15 11:31:07 UTC (rev 85381)
+++ z3c.relationfield/trunk/src/z3c/relationfield/ftests.py 2008-04-15 12:53:41 UTC (rev 85382)
@@ -7,25 +7,28 @@
from zope.app.intid import IntIds
from zope.app.intid.interfaces import IIntIds
-from z3c.relationfield.index import RelationCatalog
from z3c.relationfield.interfaces import IHasRelations
-from z3c.relationfield import schema
+from z3c.relationfield import Relation, RelationCatalog
-import zope.testbrowser.browser
-import zope.testbrowser.testing
from zope.app.testing.functional import FunctionalDocFileSuite
from z3c.relationfield.testing import FunctionalLayer
class IItem(zope.interface.Interface):
- rel = schema.Relation(title=u"Relation")
+ """Test fixture used by README.txt
+ """
+ rel = Relation(title=u"Relation")
class Item(grok.Model):
+ """Test fixture used by README.txt
+ """
grok.implements(IItem, IHasRelations)
def __init__(self):
self.rel = None
class TestApp(grok.Application, grok.Container):
+ """Test fixture used by README.txt.
+ """
grok.local_utility(IntIds, provides=IIntIds)
grok.local_utility(RelationCatalog)
Modified: z3c.relationfield/trunk/src/z3c/relationfield/index.py
===================================================================
--- z3c.relationfield/trunk/src/z3c/relationfield/index.py 2008-04-15 11:31:07 UTC (rev 85381)
+++ z3c.relationfield/trunk/src/z3c/relationfield/index.py 2008-04-15 12:53:41 UTC (rev 85382)
@@ -8,7 +8,7 @@
from zc.relation.catalog import Catalog
from zc.relation.interfaces import ICatalog
-from z3c.relationfield.interfaces import IRelation
+from z3c.relationfield.interfaces import IRelationValue
def dump(obj, catalog, cache):
intids = cache.get('intids')
@@ -27,14 +27,14 @@
def __init__(self):
Catalog.__init__(self, dump, load)
- self.addValueIndex(IRelation['from_id'])
- self.addValueIndex(IRelation['to_id'])
- self.addValueIndex(IRelation['from_attribute'],
+ self.addValueIndex(IRelationValue['from_id'])
+ self.addValueIndex(IRelationValue['to_id'])
+ self.addValueIndex(IRelationValue['from_attribute'],
btree=BTrees.family32.OI)
- self.addValueIndex(IRelation['from_interfaces_flattened'],
+ self.addValueIndex(IRelationValue['from_interfaces_flattened'],
multiple=True,
btree=BTrees.family32.OI)
- self.addValueIndex(IRelation['to_interfaces_flattened'],
+ self.addValueIndex(IRelationValue['to_interfaces_flattened'],
multiple=True,
btree=BTrees.family32.OI)
Modified: z3c.relationfield/trunk/src/z3c/relationfield/interfaces.py
===================================================================
--- z3c.relationfield/trunk/src/z3c/relationfield/interfaces.py 2008-04-15 11:31:07 UTC (rev 85381)
+++ z3c.relationfield/trunk/src/z3c/relationfield/interfaces.py 2008-04-15 12:53:41 UTC (rev 85382)
@@ -1,6 +1,22 @@
from zope.interface import Interface, Attribute
+from zope.schema.interfaces import IField
-class IRelation(Interface):
+class IHasRelations(Interface):
+ """Marker interface indicating that the object has relations.
+
+ Use this interface to make sure that the relations get added and
+ removed from the catalog when appropriate.
+ """
+
+class IRelation(IField):
+ pass
+
+class IRelationValue(Interface):
+ """A relation between the parent object and another one.
+
+ This should be stored as the value in the object when the schema uses the
+ Relation field.
+ """
from_object = Attribute("The object this relation is pointing from.")
from_id = Attribute("Id of the object this relation is pointing from.")
@@ -27,13 +43,13 @@
"The interfaces of the to object, flattened. "
"This includes all base interfaces.")
-class ITemporaryRelation(Interface):
+class ITemporaryRelationValue(Interface):
"""A temporary relation.
When importing relations from XML, we cannot resolve them into
- true Relation objects yet, as it may be that the object that is
+ true RelationValue objects yet, as it may be that the object that is
being related to has not yet been loaded. Instead we create
- a TemporaryRelation object that can be converted into a real one
+ a TemporaryRelationValue object that can be converted into a real one
after the import has been concluded.
"""
def convert():
@@ -42,13 +58,7 @@
Returns real relation object
"""
-class IHasRelations(Interface):
- """Marker interface indicating that the object has relations.
- Use this interface to make sure that the relations get added and
- removed from the catalog when appropriate.
- """
-
class IRelationInfo(Interface):
"""Relationship information for an object.
"""
Modified: z3c.relationfield/trunk/src/z3c/relationfield/relation.py
===================================================================
--- z3c.relationfield/trunk/src/z3c/relationfield/relation.py 2008-04-15 11:31:07 UTC (rev 85381)
+++ z3c.relationfield/trunk/src/z3c/relationfield/relation.py 2008-04-15 12:53:41 UTC (rev 85382)
@@ -7,11 +7,12 @@
from z3c.objpath.interfaces import IObjectPath
-from z3c.relationfield.interfaces import (IRelation, ITemporaryRelation,
+from z3c.relationfield.interfaces import (IRelationValue,
+ ITemporaryRelationValue,
IRelationInfo)
-class Relation(Persistent):
- implements(IRelation)
+class RelationValue(Persistent):
+ implements(IRelationValue)
def __init__(self, to_id):
self.to_id = to_id
@@ -58,13 +59,13 @@
return cmp(self.to_id, None)
return cmp(self.to_id, other.to_id)
-class TemporaryRelation(Persistent):
+class TemporaryRelationValue(Persistent):
"""A relation that isn't fully formed yet.
It needs to be finalized afterwards, when we are sure all potential
target objects exist.
"""
- grok.implements(ITemporaryRelation)
+ grok.implements(ITemporaryRelationValue)
def __init__(self, to_path):
self.to_path = to_path
@@ -75,7 +76,7 @@
to_object = object_path.resolve(self.to_path)
intids = component.getUtility(IIntIds)
to_id = intids.getId(to_object)
- return Relation(to_id)
+ return RelationValue(to_id)
def _object(id):
intids = component.getUtility(IIntIds)
Modified: z3c.relationfield/trunk/src/z3c/relationfield/schema.py
===================================================================
--- z3c.relationfield/trunk/src/z3c/relationfield/schema.py 2008-04-15 11:31:07 UTC (rev 85381)
+++ z3c.relationfield/trunk/src/z3c/relationfield/schema.py 2008-04-15 12:53:41 UTC (rev 85382)
@@ -2,23 +2,20 @@
from lxml import etree
-from zope import schema
from zope.interface import implements
-from zope.schema.interfaces import IField
from zope.schema import Field
-from z3c.objpath.interfaces import IObjectPath
import z3c.schema2xml
-from z3c.relationfield.relation import TemporaryRelation
+from z3c.relationfield.interfaces import IRelation
+from z3c.relationfield.relation import TemporaryRelationValue
-class IRelation(IField):
- pass
-
class Relation(Field):
implements(IRelation)
class RelationGenerator(grok.Adapter):
+ """Eport a relation to XML.
+ """
grok.context(IRelation)
grok.implements(z3c.schema2xml.IXMLGenerator)
@@ -31,4 +28,4 @@
if element.text is None:
return None
path = element.text
- return TemporaryRelation(path)
+ return TemporaryRelationValue(path)
More information about the Checkins
mailing list