Back to index

plone3  3.1.7
search.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from Products.Five import BrowserView
00003 from Products.CMFCore.utils import getToolByName
00004 from Products.PlonePAS.interfaces.browser import IPASSearchView
00005 
00006 
00007 class PASSearchView(BrowserView):
00008     implements(IPASSearchView)
00009 
00010     @staticmethod
00011     def extractCriteriaFromRequest(request):
00012         criteria=request.form.copy()
00013 
00014         for key in [ "form.submitted", "submit", 'b_start', 'b_size']:
00015             if key in criteria:
00016                 del criteria[key]
00017 
00018         for (key, value) in criteria.items():
00019             if not value:
00020                 del criteria[key]
00021 
00022         return criteria
00023 
00024 
00025     @staticmethod
00026     def merge(results, key):
00027         output={}
00028         for entry in results:
00029             id=entry[key]
00030             if id not in output:
00031                 output[id]=entry.copy()
00032             else:
00033                 buf=entry.copy()
00034                 buf.update(output[id])
00035                 output[id]=buf
00036 
00037         return output.values()
00038 
00039 
00040     def sort(self, results, key):
00041         def key_func(a):
00042             return a.get(key, "").lower()
00043         results.sort(key=key_func)
00044         return results
00045 
00046 
00047     def searchUsers(self, sort_by=None, **criteria):
00048         self.pas=getToolByName(self.context, "acl_users")
00049         results=self.merge(self.pas.searchUsers(**criteria), "userid")
00050         if sort_by is not None:
00051             results=self.sort(results, sort_by)
00052         return results
00053 
00054 
00055     def searchUsersByRequest(self, request, sort_by=None):
00056         criteria=self.extractCriteriaFromRequest(request)
00057         return self.searchUsers(sort_by=sort_by, **criteria)
00058 
00059 
00060     def searchGroups(self, **criteria):
00061         self.pas=getToolByName(self.context, "acl_users")
00062         return self.pas.searchGroups(**criteria)
00063 
00064 
00065     def searchGroupsByRequest(self, request):
00066         criteria=self.extractCriteriaFromRequest(request)
00067         return self.searchGroups(**criteria)
00068 
00069 
00070     def getPhysicalPath(self):
00071         # We call various PAS methods which can be ZCached. The ZCache
00072         # infrastructure relies on getPhysicalPath on the context being
00073         # available, which this view does not have, it not being a
00074         # persistent object. So we fake things and return the physical path
00075         # for our context.
00076         return self.context.getPhysicalPath()
00077