Skip to content

Commit

Permalink
removed duplicate code in ZopeFind and ZopeFindAndApply
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgross committed Oct 12, 2012
1 parent 41594f6 commit bda7902
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 85 deletions.
2 changes: 2 additions & 0 deletions doc/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Features Added
Restructuring
+++++++++++++

- OFS: Removed duplicate code in ZopeFind and ZopeFindAndApply

- Five: Removed obsolete metaclass.

- Five: Refactored ``browser:view`` and ``browser:page`` directives.
Expand Down
95 changes: 11 additions & 84 deletions src/OFS/FindSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,88 +68,12 @@ def ZopeFind(self, obj, obj_ids=None, obj_metatypes=None,
search_sub=0,
REQUEST=None, result=None, pre=''):
"""Zope Find interface"""

if result is None:
result=[]

if obj_metatypes and 'all' in obj_metatypes:
obj_metatypes=None

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


return self.ZopeFindAndApply(obj, obj_ids=obj_ids,
obj_metatypes=obj_metatypes, obj_searchterm=obj_searchterm,
obj_expr=obj_expr, obj_mtime=obj_mtime, obj_mspec=obj_mspec,
obj_permission=obj_permission, obj_roles=obj_roles,
search_sub=search_sub, REQUEST=REQUEST, result=result,
pre=pre, apply_func=None, apply_path='')

security.declareProtected(view_management_screens, 'PrincipiaFind')
PrincipiaFind=ZopeFind
Expand Down Expand Up @@ -212,8 +136,11 @@ def ZopeFindAndApply(self, obj, obj_ids=None, obj_metatypes=None,
and
(not obj_searchterm or
(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
(not obj_expr or expr_match(ob, obj_expr))
and
Expand Down
39 changes: 38 additions & 1 deletion src/OFS/tests/testFindSupport.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
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):

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):
from OFS.interfaces import IFindSupport
from OFS.FindSupport import FindSupport
from zope.interface.verify import verifyClass

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():
return unittest.TestSuite((
Expand Down

0 comments on commit bda7902

Please sign in to comment.