[Checkins] SVN: Products.CMFDefault/trunk/Products/CMFDefault/browser/ - moved shared batch code to widgets

Yvo Schubbe cvs-admin at zope.org
Thu Jul 12 08:48:24 UTC 2012


Log message for revision 127335:
  - moved shared batch code to widgets

Changed:
  U   Products.CMFDefault/trunk/Products/CMFDefault/browser/content/folder.py
  U   Products.CMFDefault/trunk/Products/CMFDefault/browser/content/interfaces.py
  U   Products.CMFDefault/trunk/Products/CMFDefault/browser/content/tests/test_folder.py
  U   Products.CMFDefault/trunk/Products/CMFDefault/browser/membership/members.py
  U   Products.CMFDefault/trunk/Products/CMFDefault/browser/search/search.py
  U   Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/__init__.py
  A   Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/batch.py
  A   Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/
  A   Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/__init__.py
  A   Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/test_batch.py

-=-
Modified: Products.CMFDefault/trunk/Products/CMFDefault/browser/content/folder.py
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/content/folder.py	2012-07-12 08:12:31 UTC (rev 127334)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/content/folder.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -13,7 +13,6 @@
 """Browser views for folders.
 """
 
-import sys
 import urllib
 
 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
@@ -22,18 +21,15 @@
 from zope.schema.vocabulary import SimpleTerm
 from zope.schema.vocabulary import SimpleVocabulary
 from zope.sequencesort.ssort import sort
-from ZTUtils import Batch
 from ZTUtils import LazyFilter
 
-from .interfaces import IBatchForm
 from .interfaces import IDeltaItem
 from .interfaces import IFolderItem
-from .interfaces import ISortForm
 from Products.CMFCore.interfaces import IDynamicType
 from Products.CMFCore.interfaces import IMembershipTool
 from Products.CMFDefault.browser.utils import decode
 from Products.CMFDefault.browser.utils import memoize
-from Products.CMFDefault.browser.utils import ViewBase
+from Products.CMFDefault.browser.widgets.batch import BatchViewBase
 from Products.CMFDefault.exceptions import CopyError
 from Products.CMFDefault.exceptions import zExceptions_Unauthorized
 from Products.CMFDefault.formlib.form import _EditFormMixin
@@ -43,7 +39,6 @@
 from Products.CMFDefault.permissions import ManageProperties
 from Products.CMFDefault.permissions import ViewManagementScreens
 from Products.CMFDefault.utils import Message as _
-from Products.CMFDefault.utils import thousands_commas
 
 def contents_delta_vocabulary(context):
     """Vocabulary for the pulldown for moving objects up and down.
@@ -54,174 +49,6 @@
     return SimpleVocabulary(deltas)
 
 
-class BatchViewBase(ViewBase):
-    """ Helper class for creating batch-based views.
-    """
-
-    _BATCH_SIZE = 25
-    hidden_fields = form.FormFields(IBatchForm, ISortForm)
-    prefix = ''
-
-    @memoize
-    def setUpWidgets(self, ignore_request=False):
-        self.hidden_widgets = form.setUpWidgets(self.hidden_fields,
-                            self.prefix, self.context, self.request,
-                            ignore_request=ignore_request)
-
-    @memoize
-    def _getBatchStart(self):
-        b_start = self._getHiddenVars().get('b_start', 0)
-        return int(b_start)
-
-    @memoize
-    def _getBatchObj(self):
-        b_start = self._getBatchStart()
-        items = self._get_items()
-        return Batch(items, self._BATCH_SIZE, b_start, orphan=0)
-
-    @memoize
-    def _getHiddenVars(self):
-        data = {}
-        if hasattr(self, 'hidden_widgets'):
-            form.getWidgetsData(self.hidden_widgets, self.prefix, data)
-        else:
-            data = self.request.form
-        return data
-
-    @memoize
-    def _getNavigationVars(self):
-        return self._getHiddenVars()
-
-    @memoize
-    def expand_prefix(self, key,):
-        """Return a form specific query key for use in GET strings"""
-        return "%s%s" % (form.expandPrefix(self.prefix), key)
-
-    @memoize
-    def _getNavigationURL(self, b_start=None):
-        target = self._getViewURL()
-        kw = self._getNavigationVars().copy()
-        if 'bstart' not in kw:
-            kw['b_start'] = b_start
-
-        for k, v in kw.items():
-            if not v or k == 'portal_status_message':
-                pass
-            else:
-                new_key = self.expand_prefix(k)
-                if new_key != k:
-                    kw[new_key] = v
-                    del kw[k]
-
-        query = kw and ('?%s' % urllib.urlencode(kw)) or ''
-
-        return u'%s%s' % (target, query)
-
-    # interface
-
-    @memoize
-    @decode
-    def listBatchItems(self):
-        batch_obj = self._getBatchObj()
-
-        items = []
-        for item in batch_obj:
-            item_description = item.Description()
-            item_title = item.Title()
-            item_type = remote_type = item.Type()
-            if item_type == 'Favorite':
-                try:
-                    item = item.getObject()
-                    item_description = item_description or item.Description()
-                    item_title = item_title or item.Title()
-                    remote_type = item.Type()
-                except KeyError:
-                    pass
-            is_file = remote_type in ('File', 'Image')
-            is_link = remote_type == 'Link'
-            items.append({'description': item_description,
-                          'format': is_file and item.Format() or '',
-                          'icon': item.getIconURL(),
-                          'size': is_file and ('%0.0f kb' %
-                                            (item.get_size() / 1024.0)) or '',
-                          'title': item_title,
-                          'type': item_type,
-                          'url': is_link and item.getRemoteUrl() or
-                                 item.absolute_url()})
-        return tuple(items)
-
-    @memoize
-    def navigation_previous(self):
-        batch_obj = self._getBatchObj().previous
-        if batch_obj is None:
-            return
-
-        length = len(batch_obj)
-        url = self._getNavigationURL(batch_obj.first)
-        if length == 1:
-            title = _(u'Previous item')
-        else:
-            title = _(u'Previous ${count} items', mapping={'count': length})
-        return {'title': title, 'url': url}
-
-    @memoize
-    def navigation_next(self):
-        batch_obj = self._getBatchObj().next
-        if batch_obj is None:
-            return
-
-        length = len(batch_obj)
-        url = self._getNavigationURL(batch_obj.first)
-        if length == 1:
-            title = _(u'Next item')
-        else:
-            title = _(u'Next ${count} items', mapping={'count': length})
-        return {'title': title, 'url': url}
-
-    def page_range(self):
-        """Create a range of up to ten pages around the current page"""
-        b_size = self._BATCH_SIZE
-        range_start = max(self.page_number() - 5, 0)
-        range_stop = min(max(self.page_number() + 5, 10), self.page_count())
-
-        pages = []
-        for p in range(range_start, range_stop):
-            b_start = p * b_size
-            pages.append({'number': p + 1,
-                          'url': self._getNavigationURL(b_start)})
-        return pages
-
-    @memoize
-    def page_count(self):
-        """Count total number of pages in the batch"""
-        batch_obj = self._getBatchObj()
-        count = (batch_obj.sequence_length - 1) / self._BATCH_SIZE + 1
-        return count
-
-    @memoize
-    def page_number(self):
-        """Get the number of the current page in the batch"""
-        return (self._getBatchStart() / self._BATCH_SIZE) + 1
-
-    @memoize
-    def summary_length(self):
-        length = self._getBatchObj().sequence_length
-        if sys.version_info < (2, 7):
-            # BBB: for Python 2.6
-            return length and thousands_commas(length) or ''
-        return length and '{:,}'.format(length) or ''
-
-    @memoize
-    def summary_type(self):
-        length = self._getBatchObj().sequence_length
-        return (length == 1) and _(u'item') or _(u'items')
-
-    @memoize
-    @decode
-    def summary_match(self):
-        return self.request.form.get('SearchableText')
-
-
 class ContentProxy(object):
     """Utility wrapping content item for display purposes"""
 

Modified: Products.CMFDefault/trunk/Products/CMFDefault/browser/content/interfaces.py
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/content/interfaces.py	2012-07-12 08:12:31 UTC (rev 127334)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/content/interfaces.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -16,7 +16,6 @@
 from zope.interface import Interface
 from zope.schema import Bool
 from zope.schema import Choice
-from zope.schema import Int
 from zope.schema import TextLine
 
 from Products.CMFDefault.utils import Message as _
@@ -43,23 +42,3 @@
         required=True,
         vocabulary=u'cmf.contents delta vocabulary',
         default=1)
-
-
-class IBatchForm(Interface):
-    """Schema for batch forms"""
-
-    b_start = Int(
-        title=u"Batch start",
-        required=False,
-        default=0)
-
-
-class ISortForm(Interface):
-    """Schema for sort keys"""
-    sort_key = TextLine(
-        title=u"Sort key",
-        required=False)
-
-    reverse = Int(
-        title=u"Reverse sort order",
-        required=False)

Modified: Products.CMFDefault/trunk/Products/CMFDefault/browser/content/tests/test_folder.py
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/content/tests/test_folder.py	2012-07-12 08:12:31 UTC (rev 127334)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/content/tests/test_folder.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -36,111 +36,6 @@
 from Products.CMFDefault.testing import FunctionalLayer
 
 
-class BatchViewTests(unittest.TestCase):
-
-    def _makeOne(self, batch_size=30):
-        from Products.CMFDefault.browser.content.folder import BatchViewBase
-        batch = BatchViewBase(None,
-                              TestRequest(ACTUAL_URL='http://example.com'))
-        batch._get_items = lambda: range(batch_size)
-        return batch
-
-    def test_expand_prefix(self):
-        batch = self._makeOne()
-        self.assertEqual(batch.expand_prefix('key'), 'key')
-        batch = self._makeOne()
-        batch.prefix = 'form'
-        self.assertEqual(batch.expand_prefix('key'), 'form.key')
-
-    def test_page_count(self):
-        batch = self._makeOne()
-        self.assertEqual(batch.page_count(), 2)
-        batch = self._makeOne(25)
-        self.assertEqual(batch.page_count(), 1)
-        batch = self._makeOne()
-        batch._BATCH_SIZE = 2
-        self.assertEqual(batch.page_count(), 15)
-
-    def test_page_number(self):
-        batch = self._makeOne()
-        self.assertEqual(batch.page_number(), 1)
-        batch = self._makeOne(1000)
-        batch._getBatchStart = lambda: 250
-        self.assertEqual(batch.page_number(), 11)
-
-    def test_summary_length(self):
-        batch = self._makeOne()
-        self.assertEqual(batch.summary_length(), '30')
-        batch = self._makeOne(10000)
-        self.assertEqual(batch.summary_length(), '10,000')
-        batch = self._makeOne(0)
-        self.assertEqual(batch.summary_length(), '')
-
-    def test_summary_type(self):
-        batch = self._makeOne()
-        self.assertEqual(batch.summary_type(), 'items')
-        batch = self._makeOne()
-        batch._get_items = lambda: range(1)
-        self.assertEqual(batch.summary_type(), 'item')
-
-    def test_navigation_previous(self):
-        batch = self._makeOne()
-        self.assertEqual(batch.navigation_previous(), None)
-        batch = self._makeOne(1000)
-        batch._getBatchStart = lambda: 250
-        self.assertEqual(batch.navigation_previous(),
-                         {'url': u'http://example.com?b_start=225',
-                          'title': u'Previous ${count} items'}
-                         )
-
-    def test_navigation_next(self):
-        batch = self._makeOne()
-        self.assertEqual(batch.navigation_next(),
-                         {'url': u'http://example.com?b_start=25',
-                          'title': u'Next ${count} items'}
-                         )
-        batch = self._makeOne(1000)
-        batch._getBatchStart = lambda: 250
-        self.assertEqual(batch.navigation_next(),
-                         {'url': u'http://example.com?b_start=275',
-                          'title': u'Next ${count} items'}
-                         )
-
-    def test_page_range(self):
-        batch = self._makeOne()
-        self.assertEqual(
-            batch.page_range(),
-            [{'url': u'http://example.com?b_start=0', 'number': 1},
-             {'url': u'http://example.com?b_start=25', 'number': 2}])
-        batch = self._makeOne(1000)
-        self.assertEqual(
-            batch.page_range(),
-            [{'url': u'http://example.com?b_start=0', 'number': 1},
-             {'url': u'http://example.com?b_start=25', 'number': 2},
-             {'url': u'http://example.com?b_start=50', 'number': 3},
-             {'url': u'http://example.com?b_start=75', 'number': 4},
-             {'url': u'http://example.com?b_start=100', 'number': 5},
-             {'url': u'http://example.com?b_start=125', 'number': 6},
-             {'url': u'http://example.com?b_start=150', 'number': 7},
-             {'url': u'http://example.com?b_start=175', 'number': 8},
-             {'url': u'http://example.com?b_start=200', 'number': 9},
-             {'url': u'http://example.com?b_start=225', 'number': 10}])
-        batch = self._makeOne(1000)
-        batch._getBatchStart = lambda: 250
-        self.assertEqual(
-            batch.page_range(),
-            [{'url': u'http://example.com?b_start=150', 'number': 7},
-             {'url': u'http://example.com?b_start=175', 'number': 8},
-             {'url': u'http://example.com?b_start=200', 'number': 9},
-             {'url': u'http://example.com?b_start=225', 'number': 10},
-             {'url': u'http://example.com?b_start=250', 'number': 11},
-             {'url': u'http://example.com?b_start=275', 'number': 12},
-             {'url': u'http://example.com?b_start=300', 'number': 13},
-             {'url': u'http://example.com?b_start=325', 'number': 14},
-             {'url': u'http://example.com?b_start=350', 'number': 15},
-             {'url': u'http://example.com?b_start=375', 'number': 16}])
-
-
 class FolderContentsViewTests(unittest.TestCase):
 
     def setUp(self):
@@ -275,7 +170,6 @@
 
 def test_suite():
     suite = unittest.TestSuite()
-    suite.addTest(unittest.makeSuite(BatchViewTests))
     suite.addTest(unittest.makeSuite(FolderContentsViewTests))
     suite.addTest(unittest.makeSuite(FolderViewTests))
     suite.addTest(unittest.TestSuite((ftest_suite,)))

Modified: Products.CMFDefault/trunk/Products/CMFDefault/browser/membership/members.py
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/membership/members.py	2012-07-12 08:12:31 UTC (rev 127334)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/membership/members.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -24,9 +24,9 @@
 from ZTUtils import LazyFilter
 
 from Products.CMFCore.interfaces import IMembershipTool
-from Products.CMFDefault.browser.content.folder import BatchViewBase
-from Products.CMFDefault.browser.content.interfaces import IBatchForm
 from Products.CMFDefault.browser.utils import memoize
+from Products.CMFDefault.browser.widgets.batch import BatchViewBase
+from Products.CMFDefault.browser.widgets.batch import IBatchForm
 from Products.CMFDefault.formlib.form import EditFormBase
 from Products.CMFDefault.utils import Message as _
 

Modified: Products.CMFDefault/trunk/Products/CMFDefault/browser/search/search.py
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/search/search.py	2012-07-12 08:12:31 UTC (rev 127334)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/search/search.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -21,9 +21,9 @@
 from .interfaces import ISearchSchema
 from Products.CMFCore.interfaces import ICatalogTool
 from Products.CMFCore.interfaces import IMembershipTool
-from Products.CMFDefault.browser.content.folder import BatchViewBase
-from Products.CMFDefault.browser.content.interfaces import IBatchForm
 from Products.CMFDefault.browser.utils import memoize
+from Products.CMFDefault.browser.widgets.batch import BatchViewBase
+from Products.CMFDefault.browser.widgets.batch import IBatchForm
 from Products.CMFDefault.formlib.form import EditFormBase
 from Products.CMFDefault.formlib.widgets import ChoiceMultiSelectWidget
 from Products.CMFDefault.utils import Message as _
@@ -126,7 +126,7 @@
 
     @memoize
     def listBatchItems(self):
-        return( {'description': item.Description,
+        return({'description': item.Description,
            'icon': item.getIconURL,
            'title': item.Title,
            'type': item.Type,

Modified: Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/__init__.py
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/__init__.py	2012-07-12 08:12:31 UTC (rev 127334)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/__init__.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -10,5 +10,5 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""CMFDefault form views.
+"""CMFDefault browser view widgets.
 """

Copied: Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/batch.py (from rev 127332, Products.CMFDefault/trunk/Products/CMFDefault/browser/content/folder.py)
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/batch.py	                        (rev 0)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/batch.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -0,0 +1,221 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+#
+# 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.
+#
+##############################################################################
+"""Browser views for folders.
+"""
+
+import sys
+import urllib
+
+from zope.formlib import form
+from zope.interface import Interface
+from zope.schema import Int
+from zope.schema import TextLine
+from ZTUtils import Batch
+
+from Products.CMFDefault.browser.utils import decode
+from Products.CMFDefault.browser.utils import memoize
+from Products.CMFDefault.browser.utils import ViewBase
+from Products.CMFDefault.utils import Message as _
+from Products.CMFDefault.utils import thousands_commas
+
+
+class IBatchForm(Interface):
+
+    """Schema for batch forms
+    """
+
+    b_start = Int(
+        title=u"Batch start",
+        required=False,
+        default=0)
+
+
+class ISortForm(Interface):
+
+    """Schema for sort keys
+    """
+
+    sort_key = TextLine(
+        title=u"Sort key",
+        required=False)
+
+    reverse = Int(
+        title=u"Reverse sort order",
+        required=False)
+
+
+class BatchViewBase(ViewBase):
+
+    """ Helper class for creating batch-based views.
+    """
+
+    _BATCH_SIZE = 25
+    hidden_fields = form.FormFields(IBatchForm, ISortForm)
+    prefix = ''
+
+    @memoize
+    def setUpWidgets(self, ignore_request=False):
+        self.hidden_widgets = form.setUpWidgets(self.hidden_fields,
+                            self.prefix, self.context, self.request,
+                            ignore_request=ignore_request)
+
+    @memoize
+    def _getBatchStart(self):
+        b_start = self._getHiddenVars().get('b_start', 0)
+        return int(b_start)
+
+    @memoize
+    def _getBatchObj(self):
+        b_start = self._getBatchStart()
+        items = self._get_items()
+        return Batch(items, self._BATCH_SIZE, b_start, orphan=0)
+
+    @memoize
+    def _getHiddenVars(self):
+        data = {}
+        if hasattr(self, 'hidden_widgets'):
+            form.getWidgetsData(self.hidden_widgets, self.prefix, data)
+        else:
+            data = self.request.form
+        return data
+
+    @memoize
+    def _getNavigationVars(self):
+        return self._getHiddenVars()
+
+    @memoize
+    def expand_prefix(self, key,):
+        """Return a form specific query key for use in GET strings"""
+        return "%s%s" % (form.expandPrefix(self.prefix), key)
+
+    @memoize
+    def _getNavigationURL(self, b_start):
+        target = self._getViewURL()
+        kw = self._getNavigationVars().copy()
+
+        kw['b_start'] = b_start
+        for k, v in kw.items():
+            if not v or k == 'portal_status_message':
+                pass
+            else:
+                new_key = self.expand_prefix(k)
+                if new_key != k:
+                    kw[new_key] = v
+                    del kw[k]
+
+        query = kw and ('?%s' % urllib.urlencode(kw)) or ''
+        return u'%s%s' % (target, query)
+
+    # interface
+
+    @memoize
+    @decode
+    def listBatchItems(self):
+        batch_obj = self._getBatchObj()
+
+        items = []
+        for item in batch_obj:
+            item_description = item.Description()
+            item_title = item.Title()
+            item_type = item.getPortalTypeName()
+            if item_type == 'Favorite':
+                try:
+                    item = item.getObject()
+                    item_description = item_description or item.Description()
+                    item_title = item_title or item.Title()
+                    item_type = item.getPortalTypeName()
+                except KeyError:
+                    pass
+            is_file = item_type in ('File', 'Image')
+            is_link = item_type == 'Link'
+            items.append({'description': item_description,
+                          'format': is_file and item.Format() or '',
+                          'icon': item.getIconURL(),
+                          'size': is_file and ('%0.1f kB' %
+                                             (item.get_size() / 1024.0)) or '',
+                          'title': item_title,
+                          'type': item.Type(),
+                          'url': is_link and item.getRemoteUrl() or
+                                 item.absolute_url()})
+        return tuple(items)
+
+    @memoize
+    def navigation_previous(self):
+        batch_obj = self._getBatchObj().previous
+        if batch_obj is None:
+            return None
+
+        length = len(batch_obj)
+        url = self._getNavigationURL(batch_obj.first)
+        if length == 1:
+            title = _(u'Previous item')
+        else:
+            title = _(u'Previous ${count} items', mapping={'count': length})
+        return {'title': title, 'url': url}
+
+    @memoize
+    def navigation_next(self):
+        batch_obj = self._getBatchObj().next
+        if batch_obj is None:
+            return None
+
+        length = len(batch_obj)
+        url = self._getNavigationURL(batch_obj.first)
+        if length == 1:
+            title = _(u'Next item')
+        else:
+            title = _(u'Next ${count} items', mapping={'count': length})
+        return {'title': title, 'url': url}
+
+    def page_range(self):
+        """Create a range of up to ten pages around the current page"""
+        b_size = self._BATCH_SIZE
+        range_start = max(self.page_number() - 5, 0)
+        range_stop = min(max(self.page_number() + 5, 10), self.page_count())
+
+        pages = []
+        for p in range(range_start, range_stop):
+            b_start = p * b_size
+            pages.append({'number': p + 1,
+                          'url': self._getNavigationURL(b_start)})
+        return pages
+
+    @memoize
+    def page_count(self):
+        """Count total number of pages in the batch"""
+        batch_obj = self._getBatchObj()
+        count = (batch_obj.sequence_length - 1) / self._BATCH_SIZE + 1
+        return count
+
+    @memoize
+    def page_number(self):
+        """Get the number of the current page in the batch"""
+        return (self._getBatchStart() / self._BATCH_SIZE) + 1
+
+    @memoize
+    def summary_length(self):
+        length = self._getBatchObj().sequence_length
+        if sys.version_info < (2, 7):
+            # BBB: for Python 2.6
+            return length and thousands_commas(length) or ''
+        return length and '{:,}'.format(length) or ''
+
+    @memoize
+    def summary_type(self):
+        length = self._getBatchObj().sequence_length
+        return (length == 1) and _(u'item') or _(u'items')
+
+    @memoize
+    @decode
+    def summary_match(self):
+        return self.request.form.get('SearchableText')

Added: Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/__init__.py
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/__init__.py	                        (rev 0)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/__init__.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -0,0 +1 @@
+# Unit tests for CMFDefault browser view widgets.


Property changes on: Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/__init__.py
___________________________________________________________________
Added: svn:eol-style
   + native

Copied: Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/test_batch.py (from rev 127332, Products.CMFDefault/trunk/Products/CMFDefault/browser/content/tests/test_folder.py)
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/test_batch.py	                        (rev 0)
+++ Products.CMFDefault/trunk/Products/CMFDefault/browser/widgets/tests/test_batch.py	2012-07-12 08:48:20 UTC (rev 127335)
@@ -0,0 +1,129 @@
+##############################################################################
+#
+# Copyright (c) 2008 Zope Foundation and Contributors.
+#
+# 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.
+#
+##############################################################################
+""" Test Products.CMFDefault.browser.folder
+"""
+
+import unittest
+
+from zope.publisher.browser import TestRequest
+
+
+class BatchViewTests(unittest.TestCase):
+
+    def _makeOne(self, batch_size=30):
+        from Products.CMFDefault.browser.widgets.batch import BatchViewBase
+        batch = BatchViewBase(None,
+                              TestRequest(ACTUAL_URL='http://example.com'))
+        batch._get_items = lambda: range(batch_size)
+        return batch
+
+    def test_expand_prefix(self):
+        batch = self._makeOne()
+        self.assertEqual(batch.expand_prefix('key'), 'key')
+        batch = self._makeOne()
+        batch.prefix = 'form'
+        self.assertEqual(batch.expand_prefix('key'), 'form.key')
+
+    def test_page_count(self):
+        batch = self._makeOne()
+        self.assertEqual(batch.page_count(), 2)
+        batch = self._makeOne(25)
+        self.assertEqual(batch.page_count(), 1)
+        batch = self._makeOne()
+        batch._BATCH_SIZE = 2
+        self.assertEqual(batch.page_count(), 15)
+
+    def test_page_number(self):
+        batch = self._makeOne()
+        self.assertEqual(batch.page_number(), 1)
+        batch = self._makeOne(1000)
+        batch._getBatchStart = lambda: 250
+        self.assertEqual(batch.page_number(), 11)
+
+    def test_summary_length(self):
+        batch = self._makeOne()
+        self.assertEqual(batch.summary_length(), '30')
+        batch = self._makeOne(10000)
+        self.assertEqual(batch.summary_length(), '10,000')
+        batch = self._makeOne(0)
+        self.assertEqual(batch.summary_length(), '')
+
+    def test_summary_type(self):
+        batch = self._makeOne()
+        self.assertEqual(batch.summary_type(), 'items')
+        batch = self._makeOne()
+        batch._get_items = lambda: range(1)
+        self.assertEqual(batch.summary_type(), 'item')
+
+    def test_navigation_previous(self):
+        batch = self._makeOne()
+        self.assertEqual(batch.navigation_previous(), None)
+        batch = self._makeOne(1000)
+        batch._getBatchStart = lambda: 250
+        self.assertEqual(batch.navigation_previous(),
+                         {'url': u'http://example.com?b_start=225',
+                          'title': u'Previous ${count} items'}
+                         )
+
+    def test_navigation_next(self):
+        batch = self._makeOne()
+        self.assertEqual(batch.navigation_next(),
+                         {'url': u'http://example.com?b_start=25',
+                          'title': u'Next ${count} items'}
+                         )
+        batch = self._makeOne(1000)
+        batch._getBatchStart = lambda: 250
+        self.assertEqual(batch.navigation_next(),
+                         {'url': u'http://example.com?b_start=275',
+                          'title': u'Next ${count} items'}
+                         )
+
+    def test_page_range(self):
+        batch = self._makeOne()
+        self.assertEqual(
+            batch.page_range(),
+            [{'url': u'http://example.com?b_start=0', 'number': 1},
+             {'url': u'http://example.com?b_start=25', 'number': 2}])
+        batch = self._makeOne(1000)
+        self.assertEqual(
+            batch.page_range(),
+            [{'url': u'http://example.com?b_start=0', 'number': 1},
+             {'url': u'http://example.com?b_start=25', 'number': 2},
+             {'url': u'http://example.com?b_start=50', 'number': 3},
+             {'url': u'http://example.com?b_start=75', 'number': 4},
+             {'url': u'http://example.com?b_start=100', 'number': 5},
+             {'url': u'http://example.com?b_start=125', 'number': 6},
+             {'url': u'http://example.com?b_start=150', 'number': 7},
+             {'url': u'http://example.com?b_start=175', 'number': 8},
+             {'url': u'http://example.com?b_start=200', 'number': 9},
+             {'url': u'http://example.com?b_start=225', 'number': 10}])
+        batch = self._makeOne(1000)
+        batch._getBatchStart = lambda: 250
+        self.assertEqual(
+            batch.page_range(),
+            [{'url': u'http://example.com?b_start=150', 'number': 7},
+             {'url': u'http://example.com?b_start=175', 'number': 8},
+             {'url': u'http://example.com?b_start=200', 'number': 9},
+             {'url': u'http://example.com?b_start=225', 'number': 10},
+             {'url': u'http://example.com?b_start=250', 'number': 11},
+             {'url': u'http://example.com?b_start=275', 'number': 12},
+             {'url': u'http://example.com?b_start=300', 'number': 13},
+             {'url': u'http://example.com?b_start=325', 'number': 14},
+             {'url': u'http://example.com?b_start=350', 'number': 15},
+             {'url': u'http://example.com?b_start=375', 'number': 16}])
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(BatchViewTests))
+    return suite



More information about the checkins mailing list