Continue refactoring Attribute classes
This commit is contained in:
parent
6055bcad17
commit
0b58ac5d27
32
config.py
32
config.py
@ -1,4 +1,4 @@
|
|||||||
from definitions.attribute import Attribute
|
from definitions.attribute import textAttribute, intAttribute, dateAttribute, selectAttribute
|
||||||
|
|
||||||
# MySQL information
|
# MySQL information
|
||||||
class sql_conf:
|
class sql_conf:
|
||||||
@ -8,41 +8,41 @@ class sql_conf:
|
|||||||
SQL_DB = "asset_test_db"
|
SQL_DB = "asset_test_db"
|
||||||
SQL_TABLE = "asset_test"
|
SQL_TABLE = "asset_test"
|
||||||
|
|
||||||
item_attributes = {
|
item_attributes = [
|
||||||
"assettag": Attribute(
|
textAttribute(
|
||||||
|
attrib_name="assettag",
|
||||||
display_name="Asset Tag",
|
display_name="Asset Tag",
|
||||||
html_input_type="text",
|
|
||||||
required=True,
|
required=True,
|
||||||
unique=True,
|
unique=True,
|
||||||
primary=True,
|
primary=True,
|
||||||
regex=r"^[A-Z0-9]+$", # Only uppercase letters and numbers
|
regex=r"^[A-Z0-9]+$", # Only uppercase letters and numbers
|
||||||
default_val=1000000
|
default_val="1000000"
|
||||||
),
|
),
|
||||||
"hostname": Attribute(
|
textAttribute(
|
||||||
|
attrib_name="hostname",
|
||||||
display_name="Host Name",
|
display_name="Host Name",
|
||||||
html_input_type="text",
|
|
||||||
required=True,
|
required=True,
|
||||||
unique=True,
|
unique=True,
|
||||||
regex=r"^[a-z0-9._-]+$" # Lowercase letters, numbers, dots, underscores, hyphens
|
regex=r"^[a-z0-9._-]+$" # Lowercase letters, numbers, dots, underscores, hyphens
|
||||||
),
|
),
|
||||||
"warrantyfrom": Attribute(
|
dateAttribute(
|
||||||
|
attrib_name="warrantyfrom",
|
||||||
display_name="Warranty From",
|
display_name="Warranty From",
|
||||||
html_input_type="date",
|
|
||||||
default_val="2020-03-09",
|
default_val="2020-03-09",
|
||||||
required=True
|
required=True
|
||||||
),
|
),
|
||||||
"status": Attribute(
|
selectAttribute(
|
||||||
|
attrib_name="status",
|
||||||
display_name="Status",
|
display_name="Status",
|
||||||
html_input_type="select",
|
|
||||||
required=True,
|
required=True,
|
||||||
options=["Active", "Inactive"], # Allowed values
|
options=["Active", "Inactive"], # Allowed values
|
||||||
default_val="Active"
|
default_val="Active"
|
||||||
),
|
),
|
||||||
"staffnum": Attribute(
|
intAttribute(
|
||||||
|
attrib_name="staffnum",
|
||||||
display_name="Staff No.",
|
display_name="Staff No.",
|
||||||
html_input_type="number",
|
|
||||||
required=True,
|
required=True,
|
||||||
min=100000, # 6 digits
|
min_val=100000, # 6 digits
|
||||||
max=99999999, # 8 digits
|
max_val=99999999, # 8 digits
|
||||||
)
|
)
|
||||||
}
|
]
|
@ -1,28 +1,11 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Optional, List, Tuple
|
||||||
"""
|
|
||||||
class Attribute:
|
|
||||||
def __init__(self, display_name, html_input_type="text", required=False, unique=False, primary=False, regex=None, min=None, max=None, options=None, default_val="", auto_increment=False, index=False, comment="", compareto=None):
|
|
||||||
self.display_name = display_name # Input label or table column header.
|
|
||||||
self.html_input_type = html_input_type # HTML form input type. Determines MySQL data type.
|
|
||||||
self.required = required # HTML form input "required" attribute and MySQL "Not Null" constraint
|
|
||||||
self.unique = unique # MySQL "unique" constraint
|
|
||||||
self.primary = primary # MySQL "primary key" constraint
|
|
||||||
self.regex = regex # Regex for value validation
|
|
||||||
self.min = min # HTML form input "min" attribute. Sets minimum value.
|
|
||||||
self.max = max # HTML form input "max" attribute. Sets maximum value.
|
|
||||||
self.options = options # List of options for "select" inputs
|
|
||||||
self.default_val = default_val # HTML form input "value" attribute. Sets default value.
|
|
||||||
self.auto_increment = auto_increment # bool: MySQL autoincrement
|
|
||||||
self.index = index # bool: MySQL index
|
|
||||||
self.comment = comment # Description text
|
|
||||||
self.compareto = compareto # Compare to another attribute of the item for validation: ["comparison", "referenceattrib"]
|
|
||||||
##default_val: Optional[str, int, float] = None, # HTML form input "value" attribute. Sets default value.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Base Attribute class
|
# Base Attribute class
|
||||||
class Attribute:
|
class Attribute:
|
||||||
def __init__(self,
|
def __init__(
|
||||||
|
self,
|
||||||
|
attrib_name: str, # Attribute name and id.
|
||||||
display_name: str, # Input label or table column header.
|
display_name: str, # Input label or table column header.
|
||||||
html_input_type: str = "text", # HTML form input type. Determines MySQL data type.
|
html_input_type: str = "text", # HTML form input type. Determines MySQL data type.
|
||||||
placeholder: str = "", # HTML form input placeholder.
|
placeholder: str = "", # HTML form input placeholder.
|
||||||
@ -30,80 +13,125 @@ class Attribute:
|
|||||||
unique: bool = False, # MySQL "unique" constraint
|
unique: bool = False, # MySQL "unique" constraint
|
||||||
primary: bool = False, # MySQL "primary key" constraint
|
primary: bool = False, # MySQL "primary key" constraint
|
||||||
index: bool = False, # bool: MySQL index
|
index: bool = False, # bool: MySQL index
|
||||||
compareto: Optional[List[str]] = None, # Compare to another attribute of the item for validation: ["comparison", "referenceattrib"]
|
compareto: Optional[List[Tuple[str, str]]] = None, # Compare to another attribute of the item for validation
|
||||||
title: str ="" # Description text, html "title" attribute
|
title: str = None, # Description text, HTML "title" attribute
|
||||||
):
|
):
|
||||||
|
if not attrib_name:
|
||||||
|
raise ValueError("Attribute name cannot be empty.")
|
||||||
|
if not display_name:
|
||||||
|
raise ValueError(f"Display name cannot be empty for attribute '{attrib_name}'.")
|
||||||
|
self.attrib_name = attrib_name
|
||||||
self.display_name = display_name
|
self.display_name = display_name
|
||||||
self.html_input_type = html_input_type
|
self.html_input_type = html_input_type
|
||||||
|
self.placeholder = placeholder
|
||||||
self.required = required
|
self.required = required
|
||||||
self.unique = unique
|
self.unique = unique
|
||||||
self.primary = primary
|
self.primary = primary
|
||||||
self.default_val = default_val
|
|
||||||
self.index = index
|
self.index = index
|
||||||
self.compareto = compareto
|
self.compareto = compareto
|
||||||
self.title = title
|
self.title = title
|
||||||
|
|
||||||
class textAttribute:
|
# Validate compareto
|
||||||
def __init__(self,
|
if self.compareto is not None:
|
||||||
regex: str = None, # Regex for value validation
|
validate_comparison(self)
|
||||||
default_val: str = "", # Default value
|
|
||||||
|
# Text Attribute
|
||||||
|
class textAttribute(Attribute):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
regex: Optional[str] = None, # Regex for value validation
|
||||||
|
default_val: str = None, # Default value
|
||||||
compareto: Optional[List[Tuple[str, str]]] = None,
|
compareto: Optional[List[Tuple[str, str]]] = None,
|
||||||
|
**kwargs # Additional arguments for the base class
|
||||||
):
|
):
|
||||||
self.html_input_type = "text"
|
super().__init__(html_input_type="text", **kwargs)
|
||||||
self.regex = regex
|
self.regex = regex
|
||||||
self.default_val = default_val
|
self.default_val = default_val
|
||||||
|
self.compareto = compareto
|
||||||
|
|
||||||
class intAttribute:
|
# Integer Attribute
|
||||||
def __init__(self,
|
class intAttribute(Attribute):
|
||||||
min_val: int = None, # Min value
|
def __init__(
|
||||||
max_val: int = None, # Max value
|
self,
|
||||||
step_val: int = None, # Increment step
|
min_val: Optional[int] = None, # Min value
|
||||||
default_val: int = None, # Default value
|
max_val: Optional[int] = None, # Max value
|
||||||
|
step_val: Optional[int] = None, # Increment step
|
||||||
|
default_val: Optional[int] = None, # Default value
|
||||||
auto_increment: bool = False, # bool: MySQL autoincrement
|
auto_increment: bool = False, # bool: MySQL autoincrement
|
||||||
compareto: Optional[List[Tuple[str, str]]] = None,
|
compareto: Optional[List[Tuple[str, str]]] = None,
|
||||||
|
**kwargs # Additional arguments for the base class
|
||||||
):
|
):
|
||||||
self.html_input_type = "number"
|
super().__init__(html_input_type="number", **kwargs)
|
||||||
self.min_val = min_val
|
self.min_val = min_val
|
||||||
self.max_val = max_val
|
self.max_val = max_val
|
||||||
self.step_val = step_val
|
self.step_val = step_val
|
||||||
self.default_val = default_val
|
self.default_val = default_val
|
||||||
self.auto_increment = auto_increment
|
self.auto_increment = auto_increment
|
||||||
|
self.compareto = compareto
|
||||||
|
|
||||||
class floatAttribute:
|
# Validate min_val and max_val
|
||||||
def __init__(self,
|
if self.min_val is not None and self.max_val is not None and self.min_val > self.max_val:
|
||||||
min_val: float = None, # Min value
|
raise ValueError(f"min_val ({self.min_val}) cannot be greater than max_val ({self.max_val}).")
|
||||||
max_val: float = None, # Max value
|
|
||||||
step_val: float = None, # Increment step
|
# Float Attribute
|
||||||
default_val: float = None, # Default value
|
class floatAttribute(Attribute):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
min_val: Optional[float] = None, # Min value
|
||||||
|
max_val: Optional[float] = None, # Max value
|
||||||
|
step_val: Optional[float] = None, # Increment step
|
||||||
|
default_val: Optional[float] = None, # Default value
|
||||||
auto_increment: bool = False, # bool: MySQL autoincrement
|
auto_increment: bool = False, # bool: MySQL autoincrement
|
||||||
compareto: Optional[List[Tuple[str, str]]] = None,
|
compareto: Optional[List[Tuple[str, str]]] = None,
|
||||||
|
**kwargs # Additional arguments for the base class
|
||||||
):
|
):
|
||||||
self.html_input_type = "number"
|
super().__init__(html_input_type="number", **kwargs)
|
||||||
self.min_val = min_val
|
self.min_val = min_val
|
||||||
self.max_val = max_val
|
self.max_val = max_val
|
||||||
self.step_val = step_val
|
self.step_val = step_val
|
||||||
self.default_val = default_val
|
self.default_val = default_val
|
||||||
self.auto_increment = auto_increment
|
self.auto_increment = auto_increment
|
||||||
|
self.compareto = compareto
|
||||||
|
|
||||||
class dateAttribute:
|
# Validate min_val and max_val
|
||||||
def __init__(self,
|
if self.min_val is not None and self.max_val is not None and self.min_val > self.max_val:
|
||||||
min_val = None, # Min value
|
raise ValueError(f"min_val ({self.min_val}) cannot be greater than max_val ({self.max_val}).")
|
||||||
max_val = None, # Max value
|
|
||||||
default_val = None, # Default value
|
# Date Attribute
|
||||||
|
class dateAttribute(Attribute):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
min_val: Optional[str] = None, # Min value (string in "YYYY-MM-DD" format)
|
||||||
|
max_val: Optional[str] = None, # Max value (string in "YYYY-MM-DD" format)
|
||||||
|
default_val: Optional[str] = None, # Default value (string in "YYYY-MM-DD" format)
|
||||||
compareto: Optional[List[Tuple[str, str]]] = None,
|
compareto: Optional[List[Tuple[str, str]]] = None,
|
||||||
|
**kwargs # Additional arguments for the base class
|
||||||
):
|
):
|
||||||
self.html_input_type = "date"
|
super().__init__(html_input_type="date", **kwargs)
|
||||||
self.min_val = datetime.strptime(min_val, "%Y-%m-%d")
|
self.min_val = self._parse_date(min_val) if min_val else None
|
||||||
self.max_val = datetime.strptime(max_val, "%Y-%m-%d")
|
self.max_val = self._parse_date(max_val) if max_val else None
|
||||||
self.default_val = datetime.strptime(default_val, "%Y-%m-%d")
|
self.default_val = self._parse_date(default_val) if default_val else None
|
||||||
|
self.compareto = compareto
|
||||||
|
|
||||||
class selectAttribute:
|
def _parse_date(self, date_str: str) -> datetime:
|
||||||
def __init__(self,
|
"""Parse a date string into a datetime object."""
|
||||||
|
try:
|
||||||
|
return datetime.strptime(date_str, "%Y-%m-%d")
|
||||||
|
except ValueError as e:
|
||||||
|
raise ValueError(f"Invalid date format for '{date_str}'. Expected 'YYYY-MM-DD'.") from e
|
||||||
|
|
||||||
|
# Select Attribute
|
||||||
|
class selectAttribute(Attribute):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
options: Optional[List[str]] = None, # List of options
|
options: Optional[List[str]] = None, # List of options
|
||||||
default_val: str = None, # Default value
|
default_val: Optional[str] = None, # Default value
|
||||||
|
**kwargs # Additional arguments for the base class
|
||||||
):
|
):
|
||||||
self.html_input_type = "select"
|
super().__init__(html_input_type="select", **kwargs)
|
||||||
self.options = options
|
self.options = options if options else []
|
||||||
self.default_val = default_val if default_val else self.options[0]
|
self.default_val = default_val if default_val else (self.options[0] if self.options else None)
|
||||||
|
|
||||||
|
|
||||||
|
# Validate default_val
|
||||||
|
if self.default_val and self.default_val not in self.options:
|
||||||
|
raise ValueError(f"Default value '{self.default_val}' is not in the options list.")
|
@ -13,28 +13,28 @@ def create_asset_model():
|
|||||||
attrs = {
|
attrs = {
|
||||||
'__tablename__': sql_conf.SQL_TABLE, # Table name
|
'__tablename__': sql_conf.SQL_TABLE, # Table name
|
||||||
'__init__': lambda self, **kwargs: self.__dict__.update(kwargs), # Constructor
|
'__init__': lambda self, **kwargs: self.__dict__.update(kwargs), # Constructor
|
||||||
'__repr__': lambda self: f"<Asset {getattr(self, next(attrib for attrib, config in item_attributes.items() if config.primary))}>" # Representation
|
'__repr__': lambda self: f"<Asset {getattr(self, next(attrib for attrib in item_attributes if attrib.primary))}>" # Representation
|
||||||
}
|
}
|
||||||
|
|
||||||
for attrib, config in item_attributes.items():
|
for attrib in item_attributes:
|
||||||
# Determine the column type based on the configuration
|
# Determine the column type based on the configuration
|
||||||
if config.html_input_type == "text":
|
if attrib.html_input_type == "text":
|
||||||
column_type = String(50)
|
column_type = String(50)
|
||||||
elif config.html_input_type == "date":
|
elif attrib.html_input_type == "date":
|
||||||
column_type = Date
|
column_type = Date
|
||||||
elif config.html_input_type == "select":
|
elif attrib.html_input_type == "select":
|
||||||
column_type = Enum(*config.options)
|
column_type = Enum(*attrib.options)
|
||||||
elif config.html_input_type == "number":
|
elif attrib.html_input_type == "number":
|
||||||
column_type = Integer
|
column_type = Integer
|
||||||
|
|
||||||
# Define the column with additional properties
|
# Define the column with additional properties
|
||||||
attrs[attrib] = Column(
|
attrs[attrib.attrib_name] = Column(
|
||||||
column_type,
|
column_type,
|
||||||
primary_key=config.primary,
|
primary_key=attrib.primary,
|
||||||
unique=config.unique,
|
unique=attrib.unique,
|
||||||
nullable=not config.required,
|
nullable=not attrib.required,
|
||||||
default=config.default_val,
|
default=attrib.default_val,
|
||||||
comment=config.comment
|
#title=attrib.title
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create the Asset class dynamically
|
# Create the Asset class dynamically
|
||||||
|
@ -1,106 +1,121 @@
|
|||||||
import re
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import re
|
||||||
|
from typing import List, Optional
|
||||||
|
from definitions.attribute import textAttribute, intAttribute, dateAttribute, selectAttribute
|
||||||
|
|
||||||
def _is_int(value):
|
def _is_int(value) -> bool:
|
||||||
"""Check if a value is a valid integer."""
|
"""Check if a value is a valid integer."""
|
||||||
try:
|
return isinstance(value, int)
|
||||||
int(value)
|
|
||||||
return True
|
|
||||||
except (ValueError, TypeError):
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _is_date(value):
|
def _is_date(value) -> bool:
|
||||||
"""Check if a value is a valid date in YYYY-MM-DD format."""
|
"""Check if a value is a valid date in YYYY-MM-DD format."""
|
||||||
|
if not isinstance(value, str):
|
||||||
|
return False
|
||||||
try:
|
try:
|
||||||
datetime.strptime(value, "%Y-%m-%d")
|
datetime.strptime(value, "%Y-%m-%d")
|
||||||
return True
|
return True
|
||||||
except (ValueError, TypeError):
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _validate_number(attrib, attrib_name):
|
def _validate_number(attrib: intAttribute) -> Optional[str]:
|
||||||
"""Validate number-specific attributes."""
|
"""Validate number-specific attributes. Returns an error message if invalid, otherwise None."""
|
||||||
if attrib.min and not _is_int(attrib.min):
|
if attrib.min_val is not None and not _is_int(attrib.min_val):
|
||||||
return False
|
return f"min_val must be an integer for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.max and not _is_int(attrib.max):
|
if attrib.max_val is not None and not _is_int(attrib.max_val):
|
||||||
return False
|
return f"max_val must be an integer for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.min and attrib.max and int(attrib.max) < int(attrib.min):
|
if attrib.min_val is not None and attrib.max_val is not None and attrib.min_val > attrib.max_val:
|
||||||
return False
|
return f"min_val cannot be greater than max_val for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.default_val and not _is_int(attrib.default_val):
|
if attrib.default_val is not None and not _is_int(attrib.default_val):
|
||||||
return False
|
return f"default_val must be an integer for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.default_val:
|
if attrib.default_val is not None:
|
||||||
if attrib.min and int(attrib.default_val) < int(attrib.min):
|
if attrib.min_val is not None and attrib.default_val < attrib.min_val:
|
||||||
return False
|
return f"default_val cannot be less than min_val for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.max and int(attrib.default_val) > int(attrib.max):
|
if attrib.max_val is not None and attrib.default_val > attrib.max_val:
|
||||||
return False
|
return f"default_val cannot be greater than max_val for attribute '{attrib.attrib_name}'."
|
||||||
return True
|
return None
|
||||||
|
|
||||||
def _validate_date(attrib, attrib_name):
|
def _validate_date(attrib: dateAttribute) -> Optional[str]:
|
||||||
"""Validate date-specific attributes."""
|
"""Validate date-specific attributes. Returns an error message if invalid, otherwise None."""
|
||||||
if attrib.min and not _is_date(attrib.min):
|
if attrib.min_val is not None and not _is_date(attrib.min_val):
|
||||||
return False
|
return f"min_val must be a valid date (YYYY-MM-DD) for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.max and not _is_date(attrib.max):
|
if attrib.max_val is not None and not _is_date(attrib.max_val):
|
||||||
return False
|
return f"max_val must be a valid date (YYYY-MM-DD) for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.min and attrib.max and datetime.strptime(attrib.max, "%Y-%m-%d") < datetime.strptime(attrib.min, "%Y-%m-%d"):
|
if attrib.min_val is not None and attrib.max_val is not None:
|
||||||
return False
|
min_date = datetime.strptime(attrib.min_val, "%Y-%m-%d")
|
||||||
if attrib.default_val and not _is_date(attrib.default_val):
|
max_date = datetime.strptime(attrib.max_val, "%Y-%m-%d")
|
||||||
return False
|
if max_date < min_date:
|
||||||
if attrib.default_val:
|
return f"max_val cannot be earlier than min_val for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.min and datetime.strptime(attrib.default_val, "%Y-%m-%d") < datetime.strptime(attrib.min, "%Y-%m-%d"):
|
if attrib.default_val is not None and not _is_date(attrib.default_val):
|
||||||
return False
|
return f"default_val must be a valid date (YYYY-MM-DD) for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.max and datetime.strptime(attrib.default_val, "%Y-%m-%d") > datetime.strptime(attrib.max, "%Y-%m-%d"):
|
if attrib.default_val is not None:
|
||||||
return False
|
default_date = datetime.strptime(attrib.default_val, "%Y-%m-%d")
|
||||||
return True
|
if attrib.min_val is not None:
|
||||||
|
min_date = datetime.strptime(attrib.min_val, "%Y-%m-%d")
|
||||||
|
if default_date < min_date:
|
||||||
|
return f"default_val cannot be earlier than min_val for attribute '{attrib.attrib_name}'."
|
||||||
|
if attrib.max_val is not None:
|
||||||
|
max_date = datetime.strptime(attrib.max_val, "%Y-%m-%d")
|
||||||
|
if default_date > max_date:
|
||||||
|
return f"default_val cannot be later than max_val for attribute '{attrib.attrib_name}'."
|
||||||
|
return None
|
||||||
|
|
||||||
def _validate_text(attrib, attrib_name):
|
def _validate_text(attrib: textAttribute) -> Optional[str]:
|
||||||
"""Validate text-specific attributes."""
|
"""Validate text-specific attributes. Returns an error message if invalid, otherwise None."""
|
||||||
if attrib.min or attrib.max or attrib.auto_increment or attrib.options:
|
if attrib.default_val is not None and attrib.regex is not None:
|
||||||
return False
|
if not re.match(attrib.regex, str(attrib.default_val)):
|
||||||
if attrib.default_val and attrib.regex and not re.match(attrib.regex, str(attrib.default_val)):
|
return f"default_val does not match the regex pattern for attribute '{attrib.attrib_name}'."
|
||||||
return False
|
return None
|
||||||
return True
|
|
||||||
|
|
||||||
def _validate_select(attrib, attrib_name):
|
def _validate_select(attrib: selectAttribute) -> Optional[str]:
|
||||||
"""Validate select-specific attributes."""
|
"""Validate select-specific attributes. Returns an error message if invalid, otherwise None."""
|
||||||
if not attrib.options:
|
if not attrib.options:
|
||||||
return False
|
return f"options cannot be empty for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.default_val and attrib.default_val not in attrib.options:
|
if attrib.default_val is not None and attrib.default_val not in attrib.options:
|
||||||
return False
|
return f"default_val must be one of the options for attribute '{attrib.attrib_name}'."
|
||||||
return True
|
return None
|
||||||
|
|
||||||
# Validate the configuration file to ensure all attributes are properly defined.
|
def validate_config(item_attributes: List) -> str:
|
||||||
def validate_config(item_attributes):
|
"""
|
||||||
for attrib_name, attrib in item_attributes.items():
|
Validate the configuration file to ensure all attributes are properly defined.
|
||||||
# Validate display_name and html_input_type
|
Returns "Ok" if everything is valid, otherwise returns an error message.
|
||||||
|
"""
|
||||||
|
for attrib in item_attributes:
|
||||||
|
# Validate essential properties
|
||||||
|
if not attrib.attrib_name:
|
||||||
|
return f"Missing attribute name for the {item_attributes.index(attrib) + 1}th attribute."
|
||||||
if not attrib.display_name:
|
if not attrib.display_name:
|
||||||
return f"Missing display name for attribute '{attrib_name}'."
|
return f"Missing display name for attribute '{attrib.attrib_name}'."
|
||||||
if not attrib.html_input_type:
|
if not attrib.html_input_type:
|
||||||
return f"Missing input type for attribute '{attrib_name}'."
|
return f"Missing input type for attribute '{attrib.attrib_name}'."
|
||||||
if attrib.html_input_type not in ["text", "number", "date", "select"]:
|
if attrib.html_input_type not in ["text", "number", "date", "select"]:
|
||||||
return f"Invalid input type for attribute '{attrib_name}'."
|
return f"Invalid input type for attribute '{attrib.attrib_name}'."
|
||||||
|
|
||||||
# Validate min, max, and default values based on input type
|
# Validate type-specific attributes
|
||||||
if attrib.html_input_type == "number":
|
if isinstance(attrib, intAttribute):
|
||||||
if not _validate_number(attrib, attrib_name):
|
error = _validate_number(attrib)
|
||||||
return f"Invalid number configuration for attribute '{attrib_name}'."
|
if error:
|
||||||
elif attrib.html_input_type == "date":
|
return error
|
||||||
if not _validate_date(attrib, attrib_name):
|
elif isinstance(attrib, dateAttribute):
|
||||||
return f"Invalid date configuration for attribute '{attrib_name}'."
|
error = _validate_date(attrib)
|
||||||
elif attrib.html_input_type == "text":
|
if error:
|
||||||
if not _validate_text(attrib, attrib_name):
|
return error
|
||||||
return f"Invalid text configuration for attribute '{attrib_name}'."
|
elif isinstance(attrib, textAttribute):
|
||||||
elif attrib.html_input_type == "select":
|
error = _validate_text(attrib)
|
||||||
if not _validate_select(attrib, attrib_name):
|
if error:
|
||||||
return f"Invalid select configuration for attribute '{attrib_name}'."
|
return error
|
||||||
|
elif isinstance(attrib, selectAttribute):
|
||||||
|
error = _validate_select(attrib)
|
||||||
|
if error:
|
||||||
|
return error
|
||||||
|
|
||||||
# Validate min and max values
|
# Validate comparison (if applicable)
|
||||||
if (attrib.min or attrib.max) and attrib.html_input_type not in ['number', 'date']:
|
|
||||||
return f"'{attrib_name}' must be of type 'number' or 'date' to have min or max values."
|
|
||||||
|
|
||||||
# Validate comparison
|
|
||||||
if attrib.compareto:
|
if attrib.compareto:
|
||||||
if attrib.compareto[1] not in item_attributes:
|
if not isinstance(attrib.compareto, list) or not all(isinstance(pair, tuple) and len(pair) == 2 for pair in attrib.compareto):
|
||||||
return f"Invalid reference attribute '{attrib.compareto[1]}' for comparison in attribute '{attrib_name}'."
|
return f"Invalid comparison format for attribute '{attrib.attrib_name}'. Expected a list of tuples."
|
||||||
if attrib.compareto[0] not in ["lt", "gt", "le", "ge", "eq"]:
|
for cmp, ref_attr in attrib.compareto:
|
||||||
return f"Invalid comparison operator for attribute '{attrib_name}'. Valid operators are: lt, gt, le, ge, eq."
|
if cmp not in ["lt", "gt", "le", "ge", "eq"]:
|
||||||
# Return "Ok" if everything is valid, otherwise return an error message.
|
return f"Invalid comparison operator '{cmp}' for attribute '{attrib.attrib_name}'."
|
||||||
|
if ref_attr not in [a.attrib_name for a in item_attributes]:
|
||||||
|
return f"Invalid reference attribute '{ref_attr}' for comparison in attribute '{attrib.attrib_name}'."
|
||||||
|
|
||||||
return "Ok"
|
return "Ok"
|
Loading…
Reference in New Issue
Block a user