some minor code clean-ups
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@2203 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
8fcb2712bb
commit
3a3755f466
5 changed files with 38 additions and 30 deletions
10
README.txt
10
README.txt
|
@ -19,9 +19,9 @@ with lower-level aspects like type or state management.
|
||||||
>>> site = placefulSetUp(True)
|
>>> site = placefulSetUp(True)
|
||||||
|
|
||||||
>>> from zope import component
|
>>> from zope import component
|
||||||
>>> from zope.app import zapi
|
|
||||||
>>> from zope.interface import Interface
|
>>> from zope.interface import Interface
|
||||||
>>> from zope.publisher.browser import TestRequest
|
>>> from zope.publisher.browser import TestRequest
|
||||||
|
>>> from zope.traversing.api import getName
|
||||||
|
|
||||||
|
|
||||||
Concepts and Relations
|
Concepts and Relations
|
||||||
|
@ -70,11 +70,11 @@ ConceptRelation):
|
||||||
|
|
||||||
We can now ask our concepts for their related child and parent concepts:
|
We can now ask our concepts for their related child and parent concepts:
|
||||||
|
|
||||||
>>> [zapi.getName(c) for c in cc1.getChildren()]
|
>>> [getName(c) for c in cc1.getChildren()]
|
||||||
[u'cc2']
|
[u'cc2']
|
||||||
>>> len(cc1.getParents())
|
>>> len(cc1.getParents())
|
||||||
0
|
0
|
||||||
>>> [zapi.getName(p) for p in cc2.getParents()]
|
>>> [getName(p) for p in cc2.getParents()]
|
||||||
[u'cc1']
|
[u'cc1']
|
||||||
|
|
||||||
>>> len(cc2.getChildren())
|
>>> len(cc2.getChildren())
|
||||||
|
@ -283,7 +283,7 @@ from concepts to resources:
|
||||||
>>> form = dict(action='remove', qualifier='resources',
|
>>> form = dict(action='remove', qualifier='resources',
|
||||||
... tokens=['.loops/resources/doc1:.loops/concepts/standard'])
|
... tokens=['.loops/resources/doc1:.loops/concepts/standard'])
|
||||||
>>> view = ConceptConfigureView(cc1, TestRequest(form=form))
|
>>> view = ConceptConfigureView(cc1, TestRequest(form=form))
|
||||||
>>> [zapi.getName(r.context) for r in view.resources()]
|
>>> [getName(r.context) for r in view.resources()]
|
||||||
[u'doc1']
|
[u'doc1']
|
||||||
>>> view.update()
|
>>> view.update()
|
||||||
True
|
True
|
||||||
|
@ -358,7 +358,7 @@ There are a few convienence methods for accessing parent and child nodes:
|
||||||
True
|
True
|
||||||
>>> m11.getParentNode() is m1
|
>>> m11.getParentNode() is m1
|
||||||
True
|
True
|
||||||
>>> [zapi.getName(child) for child in m11.getChildNodes()]
|
>>> [getName(child) for child in m11.getChildNodes()]
|
||||||
[u'm111', u'm112']
|
[u'm111', u'm112']
|
||||||
|
|
||||||
What is returned by these may be controlled by the nodeType attribute:
|
What is returned by these may be controlled by the nodeType attribute:
|
||||||
|
|
|
@ -112,8 +112,9 @@ class ConceptView(BaseView):
|
||||||
cm = self.loopsRoot.getConceptManager()
|
cm = self.loopsRoot.getConceptManager()
|
||||||
hasType = cm.getTypePredicate()
|
hasType = cm.getTypePredicate()
|
||||||
standard = cm.getDefaultPredicate()
|
standard = cm.getDefaultPredicate()
|
||||||
rels = sorted(self.context.getChildRelations(),
|
#rels = sorted(self.context.getChildRelations(),
|
||||||
key=(lambda x: x.second.title.lower()))
|
# key=(lambda x: x.second.title.lower()))
|
||||||
|
rels = self.context.getChildRelations()
|
||||||
for r in rels:
|
for r in rels:
|
||||||
if r.predicate == hasType:
|
if r.predicate == hasType:
|
||||||
# only show top-level entries for type instances:
|
# only show top-level entries for type instances:
|
||||||
|
|
|
@ -333,7 +333,11 @@ class NodeView(BaseView):
|
||||||
def getUrlForTarget(self, target):
|
def getUrlForTarget(self, target):
|
||||||
""" Return URL of given target view given as .targetXXX URL.
|
""" Return URL of given target view given as .targetXXX URL.
|
||||||
"""
|
"""
|
||||||
|
if isinstance(target, BaseView):
|
||||||
return '%s/.target%s' % (self.url, target.uniqueId)
|
return '%s/.target%s' % (self.url, target.uniqueId)
|
||||||
|
else:
|
||||||
|
return ('%s/.target%s' %
|
||||||
|
(self.url, util.getUidForObject(target)))
|
||||||
|
|
||||||
def getActions(self, category='object'):
|
def getActions(self, category='object'):
|
||||||
actions = []
|
actions = []
|
||||||
|
|
39
concept.py
39
concept.py
|
@ -1,5 +1,5 @@
|
||||||
#
|
#
|
||||||
# Copyright (c) 2005 Helmut Merz helmutm@cy55.de
|
# Copyright (c) 2007 Helmut Merz helmutm@cy55.de
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# 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
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
@ -23,7 +23,7 @@ $Id$
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from zope import component, schema
|
from zope import component, schema
|
||||||
from zope.app import zapi
|
#from zope.app import zapi
|
||||||
from zope.app.container.btree import BTreeContainer
|
from zope.app.container.btree import BTreeContainer
|
||||||
from zope.app.container.contained import Contained
|
from zope.app.container.contained import Contained
|
||||||
from zope.cachedescriptors.property import Lazy
|
from zope.cachedescriptors.property import Lazy
|
||||||
|
@ -31,7 +31,7 @@ from zope.component import adapts
|
||||||
from zope.interface import implements
|
from zope.interface import implements
|
||||||
from zope.interface import alsoProvides, directlyProvides, directlyProvidedBy
|
from zope.interface import alsoProvides, directlyProvides, directlyProvidedBy
|
||||||
from zope.security.proxy import removeSecurityProxy
|
from zope.security.proxy import removeSecurityProxy
|
||||||
from zope.traversing.api import getName
|
from zope.traversing.api import getName, getParent
|
||||||
from persistent import Persistent
|
from persistent import Persistent
|
||||||
|
|
||||||
from cybertools.relation import DyadicRelation
|
from cybertools.relation import DyadicRelation
|
||||||
|
@ -64,8 +64,8 @@ class BaseRelation(DyadicRelation):
|
||||||
|
|
||||||
def getPredicateName(self):
|
def getPredicateName(self):
|
||||||
baseName = super(BaseRelation, self).getPredicateName()
|
baseName = super(BaseRelation, self).getPredicateName()
|
||||||
id = zapi.getUtility(IRelationRegistry).getUniqueIdForObject(self.predicate)
|
id = util.getUidForObject(self.predicate)
|
||||||
return '.'.join((baseName, str(id)))
|
return '.'.join((baseName, id))
|
||||||
|
|
||||||
# Problem with reindex catalog, needs __parent__ - but this does not help:
|
# Problem with reindex catalog, needs __parent__ - but this does not help:
|
||||||
#__parent__ = None
|
#__parent__ = None
|
||||||
|
@ -121,7 +121,7 @@ class Concept(Contained, Persistent):
|
||||||
typePred = self.getConceptManager().getTypePredicate()
|
typePred = self.getConceptManager().getTypePredicate()
|
||||||
if typePred is None:
|
if typePred is None:
|
||||||
raise ValueError('No type predicate found for '
|
raise ValueError('No type predicate found for '
|
||||||
+ zapi.getName(self))
|
+ getName(self))
|
||||||
if current is not None:
|
if current is not None:
|
||||||
self.deassignParent(current, [typePred])
|
self.deassignParent(current, [typePred])
|
||||||
self.assignParent(concept, typePred)
|
self.assignParent(concept, typePred)
|
||||||
|
@ -131,7 +131,7 @@ class Concept(Contained, Persistent):
|
||||||
return self.conceptType
|
return self.conceptType
|
||||||
|
|
||||||
def getLoopsRoot(self):
|
def getLoopsRoot(self):
|
||||||
return zapi.getParent(self).getLoopsRoot()
|
return getParent(self).getLoopsRoot()
|
||||||
|
|
||||||
def getConceptManager(self):
|
def getConceptManager(self):
|
||||||
return self.getLoopsRoot().getConceptManager()
|
return self.getLoopsRoot().getConceptManager()
|
||||||
|
@ -158,14 +158,16 @@ class Concept(Contained, Persistent):
|
||||||
rels = getRelations(second=self, relationships=relationships)
|
rels = getRelations(second=self, relationships=relationships)
|
||||||
return [r.first for r in rels]
|
return [r.first for r in rels]
|
||||||
|
|
||||||
def getChildRelations(self, predicates=None, child=None):
|
def getChildRelations(self, predicates=None, child=None, sort='default'):
|
||||||
predicates = predicates is None and ['*'] or predicates
|
predicates = predicates is None and ['*'] or predicates
|
||||||
relationships = [ConceptRelation(self, None, p) for p in predicates]
|
relationships = [ConceptRelation(self, None, p) for p in predicates]
|
||||||
# TODO: sort...
|
if sort == 'default':
|
||||||
return getRelations(first=self, second=child, relationships=relationships)
|
sort = lambda x: (x.order, x.second.title.lower())
|
||||||
|
return sorted(getRelations(first=self, second=child, relationships=relationships),
|
||||||
|
key=sort)
|
||||||
|
|
||||||
def getChildren(self, predicates=None):
|
def getChildren(self, predicates=None, sort='default'):
|
||||||
return [r.second for r in self.getChildRelations(predicates)]
|
return [r.second for r in self.getChildRelations(predicates, sort=sort)]
|
||||||
|
|
||||||
def getParentRelations (self, predicates=None, parent=None):
|
def getParentRelations (self, predicates=None, parent=None):
|
||||||
predicates = predicates is None and ['*'] or predicates
|
predicates = predicates is None and ['*'] or predicates
|
||||||
|
@ -179,7 +181,7 @@ class Concept(Contained, Persistent):
|
||||||
def assignChild(self, concept, predicate=None, order=0, relevance=1.0):
|
def assignChild(self, concept, predicate=None, order=0, relevance=1.0):
|
||||||
if predicate is None:
|
if predicate is None:
|
||||||
predicate = self.getConceptManager().getDefaultPredicate()
|
predicate = self.getConceptManager().getDefaultPredicate()
|
||||||
registry = zapi.getUtility(IRelationRegistry)
|
registry = component.getUtility(IRelationRegistry)
|
||||||
rel = ConceptRelation(self, concept, predicate)
|
rel = ConceptRelation(self, concept, predicate)
|
||||||
if order != 0:
|
if order != 0:
|
||||||
rel.order = order
|
rel.order = order
|
||||||
|
@ -192,7 +194,7 @@ class Concept(Contained, Persistent):
|
||||||
concept.assignChild(self, predicate, order, relevance)
|
concept.assignChild(self, predicate, order, relevance)
|
||||||
|
|
||||||
def deassignChild(self, child, predicates=None):
|
def deassignChild(self, child, predicates=None):
|
||||||
registry = zapi.getUtility(IRelationRegistry)
|
registry = component.getUtility(IRelationRegistry)
|
||||||
for rel in self.getChildRelations(predicates, child):
|
for rel in self.getChildRelations(predicates, child):
|
||||||
registry.unregister(rel)
|
registry.unregister(rel)
|
||||||
|
|
||||||
|
@ -213,7 +215,7 @@ class Concept(Contained, Persistent):
|
||||||
def assignResource(self, resource, predicate=None, order=0, relevance=1.0):
|
def assignResource(self, resource, predicate=None, order=0, relevance=1.0):
|
||||||
if predicate is None:
|
if predicate is None:
|
||||||
predicate = self.getConceptManager().getDefaultPredicate()
|
predicate = self.getConceptManager().getDefaultPredicate()
|
||||||
registry = zapi.getUtility(IRelationRegistry)
|
registry = component.getUtility(IRelationRegistry)
|
||||||
rel = ResourceRelation(self, resource, predicate)
|
rel = ResourceRelation(self, resource, predicate)
|
||||||
if order != 0:
|
if order != 0:
|
||||||
rel.order = order
|
rel.order = order
|
||||||
|
@ -223,7 +225,7 @@ class Concept(Contained, Persistent):
|
||||||
registry.register(rel)
|
registry.register(rel)
|
||||||
|
|
||||||
def deassignResource(self, resource, predicates=None):
|
def deassignResource(self, resource, predicates=None):
|
||||||
registry = zapi.getUtility(IRelationRegistry)
|
registry = component.getUtility(IRelationRegistry)
|
||||||
for rel in self.getResourceRelations(predicates, resource):
|
for rel in self.getResourceRelations(predicates, resource):
|
||||||
registry.unregister(rel)
|
registry.unregister(rel)
|
||||||
|
|
||||||
|
@ -240,7 +242,7 @@ class ConceptManager(BTreeContainer):
|
||||||
predicateType = None
|
predicateType = None
|
||||||
|
|
||||||
def getLoopsRoot(self):
|
def getLoopsRoot(self):
|
||||||
return zapi.getParent(self)
|
return getParent(self)
|
||||||
|
|
||||||
def getAllParents(self, collectGrants=False):
|
def getAllParents(self, collectGrants=False):
|
||||||
return Jeep()
|
return Jeep()
|
||||||
|
@ -250,7 +252,8 @@ class ConceptManager(BTreeContainer):
|
||||||
|
|
||||||
def getTypeConcept(self):
|
def getTypeConcept(self):
|
||||||
if self.typeConcept is None:
|
if self.typeConcept is None:
|
||||||
self.typeConcept = self['type']
|
#self.typeConcept = self['type']
|
||||||
|
self.typeConcept = self.get('type')
|
||||||
return self.typeConcept
|
return self.typeConcept
|
||||||
|
|
||||||
def getDefaultPredicate(self):
|
def getDefaultPredicate(self):
|
||||||
|
|
|
@ -14,13 +14,13 @@ Let's first do some basic imports
|
||||||
>>> site = placefulSetUp(True)
|
>>> site = placefulSetUp(True)
|
||||||
|
|
||||||
>>> from zope import interface, component
|
>>> from zope import interface, component
|
||||||
>>> from zope.app import zapi
|
|
||||||
>>> from zope.interface import Interface
|
>>> from zope.interface import Interface
|
||||||
>>> from zope.publisher.browser import TestRequest
|
>>> from zope.publisher.browser import TestRequest
|
||||||
|
|
||||||
and provide a relation registry:
|
and provide a relation registry:
|
||||||
|
|
||||||
>>> from cybertools.relation.interfaces import IRelationRegistry
|
>>> from cybertools.relation.tests import IntIdsStub
|
||||||
|
>>> component.provideUtility(IntIdsStub())
|
||||||
>>> from cybertools.relation.registry import DummyRelationRegistry
|
>>> from cybertools.relation.registry import DummyRelationRegistry
|
||||||
>>> component.provideUtility(DummyRelationRegistry())
|
>>> component.provideUtility(DummyRelationRegistry())
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ resources,
|
||||||
>>> file1 = resources['file1'] = Resource(u'A file')
|
>>> file1 = resources['file1'] = Resource(u'A file')
|
||||||
>>> file1.resourceType = concepts['file']
|
>>> file1.resourceType = concepts['file']
|
||||||
|
|
||||||
(the use of Document may get deprecated soon:)
|
(note: the use of Document is deprecated)
|
||||||
|
|
||||||
>>> from loops.resource import Document
|
>>> from loops.resource import Document
|
||||||
>>> doc1 = Document(u'Zope Info')
|
>>> doc1 = Document(u'Zope Info')
|
||||||
|
@ -176,7 +176,7 @@ Now let's have a look at resources.
|
||||||
>>> file1_type.factory
|
>>> file1_type.factory
|
||||||
<class 'loops.resource.Resource'>
|
<class 'loops.resource.Resource'>
|
||||||
|
|
||||||
(The use of Document will be deprecated soon...)
|
(The use of Document is deprecated!)
|
||||||
|
|
||||||
>>> from loops.interfaces import IResource, IDocument
|
>>> from loops.interfaces import IResource, IDocument
|
||||||
>>> from loops.type import ResourceType
|
>>> from loops.type import ResourceType
|
||||||
|
|
Loading…
Add table
Reference in a new issue