Commit bda79029 authored by Tom Gross's avatar Tom Gross

removed duplicate code in ZopeFind and ZopeFindAndApply

parent 41594f6d
...@@ -78,6 +78,8 @@ Features Added ...@@ -78,6 +78,8 @@ Features Added
Restructuring Restructuring
+++++++++++++ +++++++++++++
- OFS: Removed duplicate code in ZopeFind and ZopeFindAndApply
- Five: Removed obsolete metaclass. - Five: Removed obsolete metaclass.
- Five: Refactored ``browser:view`` and ``browser:page`` directives. - Five: Refactored ``browser:view`` and ``browser:page`` directives.
......
...@@ -68,88 +68,12 @@ class FindSupport(Base): ...@@ -68,88 +68,12 @@ class FindSupport(Base):
search_sub=0, search_sub=0,
REQUEST=None, result=None, pre=''): REQUEST=None, result=None, pre=''):
"""Zope Find interface""" """Zope Find interface"""
return self.ZopeFindAndApply(obj, obj_ids=obj_ids,
if result is None: obj_metatypes=obj_metatypes, obj_searchterm=obj_searchterm,
result=[] obj_expr=obj_expr, obj_mtime=obj_mtime, obj_mspec=obj_mspec,
obj_permission=obj_permission, obj_roles=obj_roles,
if obj_metatypes and 'all' in obj_metatypes: search_sub=search_sub, REQUEST=REQUEST, result=result,
obj_metatypes=None pre=pre, apply_func=None, apply_path='')
if obj_mtime and type(obj_mtime)==type('s'):
obj_mtime=DateTime(obj_mtime).timeTime()
if obj_permission:
obj_permission=p_name(obj_permission)
if obj_roles and type(obj_roles) is type('s'):
obj_roles=[obj_roles]
if obj_expr:
# Setup expr machinations
md=td()
obj_expr=(Eval(obj_expr), md, md._push, md._pop)
base = aq_base(obj)
if hasattr(base, 'objectItems'):
try: items=obj.objectItems()
except: return result
else:
return result
try: add_result=result.append
except:
raise AttributeError, `result`
for id, ob in items:
if pre: p="%s/%s" % (pre, id)
else: p=id
dflag=0
if hasattr(ob, '_p_changed') and (ob._p_changed == None):
dflag=1
bs = aq_base(ob)
if (
(not obj_ids or absattr(bs.getId()) in obj_ids)
and
(not obj_metatypes or (hasattr(bs, 'meta_type') and
bs.meta_type in obj_metatypes))
and
(not obj_searchterm or
(hasattr(ob, 'PrincipiaSearchSource') and
ob.PrincipiaSearchSource().find(str(obj_searchterm)) >= 0
)
or
(hasattr(ob, 'SearchableText') and
ob.SearchableText().find(str(obj_searchterm)) >= 0)
)
and
(not obj_expr or expr_match(ob, obj_expr))
and
(not obj_mtime or mtime_match(ob, obj_mtime, obj_mspec))
and
( (not obj_permission or not obj_roles) or \
role_match(ob, obj_permission, obj_roles)
)
):
add_result((p, ob))
dflag=0
if search_sub and (hasattr(bs, 'objectItems')):
subob = ob
sub_p = p
self.ZopeFind(subob, obj_ids, obj_metatypes,
obj_searchterm, obj_expr,
obj_mtime, obj_mspec,
obj_permission, obj_roles,
search_sub,
REQUEST, result, sub_p)
if dflag: ob._p_deactivate()
return result
security.declareProtected(view_management_screens, 'PrincipiaFind') security.declareProtected(view_management_screens, 'PrincipiaFind')
PrincipiaFind=ZopeFind PrincipiaFind=ZopeFind
...@@ -212,8 +136,11 @@ class FindSupport(Base): ...@@ -212,8 +136,11 @@ class FindSupport(Base):
and and
(not obj_searchterm or (not obj_searchterm or
(hasattr(ob, 'PrincipiaSearchSource') and (hasattr(ob, 'PrincipiaSearchSource') and
ob.PrincipiaSearchSource().find(obj_searchterm) >= 0 obj_searchterm in ob.PrincipiaSearchSource())
)) or
(hasattr(ob, 'SearchableText') and
obj_searchterm in ob.SearchableText())
)
and and
(not obj_expr or expr_match(ob, obj_expr)) (not obj_expr or expr_match(ob, obj_expr))
and and
......
import unittest import unittest
from OFS.FindSupport import FindSupport
class DummyItem(FindSupport):
""" """
def __init__(self, id):
self.id = id
def getId(self):
return self.id
class DummyFolder(DummyItem, dict):
def objectItems(self):
return self.items()
class TestFindSupport(unittest.TestCase): class TestFindSupport(unittest.TestCase):
def setUp(self):
self.base = DummyFolder('base')
self.base['1'] = DummyItem('1')
self.base['2'] = DummyItem('2')
self.base['3'] = DummyItem('3')
def test_interfaces(self): def test_interfaces(self):
from OFS.interfaces import IFindSupport from OFS.interfaces import IFindSupport
from OFS.FindSupport import FindSupport
from zope.interface.verify import verifyClass from zope.interface.verify import verifyClass
verifyClass(IFindSupport, FindSupport) verifyClass(IFindSupport, FindSupport)
def test_find(self):
self.assertEqual(self.base.ZopeFind(
self.base, obj_ids=['1'])[0][0], '1')
def test_find_apply(self):
def func(obj, p):
obj.id = 'foo' + obj.id
# ZopeFindAndApply does not return anything
# but applies a function to the objects found
self.assertFalse(self.base.ZopeFindAndApply(
self.base, obj_ids=['2'], apply_func=func))
self.assertEqual(self.base['1'].id, '1')
self.assertEqual(self.base['2'].id, 'foo2')
self.assertEqual(self.base['3'].id, '3')
def test_suite(): def test_suite():
return unittest.TestSuite(( return unittest.TestSuite((
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment