IView.target attribute: implement with 1:n relation

git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@954 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2005-12-21 08:02:39 +00:00
parent edf3e0ecf6
commit 535e2d3f3b
4 changed files with 24 additions and 16 deletions

View file

@ -107,7 +107,7 @@ We first need a view manager:
>>> views = loops['views'] >>> views = loops['views']
The view space is typically built up with nodes; a node may be a top-level The view space is typically built up with nodes; a node may be a top-level
menu that may contain other nodes as menu items: menu that may contain other nodes as menu or content items:
>>> m1 = Node(u'Menu') >>> m1 = Node(u'Menu')
>>> views['m1'] = m1 >>> views['m1'] = m1
@ -126,5 +126,12 @@ We can associate a node with a concept or directly with a resource via the
view class's target attribute: view class's target attribute:
>>> m111.target = zope_info >>> m111.target = zope_info
>>> m112.target = zope3 >>> m111.target is zope_info
True
>>> m111.target = zope_info
>>> m111.target is zope_info
True
>>> m111.target = zope3
>>> m111.target is zope3
True

View file

@ -166,9 +166,9 @@
permission="zope.ManageContent" permission="zope.ManageContent"
set_schema=".interfaces.INode" /> set_schema=".interfaces.INode" />
<require <!--<require
permission="zope.View" permission="zope.View"
interface="zope.app.container.interfaces.IReadContainer" /> interface="zope.app.container.interfaces.IReadContainer" />-->
</content> </content>

View file

@ -26,7 +26,7 @@ from zope.interface import Interface, Attribute
from zope.i18nmessageid import MessageFactory from zope.i18nmessageid import MessageFactory
from zope import schema from zope import schema
from zope.app.container.constraints import contains, containers from zope.app.container.constraints import contains, containers
from zope.app.container.interfaces import IContainer from zope.app.container.interfaces import IContainer, IOrderedContainer
from zope.app.file.interfaces import IFile as IBaseFile from zope.app.file.interfaces import IFile as IBaseFile
from zope.app.folder.interfaces import IFolder from zope.app.folder.interfaces import IFolder
@ -153,7 +153,7 @@ class IResourceManagerContained(Interface):
class IView(Interface): class IView(Interface):
""" A view is a user interface component that provides access to one """ A view is a user interface component that provides access to one
or more concepts. or more concepts, resources, or other views.
""" """
title = schema.TextLine( title = schema.TextLine(
@ -171,7 +171,7 @@ class IView(Interface):
target = Attribute('Target object that is referenced by this view') target = Attribute('Target object that is referenced by this view')
class INode(IView): class INode(IView, IOrderedContainer):
""" A node is a view that may contain other views, thus building a """ A node is a view that may contain other views, thus building a
menu or folder hierarchy. menu or folder hierarchy.
""" """

19
view.py
View file

@ -51,23 +51,24 @@ class View(object):
description = property(getDescription, setDescription) description = property(getDescription, setDescription)
def getTarget(self): def getTarget(self):
rels = getRelations(first=self) rels = getRelations(first=self, relationships=[TargetRelation])
if len(rels) == 0: if len(rels) == 0:
return None return None
if len(rels) > 1: if len(rels) > 1:
raise ValueError, 'There may be only one target for a View object.' raise ValueError, 'There may be only one target for a View object.'
return rels.next().second return list(rels)[0].second
def setTarget(self, target): def setTarget(self, target):
return
registry = zapi.getUtility(IRelationsRegistry) registry = zapi.getUtility(IRelationsRegistry)
rels = registry.query(first=self) rels = list(registry.query(first=self, relationship=TargetRelation))
if len(rels) > 0: if len(rels) > 0:
if rels[0].second != target: oldRel = rels[0]
registry.unregister(target) if oldRel.second is target:
else: return
rel = relationship(self, concept) else:
registry.register(rel) registry.unregister(oldRel)
rel = TargetRelation(self, target)
registry.register(rel)
target = property(getTarget, setTarget) target = property(getTarget, setTarget)