Fix bug about missing Interval field (closes #1463996). stable
authorDamien Garaud <damien.garaud@logilab.fr>
Mon, 20 Feb 2012 11:45:16 +0100
branchstable
changeset 8248 9550555e4c26
parent 8247 65b0d2587fb5
child 8249 c59c05c51321
child 8252 3e769d21f67a
Fix bug about missing Interval field (closes #1463996). Implement Field and Widget dedicated to the yams entity 'Interval': - TimeIntervalField - IntervalTimeWidget Allow to set an integer with a character as a period of time '20s', '48h' or '3d' for instance (turn into seconds).
i18n/de.po
i18n/en.po
i18n/es.po
i18n/fr.po
web/formfields.py
--- a/i18n/de.po	Wed Feb 15 18:12:38 2012 +0100
+++ b/i18n/de.po	Mon Feb 20 11:45:16 2012 +0100
@@ -962,6 +962,9 @@
 msgid "a float is expected"
 msgstr "Eine Dezimalzahl (float) wird erwartet."
 
+msgid "a number (in seconds) or 20s, 10min, 24h or 4d are expected"
+msgstr ""
+
 msgid ""
 "a simple cache entity characterized by a name and a validity date. The "
 "target application is responsible for updating timestamp when necessary to "
--- a/i18n/en.po	Wed Feb 15 18:12:38 2012 +0100
+++ b/i18n/en.po	Mon Feb 20 11:45:16 2012 +0100
@@ -922,6 +922,9 @@
 msgid "a float is expected"
 msgstr ""
 
+msgid "a number (in seconds) or 20s, 10min, 24h or 4d are expected"
+msgstr ""
+
 msgid ""
 "a simple cache entity characterized by a name and a validity date. The "
 "target application is responsible for updating timestamp when necessary to "
--- a/i18n/es.po	Wed Feb 15 18:12:38 2012 +0100
+++ b/i18n/es.po	Mon Feb 20 11:45:16 2012 +0100
@@ -967,6 +967,9 @@
 msgid "a float is expected"
 msgstr "un nĂºmero flotante es requerido"
 
+msgid "a number (in seconds) or 20s, 10min, 24h or 4d are expected"
+msgstr ""
+
 msgid ""
 "a simple cache entity characterized by a name and a validity date. The "
 "target application is responsible for updating timestamp when necessary to "
--- a/i18n/fr.po	Wed Feb 15 18:12:38 2012 +0100
+++ b/i18n/fr.po	Mon Feb 20 11:45:16 2012 +0100
@@ -967,6 +967,9 @@
 msgid "a float is expected"
 msgstr "un nombre flottant est attendu"
 
+msgid "a number (in seconds) or 20s, 10min, 24h or 4d are expected"
+msgstr "un nombre (en seconde) ou 20s, 10min, 24h ou 4d sont attendus"
+
 msgid ""
 "a simple cache entity characterized by a name and a validity date. The "
 "target application is responsible for updating timestamp when necessary to "
--- a/web/formfields.py	Wed Feb 15 18:12:38 2012 +0100
+++ b/web/formfields.py	Mon Feb 20 11:45:16 2012 +0100
@@ -1,4 +1,4 @@
-# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -43,6 +43,7 @@
 .. autoclass:: cubicweb.web.formfields.DateField()
 .. autoclass:: cubicweb.web.formfields.DateTimeField()
 .. autoclass:: cubicweb.web.formfields.TimeField()
+.. autoclass:: cubicweb.web.formfields.TimeIntervalField()
 
 Compound fields
 ''''''''''''''''
@@ -63,11 +64,13 @@
 __docformat__ = "restructuredtext en"
 
 from warnings import warn
-from datetime import datetime
+from datetime import datetime, timedelta
 
 from logilab.mtconverter import xml_escape
 from logilab.common import nullobject
 from logilab.common.date import ustrftime
+from logilab.common.configuration import format_time
+from logilab.common.textutils import apply_units, TIME_UNITS
 
 from yams.schema import KNOWN_METAATTRIBUTES, role_name
 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint,
@@ -929,6 +932,38 @@
         return None
 
 
+class TimeIntervalField(StringField):
+    """Use this field to edit time interval (`Interval` yams type).
+
+    Unless explicitly specified, the widget for this field will be a
+    :class:`~cubicweb.web.formwidgets.TextInput`.
+    """
+    widget = fw.TextInput
+
+    def format_single_value(self, req, value):
+        if value:
+            value = format_time(value.days * 24 * 3600 + value.seconds)
+            return unicode(value)
+        return u''
+
+    def example_format(self, req):
+        """return a sample string describing what can be given as input for this
+        field
+        """
+        return u'20s, 10min, 24h, 4d'
+
+    def _ensure_correctly_typed(self, form, value):
+        if isinstance(value, basestring):
+            value = value.strip()
+            if not value:
+                return None
+            try:
+                value = apply_units(value, TIME_UNITS)
+            except ValueError:
+                raise ProcessFormError(form._cw._('a number (in seconds) or 20s, 10min, 24h or 4d are expected'))
+        return timedelta(0, value)
+
+
 class DateField(StringField):
     """Use this field to edit date (`Date` yams type).
 
@@ -1201,5 +1236,5 @@
     'TZDatetime': DateTimeField,
     'Time':       TimeField,
     'TZTime':     TimeField,
-    # XXX implement 'Interval': TimeIntervalField,
+    'Interval':   TimeIntervalField,
     }