Spaces:
Runtime error
Runtime error
| """ | |
| Service for user settings management. | |
| """ | |
| from sqlalchemy.orm import Session | |
| from typing import Optional, Dict | |
| from datetime import datetime | |
| from app.database.models import UserSettings | |
| class SettingsService: | |
| """Service for managing user settings and preferences.""" | |
| def get_or_create_settings(db: Session, user_id: str) -> UserSettings: | |
| """ | |
| Get user settings or create default if not exists. | |
| Args: | |
| db: Database session | |
| user_id: User ID | |
| Returns: | |
| UserSettings object | |
| """ | |
| settings = db.query(UserSettings).filter(UserSettings.user_id == user_id).first() | |
| if not settings: | |
| # Create default settings | |
| settings = UserSettings(user_id=user_id) | |
| db.add(settings) | |
| db.commit() | |
| db.refresh(settings) | |
| return settings | |
| def update_profile( | |
| db: Session, | |
| user_id: str, | |
| bio: Optional[str] = None, | |
| phone: Optional[str] = None, | |
| company: Optional[str] = None | |
| ) -> UserSettings: | |
| """ | |
| Update user profile settings. | |
| Args: | |
| db: Database session | |
| user_id: User ID | |
| bio: User bio | |
| phone: Phone number | |
| company: Company name | |
| Returns: | |
| Updated UserSettings object | |
| """ | |
| settings = SettingsService.get_or_create_settings(db, user_id) | |
| if bio is not None: | |
| settings.bio = bio | |
| if phone is not None: | |
| settings.phone = phone | |
| if company is not None: | |
| settings.company = company | |
| settings.updated_at = datetime.utcnow() | |
| db.commit() | |
| db.refresh(settings) | |
| return settings | |
| def update_appearance( | |
| db: Session, | |
| user_id: str, | |
| theme: str | |
| ) -> UserSettings: | |
| """ | |
| Update appearance settings. | |
| Args: | |
| db: Database session | |
| user_id: User ID | |
| theme: Theme preference (light, dark, system) | |
| Returns: | |
| Updated UserSettings object | |
| """ | |
| if theme not in ['light', 'dark', 'system']: | |
| raise ValueError("Theme must be 'light', 'dark', or 'system'") | |
| settings = SettingsService.get_or_create_settings(db, user_id) | |
| settings.theme = theme | |
| settings.updated_at = datetime.utcnow() | |
| db.commit() | |
| db.refresh(settings) | |
| return settings | |
| def update_notifications( | |
| db: Session, | |
| user_id: str, | |
| email_notifications: Optional[bool] = None, | |
| update_notifications: Optional[bool] = None | |
| ) -> UserSettings: | |
| """ | |
| Update notification settings. | |
| Args: | |
| db: Database session | |
| user_id: User ID | |
| email_notifications: Enable/disable email notifications | |
| update_notifications: Enable/disable update notifications | |
| Returns: | |
| Updated UserSettings object | |
| """ | |
| settings = SettingsService.get_or_create_settings(db, user_id) | |
| if email_notifications is not None: | |
| settings.email_notifications = 1 if email_notifications else 0 | |
| if update_notifications is not None: | |
| settings.update_notifications = 1 if update_notifications else 0 | |
| settings.updated_at = datetime.utcnow() | |
| db.commit() | |
| db.refresh(settings) | |
| return settings | |
| # Global service instance | |
| settings_service = SettingsService() | |