[Checkins] SVN: z3c.relationfieldui/trunk/ Properly handle broken relations.
Martijn Faassen
faassen at infrae.com
Tue Jan 20 16:17:16 EST 2009
Log message for revision 94889:
Properly handle broken relations.
Changed:
U z3c.relationfieldui/trunk/CHANGES.txt
U z3c.relationfieldui/trunk/buildout.cfg
U z3c.relationfieldui/trunk/setup.py
U z3c.relationfieldui/trunk/src/z3c/relationfieldui/README.txt
U z3c.relationfieldui/trunk/src/z3c/relationfieldui/widget.py
-=-
Modified: z3c.relationfieldui/trunk/CHANGES.txt
===================================================================
--- z3c.relationfieldui/trunk/CHANGES.txt 2009-01-20 21:14:12 UTC (rev 94888)
+++ z3c.relationfieldui/trunk/CHANGES.txt 2009-01-20 21:17:15 UTC (rev 94889)
@@ -4,7 +4,9 @@
0.4 (unreleased)
================
-* ...
+* Use improved ``z3c.relationfield`` to better handle broken
+ relations. Broken relations are now accepted by the UI but will be
+ stored as broken.
0.3 (2009-01-16)
================
Modified: z3c.relationfieldui/trunk/buildout.cfg
===================================================================
--- z3c.relationfieldui/trunk/buildout.cfg 2009-01-20 21:14:12 UTC (rev 94888)
+++ z3c.relationfieldui/trunk/buildout.cfg 2009-01-20 21:17:15 UTC (rev 94889)
@@ -1,7 +1,8 @@
[buildout]
-develop = .
+develop = .
+
parts = test
-extends = http://grok.zope.org/releaseinfo/grok-0.14.cfg
+extends = http://grok.zope.org/releaseinfo/grok-0.14.1.cfg
versions = versions
[versions]
Modified: z3c.relationfieldui/trunk/setup.py
===================================================================
--- z3c.relationfieldui/trunk/setup.py 2009-01-20 21:14:12 UTC (rev 94888)
+++ z3c.relationfieldui/trunk/setup.py 2009-01-20 21:17:15 UTC (rev 94889)
@@ -30,7 +30,7 @@
zip_safe=False,
install_requires=[
'setuptools',
- 'z3c.relationfield',
+ 'z3c.relationfield >= 0.3.1',
'z3c.schema2xml >= 1.0',
'grokcore.component',
'grokcore.view',
Modified: z3c.relationfieldui/trunk/src/z3c/relationfieldui/README.txt
===================================================================
--- z3c.relationfieldui/trunk/src/z3c/relationfieldui/README.txt 2009-01-20 21:14:12 UTC (rev 94888)
+++ z3c.relationfieldui/trunk/src/z3c/relationfieldui/README.txt 2009-01-20 21:17:15 UTC (rev 94889)
@@ -7,8 +7,9 @@
.. _`z3c.relationfield`: http://pypi.python.org/pypi/z3c.relationfield
-This package does not provide a ``z3c.form`` widget for ``z3c.relationfield``, but it is hoped
-that will eventually be developed as well (in another package).
+This package does not provide a ``z3c.form`` widget for
+``z3c.relationfield``, but it is hoped that will eventually be
+developed as well (in another package).
Setup
=====
@@ -72,10 +73,26 @@
... def path(self, obj):
... return obj.__name__
... def resolve(self, path):
- ... return root[path]
+ ... try:
+ ... return root[path]
+ ... except KeyError:
+ ... raise ValueError("Cannot resolve: %s" % path)
>>> grok.testing.grok_component('ObjectPath', ObjectPath)
True
+Let's also set up a broken relation::
+
+ >>> d = root['d'] = Item()
+ >>> d_id = intids.getId(root['d'])
+ >>> c = Item()
+ >>> c.rel = RelationValue(d_id)
+ >>> root['c'] = c
+ >>> del root['d']
+ >>> root['c'].rel.to_object is None
+ True
+ >>> root['c'].rel.isBroken()
+ True
+
The relation widget
===================
@@ -104,15 +121,24 @@
>>> from z3c.relationfieldui import RelationWidget
>>> request = TestRequest()
>>> field = IItem['rel']
- >>> class Context(object):
- ... pass
- >>> obj = Context()
- >>> obj.__name__ = 'something'
- >>> field.context = obj
- >>> widget = RelationWidget(field, request)
+ >>> bound = field.bind(root['b'])
+ >>> widget = RelationWidget(bound, request)
+ >>> widget.setRenderedValue(bound.get(root['b']))
>>> print widget()
- <input class="textType" id="field.rel" name="field.rel" size="20" type="text" value="" /><input class="buttonType" onclick="Z3C.relation.popup(this.previousSibling, 'http://grok.zope.org?from_attribute=rel&from_path=something')" type="button" value="get relation" />
+ <input class="textType" id="field.rel" name="field.rel" size="20" type="text" value="a" /><input class="buttonType" onclick="Z3C.relation.popup(this.previousSibling, 'http://grok.zope.org?from_attribute=rel&from_path=b')" type="button" value="get relation" />
+Let's also try it with the broken relation::
+
+ >>> bound = field.bind(root['c'])
+ >>> widget = RelationWidget(bound, request)
+ >>> widget.setRenderedValue(bound.get(root['c']))
+
+When we render the widget, the value is still correct (even though
+it's broken)::
+
+ >>> print widget()
+ <input class="textType" id="field.rel" name="field.rel" size="20" type="text" value="d" /><input class="buttonType" onclick="Z3C.relation.popup(this.previousSibling, 'http://grok.zope.org?from_attribute=rel&from_path=c')" type="button" value="get relation" />
+
Relation display widget
=======================
@@ -122,12 +148,10 @@
widget will link directly to the object::
>>> from z3c.relationfieldui import RelationDisplayWidget
- >>> widget = RelationDisplayWidget(IItem['rel'], request)
+ >>> bound = field.bind(root['b'])
+ >>> widget = RelationDisplayWidget(bound, request)
+ >>> widget.setRenderedValue(bound.get(root['b']))
-We have to set the widget up with some data::
-
- >>> widget._data = b.rel
-
The widget will point to the plain URL of ``rel``'s ``to_object``::
>>> print widget()
@@ -146,3 +170,11 @@
>>> print widget()
<a href="http://127.0.0.1/root/a/edit">a</a>
+
+When the relation is broken, it will still display, but as broken::
+
+ >>> bound = field.bind(root['c'])
+ >>> widget = RelationDisplayWidget(bound, request)
+ >>> widget.setRenderedValue(bound.get(root['c']))
+ >>> print widget()
+ Broken relation to: d
Modified: z3c.relationfieldui/trunk/src/z3c/relationfieldui/widget.py
===================================================================
--- z3c.relationfieldui/trunk/src/z3c/relationfieldui/widget.py 2009-01-20 21:14:12 UTC (rev 94888)
+++ z3c.relationfieldui/trunk/src/z3c/relationfieldui/widget.py 2009-01-20 21:17:15 UTC (rev 94889)
@@ -60,6 +60,8 @@
value = self.context.default
if value == self.context.missing_value:
return ""
+ if value.isBroken():
+ return u"Broken relation to: %s" % value.to_path
to_object = value.to_object
try:
to_url = component.getMultiAdapter((to_object, self.request),
More information about the Checkins
mailing list