|
| 1 | +""" |
| 2 | +Configuration data models. |
| 3 | +""" |
| 4 | + |
| 5 | +from dataclasses import dataclass, asdict, field |
| 6 | +from datetime import datetime, timezone |
| 7 | +from typing import Optional, List |
| 8 | +import uuid |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class ConfigPreset: |
| 13 | + """Model for configuration presets.""" |
| 14 | + |
| 15 | + id: str |
| 16 | + name: str |
| 17 | + description: str |
| 18 | + config: dict |
| 19 | + visible_to_groups: List[str] = field(default_factory=list) |
| 20 | + created_at: Optional[str] = None |
| 21 | + created_by: Optional[str] = None |
| 22 | + updated_at: Optional[str] = None |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def create(cls, name: str, description: str, config: dict, |
| 26 | + created_by: Optional[str] = None, |
| 27 | + visible_to_groups: Optional[List[str]] = None) -> 'ConfigPreset': |
| 28 | + """Create a new ConfigPreset instance.""" |
| 29 | + now = datetime.now(timezone.utc).isoformat() |
| 30 | + return cls( |
| 31 | + id=str(uuid.uuid4()), |
| 32 | + name=name, |
| 33 | + description=description, |
| 34 | + config=config, |
| 35 | + visible_to_groups=visible_to_groups or [], |
| 36 | + created_at=now, |
| 37 | + created_by=created_by, |
| 38 | + updated_at=now |
| 39 | + ) |
| 40 | + |
| 41 | + def to_dict(self) -> dict: |
| 42 | + """Convert to dictionary for JSON serialization.""" |
| 43 | + return asdict(self) |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def from_dict(cls, data: dict) -> 'ConfigPreset': |
| 47 | + """Create instance from dictionary.""" |
| 48 | + return cls(**data) |
| 49 | + |
| 50 | + def update(self, **kwargs) -> None: |
| 51 | + """Update preset fields.""" |
| 52 | + for key, value in kwargs.items(): |
| 53 | + if hasattr(self, key) and key not in ['id', 'created_at', 'created_by']: |
| 54 | + setattr(self, key, value) |
| 55 | + self.updated_at = datetime.now(timezone.utc).isoformat() |
| 56 | + |
| 57 | + |
| 58 | +@dataclass |
| 59 | +class UserConfig: |
| 60 | + """Model for user configurations.""" |
| 61 | + |
| 62 | + user_id: str |
| 63 | + api_keys: dict = field(default_factory=dict) |
| 64 | + selected_preset_id: Optional[str] = None |
| 65 | + custom_settings: dict = field(default_factory=dict) |
| 66 | + config_mode: str = "server" # server or custom |
| 67 | + updated_at: Optional[str] = None |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def create(cls, user_id: str, **kwargs) -> 'UserConfig': |
| 71 | + """Create a new UserConfig instance.""" |
| 72 | + return cls( |
| 73 | + user_id=user_id, |
| 74 | + updated_at=datetime.now(timezone.utc).isoformat(), |
| 75 | + **kwargs |
| 76 | + ) |
| 77 | + |
| 78 | + def to_dict(self) -> dict: |
| 79 | + """Convert to dictionary for JSON serialization.""" |
| 80 | + return asdict(self) |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def from_dict(cls, data: dict) -> 'UserConfig': |
| 84 | + """Create instance from dictionary.""" |
| 85 | + return cls(**data) |
| 86 | + |
| 87 | + def update(self, **kwargs) -> None: |
| 88 | + """Update config fields.""" |
| 89 | + for key, value in kwargs.items(): |
| 90 | + if hasattr(self, key) and key != 'user_id': |
| 91 | + setattr(self, key, value) |
| 92 | + self.updated_at = datetime.now(timezone.utc).isoformat() |
0 commit comments