[Mpuls-commits] r3143 - in base/trunk: . mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Jul 6 13:15:02 CEST 2010


Author: bh
Date: 2010-07-06 13:15:00 +0200 (Tue, 06 Jul 2010)
New Revision: 3143

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/lib/helpers.py
Log:
* mpulsweb/lib/helpers.py (format_object): New.  Generic object
formatter for use in templates where the type of the object to
render is may vary.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2010-07-06 10:40:34 UTC (rev 3142)
+++ base/trunk/ChangeLog	2010-07-06 11:15:00 UTC (rev 3143)
@@ -1,3 +1,9 @@
+2010-07-06  Bernhard Herzog  <bh at intevation.de>
+
+	* mpulsweb/lib/helpers.py (format_object): New.  Generic object
+	formatter for use in templates where the type of the object to
+	render is may vary.
+
 2010-07-06  Torsten Irländer <torsten.irlaender at intevation.de>
 
 	* mpulsweb/lib/renderer.py (ViewRenderer._renderInt.uformat_int):

Modified: base/trunk/mpulsweb/lib/helpers.py
===================================================================
--- base/trunk/mpulsweb/lib/helpers.py	2010-07-06 10:40:34 UTC (rev 3142)
+++ base/trunk/mpulsweb/lib/helpers.py	2010-07-06 11:15:00 UTC (rev 3143)
@@ -294,6 +294,29 @@
     return " ".join(out)
 
 
+# formatters for the format_object funtion.
+format_object_handlers = [
+    (datetime.date, format_date),
+    (datetime.datetime, format_datetime),
+    (datetime.time, format_time),
+    (type(None), lambda obj: ''),
+    ]
+
+def format_object(obj):
+    """Return a string representation of obj suitable for the user.
+    Date/time objects are formatted with the appropriate format_date
+    function.  None objects are converted to an empty string.  Objects
+    that are already strings (bytestrings or unicode) are returned as
+    is.  Other objects are converted using str().
+    """
+    for typeobj, handler in format_object_handlers:
+        if isinstance(obj, typeobj):
+            return handler(obj)
+    if isinstance(obj, basestring):
+        return obj
+    return str(obj)
+
+
 # Fallback functions for the localized format_* functions. If localalisation
 # fails because of dates before 1900 (which aren't sensefull anyway') the
 # fallback functions are called. These functions will convert dates and time in



More information about the Mpuls-commits mailing list