39 lines
1,015 B
Python
39 lines
1,015 B
Python
from typing import Protocol
|
|
|
|
from app.extensions import db
|
|
|
|
|
|
THEMES = {"auto", "light", "dark"}
|
|
NOTIFICATION_FREQUENCIES = {"quiet", "moderate", "alert"}
|
|
|
|
|
|
class SettingsUser(Protocol):
|
|
theme: str
|
|
dyslexia_font: bool
|
|
completion_chime: bool
|
|
notification_frequency: str
|
|
|
|
|
|
def update_preferences(
|
|
user: SettingsUser,
|
|
*,
|
|
theme: str,
|
|
dyslexia_font: bool,
|
|
completion_chime: bool,
|
|
notification_frequency: str,
|
|
) -> None:
|
|
if theme not in THEMES:
|
|
raise ValueError("Unknown theme.")
|
|
if notification_frequency not in NOTIFICATION_FREQUENCIES:
|
|
raise ValueError("Unknown notification frequency.")
|
|
if not isinstance(dyslexia_font, bool) or not isinstance(
|
|
completion_chime, bool
|
|
):
|
|
raise ValueError("Accessibility preferences must be true or false.")
|
|
|
|
user.theme = theme
|
|
user.dyslexia_font = dyslexia_font
|
|
user.completion_chime = completion_chime
|
|
user.notification_frequency = notification_frequency
|
|
db.session.commit()
|
|
|