- Split `definitions/attribute.py` into separate files under `definitions/attributes/`. - Added `__init__.py` for simplified imports. - Improved code organization and maintainability.
29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
from typing import List, Optional, Tuple, Set
|
|
from dataclasses import dataclass, field
|
|
|
|
ALLOWED_INPUT_TYPES = {"text", "number", "date", "select"}
|
|
|
|
@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 |