Back to index

plone3  3.1.7
users.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 UsersSource(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       >>> users = ('user1', 'user2')
00020       >>> def getUserById(value, default):
00021       ...     return value in users and value or default
00022       >>> tool.getUserById = getUserById
00023       >>> def searchUsers(fullname=None):
00024       ...     return [dict(userid=u) for u in users]
00025       >>> tool.searchUsers = searchUsers
00026       >>> context.acl_users = tool
00027 
00028       >>> source = UsersSource(context)
00029       >>> source
00030       <plone.app.vocabularies.users.UsersSource object at ...>
00031 
00032       >>> len(source.search(None))
00033       2
00034 
00035       >>> 'user1' in source, 'noone' in source
00036       (True, False)
00037 
00038       >>> source.get('user1'), source.get('noone')
00039       ('user1', None)
00040     """
00041     implements(ISource)
00042     classProvides(IContextSourceBinder)
00043 
00044     def __init__(self, context):
00045         self.context = context
00046         self.users = getToolByName(context, "acl_users")
00047 
00048     def __contains__(self, value):
00049         """Return whether the value is available in this source
00050         """
00051         if self.get(value) is None:
00052             return False
00053         return True
00054 
00055     def search(self, query):
00056         return [u['userid'] for u in self.users.searchUsers(fullname=query)]
00057         
00058     def get(self, value):
00059         return self.users.getUserById(value, None)
00060 
00061 
00062 class UsersSourceQueryView(object):
00063     """
00064       >>> from plone.app.vocabularies.tests.base import DummyContext
00065       >>> from plone.app.vocabularies.tests.base import DummyTool
00066       >>> from plone.app.vocabularies.tests.base import Request
00067 
00068       >>> context = DummyContext()
00069 
00070       >>> class User(object):
00071       ...     def __init__(self, id):
00072       ...         self.id = id
00073       ...
00074       ...     def getProperty(self, value, default):
00075       ...         return self.id
00076       ...
00077       ...     getId = getProperty
00078 
00079       >>> tool = DummyTool('acl_users')
00080       >>> users = ('user1', 'user2')
00081       >>> def getUserById(value, default):
00082       ...     return value in users and User(value) or None
00083       >>> tool.getUserById = getUserById
00084       >>> def searchUsers(fullname=None):
00085       ...     return [dict(userid=u) for u in users]
00086       >>> tool.searchUsers = searchUsers
00087       >>> context.acl_users = tool
00088 
00089       >>> source = UsersSource(context)
00090       >>> source
00091       <plone.app.vocabularies.users.UsersSource object at ...>
00092 
00093       >>> view = UsersSourceQueryView(source, Request())
00094       >>> view
00095       <plone.app.vocabularies.users.UsersSourceQueryView object at ...>
00096 
00097       >>> view.getTerm('user1')
00098       <zope.schema.vocabulary.SimpleTerm object at ...>
00099 
00100       >>> view.getValue('user1')
00101       'user1'
00102 
00103       >>> view.getValue('noone')
00104       Traceback (most recent call last):
00105       ...
00106       LookupError: noone
00107 
00108       >>> template = view.render(name='t')
00109 
00110       >>> u'<input type="text" name="t.query" value="" />' in template
00111       True
00112 
00113       >>> u'<input type="submit" name="t.search" value="Search" />' in template
00114       True
00115 
00116       >>> request = Request(form={'t.search' : True, 't.query' : 'value'})
00117       >>> view = UsersSourceQueryView(source, request)
00118       >>> view.results('t')
00119       ['user1', 'user2']
00120     """
00121 
00122     implements(ITerms,
00123                ISourceQueryView)
00124 
00125     template = ViewPageTemplateFile('searchabletextsource.pt')
00126 
00127     def __init__(self, context, request):
00128         self.context = context
00129         self.request = request
00130 
00131     def getTerm(self, value):
00132         user = self.context.get(value)
00133         token = value
00134         title = value
00135         if user is not None:
00136             title = user.getProperty('fullname', None) or user.getId()
00137         return SimpleTerm(value, token=token, title=title)
00138 
00139     def getValue(self, token):
00140         if token not in self.context:
00141             raise LookupError(token)
00142         return token
00143 
00144     def render(self, name):
00145         return self.template(name=name)
00146 
00147     def results(self, name):
00148         # check whether the normal search button was pressed
00149         if name+".search" in self.request.form:
00150             query_fieldname = name+".query"
00151             if query_fieldname in self.request.form:
00152                 query = self.request.form[query_fieldname]
00153                 if query != '':
00154                     return self.context.search(query)