[Checkins] SVN: lovely.relation/trunk/src/lovely/relation/ minimize the requirements when instantiating DataRelationship

Juergen Kartnaller juergen at kartnaller.at
Fri Sep 14 04:42:15 EDT 2007


Log message for revision 79621:
  minimize the requirements when instantiating DataRelationship

Changed:
  U   lovely.relation/trunk/src/lovely/relation/app.py
  U   lovely.relation/trunk/src/lovely/relation/configure.zcml
  U   lovely.relation/trunk/src/lovely/relation/dataproperty.py
  U   lovely.relation/trunk/src/lovely/relation/dataproperty.txt

-=-
Modified: lovely.relation/trunk/src/lovely/relation/app.py
===================================================================
--- lovely.relation/trunk/src/lovely/relation/app.py	2007-09-13 20:07:12 UTC (rev 79620)
+++ lovely.relation/trunk/src/lovely/relation/app.py	2007-09-14 08:42:14 UTC (rev 79621)
@@ -73,7 +73,7 @@
         rels = PersistentList()
         for relation in relations:
             rels.append(self._lookup(relation))
-        self._relations = removeSecurityProxy(rels)
+        self._relations = rels
         super(Relationship, self).__init__()
 
     @apply

Modified: lovely.relation/trunk/src/lovely/relation/configure.zcml
===================================================================
--- lovely.relation/trunk/src/lovely/relation/configure.zcml	2007-09-13 20:07:12 UTC (rev 79620)
+++ lovely.relation/trunk/src/lovely/relation/configure.zcml	2007-09-14 08:42:14 UTC (rev 79621)
@@ -11,6 +11,17 @@
         />
   </class>
 
+  <class class=".dataproperty.DataRelationship">
+    <require
+        permission="zope.View"
+        interface=".interfaces.IDataRelationship"
+        />
+    <require
+        permission="zope.ManageContent"
+        set_schema=".interfaces.IDataRelationship"
+        />
+  </class>
+
   <class class=".app.Relations">
     <require
         permission="zope.View"

Modified: lovely.relation/trunk/src/lovely/relation/dataproperty.py
===================================================================
--- lovely.relation/trunk/src/lovely/relation/dataproperty.py	2007-09-13 20:07:12 UTC (rev 79620)
+++ lovely.relation/trunk/src/lovely/relation/dataproperty.py	2007-09-14 08:42:14 UTC (rev 79621)
@@ -30,41 +30,29 @@
 class DataRelationship(O2OStringTypeRelationship):
     interface.implements(IDataRelationship, IAttributeAnnotatable)
 
-    def __init__(self, field, target):
-        super(DataRelationship, self).__init__(None, [field._relType], target)
+    def __init__(self, target=None, field=None):
+        if field is None:
+            rels = []
+        else:
+            rels = [field._relType]
+        super(DataRelationship, self).__init__(None, rels, target)
 
-    @apply
-    def source():
-        def get(self):
-            if isinstance(self._sources, (ListType, TupleType)):
-                return self._sources[0]
-            return self._sources
-        def set(self, value):
-            self._sources = value
-        return property(get, set)
+    source = O2OStringTypeRelationship.sources
+    target = O2OStringTypeRelationship.targets
 
-    @apply
-    def target():
-        def get(self):
-            if isinstance(self._targets, (ListType, TupleType)):
-                return self._targets[0]
-            return self._targets
-        def set(self, value):
-            self._targets = value
-        return property(get, set)
+    def __repr__(self):
+        return '<%s %r, %r, %r>'% (
+                    self.__class__.__name__,
+                    self.source,
+                    self.target,
+                    self.relations)
 
 
 class DataRelationPropertyOut(RelationPropertyOut):
     interface.implements(IDataRelationPropertyOut)
 
-    def __init__(self, manager, name=None, uids=False, relType=None):
-        super(DataRelationPropertyOut, self).__init__(manager,
-                                                      name,
-                                                      uids,
-                                                      relType)
-
     def new(self, target):
-        return DataRelationship(self, target)
+        return DataRelationship(target, self)
 
     def __set__(self, inst, value):
         if self._field.readonly:
@@ -74,15 +62,23 @@
         elif not self._manager.seqOut:
             if not IDataRelationship.providedBy(value):
                 raise TypeError
+            if value.target is None:
+                raise ValueError('target for data relation must not be None')
             v = value
             v.source = inst
+            if v.relations == []:
+                v.relations = [self._relType]
         else:
             v = value
             for val in v:
                 if not IDataRelationship.providedBy(val):
                     raise TypeError
+                if val.target is None:
+                    raise ValueError('target for data relation must not be None')
             for val in v:
                 val.source = inst
+                if val.relations == []:
+                    val.relations = [self._relType]
         self._manager.setTargetRelations(inst, v, self._relType)
         if self._ordered:
             if value is not None:

Modified: lovely.relation/trunk/src/lovely/relation/dataproperty.txt
===================================================================
--- lovely.relation/trunk/src/lovely/relation/dataproperty.txt	2007-09-13 20:07:12 UTC (rev 79620)
+++ lovely.relation/trunk/src/lovely/relation/dataproperty.txt	2007-09-14 08:42:14 UTC (rev 79621)
@@ -82,15 +82,27 @@
 
   >>> rel1 = Document.teaser.new(img1)
   >>> rel1
-  <DataRelationship None>
+  <DataRelationship None, <image u'Image One'>, ['zope.schema._field.Object.teaser:None']>
 
-At this time the relation is not bound to the source.
+We can also get a data relation with no target.
 
-  >>> rel1.source is None
+  >>> rel1 = Document.teaser.new(None)
+  >>> rel1
+  <DataRelationship None, None, ['zope.schema._field.Object.teaser:None']>
+  >>> rel1.target is None
   True
+
+And assign the target.
+
+  >>> rel1.target = img1
   >>> rel1.target
   <image u'Image One'>
 
+At this time the relation is not bound to the source.
+
+  >>> rel1.source is None
+  True
+
 We can bind it :
 
   >>> doc1.teaser is None
@@ -100,6 +112,8 @@
   True
   >>> rel1.source
   <document u'Doc One'>
+  >>> rel1
+  <DataRelationship <document u'Doc One'>, <image u'Image One'>, ['zope.schema._field.Object.teaser:None']>
 
 It is now possible to annotate the teaser reference.
 
@@ -125,18 +139,47 @@
   >>> params
   <ImagePositionParameters ...>
 
+It is also possible to instantiate a data relation manually.
+
+  >>> from lovely.relation.dataproperty import DataRelationship
+  >>> manualRel = DataRelationship()
+
+The new relation has no source, target and relation type.
+
+  >>> manualRel
+  <DataRelationship None, None, []>
+
+If we try to set a relation with no target we get a ValueError.
+
+  >>> doc1.teaser = manualRel
+  Traceback (most recent call last):
+  ...
+  ValueError: target for data relation must not be None
+
+  >>> manualRel.target = img1
+  >>> manualRel
+  <DataRelationship None, <image u'Image One'>, []>
+  >>> doc1.teaser = manualRel
+  >>> manualRel
+  <DataRelationship <document u'Doc One'>, <image u'Image One'>, ['zope.schema._field.Object.teaser:None']>
+
 Let's use a second property from the same relation manager but with another
 relation type.
 
   >>> img2 = Image(u'Image Two')
-  >>> smallImgRel = Document.smallImg.new(img2)
+
+This time we do not use new but instantiate the data relation manually.
+
+  >>> smallImgRel = DataRelationship(img2)
   >>> smallImgRel
-  <DataRelationship None>
+  <DataRelationship None, <image u'Image Two'>, []>
   >>> doc1.smallImgRel = smallImgRel
   >>> doc1.smallImgRel is smallImgRel
   True
   >>> doc1.teaser is rel1
   True
+  >>> smallImgRel
+  <DataRelationship None, <image u'Image Two'>, []>
 
   >>> doc1.teaser = None
   >>> doc1.teaser is None
@@ -151,7 +194,7 @@
   >>> chapter1 = Chapter(u'Chapter One')
   >>> chapterRel1 = Document.chapters.new(chapter1)
   >>> chapterRel1
-  <DataRelationship None>
+  <DataRelationship None, <chapter u'Chapter One'>, ['zope.schema._field.List.chapters:zope.schema._field.Object.document']>
   >>> doc1.chapters = (chapterRel1, )
   >>> [c.target for c in doc1.chapters]
   [<chapter u'Chapter One'>]
@@ -181,7 +224,7 @@
   ...      info = schema.Text(title=u'Info')
   >>> class MyImageRelationship(DataRelationship):
   ...     interface.implements(IMyImageRelationship)
-  >>> myRel = MyImageRelationship(Document.teaser, img1)
+  >>> myRel = MyImageRelationship(img1, Document.teaser)
   >>> myRel.info = u'my info field'
   >>> doc1.teaser = myRel
   >>> doc1.teaser is myRel



More information about the Checkins mailing list