[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/pas/browser/ Sprinting with Jim:

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Oct 13 03:31:19 EDT 2004


Log message for revision 28038:
  Sprinting with Jim:
  
  Provide a Schema-based principal search implementation.
  

Changed:
  A   Zope3/trunk/src/zope/app/pas/browser/schemasearch.py
  A   Zope3/trunk/src/zope/app/pas/browser/schemasearch.txt
  A   Zope3/trunk/src/zope/app/pas/browser/tests.py

-=-
Added: Zope3/trunk/src/zope/app/pas/browser/schemasearch.py
===================================================================
--- Zope3/trunk/src/zope/app/pas/browser/schemasearch.py	2004-10-13 07:29:49 UTC (rev 28037)
+++ Zope3/trunk/src/zope/app/pas/browser/schemasearch.py	2004-10-13 07:31:17 UTC (rev 28038)
@@ -0,0 +1,72 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Search interface for queriables.
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+from zope.interface import implements
+from zope.i18n import translate
+from zope.schema import getFieldsInOrder
+from zope.app.pas.interfaces import IQuerySchemaSearch
+from zope.app.form.utility import setUpWidgets, getWidgetsData
+from zope.app.form.interfaces import IInputWidget
+from zope.app.form.browser.interfaces import ISourceQueryView
+from zope.app.i18n import ZopeMessageIDFactory as _
+
+search_label = _('search-button', 'Search')
+
+class QuerySchemaSearchView(object):
+    implements(ISourceQueryView)
+
+    def __init__(self, context, request):
+        self.context = context
+        self.request = request
+
+    def render(self, name):
+        schema = self.context.schema
+        setUpWidgets(self, schema, IInputWidget, prefix=name+'.field')
+        html = []
+        for field_name, field in getFieldsInOrder(schema):
+            widget = getattr(self, field_name+'_widget')
+            html.append('<div class="row">')
+            html.append('<div class="label">')
+            html.append('<label for="%s" title="%s">'
+                        % (widget.name, widget.hint))
+            html.append(widget.label)
+            html.append('</label>')
+            html.append('</div>')
+            html.append('<div class="field">')
+            html.append(widget())
+            html.append('</div>')
+            if widget.error():
+                html.append('<div class="error">')
+                html.append(widget.error())
+                html.append('</div>')
+            html.append('</div>')
+
+        html.append('<br /><input type="submit" name="%s" value="%s" />'
+                    % (name+'.search',
+                       translate(search_label, context=self.request)))
+
+        return '\n'.join(html)
+
+    def results(self, name):
+        if not (name+'.search' in self.request):
+            return None
+        schema = self.context.schema
+        setUpWidgets(self, schema, IInputWidget, prefix=name+'.field')
+        data = getWidgetsData(self, schema)
+        return self.context.search(data)

Added: Zope3/trunk/src/zope/app/pas/browser/schemasearch.txt
===================================================================
--- Zope3/trunk/src/zope/app/pas/browser/schemasearch.txt	2004-10-13 07:29:49 UTC (rev 28037)
+++ Zope3/trunk/src/zope/app/pas/browser/schemasearch.txt	2004-10-13 07:31:17 UTC (rev 28038)
@@ -0,0 +1,73 @@
+The Query View for Schema Search Plugins
+========================================
+
+If a plugin supports `IQuerySchemaSearch`: 
+
+  >>> from zope.interface import Interface
+  >>> import zope.schema
+  >>> class ISearchCriteria(Interface):
+  ...     search = zope.schema.TextLine(title=u"Search String")
+
+  >>> class MySearchPlugin:
+  ...     schema = ISearchCriteria
+  ...     data = ['foo', 'bar', 'blah']
+  ...
+  ...     def get(self, id):
+  ...         if id in self.data:
+  ...             return {}
+  ...
+  ...     def search(self, query, start=None, batch_size=None):
+  ...         search = query.get('search')
+  ...         if search is not None:
+  ...             i = 0
+  ...             n = 0
+  ...             for value in self.data:
+  ...                 if search in value:
+  ...                     if not ((start is not None and i < start)
+  ...                             or
+  ...                             (batch_size is not None and n > batch_size)):
+  ...                         yield value
+
+then we can get a view:
+
+  >>> from zope.app.pas.browser.schemasearch import QuerySchemaSearchView 
+  >>> from zope.publisher.browser import TestRequest
+  >>> request = TestRequest()
+  >>> view = QuerySchemaSearchView(MySearchPlugin(), request)
+
+This allows us to render a search form.
+
+  >>> print view.render('test') # doctest: +NORMALIZE_WHITESPACE
+  <div class="row">
+  <div class="label">
+  <label for="test.field.search" title="">
+  Search String
+  </label>
+  </div>
+  <div class="field">
+  <input class="textType" id="test.field.search" name="test.field.search" 
+         size="20" type="text" value=""  />
+  </div>
+  </div>
+  <br /><input type="submit" name="test.search" value="Search" />
+
+If we ask for results:
+
+  >>> view.results('test')
+
+We don't get any, since we did not provide any. But if we give input:
+
+  >>> request.form['test.field.search'] = 'a'
+
+we still don't get any:
+
+  >>> view.results('test')
+
+because we did not press the button. So let's press the button:
+
+  >>> request.form['test.search'] = 'Search'
+
+so that we now get results (!):
+
+  >>> list(view.results('test'))
+  ['bar', 'blah']
\ No newline at end of file

Added: Zope3/trunk/src/zope/app/pas/browser/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/pas/browser/tests.py	2004-10-13 07:29:49 UTC (rev 28037)
+++ Zope3/trunk/src/zope/app/pas/browser/tests.py	2004-10-13 07:31:17 UTC (rev 28038)
@@ -0,0 +1,39 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Pluggable Authentication Service Tests
+
+$Id: tests.py 27985 2004-10-12 08:00:42Z srichter $
+"""
+__docformat__ = "reStructuredText"
+import unittest
+from zope.testing import doctest
+from zope.schema.interfaces import ITextLine
+from zope.app.tests import placelesssetup
+from zope.app.tests import ztapi
+from zope.app.form.browser import TextWidget
+from zope.app.form.interfaces import IInputWidget
+
+def setUp(test):
+    placelesssetup.setUp()
+    ztapi.browserView(ITextLine, '', TextWidget, providing=IInputWidget)
+
+def test_suite():
+    return unittest.TestSuite((
+        doctest.DocFileSuite('schemasearch.txt',
+                             setUp=setUp, tearDown=placelesssetup.tearDown),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
+



More information about the Zope3-Checkins mailing list