Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #474 (closed defect: fixed)

Opened 13 years ago

Last modified 12 years ago

[PATCH] Fixes on widgets and validators

Reported by: jpaulofarias at gmail dot com Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TG Widgets Version:
Severity: normal Keywords:


I've fixed the CalendarDatePicker? widget to use the format variable when it shows the value. Also a fix on DateTimeConverter? where the passed value is empty and allow_empty is False. Something weird happened when I asked tortoisesvn to create the patch, it is deleting and adding exactly same lines. I dunno if I can remove those lines, so I left it as it was created.

Inlined bellow:

Index: turbogears/validators.py
--- turbogears/validators.py	(revision 584)
+++ turbogears/validators.py	(working copy)
@@ -13,27 +13,30 @@
 class DateTimeConverter(FancyValidator):
     messages = {
         'badFormat': 'Invalid datetime format',
+        'empty': 'Empty values not allowed',
     def __init__(self, format = "%Y/%m/%d %H:%M", allow_empty=False, *args, **kwargs):
         self.format = format
-        self.allow_empty = allow_empty
+        self.allow_empty = allow_empty
     def _to_python(self, value, state):
-        """ parse a string and return a datetime object. """
-        if self.allow_empty and not value:
-            return None
+        """ parse a string and return a datetime object. """
+        if self.allow_empty and not value:
+            return None
+        elif not self.allow_empty and not value:
+            raise Invalid(self.message('empty', state), value, state)
                 tpl = time.strptime(value, self.format)
             except ValueError:
-                raise Invalid(self.message('badFormat', state), value, state)
+                raise Invalid(self.message('badFormat', state), value, state)
             timestamp = time.mktime(tpl)
-            return datetime.fromtimestamp(timestamp)
+            return datetime.fromtimestamp(timestamp)
-    def _from_python(self, value, state):
-        if self.allow_empty and not value:
-            return None
+    def _from_python(self, value, state):
+        if self.allow_empty and not value:
+            return None
             return value.strftime(self.format)
Index: turbogears/widgets/base.py
--- turbogears/widgets/base.py	(revision 584)
+++ turbogears/widgets/base.py	(working copy)
@@ -534,7 +534,7 @@
                 JSLink(static, "calendar-setup.js")]
     template = """
     <div xmlns:py="http://purl.org/kid/ns#" py:strip="True">
-    <input type="text" id="${widget.name}" name="${widget.name}" value="${widget_value}" class="date_field"/>
+    <input type="text" id="${widget.name}" name="${widget.name}" value="${widget_value.strftime(widget.format)}" class="date_field"/>
     <button id="${widget.name}-trigger" class="date_field_button">Choose</button>
     <script type="text/javascript">
@@ -556,14 +556,14 @@
         Use a javascript calendar system to allow picking of calendar dates.
         The format is in mm/dd/yyyy unless otherwise specified
-        '''
+        '''
         if default is not None or self.allow_empty:
             self._default = default
         self.format = format
-        self.picker_shows_time = picker_shows_time
+        self.picker_shows_time = picker_shows_time
-        self.validator = DateTimeConverter(format=format, allow_empty=self.allow_empty)
+        self.validator = validators.DateTimeConverter(format=format, allow_empty=self.allow_empty)
         super(CalendarDatePicker, self).__init__(name, **kw)
     def _get_default(self):

Change History

comment:1 Changed 13 years ago by Max <ischenko@…>

  • Status changed from new to closed
  • Resolution set to fixed

Applied, more-or-less completely, while fixing other TG issues.

Note: See TracTickets for help on using tickets.