assettrack/definitions/attributes/Attribute.py
2025-03-15 09:42:47 +05:30

35 lines
1.5 KiB
Python

from typing import List, Optional, Tuple, Set
from dataclasses import dataclass, field
ALLOWED_INPUT_TYPES = {"text", "number", "date", "select"}
VALID_COMPARISONS = {
"text": {"lt", "st", "eq", "ne"}, # longer than, shorter than, eq, or not eq, to the ref attrib
"number": {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, !=
"date": {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, !=
"select": {"eq", "ne"} # eq, or not eq, to the ref attrib
}
@dataclass
class Attribute:
"""Base class for all attribute types."""
attrib_name: str
display_name: str
html_input_type: Optional[str] = "text"
required: bool = False
unique: bool = False
primary: bool = False
default_val: Optional[str] = None
compareto: Optional[List[Tuple[str, str]]] = None
#valid_comparisons: Optional[Set[str]] = None
def validate(self) -> Optional[str]:
"""Validate common attributes. Returns an error message if invalid, otherwise None."""
if not self.attrib_name:
return "Missing attribute name."
if not self.display_name:
return f"Missing display name for attribute '{self.attrib_name}'."
if not self.html_input_type:
return f"Missing input type for attribute '{self.attrib_name}'."
if self.html_input_type not in ALLOWED_INPUT_TYPES:
return f"Invalid input type '{self.html_input_type}' for attribute '{self.attrib_name}'."
return None