[Checkins] SVN: z3c.currency/trunk/ the code for the project.

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Jun 25 18:56:42 EDT 2007


Log message for revision 77083:
  the code for the project.
  

Changed:
  A   z3c.currency/trunk/
  A   z3c.currency/trunk/.installed.cfg
  A   z3c.currency/trunk/AUTHOR.txt
  A   z3c.currency/trunk/CHANGES.txt
  A   z3c.currency/trunk/LICENSE.txt
  A   z3c.currency/trunk/README.txt
  A   z3c.currency/trunk/TODOS.txt
  A   z3c.currency/trunk/bootstrap.py
  A   z3c.currency/trunk/buildout.cfg
  A   z3c.currency/trunk/setup.py
  A   z3c.currency/trunk/src/
  A   z3c.currency/trunk/src/z3c/
  A   z3c.currency/trunk/src/z3c/__init__.py
  A   z3c.currency/trunk/src/z3c/currency/
  A   z3c.currency/trunk/src/z3c/currency/README.txt
  A   z3c.currency/trunk/src/z3c/currency/SETUP.cfg
  A   z3c.currency/trunk/src/z3c/currency/__init__.py
  A   z3c.currency/trunk/src/z3c/currency/configure.zcml
  A   z3c.currency/trunk/src/z3c/currency/converter.py
  A   z3c.currency/trunk/src/z3c/currency/field.py
  A   z3c.currency/trunk/src/z3c/currency/i18n.py
  A   z3c.currency/trunk/src/z3c/currency/interfaces.py
  A   z3c.currency/trunk/src/z3c/currency/tests.py
  A   z3c.currency/trunk/src/z3c/currency/z3c.currency-configure.zcml

-=-
Added: z3c.currency/trunk/.installed.cfg
===================================================================
--- z3c.currency/trunk/.installed.cfg	                        (rev 0)
+++ z3c.currency/trunk/.installed.cfg	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,28 @@
+[buildout]
+installed_develop_eggs = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/develop-eggs/z3c.currency.egg-link
+parts = test
+
+[test]
+__buildout_installed__ = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/parts/test
+	/opt/zope/sr/z3cForm/trunk/packages/z3c.currency/bin/test
+__buildout_signature__ = zc.recipe.testrunner-1.0.0b7-py2.4.egg zc.recipe.egg-1.0.0b6-py2.4.egg setuptools-0.6c6-py2.4.egg zope.testing-3.4-py2.4.egg zc.buildout-1.0.0b27-py2.4.egg zc.buildout-1.0.0b27-py2.4.egg
+_b = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/bin
+_d = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/develop-eggs
+_e = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/eggs
+bin-directory = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/bin
+develop-eggs-directory = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/develop-eggs
+eggs = z3c.currency [test]
+eggs-directory = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/eggs
+executable = /usr/bin/py24
+location = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/parts/test
+recipe = zc.recipe.testrunner
+script = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/bin/test
+
+[buildout]
+installed_develop_eggs = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/develop-eggs/z3c.currency.egg-link
+
+[buildout]
+installed_develop_eggs = /opt/zope/sr/z3cForm/trunk/packages/z3c.currency/develop-eggs/z3c.currency.egg-link
+
+[buildout]
+parts = test

Added: z3c.currency/trunk/AUTHOR.txt
===================================================================
--- z3c.currency/trunk/AUTHOR.txt	                        (rev 0)
+++ z3c.currency/trunk/AUTHOR.txt	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,3 @@
+Stephan Richter (stephan.richter <at> gmail.com)
+
+Thanks goes to Bayview Financial for supporting this work.


Property changes on: z3c.currency/trunk/AUTHOR.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.currency/trunk/CHANGES.txt
===================================================================
--- z3c.currency/trunk/CHANGES.txt	                        (rev 0)
+++ z3c.currency/trunk/CHANGES.txt	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,12 @@
+=======
+CHANGES
+=======
+
+Version 0.1.0 (6/??/2007)
+-------------------------
+
+- Initial Release
+
+  * Implementation of ``Currency`` field supporting precision and a unit.
+
+  * Implementation of data converter.


Property changes on: z3c.currency/trunk/CHANGES.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.currency/trunk/LICENSE.txt
===================================================================
--- z3c.currency/trunk/LICENSE.txt	                        (rev 0)
+++ z3c.currency/trunk/LICENSE.txt	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,54 @@
+Zope Public License (ZPL) Version 2.1
+-------------------------------------
+
+A copyright notice accompanies this license document that
+identifies the copyright holders.
+
+This license has been certified as open source. It has also
+been designated as GPL compatible by the Free Software
+Foundation (FSF).
+
+Redistribution and use in source and binary forms, with or
+without modification, are permitted provided that the
+following conditions are met:
+
+1. Redistributions in source code must retain the
+   accompanying copyright notice, this list of conditions,
+   and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the accompanying
+   copyright notice, this list of conditions, and the
+   following disclaimer in the documentation and/or other
+   materials provided with the distribution.
+
+3. Names of the copyright holders must not be used to
+   endorse or promote products derived from this software
+   without prior written permission from the copyright
+   holders.
+
+4. The right to distribute this software or to use it for
+   any purpose does not give you the right to use
+   Servicemarks (sm) or Trademarks (tm) of the copyright
+   holders. Use of them is covered by separate agreement
+   with the copyright holders.
+
+5. If any files are modified, you must cause the modified
+   files to carry prominent notices stating that you changed
+   the files and the date of any change.
+
+Disclaimer
+
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS''
+  AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
+  NO EVENT SHALL THE COPYRIGHT HOLDERS BE
+  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+  DAMAGE.


Property changes on: z3c.currency/trunk/LICENSE.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.currency/trunk/README.txt
===================================================================
--- z3c.currency/trunk/README.txt	                        (rev 0)
+++ z3c.currency/trunk/README.txt	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,2 @@
+A package implementing a currency field for ``zope.schema`` and support for
+using the field with ``z3c.form``.


Property changes on: z3c.currency/trunk/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.currency/trunk/TODOS.txt
===================================================================
--- z3c.currency/trunk/TODOS.txt	                        (rev 0)
+++ z3c.currency/trunk/TODOS.txt	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,3 @@
+=====
+TODOS
+=====


Property changes on: z3c.currency/trunk/TODOS.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.currency/trunk/bootstrap.py
===================================================================
--- z3c.currency/trunk/bootstrap.py	                        (rev 0)
+++ z3c.currency/trunk/bootstrap.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,52 @@
+##############################################################################
+#
+# Copyright (c) 2007 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.
+#
+##############################################################################
+"""Bootstrap a buildout-based project
+
+Simply run this script in a directory containing a buildout.cfg.
+The script accepts buildout command-line options, so you can
+use the -c option to specify an alternate configuration file.
+
+$Id$
+"""
+
+import os, shutil, sys, tempfile, urllib2
+
+tmpeggs = tempfile.mkdtemp()
+
+ez = {}
+exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
+                     ).read() in ez
+ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
+
+import pkg_resources
+
+cmd = 'from setuptools.command.easy_install import main; main()'
+if sys.platform == 'win32':
+    cmd = '"%s"' % cmd # work around spawn lamosity on windows
+
+ws = pkg_resources.working_set
+assert os.spawnle(
+    os.P_WAIT, sys.executable, sys.executable,
+    '-c', cmd, '-mqNxd', tmpeggs, 'zc.buildout',
+    dict(os.environ,
+         PYTHONPATH=
+         ws.find(pkg_resources.Requirement.parse('setuptools')).location
+         ),
+    ) == 0
+
+ws.add_entry(tmpeggs)
+ws.require('zc.buildout')
+import zc.buildout.buildout
+zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
+shutil.rmtree(tmpeggs)


Property changes on: z3c.currency/trunk/bootstrap.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/buildout.cfg
===================================================================
--- z3c.currency/trunk/buildout.cfg	                        (rev 0)
+++ z3c.currency/trunk/buildout.cfg	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,7 @@
+[buildout]
+develop = .
+parts = test
+
+[test]
+recipe = zc.recipe.testrunner
+eggs = z3c.currency [test]

Added: z3c.currency/trunk/setup.py
===================================================================
--- z3c.currency/trunk/setup.py	                        (rev 0)
+++ z3c.currency/trunk/setup.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,70 @@
+##############################################################################
+#
+# Copyright (c) 2007 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.
+#
+##############################################################################
+"""Setup
+
+$Id$
+"""
+import os
+from setuptools import setup, find_packages
+
+def read(*rnames):
+    return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
+
+setup (
+    name='z3c.currency',
+    version='0.1.0',
+    author = "Stephan Richter and the Zope Community",
+    author_email = "zope3-dev at zope.org",
+    description = "A currency field and support for ``z3c.form``.",
+    long_description=(
+        read('README.txt')
+        + '\n\n' +
+        'Detailed Documentation\n'
+        '**********************\n'
+        + '\n' +
+        read('src', 'z3c', 'currency', 'README.txt')
+        + '\n\n' +
+        read('CHANGES.txt')
+        ),
+    license = "ZPL 2.1",
+    keywords = "zope3 currency schema field widget form",
+    classifiers = [
+        'Development Status :: 4 - Beta',
+        'Environment :: Web Environment',
+        'Intended Audience :: Developers',
+        'License :: OSI Approved :: Zope Public License',
+        'Programming Language :: Python',
+        'Natural Language :: English',
+        'Operating System :: OS Independent',
+        'Topic :: Internet :: WWW/HTTP',
+        'Framework :: Zope3'],
+    url = 'http://svn.zope.org/z3c.currency',
+    packages = find_packages('src'),
+    include_package_data = True,
+    package_dir = {'':'src'},
+    namespace_packages = ['z3c'],
+    extras_require = dict(
+        test = ['z3c.form', 'zope.testing'],
+        form = ['z3c.form'],
+        ),
+    install_requires = [
+        'setuptools',
+        'zope.i18n',
+        'zope.i18nmessageid',
+        'zope.interface',
+        'zope.schema',
+        ],
+    dependency_links = ['http://download.zope.org/distribution'],
+    zip_safe = False,
+    )


Property changes on: z3c.currency/trunk/setup.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/src/z3c/__init__.py
===================================================================
--- z3c.currency/trunk/src/z3c/__init__.py	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/__init__.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,7 @@
+try:
+    # Declare this a namespace package if pkg_resources is available.
+    import pkg_resources
+    pkg_resources.declare_namespace('z3c')
+except ImportError:
+    pass
+


Property changes on: z3c.currency/trunk/src/z3c/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/src/z3c/currency/README.txt
===================================================================
--- z3c.currency/trunk/src/z3c/currency/README.txt	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/README.txt	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,152 @@
+==================
+The Currency Field
+==================
+
+The currency field is a numerical field, specifically designed to manage
+monetary values.
+
+  >>> from z3c.currency import field, interfaces
+
+  >>> price = field.Currency(
+  ...    title=u'Price',
+  ...    description=u'The price of the item.')
+
+Besides the common attributes, the currency field also provides two additional
+attributes, the precision and unit. The precision is intended to allow for
+specifying whether the value is provided whole units or 1/100th of the whole
+unit -- in the US dollar and cents. By default this field is set to cents:
+
+  >>> price.precision is interfaces.CENTS
+  True
+
+It can be set to be dollars
+
+  >>> price.precision = interfaces.DOLLARS
+  >>> price.precision is interfaces.DOLLARS
+  True
+
+Note: Is there a more "internationalized" word for the whole unit of a
+currency?
+
+The unit specifies the symbol of the currency used. It is also used for
+formatting the numerical value to a string.
+
+  >>> price.unit
+  u'$'
+  >>> price.unit = u'€'
+  >>> price.unit
+  u'\xe2\x82\xac'
+
+Of course, both of those attributes are available as constructor arguments:
+
+  >>> price = field.Currency(
+  ...    title=u'Price',
+  ...    description=u'The price of the item.',
+  ...    precision=interfaces.DOLLARS,
+  ...    unit=u'€')
+
+  >>> price.precision is interfaces.DOLLARS
+  True
+  >>> price.unit
+  u'\xe2\x82\xac'
+
+Let's now have a look at the validation. First of all, the value must always
+be a decimal:
+
+  >>> import decimal
+  >>> price.validate(decimal.Decimal('12'))
+
+  >>> price.validate(12)
+  Traceback (most recent call last):
+  ...
+  ValidationError: Value must be of type 'Decimal', not 'int'.
+
+  >>> price.validate(12.0)
+  Traceback (most recent call last):
+  ...
+  ValidationError: Value must be of type 'Decimal', not 'float'.
+
+Also, when the precision is set to DOLLARS as it is the case here, the value
+must be a whole number:
+
+  >>> price.validate(decimal.Decimal('12'))
+
+  >>> price.validate(decimal.Decimal('12.01'))
+  Traceback (most recent call last):
+  ...
+  ValidationError: The value must be a whole number.
+
+  >>> price.validate(decimal.Decimal('12.00'))
+  Traceback (most recent call last):
+  ...
+  ValidationError: The value must be a whole number.
+
+When the precision is set to cents, ...
+
+  >>> price.precision = interfaces.CENTS
+
+... then values only with two decimal places are accepted:
+
+  >>> price.validate(decimal.Decimal('12.00'))
+
+  >>> price.validate(decimal.Decimal('12'))
+  Traceback (most recent call last):
+  ...
+  ValidationError: The value must have two decimal places.
+
+  >>> price.validate(decimal.Decimal('12.0'))
+  Traceback (most recent call last):
+  ...
+  ValidationError: The value must have two decimal places.
+
+Note that the ``IFromUnicode`` interface is purposefully not supported:
+
+  >>> price.fromUnicode
+  Traceback (most recent call last):
+  ...
+  AttributeError: 'Currency' object has no attribute 'fromUnicode'
+
+
+``z3c.form`` Support
+--------------------
+
+This package also provides support for integration with the ``z3c.form``
+package. In particular it implements a data converter from the ``Currency``
+field to any widget accepting a unicode string.
+
+  >>> from z3c.currency import converter
+  >>> conv = converter.CurrencyConverter(price, None)
+  >>> conv
+  <DataConverter from Currency to NoneType>
+
+The converter easily produces a string from any value:
+
+  >>> conv.toWidgetValue(decimal.Decimal(12))
+  u'12'
+  >>> conv.toWidgetValue(decimal.Decimal(1200))
+  u'1,200'
+  >>> conv.toWidgetValue(decimal.Decimal(-12))
+  u'-12'
+  >>> conv.toWidgetValue(decimal.Decimal('-12.0'))
+  u'-12.00'
+  >>> conv.toWidgetValue(decimal.Decimal('-12.00'))
+  u'-12.00'
+
+Note that always two decimal places are printed.
+
+Let's now parse a value. The parser is a little bit for flexible, not only
+accepting the output values, ...
+
+  >>> conv.toFieldValue(u'12')
+  Decimal("12")
+  >>> conv.toFieldValue(u'1,200')
+  Decimal("1200")
+  >>> conv.toFieldValue(u'-12')
+  Decimal("-12")
+  >>> conv.toFieldValue(u'-12.00')
+  Decimal("-12.00")
+
+but also other input values:
+
+  >>> conv.toFieldValue(u'1200')
+  Decimal("1200")


Property changes on: z3c.currency/trunk/src/z3c/currency/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.currency/trunk/src/z3c/currency/SETUP.cfg
===================================================================
--- z3c.currency/trunk/src/z3c/currency/SETUP.cfg	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/SETUP.cfg	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+  z3c.currency-*.zcml
+</data-files>

Added: z3c.currency/trunk/src/z3c/currency/__init__.py
===================================================================
--- z3c.currency/trunk/src/z3c/currency/__init__.py	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/__init__.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1 @@
+# Make a package.


Property changes on: z3c.currency/trunk/src/z3c/currency/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/src/z3c/currency/configure.zcml
===================================================================
--- z3c.currency/trunk/src/z3c/currency/configure.zcml	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/configure.zcml	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,8 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope">
+
+  <adapter
+      factory=".converter.CurrencyConverter"
+      />
+
+</configure>


Property changes on: z3c.currency/trunk/src/z3c/currency/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.currency/trunk/src/z3c/currency/converter.py
===================================================================
--- z3c.currency/trunk/src/z3c/currency/converter.py	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/converter.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,56 @@
+##############################################################################
+#
+# Copyright (c) 2007 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.
+#
+##############################################################################
+"""Currency Field to Text-based widget implementation
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import decimal
+import zope.component
+import zope.interface
+from z3c.form.interfaces import IDataConverter, IWidget
+from z3c.currency import interfaces
+from zope.i18n import format
+
+class CurrencyConverter(object):
+    """Converts currency fields to text representations."""
+    zope.component.adapts(interfaces.ICurrency, IWidget)
+    zope.interface.implements(IDataConverter)
+
+    inputPatterns = ('#,##0.00;-#,##0.00', '#,##0;-#,##0',)
+    outputPattern = '#,##0.00;-#,##0.00'
+
+    def __init__(self, field, widget):
+        self.field = field
+        self.widget = widget
+
+    def toWidgetValue(self, value):
+        """See interfaces.IDataConverter"""
+        formatter = format.NumberFormat(self.outputPattern)
+        return formatter.format(value)
+
+    def toFieldValue(self, value):
+        """See interfaces.IDataConverter"""
+        formatter = format.NumberFormat()
+        formatter.type = decimal.Decimal
+        for pattern in self.inputPatterns:
+            try:
+                return formatter.parse(value, pattern)
+            except (format.NumberParseError, ValueError):
+                pass
+        raise ValueError('Could not parse %r.' %value)
+
+    def __repr__(self):
+        return '<DataConverter from %s to %s>' %(
+            self.field.__class__.__name__, self.widget.__class__.__name__)


Property changes on: z3c.currency/trunk/src/z3c/currency/converter.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/src/z3c/currency/field.py
===================================================================
--- z3c.currency/trunk/src/z3c/currency/field.py	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/field.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,51 @@
+##############################################################################
+#
+# Copyright (c) 2007 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.
+#
+##############################################################################
+"""Currency Field implementation
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import decimal
+import zope.interface
+import zope.schema
+from zope.schema.fieldproperty import FieldProperty
+
+from z3c.currency import interfaces
+
+class Currency(zope.schema.Orderable, zope.schema.Field):
+    zope.interface.implements(interfaces.ICurrency)
+
+    precision = FieldProperty(interfaces.ICurrency['precision'])
+    unit = FieldProperty(interfaces.ICurrency['unit'])
+
+    def __init__(self, precision=interfaces.CENTS, unit=u'$', *args, **kw):
+        super(Currency, self).__init__(*args, **kw)
+        self.precision = precision
+        self.unit = unit
+
+    def validate(self, value):
+        if not isinstance(value, decimal.Decimal):
+            raise zope.schema.ValidationError(
+                "Value must be of type 'Decimal', not '%s'." % (
+                type(value).__name__) )
+
+        if self.precision is interfaces.DOLLARS and value.as_tuple()[-1] != 0:
+            raise zope.schema.ValidationError(
+                "The value must be a whole number.")
+
+        if self.precision is interfaces.CENTS and value.as_tuple()[-1] != -2:
+            raise zope.schema.ValidationError(
+                "The value must have two decimal places.")
+
+        super(Currency, self).validate(value)


Property changes on: z3c.currency/trunk/src/z3c/currency/field.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/src/z3c/currency/i18n.py
===================================================================
--- z3c.currency/trunk/src/z3c/currency/i18n.py	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/i18n.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,22 @@
+##############################################################################
+#
+# Copyright (c) 2007 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.
+#
+##############################################################################
+"""I18n message factory z3c.currency
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import zope.i18nmessageid
+
+MessageFactory = zope.i18nmessageid.MessageFactory('z3c.currency')


Property changes on: z3c.currency/trunk/src/z3c/currency/i18n.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/src/z3c/currency/interfaces.py
===================================================================
--- z3c.currency/trunk/src/z3c/currency/interfaces.py	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/interfaces.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,40 @@
+##############################################################################
+#
+# Copyright (c) 2007 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.
+#
+##############################################################################
+"""Currency Field interfaces
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import zope.schema
+from zope.schema import interfaces
+
+from z3c.currency.i18n import MessageFactory as _
+
+# Precisions
+DOLLARS = 0
+CENTS = 1
+
+class ICurrency(interfaces.IMinMax, interfaces.IField):
+
+    precision = zope.schema.Int(
+        title=_('Precision'),
+        description=_('The precision in which the amount is kept.'),
+        default=CENTS,
+        required=True)
+
+    unit = zope.schema.TextLine(
+        title=_('Unit'),
+        description=_('The unit in which the currency amount is kept.'),
+        default=u'$',
+        required=True)


Property changes on: z3c.currency/trunk/src/z3c/currency/interfaces.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/src/z3c/currency/tests.py
===================================================================
--- z3c.currency/trunk/src/z3c/currency/tests.py	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/tests.py	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1,32 @@
+##############################################################################
+#
+# Copyright (c) 2005 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.
+#
+##############################################################################
+"""Currency Test Setup
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import unittest
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+
+def test_suite():
+    return unittest.TestSuite((
+        DocFileSuite('README.txt',
+                     optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+                     ),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.currency/trunk/src/z3c/currency/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.currency/trunk/src/z3c/currency/z3c.currency-configure.zcml
===================================================================
--- z3c.currency/trunk/src/z3c/currency/z3c.currency-configure.zcml	                        (rev 0)
+++ z3c.currency/trunk/src/z3c/currency/z3c.currency-configure.zcml	2007-06-25 22:56:42 UTC (rev 77083)
@@ -0,0 +1 @@
+<include package="z3c.currency" />


Property changes on: z3c.currency/trunk/src/z3c/currency/z3c.currency-configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list