Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
29 substanced/catalog/__init__.py
@@ -9,6 +9,7 @@
from repoze.catalog.catalog import Catalog as _Catalog
from pyramid.threadlocal import get_current_registry
+from pyramid.traversal import resource_path
from ..objectmap import find_objectmap
@@ -18,7 +19,6 @@
)
from ..service import find_service
-from ..util import resource_or_none
logger = logging.getLogger(__name__)
@@ -83,15 +83,21 @@ def commit_or_abort():
i = 1
objectmap = find_objectmap(self)
for objectid in self.objectids:
- path = objectmap.path_for(objectid)
- upath = u'/'.join(path)
- if path_re is not None and path_re.match(upath) is None:
- continue
- output and output('reindexing %s' % upath)
- resource = resource_or_none(self, path)
+ resource = objectmap.object_for(objectid)
if resource is None:
- output and output('error: %s not found' % upath)
+ path = objectmap.path_for(objectid)
+ if path is None:
+ output and output(
+ 'error: no path for objectid %s in object map' %
+ objectid)
+ continue
+ upath = u'/'.join(path)
+ output and output('error: object at path %s not found' % upath)
+ continue
+ path = resource_path(resource)
+ if path_re is not None and path_re.match(path) is None:
continue
+ output and output('reindexing %s' % path)
if indexes is None:
self.reindex_doc(objectid, resource)
@@ -136,12 +142,9 @@ def __init__(self, context):
self.objectmap = find_objectmap(self.context)
def resolver(self, objectid):
- path = self.objectmap.path_for(objectid)
- if path is None:
- return None
- resource = resource_or_none(self.context, path)
+ resource = self.objectmap.object_for(objectid)
if resource is None:
- logger.warn('Resource missing: %s' % (path,))
+ logger.warn('Resource for objectid %s missing' % (objectid,))
return resource
def query(self, q, **kw):
View
6 substanced/catalog/subscribers.py
@@ -1,3 +1,4 @@
+import BTrees
from zope.interface import Interface
from pyramid.events import subscriber
@@ -33,9 +34,8 @@ def object_will_be_removed(obj, event):
if objectmap is None or catalog is None:
return
objectids = objectmap.pathlookup(obj)
- for oid in objectids:
- if oid in catalog.objectids:
- catalog.unindex_doc(oid)
+ for oid in BTrees.family32.IF.intersection(objectids, catalog.objectids):
+ catalog.unindex_doc(oid)
@subscriber([Interface, IObjectModifiedEvent])
def object_modified(obj, event):
View
70 substanced/catalog/tests/test_catalog.py
@@ -25,7 +25,7 @@ def setUp(self):
def tearDown(self):
testing.tearDown()
-
+
def _makeOne(self):
from .. import Catalog
return Catalog()
@@ -65,13 +65,12 @@ def test_reindex_doc_notexists(self):
def test_reindex(self):
a = testing.DummyModel()
- self.config.testing_resources({'/a':a})
L = []
transaction = DummyTransaction()
inst = self._makeOne()
- objectmap = DummyObjectMap()
- objectmap.objectid_to_path = {1:(u'', u'a')}
- _makeSite(catalog=inst, objectmap=objectmap)
+ objectmap = DummyObjectMap({1:[a, (u'', u'a')]})
+ site = _makeSite(catalog=inst, objectmap=objectmap)
+ site['a'] = a
inst.objectids = [1]
inst.reindex_doc = lambda objectid, model: L.append((objectid, model))
out = []
@@ -87,13 +86,14 @@ def test_reindex(self):
def test_reindex_with_missing_resource(self):
a = testing.DummyModel()
- self.config.testing_resources({'/a':a})
L = []
transaction = DummyTransaction()
- objectmap = DummyObjectMap()
- objectmap.objectid_to_path = {1: (u'', u'a'), 2:(u'', u'b')}
+ objectmap = DummyObjectMap(
+ {1: [a, (u'', u'a')], 2:[None, (u'', u'b')]}
+ )
inst = self._makeOne()
- _makeSite(catalog=inst, objectmap=objectmap)
+ site = _makeSite(catalog=inst, objectmap=objectmap)
+ site['a'] = a
inst.objectids = [1, 2]
inst.reindex_doc = lambda objectid, model: L.append((objectid, model))
out = []
@@ -104,21 +104,20 @@ def test_reindex_with_missing_resource(self):
'refreshed',
'*** committing ***',
"reindexing /a",
- "reindexing /b",
- "error: /b not found",
+ "error: object at path /b not found",
'*** committing ***'])
self.assertEqual(transaction.committed, 2)
def test_reindex_pathre(self):
a = testing.DummyModel()
b = testing.DummyModel()
- self.config.testing_resources({'/a':a, '/b':b})
L = []
- objectmap = DummyObjectMap()
- objectmap.objectid_to_path = {1: (u'', u'a'), 2: (u'', u'b')}
+ objectmap = DummyObjectMap({1: [a, (u'', u'a')], 2: [b, (u'', u'b')]})
transaction = DummyTransaction()
inst = self._makeOne()
- _makeSite(catalog=inst, objectmap=objectmap)
+ site = _makeSite(catalog=inst, objectmap=objectmap)
+ site['a'] = a
+ site['b'] = b
inst.objectids = [1, 2]
inst.reindex_doc = lambda objectid, model: L.append((objectid, model))
out = []
@@ -139,13 +138,13 @@ def test_reindex_pathre(self):
def test_reindex_dryrun(self):
a = testing.DummyModel()
b = testing.DummyModel()
- self.config.testing_resources({'/a':a, '/b':b})
L = []
- objectmap = DummyObjectMap()
- objectmap.objectid_to_path = {1: (u'', u'a'), 2: (u'', u'b')}
+ objectmap = DummyObjectMap({1: [a, (u'', u'a')], 2: [b, (u'', u'b')]})
transaction = DummyTransaction()
inst = self._makeOne()
- _makeSite(catalog=inst, objectmap=objectmap)
+ site = _makeSite(catalog=inst, objectmap=objectmap)
+ site['a'] = a
+ site['b'] = b
inst.objectids = [1,2]
inst.reindex_doc = lambda objectid, model: L.append((objectid, model))
out = []
@@ -163,13 +162,12 @@ def test_reindex_dryrun(self):
def test_reindex_with_indexes(self):
a = testing.DummyModel()
- self.config.testing_resources({'/a':a})
L = []
- objectmap = DummyObjectMap()
- objectmap.objectid_to_path = {1: (u'', u'a')}
+ objectmap = DummyObjectMap({1: [a, (u'', u'a')]})
transaction = DummyTransaction()
inst = self._makeOne()
- _makeSite(catalog=inst, objectmap=objectmap)
+ site = _makeSite(catalog=inst, objectmap=objectmap)
+ site['a'] = a
inst.objectids = [1]
index = DummyIndex()
inst['index'] = index
@@ -246,11 +244,10 @@ def test_search(self):
self.assertEqual(list(objectids), [])
def test_query_peachy_keen(self):
- objectmap = DummyObjectMap({1:(u'',)})
+ ob = object()
+ objectmap = DummyObjectMap({1:[ob, (u'',)]})
catalog = DummyCatalog((1, [1]))
site = _makeSite(objectmap=objectmap, catalog=catalog)
- ob = object()
- self.config.testing_resources({'/':ob})
adapter = self._makeOne(site)
q = DummyQuery()
num, objectids, resolver = adapter.query(q)
@@ -260,7 +257,7 @@ def test_query_peachy_keen(self):
def test_query_unfound_model(self):
catalog = DummyCatalog((1, [1]))
- objectmap = DummyObjectMap({1:(u'', u'a')})
+ objectmap = DummyObjectMap({1:[None, (u'', u'a')]})
site = _makeSite(catalog=catalog, objectmap=objectmap)
adapter = self._makeOne(site)
q = DummyQuery()
@@ -322,13 +319,22 @@ class DummyQuery(object):
pass
class DummyObjectMap(object):
- def __init__(self, objectid_to_path=None):
- if objectid_to_path is None:
- objectid_to_path = {}
- self.objectid_to_path = objectid_to_path
+ def __init__(self, objectid_to=None):
+ if objectid_to is None:
+ objectid_to = {}
+ self.objectid_to = objectid_to
def path_for(self, objectid):
- return self.objectid_to_path.get(objectid)
+ data = self.objectid_to.get(objectid)
+ if data is None:
+ return
+ return data[1]
+
+ def object_for(self, objectid):
+ data = self.objectid_to.get(objectid)
+ if data is None:
+ return
+ return data[0]
class DummyCatalog(object):
def __init__(self, result=(0, [])):
View
9 substanced/catalog/tests/test_subscribers.py
@@ -1,11 +1,10 @@
import unittest
+import BTrees
from zope.interface import alsoProvides
from pyramid import testing
-from pyramid.traversal import resource_path_tuple
-
def _makeSite(**kw):
from ...interfaces import IFolder
site = testing.DummyResource(__provides__=kw.pop('__provides__', None))
@@ -75,7 +74,7 @@ def test_no_catalog(self):
def test_with_pathlookup(self):
model = testing.DummyResource()
catalog = DummyCatalog()
- catalog.objectids = [1,2]
+ catalog.objectids = BTrees.family32.IF.Set([1,2])
objectmap = DummyObjectMap()
site = _makeSite(objectmap=objectmap, catalog=catalog)
site['model'] = model
@@ -87,7 +86,7 @@ def test_with_pathlookup(self):
def test_with_pathlookup_limited_by_objectids(self):
model = testing.DummyResource()
catalog = DummyCatalog()
- catalog.objectids = [1]
+ catalog.objectids = BTrees.family32.IF.Set([1])
objectmap = DummyObjectMap()
site = _makeSite(objectmap=objectmap, catalog=catalog)
site['model'] = model
@@ -154,7 +153,7 @@ def reindex_doc(self, objectid, obj):
class DummyObjectMap:
def pathlookup(self, obj):
- return [1,2]
+ return BTrees.family32.IF.Set([1,2])
class DummyEvent(object):
def __init__(self, parent):
View
17 substanced/objectmap/__init__.py
@@ -5,8 +5,11 @@
import BTrees
-from pyramid.traversal import resource_path_tuple
from pyramid.events import subscriber
+from pyramid.traversal import (
+ resource_path_tuple,
+ find_resource,
+ )
from ..service import find_service
from ..util import postorder
@@ -119,6 +122,18 @@ def objectid_for(self, obj_or_path_tuple):
def path_for(self, objectid):
return self.objectid_to_path.get(objectid)
+
+ def object_for(self, objectid_or_path_tuple):
+ if isinstance(objectid_or_path_tuple, int):
+ path_tuple = self.objectid_to_path.get(objectid_or_path_tuple)
+ elif isinstance(objectid_or_path_tuple, tuple):
+ path_tuple = objectid_or_path_tuple
+ else:
+ raise ValueError('Unknown input %s' % (objectid_or_path_tuple,))
+ try:
+ return find_resource(self.__parent__, path_tuple)
+ except KeyError:
+ return None
def add(self, obj, path_tuple):
if not isinstance(path_tuple, tuple):
View
17 substanced/principal/__init__.py
@@ -1,4 +1,3 @@
-from persistent import Persistent
from BTrees.IFBTree import IFTreeSet
from zope.interface import implementer
@@ -30,7 +29,6 @@
from ..content import content
from ..models.folder import Folder
-from ..util import resource_or_none
@implementer(IPrincipals)
class Principals(Folder):
@@ -133,8 +131,7 @@ def get_members(self):
L = []
objectmap = find_service(self, 'objectmap')
for memberid in self.members:
- path = objectmap.path_for(memberid)
- member = resource_or_none(self, path)
+ member = objectmap.object_for(memberid)
if member is not None:
L.append(member)
return L
@@ -254,8 +251,7 @@ def get_groups(self):
L = []
objectmap = find_service(self, 'objectmap')
for groupid in self.groups:
- path = objectmap.path_for(groupid)
- group = resource_or_none(self, path)
+ group = objectmap.object_for(groupid)
if group is not None:
L.append(group)
return L
@@ -273,8 +269,7 @@ def user_added(user, event):
objectmap = find_service(user, 'objectmap')
userid = user.__objectid__
for groupid in user.groups:
- path = objectmap.path_for(groupid)
- group = resource_or_none(user, path)
+ group = objectmap.object_for(groupid)
if group is not None:
group.members.insert(userid)
@@ -291,8 +286,7 @@ def group_added(group, event):
groupid = group.__objectid__
objectmap = find_service(group, 'objectmap')
for userid in group.members:
- path = objectmap.path_for(userid)
- user = resource_or_none(path)
+ user = objectmap.object_for(userid)
if user is not None:
user.groups.insert(groupid)
@@ -347,8 +341,7 @@ def group_modified(group, event):
def groupfinder(userid, request):
context = request.context
objectmap = find_service(context, 'objectmap')
- path = objectmap.path_for(userid)
- user = resource_or_none(context, path)
+ user = objectmap.object_for(userid)
if user is None:
return None
return list(user.groups)
View
6 substanced/sdi/__init__.py
@@ -17,7 +17,6 @@
from . import helpers
from ..service import find_service
-from ..util import resource_or_none
def as_sorted_tuple(val):
if not is_nonstr_iter(val):
@@ -159,10 +158,7 @@ def get_user(request):
if userid is None:
return None
objectmap = find_service(request.context, 'objectmap')
- path = objectmap.path_for(userid)
- if path is None:
- return None
- return resource_or_none(request.context, path)
+ return objectmap.object_for(userid)
def includeme(config): # pragma: no cover
config.add_directive('add_mgmt_view', add_mgmt_view)
View
4 substanced/sdi/acl.py
@@ -17,7 +17,6 @@
from . import mgmt_view
from .helpers import check_csrf_token
-from ..util import resource_or_none
def get_workflow(*arg, **kw):
return # XXX
@@ -164,8 +163,7 @@ def acl_edit_view(context, request):
break
if permissions == ALL_PERMISSIONS:
permissions = ('-- ALL --',)
- path = objectmap.path_for(principal_id)
- principal = resource_or_none(context, path)
+ principal = objectmap.object_for(principal_id)
if principal is None:
pname = '<deleted principal>'
else:
View
8 substanced/sdi/helpers.py
@@ -7,21 +7,19 @@
from pyramid.renderers import get_renderer
from pyramid.request import Request
from pyramid.httpexceptions import HTTPBadRequest
-from pyramid.security import authenticated_userid
-from ..objectmap import find_objectmap
-from ..util import resource_or_none
+from ..service import find_service
MANAGE_ROUTE_NAME = 'substanced_manage'
def get_subnodes(request, context=None):
if context is None:
context = request.context
- objectmap = find_objectmap(context)
+ objectmap = find_service(context, 'objectmap')
nodes = objectmap.navgen(context, depth=1)
L = []
for node in nodes:
- obj = resource_or_none(context, node['path'])
+ obj = objectmap.object_for(node['path'])
if obj is not None:
if get_mgmt_views(request, obj):
node['url'] = request.mgmt_path(obj)
View
8 substanced/util/__init__.py
@@ -1,15 +1,7 @@
import calendar
-from pyramid.traversal import find_resource
-
from ..interfaces import IFolder
-def resource_or_none(resource, path):
- try:
- return find_resource(resource, path)
- except KeyError:
- return None
-
def coarse_datetime_repr(date):
"""Convert a datetime to an integer with 100 second granularity.

No commit comments for this range

Something went wrong with that request. Please try again.