Use dataclass for class definitions
This commit is contained in:
parent
e00b1a128c
commit
3731a68d3c
@ -1,30 +1,21 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import re
|
import re
|
||||||
from typing import List, Optional, Tuple
|
from typing import List, Optional, Tuple
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
ALLOWED_INPUT_TYPES = {"text", "number", "date", "select"}
|
ALLOWED_INPUT_TYPES = {"text", "number", "date", "select"}
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class Attribute:
|
class Attribute:
|
||||||
"""Base class for all attribute types."""
|
"""Base class for all attribute types."""
|
||||||
def __init__(
|
attrib_name: str
|
||||||
self,
|
display_name: str
|
||||||
attrib_name: str,
|
html_input_type: str
|
||||||
display_name: str,
|
required: bool = False
|
||||||
html_input_type: str,
|
unique: bool = False
|
||||||
required: bool = False,
|
primary: bool = False
|
||||||
unique: bool = False,
|
default_val: Optional[str] = None
|
||||||
primary: bool = False,
|
compareto: Optional[List[Tuple[str, str]]] = None
|
||||||
default_val: Optional[str] = None,
|
|
||||||
compareto: Optional[List[Tuple[str, str]]] = None,
|
|
||||||
):
|
|
||||||
self.attrib_name = attrib_name
|
|
||||||
self.display_name = display_name
|
|
||||||
self.html_input_type = html_input_type
|
|
||||||
self.required = required
|
|
||||||
self.unique = unique
|
|
||||||
self.primary = primary
|
|
||||||
self.default_val = default_val
|
|
||||||
self.compareto = compareto
|
|
||||||
|
|
||||||
def validate(self) -> Optional[str]:
|
def validate(self) -> Optional[str]:
|
||||||
"""Validate common attributes. Returns an error message if invalid, otherwise None."""
|
"""Validate common attributes. Returns an error message if invalid, otherwise None."""
|
||||||
@ -38,24 +29,17 @@ class Attribute:
|
|||||||
return f"Invalid input type '{self.html_input_type}' for attribute '{self.attrib_name}'."
|
return f"Invalid input type '{self.html_input_type}' for attribute '{self.attrib_name}'."
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class textAttribute(Attribute):
|
class textAttribute(Attribute):
|
||||||
"""Class for text attributes."""
|
"""Class for text attributes."""
|
||||||
def __init__(
|
regex: Optional[str] = None
|
||||||
self,
|
min_length: Optional[int] = None
|
||||||
attrib_name: str,
|
max_length: Optional[int] = 50
|
||||||
display_name: str,
|
allowed_chars: Optional[str] = None
|
||||||
regex: Optional[str] = None,
|
|
||||||
min_length: Optional[int] = None,
|
def __post_init__(self):
|
||||||
max_length: Optional[int] = 50,
|
"""Post-initialization to set the HTML input type."""
|
||||||
allowed_chars: Optional[str] = None,
|
self.html_input_type = "text"
|
||||||
**kwargs
|
|
||||||
):
|
|
||||||
super().__init__(attrib_name, display_name, html_input_type="text", **kwargs)
|
|
||||||
self.regex = regex
|
|
||||||
self.allowed_chars = allowed_chars
|
|
||||||
self.min_length = min_length
|
|
||||||
self.max_length = max_length
|
|
||||||
|
|
||||||
def validate(self) -> Optional[str]:
|
def validate(self) -> Optional[str]:
|
||||||
"""Validate text-specific attributes."""
|
"""Validate text-specific attributes."""
|
||||||
@ -84,22 +68,16 @@ class textAttribute(Attribute):
|
|||||||
return f"Invalid default value for '{self.attrib_name}'. The maximum length is: {self.max_length}"
|
return f"Invalid default value for '{self.attrib_name}'. The maximum length is: {self.max_length}"
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class intAttribute(Attribute):
|
class intAttribute(Attribute):
|
||||||
"""Class for integer attributes."""
|
"""Class for integer attributes."""
|
||||||
def __init__(
|
min_val: Optional[int] = None
|
||||||
self,
|
max_val: Optional[int] = None
|
||||||
attrib_name: str,
|
step: int = 1
|
||||||
display_name: str,
|
|
||||||
min_val: Optional[int] = None,
|
def __post_init__(self):
|
||||||
max_val: Optional[int] = None,
|
"""Post-initialization to set the HTML input type."""
|
||||||
step: int = 1,
|
self.html_input_type = "number"
|
||||||
**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]:
|
def validate(self) -> Optional[str]:
|
||||||
"""Validate integer-specific attributes."""
|
"""Validate integer-specific attributes."""
|
||||||
@ -130,22 +108,16 @@ class intAttribute(Attribute):
|
|||||||
return f"default_val cannot be greater than max_val for attribute '{self.attrib_name}'."
|
return f"default_val cannot be greater than max_val for attribute '{self.attrib_name}'."
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class floatAttribute(Attribute):
|
class floatAttribute(Attribute):
|
||||||
"""Class for float attributes."""
|
"""Class for float attributes."""
|
||||||
def __init__(
|
min_val: Optional[float] = None
|
||||||
self,
|
max_val: Optional[float] = None
|
||||||
attrib_name: str,
|
step: float = 0.1
|
||||||
display_name: str,
|
|
||||||
min_val: Optional[float] = None,
|
def __post_init__(self):
|
||||||
max_val: Optional[float] = None,
|
"""Post-initialization to set the HTML input type."""
|
||||||
step: float = 0.1,
|
self.html_input_type = "number"
|
||||||
**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]:
|
def validate(self) -> Optional[str]:
|
||||||
"""Validate float-specific attributes."""
|
"""Validate float-specific attributes."""
|
||||||
@ -159,7 +131,7 @@ class floatAttribute(Attribute):
|
|||||||
if self.min_val is not None and self.max_val is not None and self.min_val > self.max_val:
|
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}'."
|
return f"min_val cannot be greater than max_val for attribute '{self.attrib_name}'."
|
||||||
if self.step is not None:
|
if self.step is not None:
|
||||||
if not isinstance(self.step, float):
|
if not isinstance(self.step, (int, float)):
|
||||||
return f"Step must be an float for attribute '{self.attrib_name}'."
|
return f"Step must be an float for attribute '{self.attrib_name}'."
|
||||||
if self.step <= 0:
|
if self.step <= 0:
|
||||||
return f"Step must be a positive float for attribute '{self.attrib_name}'."
|
return f"Step must be a positive float for attribute '{self.attrib_name}'."
|
||||||
@ -176,20 +148,15 @@ class floatAttribute(Attribute):
|
|||||||
return f"default_val cannot be greater than max_val for attribute '{self.attrib_name}'."
|
return f"default_val cannot be greater than max_val for attribute '{self.attrib_name}'."
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class dateAttribute(Attribute):
|
class dateAttribute(Attribute):
|
||||||
"""Class for date attributes."""
|
"""Class for date attributes."""
|
||||||
def __init__(
|
min_val: Optional[str] = None
|
||||||
self,
|
max_val: Optional[str] = None
|
||||||
attrib_name: str,
|
|
||||||
display_name: str,
|
def __post_init__(self):
|
||||||
min_val: Optional[str] = None,
|
"""Post-initialization to set the HTML input type."""
|
||||||
max_val: Optional[str] = None,
|
self.html_input_type = "date"
|
||||||
**kwargs
|
|
||||||
):
|
|
||||||
super().__init__(attrib_name, display_name, html_input_type="date", **kwargs)
|
|
||||||
self.min_val = min_val
|
|
||||||
self.max_val = max_val
|
|
||||||
|
|
||||||
def _is_date(self, value: str) -> bool:
|
def _is_date(self, value: str) -> 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."""
|
||||||
@ -227,18 +194,14 @@ class dateAttribute(Attribute):
|
|||||||
return f"default_val cannot be later than max_val for attribute '{self.attrib_name}'."
|
return f"default_val cannot be later than max_val for attribute '{self.attrib_name}'."
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class selectAttribute(Attribute):
|
class selectAttribute(Attribute):
|
||||||
"""Class for select attributes."""
|
"""Class for select attributes."""
|
||||||
def __init__(
|
options: List[str]
|
||||||
self,
|
|
||||||
attrib_name: str,
|
def __post_init__(self):
|
||||||
display_name: str,
|
"""Post-initialization to set the HTML input type."""
|
||||||
options: List[str],
|
self.html_input_type = "select"
|
||||||
**kwargs
|
|
||||||
):
|
|
||||||
super().__init__(attrib_name, display_name, html_input_type="select", **kwargs)
|
|
||||||
self.options = options
|
|
||||||
|
|
||||||
def validate(self) -> Optional[str]:
|
def validate(self) -> Optional[str]:
|
||||||
"""Validate select-specific attributes."""
|
"""Validate select-specific attributes."""
|
||||||
|
Loading…
Reference in New Issue
Block a user