From edf3e0ecf6e72d01bd08fbfb281f80b6a6d07d68 Mon Sep 17 00:00:00 2001 From: helmutm Date: Wed, 7 Dec 2005 08:46:36 +0000 Subject: [PATCH] Work in progress: reorganized relation classes; some preliminary work on views git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@849 fd906abe-77d9-0310-91a1-e0d9ade77398 --- README.txt | 8 ++++++-- concept.py | 27 +++++++++++++++++++++++---- interfaces.py | 8 +++----- relations.py | 49 ------------------------------------------------- view.py | 30 ++++++++++++++++++++++++++++-- 5 files changed, 60 insertions(+), 62 deletions(-) delete mode 100644 relations.py diff --git a/README.txt b/README.txt index a22f019..edae630 100755 --- a/README.txt +++ b/README.txt @@ -89,7 +89,7 @@ We can associate a resource with a concept by assigning it to the concept: [] The resource also provides access to the associated concepts (or views, see -below): +below) via the getClients() method: >>> conc = zope_info.getClients() >>> len(conc) @@ -122,5 +122,9 @@ menu that may contain other nodes as menu items: >>> m112.description u'' -We can associate a node with a concept or directly with a resource: +We can associate a node with a concept or directly with a resource via the +view class's target attribute: + + >>> m111.target = zope_info + >>> m112.target = zope3 diff --git a/concept.py b/concept.py index ee06327..c7f65f0 100644 --- a/concept.py +++ b/concept.py @@ -17,7 +17,7 @@ # """ -Definition of the Concept class. +Definition of the Concept and related classes. $Id$ """ @@ -34,9 +34,22 @@ from cybertools.relation.registry import IRelationsRegistry, getRelations from interfaces import IConcept from interfaces import IConceptManager, IConceptManagerContained from interfaces import ILoopsContained -from relations import ConceptRelation, ConceptResourceRelation +# relation classes + +class ConceptRelation(DyadicRelation): + """ A relation between concept objects. + """ + + +class ResourceRelation(DyadicRelation): + """ A relation between a concept and a resource object. + """ + + +# concept + class Concept(Contained, Persistent): implements(IConcept, IConceptManagerContained) @@ -71,6 +84,8 @@ class Concept(Contained, Persistent): # TODO (?): avoid duplicates def deassignConcept(self, concept, relationships=None): + if relationships is None: + relationships = [ConceptRelation] registry = zapi.getUtility(IRelationsRegistry) relations = registry.query(first=self, second=concept, relationships=relationships) @@ -81,17 +96,19 @@ class Concept(Contained, Persistent): def getResources(self, relationships=None): if relationships is None: - relationships = [ConceptResourceRelation] + relationships = [ResourceRelation] rels = getRelations(first=self, relationships=relationships) return [r.second for r in rels] # TODO: sort... - def assignResource(self, resource, relationship=ConceptResourceRelation): + def assignResource(self, resource, relationship=ResourceRelation): registry = zapi.getUtility(IRelationsRegistry) registry.register(relationship(self, resource)) # TODO (?): avoid duplicates def deassignResource(self, resource, relationships=None): + if relationships is None: + relationships = [ResourceRelation] registry = zapi.getUtility(IRelationsRegistry) relations = registry.query(first=self, second=resource, relationships=relationships) @@ -99,6 +116,8 @@ class Concept(Contained, Persistent): registry.unregister(relation) +# concept manager + class ConceptManager(BTreeContainer): implements(IConceptManager, ILoopsContained) diff --git a/interfaces.py b/interfaces.py index 16b762c..848c576 100644 --- a/interfaces.py +++ b/interfaces.py @@ -22,7 +22,7 @@ loops interface definitions. $Id$ """ -from zope.interface import Interface +from zope.interface import Interface, Attribute from zope.i18nmessageid import MessageFactory from zope import schema from zope.app.container.constraints import contains, containers @@ -168,10 +168,8 @@ class IView(Interface): default=u'', required=False) - def getConcepts(): - """ Return an ordered sequence of concepts referenced by this view. - """ - + target = Attribute('Target object that is referenced by this view') + class INode(IView): """ A node is a view that may contain other views, thus building a diff --git a/relations.py b/relations.py deleted file mode 100644 index d6c72ac..0000000 --- a/relations.py +++ /dev/null @@ -1,49 +0,0 @@ -# -# Copyright (c) 2005 Helmut Merz helmutm@cy55.de -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -""" -Definition of relation classes. - -$Id$ -""" - -from zope.app import zapi -from zope.interface import implements - -from cybertools.relation import DyadicRelation - - -class ConceptRelation(DyadicRelation): - """ A relation between concept objects. - """ - - -class ConceptResourceRelation(DyadicRelation): - """ A relation between a concept and a resource object. - """ - - -class ViewConceptRelation(DyadicRelation): - """ A relation between a view and a concept object. - """ - - -class ViewResourceRelation(DyadicRelation): - """ A relation between a view and a resource object. - """ - diff --git a/view.py b/view.py index 54a0c0e..8e4ca54 100644 --- a/view.py +++ b/view.py @@ -28,6 +28,8 @@ from zope.app.container.contained import Contained from zope.app.container.ordered import OrderedContainer from zope.interface import implements from persistent import Persistent +from cybertools.relation import DyadicRelation +from cybertools.relation.registry import IRelationsRegistry, getRelations from interfaces import IView, INode from interfaces import IViewManager, INodeContained @@ -48,13 +50,32 @@ class View(object): def setDescription(self, description): self._description = description description = property(getDescription, setDescription) + def getTarget(self): + rels = getRelations(first=self) + if len(rels) == 0: + return None + if len(rels) > 1: + raise ValueError, 'There may be only one target for a View object.' + return rels.next().second + + def setTarget(self, target): + return + registry = zapi.getUtility(IRelationsRegistry) + rels = registry.query(first=self) + if len(rels) > 0: + if rels[0].second != target: + registry.unregister(target) + else: + rel = relationship(self, concept) + registry.register(rel) + + target = property(getTarget, setTarget) + def __init__(self, title=u'', description=u''): self.title = title self.description = description super(View, self).__init__() - def getConcepts(self): - return [] class Node(View, OrderedContainer): @@ -67,3 +88,8 @@ class ViewManager(BTreeContainer): implements(IViewManager, ILoopsContained) +class TargetRelation(DyadicRelation): + """ A relation between a view and a concept or resource object. + """ + + \ No newline at end of file