move parse_datetime on appobject
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 28 May 2009 18:28:29 +0200
changeset 1984 8d571df6012a
parent 1983 5edd22d0e61a
child 1985 9c1db4e06095
move parse_datetime on appobject
appobject.py
web/controller.py
--- a/appobject.py	Thu May 28 18:28:05 2009 +0200
+++ b/appobject.py	Thu May 28 18:28:29 2009 +0200
@@ -301,6 +301,35 @@
             return self.req.property_value('ui.float-format') % num
         return u''
 
+    def parse_datetime(self, value, etype='Datetime'):
+        """get a datetime or time from a string (according to etype)
+        Datetime formatted as Date are accepted
+        """
+        assert etype in ('Datetime', 'Date', 'Time'), etype
+        # XXX raise proper validation error
+        if etype == 'Datetime':
+            format = self.req.property_value('ui.datetime-format')
+            try:
+                return todatetime(strptime(value, format))
+            except:
+                pass
+        elif etype == 'Time':
+            format = self.req.property_value('ui.time-format')
+            try:
+                # (adim) I can't find a way to parse a Time with a custom format
+                date = strptime(value, format) # this returns a DateTime
+                return datetime.time(date.hour, date.minute, date.second)
+            except:
+                raise ValueError('can\'t parse %r (expected %s)' % (value, format))
+        try:
+            format = self.req.property_value('ui.date-format')
+            dt = strptime(value, format)
+            if etype == 'Datetime':
+                return todatetime(dt)
+            return todate(dt)
+        except:
+            raise ValueError('can\'t parse %r (expected %s)' % (value, format))
+
     # security related methods ################################################
 
     def ensure_ro_rql(self, rql):
--- a/web/controller.py	Thu May 28 18:28:05 2009 +0200
+++ b/web/controller.py	Thu May 28 18:28:29 2009 +0200
@@ -108,35 +108,6 @@
             raise RequestError('missing required parameter(s): %s'
                                % ','.join(missing))
 
-    def parse_datetime(self, value, etype='Datetime'):
-        """get a datetime or time from a string (according to etype)
-        Datetime formatted as Date are accepted
-        """
-        assert etype in ('Datetime', 'Date', 'Time'), etype
-        # XXX raise proper validation error
-        if etype == 'Datetime':
-            format = self.req.property_value('ui.datetime-format')
-            try:
-                return todatetime(strptime(value, format))
-            except:
-                pass
-        elif etype == 'Time':
-            format = self.req.property_value('ui.time-format')
-            try:
-                # (adim) I can't find a way to parse a Time with a custom format
-                date = strptime(value, format) # this returns a DateTime
-                return datetime.time(date.hour, date.minute, date.second)
-            except:
-                raise ValueError('can\'t parse %r (expected %s)' % (value, format))
-        try:
-            format = self.req.property_value('ui.date-format')
-            dt = strptime(value, format)
-            if etype == 'Datetime':
-                return todatetime(dt)
-            return todate(dt)
-        except:
-            raise ValueError('can\'t parse %r (expected %s)' % (value, format))
-
 
     def notify_edited(self, entity):
         """called by edit_entity() to notify which entity is edited"""