From cec4e326c87110b79c777ba43949552f8bbd7e7d Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Thu, 9 Jun 2022 17:24:53 +0200 Subject: [PATCH] prevent "None" showing up in text inputs --- owrx/form/input/__init__.py | 5 ++++- owrx/form/input/converter.py | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/owrx/form/input/__init__.py b/owrx/form/input/__init__.py index 5d95b6b..06a3d79 100644 --- a/owrx/form/input/__init__.py +++ b/owrx/form/input/__init__.py @@ -1,7 +1,7 @@ from abc import ABC from owrx.modes import Modes from owrx.form.input.validator import Validator -from owrx.form.input.converter import Converter, NullConverter, IntConverter, FloatConverter, EnumConverter +from owrx.form.input.converter import Converter, NullConverter, IntConverter, FloatConverter, EnumConverter, TextConverter from enum import Enum @@ -106,6 +106,9 @@ class TextInput(Input): props["type"] = "text" return props + def defaultConverter(self): + return TextConverter() + class NumberInput(Input): def __init__(self, id, label, infotext=None, append="", converter: Converter = None, validator: Validator = None): diff --git a/owrx/form/input/converter.py b/owrx/form/input/converter.py index bf9b5f1..ddb2d0e 100644 --- a/owrx/form/input/converter.py +++ b/owrx/form/input/converter.py @@ -14,6 +14,10 @@ class Converter(ABC): class NullConverter(Converter): + """ + The default converter class + Does not change the value in any way, just passes them through + """ def convert_to_form(self, value): return value @@ -21,10 +25,25 @@ class NullConverter(Converter): return value +class TextConverter(Converter): + """ + Converter class for text inputs + Does nothing more than to prevent the special python value "None" from appearing in the form + The string "None" should pass + """ + def convert_to_form(self, value): + if value is None: + return "" + return value + + def convert_from_form(self, value): + return value + + class OptionalConverter(Converter): """ Transforms a special form value to None - The default is look for an empty string, but this can be used to adopt to other types. + The default is to look for an empty string, but this can be used to adopt to other types. If the default is not found, the actual value is passed to the sub_converter for further transformation. useful for optional fields since None is not stored in the configuration """