diff src/tools/xml_tools.py @ 1220:f91e7028e2c3

memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
author souliane <souliane@mailoo.org>
date Fri, 03 Oct 2014 12:27:43 +0200
parents 96fb74a4714d
children 069ad98b360d
line wrap: on
line diff
--- a/src/tools/xml_tools.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/src/tools/xml_tools.py	Fri Oct 03 12:27:43 2014 +0200
@@ -70,6 +70,9 @@
         if widget_args[0] is None:
             widget_args[0] = 'false'
         widget_kwargs['read_only'] = read_only
+    elif field.fieldType == 'integer':
+        widget_type = "integer"
+        widget_kwargs['read_only'] = read_only
     elif field.fieldType == 'list-single':
         widget_type = "list"
         widget_kwargs["options"] = [(option.value, option.label or option.value) for option in field.options]
@@ -696,6 +699,18 @@
     type = 'jid_input'
 
 
+# TODO handle min and max values
+class IntWidget(StringWidget):
+    type = 'int'
+
+    def __init__(self, xmlui, value=0, name=None, parent=None, read_only=False):
+        try:
+            int(value)
+        except ValueError:
+            raise exceptions.DataError(_("Value must be an integer"))
+        super(IntWidget, self).__init__(xmlui, value, name, parent, read_only=read_only)
+
+
 class BoolWidget(InputWidget):
     type = 'bool'
 
@@ -703,10 +718,10 @@
         if isinstance(value, bool):
             value = 'true' if value else 'false'
         elif value == '0':
-            value='false'
+            value = 'false'
         elif value == '1':
-            value='true'
-        if not value in ('true', 'false'):
+            value = 'true'
+        if value not in ('true', 'false'):
             raise exceptions.DataError(_("Value must be 0, 1, false or true"))
         super(BoolWidget, self).__init__(xmlui, name, parent, read_only=read_only)
         self.elem.setAttribute('value', value)