Back to index

plone3  3.1.7
groups.py
Go to the documentation of this file.
00001 import itertools
00002 from zope.interface import implements, classProvides
00003 from zope.schema.interfaces import ISource, IContextSourceBinder
00004 from zope.schema.vocabulary import SimpleTerm
00005 
00006 from zope.app.form.browser.interfaces import ISourceQueryView, ITerms
00007 from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
00008 
00009 from Products.CMFCore.utils import getToolByName
00010 
00011 class GroupsSource(object):
00012     """
00013       >>> from plone.app.vocabularies.tests.base import DummyContext
00014       >>> from plone.app.vocabularies.tests.base import DummyTool
00015 
00016       >>> context = DummyContext()
00017 
00018       >>> tool = DummyTool('acl_users')
00019       >>> groups = ('group1', 'group2')
00020       >>> def getGroupById(value, default):
00021       ...     return value in groups and value or default
00022       >>> tool.getGroupById = getGroupById
00023       >>> def searchGroups(name=None):
00024       ...     return [dict(groupid=u) for u in groups]
00025       >>> tool.searchGroups = searchGroups
00026       >>> context.acl_users = tool
00027 
00028       >>> source = GroupsSource(context)
00029       >>> source
00030       <plone.app.vocabularies.groups.GroupsSource object at ...>
00031 
00032       >>> len(source.search(''))
00033       2
00034 
00035       >>> len(source.search(u'\xa4'))
00036       2
00037 
00038       >>> 'group1' in source, 'noone' in source
00039       (True, False)
00040 
00041       >>> source.get('group1'), source.get('noone')
00042       ('group1', None)
00043     """
00044     implements(ISource)
00045     classProvides(IContextSourceBinder)
00046 
00047     def __init__(self, context):
00048         self.context = context
00049         self.users = getToolByName(context, "acl_users")
00050 
00051     def __contains__(self, value):
00052         """Return whether the value is available in this source
00053         """
00054         if self.get(value) is None:
00055             return False
00056         return True
00057 
00058     def search(self, query):
00059         # XXX: For some reason, this doesn't seem to know how to match on
00060         # title, only name, and seems to match other random groups if
00061         # it's unicode
00062         
00063         try:
00064             name = query.encode('ascii')
00065         except UnicodeEncodeError:
00066             name = query
00067         
00068         return [u['groupid'] for u in self.users.searchGroups(name=name)]
00069         
00070     def get(self, value):
00071         return self.users.getGroupById(value, None)
00072 
00073 
00074 class GroupsSourceQueryView(object):
00075     """
00076       >>> from plone.app.vocabularies.tests.base import DummyContext
00077       >>> from plone.app.vocabularies.tests.base import DummyTool
00078       >>> from plone.app.vocabularies.tests.base import Request
00079 
00080       >>> context = DummyContext()
00081 
00082       >>> class Group(object):
00083       ...     def __init__(self, id):
00084       ...         self.id = id
00085       ...
00086       ...     def getProperty(self, value, default):
00087       ...         return self.id
00088       ...
00089       ...     getId = getProperty
00090 
00091       >>> tool = DummyTool('acl_users')
00092       >>> groups = ('group1', 'group2')
00093       >>> def getGroupById(value, default):
00094       ...     return value in groups and Group(value) or None
00095       >>> tool.getGroupById = getGroupById
00096       >>> def searchGroups(name=None):
00097       ...     return [dict(groupid=u) for u in groups]
00098       >>> tool.searchGroups = searchGroups
00099       >>> context.acl_users = tool
00100 
00101       >>> source = GroupsSource(context)
00102       >>> source
00103       <plone.app.vocabularies.groups.GroupsSource object at ...>
00104 
00105       >>> view = GroupsSourceQueryView(source, Request())
00106       >>> view
00107       <plone.app.vocabularies.groups.GroupsSourceQueryView object at ...>
00108 
00109       >>> view.getTerm('group1')
00110       <zope.schema.vocabulary.SimpleTerm object at ...>
00111 
00112       >>> view.getValue('group1')
00113       'group1'
00114 
00115       >>> view.getValue('noone')
00116       Traceback (most recent call last):
00117       ...
00118       LookupError: noone
00119 
00120       >>> template = view.render(name='t')
00121 
00122       >>> u'<input type="text" name="t.query" value="" />' in template
00123       True
00124 
00125       >>> u'<input type="submit" name="t.search" value="Search" />' in template
00126       True
00127 
00128       >>> request = Request(form={'t.search' : True, 't.query' : 'value'})
00129       >>> view = GroupsSourceQueryView(source, request)
00130       >>> view.results('t')
00131       ['group1', 'group2']
00132     """
00133     implements(ITerms,
00134                ISourceQueryView)
00135 
00136     template = ViewPageTemplateFile('searchabletextsource.pt')
00137 
00138     def __init__(self, context, request):
00139         self.context = context
00140         self.request = request
00141 
00142     def getTerm(self, value):
00143         group = self.context.get(value)
00144         token = value
00145         title = value
00146         if group is not None:
00147             title = group.getProperty('title', None) or group.getId()
00148         return SimpleTerm(value, token=token, title=title)
00149 
00150     def getValue(self, token):
00151         if token not in self.context:
00152             raise LookupError(token)
00153         return token
00154 
00155     def render(self, name):
00156         return self.template(name=name)
00157 
00158     def results(self, name):
00159         # check whether the normal search button was pressed
00160         if name+".search" in self.request.form:
00161             query_fieldname = name+".query"
00162             if query_fieldname in self.request.form:
00163                 query = self.request.form[query_fieldname]
00164                 if query != '':
00165                     return self.context.search(query)