[Zope3-checkins] CVS: Zope3/src/zope/publisher - browser.py:1.9

Tres Seaver tseaver@zope.com
Thu, 13 Feb 2003 12:46:51 -0500


Update of /cvs-repository/Zope3/src/zope/publisher
In directory cvs.zope.org:/tmp/cvs-serv4623/src/zope/publisher

Modified Files:
	browser.py 
Log Message:


  - src/zope/publisher/browser.py:

    o Remove dependency on 'zope.app.datetimeutils';  the 'field2date'
      converter is now in 'zope.app.publisher.fieldconverters'.

    o Add an API for registering / overriding field converters from
      application code.

  - src/zope/app/publisher/fieldconverters.py:

    o New home for the 'field2date' converter: it is *not available by
      default*, but must be installed by application code which needs
      it, via the 'registerZopeConverters' API.

      * This choice avoids prescribing a US-centric date/time parsing
        scheme by default.

      * Example application code which activates this converter::

        from zope.app.publisher.zopeconverters import registerZopeConverters
        registerZopeConverters()


=== Zope3/src/zope/publisher/browser.py 1.8 => 1.9 ===
--- Zope3/src/zope/publisher/browser.py:1.8	Thu Feb 13 11:48:43 2003
+++ Zope3/src/zope/publisher/browser.py	Thu Feb 13 12:46:20 2003
@@ -21,7 +21,6 @@
 from cgi import FieldStorage, escape
 from datetime import datetime
 
-from zope.app.datetimeutils import parse as parseDateTime
 from zope.i18n.interfaces import IUserPreferredLanguages
 from zope.i18n.interfaces import IUserPreferredCharsets
 from zope.publisher.interfaces.browser import IBrowserPresentation
@@ -146,32 +145,6 @@
         return result
     return field2text(v).split('\n')
 
-def field2date(v):
-    if hasattr(v,'read'):
-        v = v.read()
-    else:
-        v = str(v)
-
-    # *Don't* force a timezone if not passed explicitly;  leave it as
-    # "naive" datetime.
-    year, month, day, hour, minute, second, tzname = parseDateTime(v, local=0)
-
-    # TODO:  look up a real tzinfo object using 'tzname'
-    #
-    # Option 1:  Use 'timezones' module as global registry::
-    #
-    #   from zope.app.timezones import getTimezoneInfo
-    #   tzinfo = getTimezoneInfo(tzname)
-    #
-    # Option 2:  Use a utility.
-    #
-    #   tz_lookup = getUtility(None, ITimezoneLookup)
-    #   tzinfo = tz_lookup(tzname)
-    #
-    return datetime(year, month, day, hour, minute, second,
-                  # tzinfo=tzinfo
-                   )
-
 def field2boolean(v):
     return v
 
@@ -180,7 +153,6 @@
     'int':      field2int,
     'long':     field2long,
     'string':   field2string,
-    'date':     field2date,
     'required': field2required,
     'tokens':   field2tokens,
     'lines':    field2lines,
@@ -189,6 +161,20 @@
     }
 
 get_converter=type_converters.get
+
+def registerTypeConverter(field_type, converter, replace=False):
+    """Add a custom type converter to the registry.
+
+    o If 'replace' is not true, raise a KeyError if a converter is
+      already registered for 'field_type'.
+    """
+    existing = type_converters.get(field_type)
+
+    if existing is not None and not replace:
+        raise KeyError, 'Existing converter for field_type: %s' % field_type
+
+    type_converters[field_type] = converter
+
 
 isCGI_NAME = {
     # These fields are placed in request.environ instead of request.form.