[Checkins] SVN: z3c.widget/trunk/src/z3c/widget/country/ This package provides widgets to select a country.

Adam Groszer adamg at fw.hu
Sat Jan 13 05:23:13 EST 2007


Log message for revision 71995:
  This package provides widgets to select a country.
  The dropdown type is registered as a default for the Country schema.

Changed:
  A   z3c.widget/trunk/src/z3c/widget/country/
  A   z3c.widget/trunk/src/z3c/widget/country/DEPENDENCIES.cfg
  A   z3c.widget/trunk/src/z3c/widget/country/README.txt
  A   z3c.widget/trunk/src/z3c/widget/country/SETUP.cfg
  A   z3c.widget/trunk/src/z3c/widget/country/__init__.py
  A   z3c.widget/trunk/src/z3c/widget/country/configure.zcml
  A   z3c.widget/trunk/src/z3c/widget/country/tests.py
  A   z3c.widget/trunk/src/z3c/widget/country/widget.py
  A   z3c.widget/trunk/src/z3c/widget/country/z3c.widget.country-configure.zcml

-=-
Added: z3c.widget/trunk/src/z3c/widget/country/DEPENDENCIES.cfg
===================================================================
--- z3c.widget/trunk/src/z3c/widget/country/DEPENDENCIES.cfg	2007-01-13 09:33:29 UTC (rev 71994)
+++ z3c.widget/trunk/src/z3c/widget/country/DEPENDENCIES.cfg	2007-01-13 10:23:11 UTC (rev 71995)
@@ -0,0 +1,4 @@
+zope.interface
+zope.schema
+zope.app.form
+z3c.i18n


Property changes on: z3c.widget/trunk/src/z3c/widget/country/DEPENDENCIES.cfg
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/country/README.txt
===================================================================
--- z3c.widget/trunk/src/z3c/widget/country/README.txt	2007-01-13 09:33:29 UTC (rev 71994)
+++ z3c.widget/trunk/src/z3c/widget/country/README.txt	2007-01-13 10:23:11 UTC (rev 71995)
@@ -0,0 +1,100 @@
+=========================
+Country selection Widgets
+=========================
+
+This package provides widgets to select a country.
+The dropdown type is registered as a default for the Country schema.
+
+The pain was to sort the options after the translation.
+
+
+
+Before we can start, we have to do a little bit of setup:
+
+  >>> import zope.component
+  >>> import zope.schema
+  >>> import zope.app.form.browser
+  >>> from z3c.widget.country.widget import CountryInputDropdown
+  >>> from z3c.widget.country import ICountry
+  >>> from z3c.i18n.iso import territoryVocabularyFactory
+  >>> from zope.publisher.interfaces.browser import IBrowserRequest
+
+First we have to create a field and a request:
+
+  >>> from z3c.widget.country import Country
+
+  >>> countryFld = Country(
+  ...     __name__='country',
+  ...     title=u'Country',
+  ...     description=u'Select a Country')
+
+  >>> from zope.publisher.browser import TestRequest
+  >>> request = TestRequest()
+
+Now we can initialize the widget.
+
+  >>> class Content(object):
+  ...     country = None
+  >>> content = Content()
+  >>> boundCountry = countryFld.bind(content)
+
+  >>> widget = CountryInputDropdown(boundCountry,
+  ...   territoryVocabularyFactory(None), request)
+
+Let's make sure that all fields have the correct value:
+
+  >>> widget.name
+  'field.country'
+
+  >>> widget.label
+  u'Country'
+
+  >>> widget.hint
+  u'Select a Country'
+
+  >>> widget.visible
+  True
+
+Let's see how the widget is rendered:
+
+  >>> print widget()
+  <div>
+  <div class="value">
+  <select id="field.country" name="field.country" size="1" >
+  <option value="AF">Afghanistan</option>
+  <option value="AL">Albania</option>
+  <option value="DZ">Algeria</option>
+  ...
+  <option value="HU">Hungary</option>
+  <option value="IS">Iceland</option>
+  <option value="IN">India</option>
+  ...
+  <option value="ZM">Zambia</option>
+  <option value="ZW">Zimbabwe</option>
+  </select>
+  ...
+
+#Let's see the german translation:
+#z3c.i18n registrations required!!!
+#
+#  >>> request = TestRequest(HTTP_ACCEPT_LANGUAGE='de')
+#
+#  >>> widget = CountryInputDropdown(boundCountry,
+#  ...   territoryVocabularyFactory(None), request)
+#
+#  >>> print widget()
+#  <div>
+#  <div class="value">
+#  <select id="field.country" name="field.country" size="1" >
+#  <option value="AF">Afghanistan</option>
+#  <option value="AL">Albania</option>
+#  <option value="DZ">Algeria</option>
+#  ...
+#  <option value="HU">Hungary</option>
+#  <option value="IS">Iceland</option>
+#  <option value="IN">India</option>
+#  ...
+#  <option value="ZM">Zambia</option>
+#  <option value="ZW">Zimbabwe</option>
+#  </select>
+#  ...


Property changes on: z3c.widget/trunk/src/z3c/widget/country/README.txt
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/country/SETUP.cfg
===================================================================
--- z3c.widget/trunk/src/z3c/widget/country/SETUP.cfg	2007-01-13 09:33:29 UTC (rev 71994)
+++ z3c.widget/trunk/src/z3c/widget/country/SETUP.cfg	2007-01-13 10:23:11 UTC (rev 71995)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+  z3c.widget.country-*.zcml
+</data-files>


Property changes on: z3c.widget/trunk/src/z3c/widget/country/SETUP.cfg
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/country/__init__.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/country/__init__.py	2007-01-13 09:33:29 UTC (rev 71994)
+++ z3c.widget/trunk/src/z3c/widget/country/__init__.py	2007-01-13 10:23:11 UTC (rev 71995)
@@ -0,0 +1,20 @@
+from zope.interface import implements
+
+from zope.schema import Choice
+from zope.schema.interfaces import IChoice
+                                    
+from z3c.i18n.iso import territoryVocabularyFactory
+
+class ICountry(IChoice):
+    pass
+
+class Country(Choice):
+    """Choice of countries"""
+    
+    implements(ICountry)
+    
+    def __init__(self, **kw):
+        #kw['vocabulary'] = sortedTerritoryVocabularyFactory(None)
+        kw['vocabulary'] = territoryVocabularyFactory(None)
+        
+        super(Country, self).__init__(**kw)


Property changes on: z3c.widget/trunk/src/z3c/widget/country/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/country/configure.zcml
===================================================================
--- z3c.widget/trunk/src/z3c/widget/country/configure.zcml	2007-01-13 09:33:29 UTC (rev 71994)
+++ z3c.widget/trunk/src/z3c/widget/country/configure.zcml	2007-01-13 10:23:11 UTC (rev 71995)
@@ -0,0 +1,14 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    xmlns:browser="http://namespaces.zope.org/browser">
+
+  <view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for=".ICountry
+           zope.schema.interfaces.IVocabularyTokenized"
+      provides="zope.app.form.interfaces.IInputWidget"
+      factory=".widget.CountryInputDropdown"
+      permission="zope.Public"
+      />
+
+</configure>


Property changes on: z3c.widget/trunk/src/z3c/widget/country/configure.zcml
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/country/tests.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/country/tests.py	2007-01-13 09:33:29 UTC (rev 71994)
+++ z3c.widget/trunk/src/z3c/widget/country/tests.py	2007-01-13 10:23:11 UTC (rev 71995)
@@ -0,0 +1,35 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation 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.
+#
+##############################################################################
+"""Optional Dropdown Widget Tests
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import doctest
+import unittest
+from zope.testing import doctestunit
+from zope.app.testing import placelesssetup
+
+def test_suite():
+    return unittest.TestSuite((
+        doctestunit.DocFileSuite(
+            'README.txt',
+            setUp=placelesssetup.setUp, tearDown=placelesssetup.tearDown,
+            globs={'pprint': doctestunit.pprint},
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
+


Property changes on: z3c.widget/trunk/src/z3c/widget/country/tests.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/country/widget.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/country/widget.py	2007-01-13 09:33:29 UTC (rev 71994)
+++ z3c.widget/trunk/src/z3c/widget/country/widget.py	2007-01-13 10:23:11 UTC (rev 71995)
@@ -0,0 +1,70 @@
+from zope.app.form.browser.itemswidgets import DropdownWidget
+from zope.app.form.browser.itemswidgets import SelectWidget
+from zope.app.form.browser.itemswidgets import RadioWidget
+
+def _renderItemsWithValues(self, values):
+    """Render the list of possible values, with those found in
+    `values` being marked as selected."""
+
+    cssClass = self.cssClass
+
+    # multiple items with the same value are not allowed from a
+    # vocabulary, so that need not be considered here
+    rendered_items = []
+    count = 0
+
+    # Handle case of missing value
+    missing = self._toFormValue(self.context.missing_value)
+
+    if self._displayItemForMissingValue and not self.context.required:
+        if missing in values:
+            render = self.renderSelectedItem
+        else:
+            render = self.renderItem
+
+        missing_item = render(count,
+            self.translate(self._messageNoValue),
+            missing,
+            self.name,
+            cssClass)
+        rendered_items.append(missing_item)
+        count += 1
+    
+    texts = sorted([(self.textForValue(term), term)
+        for term in self.vocabulary])
+
+    # Render normal values
+    for item_text, term in texts:
+        if term.value in values:
+            render = self.renderSelectedItem
+        else:
+            render = self.renderItem
+
+        rendered_item = render(count,
+            item_text,
+            term.token,
+            self.name,
+            cssClass)
+
+        rendered_items.append(rendered_item)
+        count += 1
+
+    return rendered_items
+
+class CountryInputDropdown(DropdownWidget):
+    def renderItemsWithValues(self, values):
+        """Render the list of possible values, with those found in
+        `values` being marked as selected."""
+        return _renderItemsWithValues(self, values)
+
+class CountryInputSelect(SelectWidget):
+    def renderItemsWithValues(self, values):
+        """Render the list of possible values, with those found in
+        `values` being marked as selected."""
+        return _renderItemsWithValues(self, values)
+
+class CountryInputRadio(RadioWidget):
+    def renderItemsWithValues(self, values):
+        """Render the list of possible values, with those found in
+        `values` being marked as selected."""
+        return _renderItemsWithValues(self, values)


Property changes on: z3c.widget/trunk/src/z3c/widget/country/widget.py
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/country/z3c.widget.country-configure.zcml
===================================================================
--- z3c.widget/trunk/src/z3c/widget/country/z3c.widget.country-configure.zcml	2007-01-13 09:33:29 UTC (rev 71994)
+++ z3c.widget/trunk/src/z3c/widget/country/z3c.widget.country-configure.zcml	2007-01-13 10:23:11 UTC (rev 71995)
@@ -0,0 +1 @@
+<include package="z3c.widget.country"/>
\ No newline at end of file


Property changes on: z3c.widget/trunk/src/z3c/widget/country/z3c.widget.country-configure.zcml
___________________________________________________________________
Name: svn:keywords
   + Date Author Id Revision
Name: svn:eol-style
   + native



More information about the Checkins mailing list