Commits

Andriy Kornatskyy committed 162a376

Added ability to format value before it being passed to widget for rendering

Comments (0)

Files changed (3)

 
 setup(
     name='wheezy.html',
-    version='0.1',
+    version='0.1.1',
     description='A lightweight html rendering library',
     long_description=README,
     url='https://bitbucket.org/akorn/wheezy.html',

src/wheezy/html/builder.py

 """ ``builder`` module.
 """
 
+from datetime import date
+from datetime import datetime
+
 from wheezy.html.comp import str_type
 from wheezy.html.markup import Tag
 from wheezy.html.widgets import default
         <span class="error">required</span>
     """
 
-    __slots__ = ['name', 'value', 'errors']
+    __slots__ = ['name', 'value', 'errors', 'formatted']
 
     def __init__(self, name, value, errors):
         self.name = name
         self.value = value
         self.errors = errors
+        self.formatted = None
+
+    def format(self, format_string=None, format_provider=None):
+        """ Formats widget value.
+
+            ``format_provider`` - a callable of the following form::
+
+                def my_formatter(value, format_string):
+                    return value_formatted
+
+            >>> h = WidgetBuilder('age', date(2012, 2, 6), None)
+            >>> h.format('%m-%d-%y')
+            '02-06-12'
+            >>> h = WidgetBuilder('age', date(2012, 2, 6), None)
+            >>> h.format(format_provider=lambda value, ignore:
+            ...         value.strftime('%m-%d-%y'))
+            '02-06-12'
+        """
+        if self.formatted is not None:
+            return self.formatted
+        value = self.value
+        if format_provider is None:
+            formatter_name = type(value).__name__
+            format_provider = format_providers[formatter_name]
+        self.formatted = value = format_provider(self.value, format_string)
+        return value
 
     def __repr__(self):
         """
             >>> h
             0
         """
-        return self.value
+        return self.format()
 
     def __getattr__(self, tag_name):
-        return Widget(tag_name, self.name, self.value,
+        return Widget(tag_name, self.name, self.format(),
                 self.errors)
 
     def error(self):
             })
         else:
             return ''
+
+
+str_format_provider = lambda value, format_string: str_type(value)
+
+
+def date_format_provider(value, format_string=None):
+    """ Default format provider for ``datetime.date``.
+
+        >>> date_format_provider(date.min)
+        ''
+        >>> date_format_provider(date(2012, 2, 6))
+        '2012/02/06'
+    """
+    if date.min == value:
+        return ''
+    return value.strftime(format_string or '%Y/%m/%d')
+
+
+def datetime_format_provider(value, format_string=None):
+    """ Default format provider for ``datetime.datetime``.
+
+        >>> datetime_format_provider(datetime.min)
+        ''
+        >>> datetime_format_provider(datetime(2012, 2, 6, 15, 17))
+        '2012/02/06 15:17'
+    """
+    if datetime.min == value:
+        return ''
+    return value.strftime(format_string or '%Y/%m/%d %H:%M')
+
+
+format_providers = {
+        'str': lambda value, format_string: html_escape(str_type(value)),
+        'unicode': lambda value, format_string: html_escape(value),
+        'int': str_format_provider,
+        'Decimal': str_format_provider,
+        'bool': str_format_provider,
+        'float': str_format_provider,
+        'date': date_format_provider,
+        'time': lambda value, format_string: value.strftime(
+            format_string or '%H:%M'),
+        'datetime': datetime_format_provider
+}

src/wheezy/html/factory.py

         try:
             return self.builders[name]
         except KeyError:
-            value = html_escape(str_type(getattr(self.model, name)))
+            value = getattr(self.model, name)
             builder = WidgetBuilder(name, value, self.errors.get(name, None))
             self.builders[name] = builder
             return builder