From d396b33686fd1275e1520d2dc457d561252e7a08 Mon Sep 17 00:00:00 2001 From: candifloss Date: Thu, 27 Feb 2025 15:59:41 +0530 Subject: [PATCH] Minor additions to Attribute properties --- definitions/attribute.py | 52 +++++++++++++++++++++++++++++++--------- templates/create.html | 1 + 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/definitions/attribute.py b/definitions/attribute.py index 2886896..7163a07 100644 --- a/definitions/attribute.py +++ b/definitions/attribute.py @@ -62,13 +62,25 @@ class textAttribute(Attribute): error = super().validate() if error: return error - if self.default_val is not None and self.regex is not None: - if not re.match(self.regex, str(self.default_val)): - return f"Invalid default value for '{self.attrib_name}'. It must match the pattern: {self.regex}" - if self.allowed_chars is not None and self.default_val is not None: - for char in self.default_val: - if char not in self.allowed_chars: - return f"Invalid character '{char}' in default value for '{self.attrib_name}'. Allowed characters are: {self.allowed_chars}" + if self.min_length is not None and self.max_length is not None: + if not isinstance(self.min_length, int) or not isinstance(self.max_length, int): + return f"Min and max lengths must be integers for '{self.attrib_name}'." + if int(self.min_length) > int(self.max_length): + return f"Max length must be greater than min length for '{self.attrib_name}'." + if self.default_val is not None: + if self.regex is not None: + if not re.match(self.regex, str(self.default_val)): + return f"Default value for '{self.attrib_name}' must match the pattern: {self.regex}" + if self.allowed_chars is not None: + for char in self.default_val: + if char not in self.allowed_chars: + return f"Invalid character '{char}' in default value for '{self.attrib_name}'. Allowed characters are: {self.allowed_chars}" + if self.min_length is not None: + if len(self.default_val) < int(self.min_length): + return f"Invalid default value for '{self.attrib_name}'. The minimum length is: {self.min_length}" + if self.max_length is not None: + if len(self.default_val) > int(self.max_length): + return f"Invalid default value for '{self.attrib_name}'. The maximum length is: {self.max_length}" return None @@ -80,11 +92,13 @@ class intAttribute(Attribute): display_name: str, min_val: Optional[int] = None, max_val: Optional[int] = None, + step: int = 1, **kwargs ): super().__init__(attrib_name, display_name, html_input_type="number", **kwargs) self.min_val = min_val self.max_val = max_val + self.step = step def validate(self) -> Optional[str]: """Validate integer-specific attributes.""" @@ -97,9 +111,16 @@ class intAttribute(Attribute): return f"max_val must be an integer for attribute '{self.attrib_name}'." if self.min_val is not None and self.max_val is not None and self.min_val > self.max_val: return f"min_val cannot be greater than max_val for attribute '{self.attrib_name}'." - if self.default_val is not None and not isinstance(self.default_val, int): - return f"default_val must be an integer for attribute '{self.attrib_name}'." + if self.step is not None: + if not isinstance(self.step, int): + return f"Step must be an integer for attribute '{self.attrib_name}'." + if self.step <= 0: + return f"Step must be a positive integer for attribute '{self.attrib_name}'." + if self.min_val is not None and self.max_val is not None and self.step > (self.max_val - self.min_val): + return f"Step value is too large for attribute '{self.attrib_name}'." if self.default_val is not None: + if not isinstance(self.default_val, int): + return f"default_val must be an integer for attribute '{self.attrib_name}'." if self.min_val is not None and self.default_val < self.min_val: return f"default_val cannot be less than min_val for attribute '{self.attrib_name}'." if self.max_val is not None and self.default_val > self.max_val: @@ -115,11 +136,13 @@ class floatAttribute(Attribute): display_name: str, min_val: Optional[float] = None, max_val: Optional[float] = None, + step: float = 0.1, **kwargs ): super().__init__(attrib_name, display_name, html_input_type="number", **kwargs) self.min_val = min_val self.max_val = max_val + self.step = step def validate(self) -> Optional[str]: """Validate float-specific attributes.""" @@ -132,9 +155,16 @@ class floatAttribute(Attribute): return f"max_val must be a number for attribute '{self.attrib_name}'." if self.min_val is not None and self.max_val is not None and self.min_val > self.max_val: return f"min_val cannot be greater than max_val for attribute '{self.attrib_name}'." - if self.default_val is not None and not isinstance(self.default_val, (int, float)): - return f"default_val must be a number for attribute '{self.attrib_name}'." + if self.step is not None: + if not isinstance(self.step, float): + return f"Step must be an float for attribute '{self.attrib_name}'." + if self.step <= 0: + return f"Step must be a positive float for attribute '{self.attrib_name}'." + if self.min_val is not None and self.max_val is not None and self.step > (self.max_val - self.min_val): + return f"Step value is too large for attribute '{self.attrib_name}'." if self.default_val is not None: + if not isinstance(self.default_val, float): + return f"default_val must be a number for attribute '{self.attrib_name}'." if self.min_val is not None and self.default_val < self.min_val: return f"default_val cannot be less than min_val for attribute '{self.attrib_name}'." if self.max_val is not None and self.default_val > self.max_val: diff --git a/templates/create.html b/templates/create.html index fc9d364..3b4c7fc 100644 --- a/templates/create.html +++ b/templates/create.html @@ -38,6 +38,7 @@ {% if attrib.html_input_type == "number" %} {% if attrib.min_val is not none %} min="{{ attrib.min_val }}" {% endif %} {% if attrib.max_val is not none %} max="{{ attrib.max_val }}" {% endif %} + {% if attrib.step is not none %} step="{{ attrib.step }}" {% endif %} {% endif %} {% if attrib.html_input_type == "text" %} {% if attrib.max_length is not none %} maxlength="{{ attrib.max_length }}" {% endif %}