Source code for simdb.database.models.watcher
from typing import Any, Dict, Final
from email_validator import validate_email
from sqlalchemy import Column
from sqlalchemy import types as sql_types
from sqlalchemy.orm import validates
from simdb.docstrings import inherit_docstrings
from simdb.notifications import Notification
from simdb.remote.models import WatcherData
from .base import Base
from .types import ChoiceType
from .utils import checked_get
[docs]
@inherit_docstrings
class Watcher(Base):
"""
Class to represent people watching simulations for updates.
"""
NOTIFICATION_CHOICES: Final[Dict[Any, str]] = {
Notification.VALIDATION: "V",
Notification.REVISION: "R",
Notification.OBSOLESCENCE: "O",
Notification.ALL: "A",
}
__tablename__ = "watchers"
id = Column(sql_types.Integer, primary_key=True)
username = Column(sql_types.String(250))
email = Column(sql_types.String(1000), nullable=False)
notification = Column(
ChoiceType(choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification)
)
[docs]
@validates("email")
def validate_email(self, key, address):
validate_email(address)
return address
def __init__(self, username: str, email: str, notification: "Watcher.Notification"):
self.username = username
self.email = email
self.notification = notification
[docs]
@classmethod
def from_data(cls, data: Dict) -> "Watcher":
username = checked_get(data, "username", str)
email = checked_get(data, "email", str)
notification = checked_get(data, "notification", str)
watcher = Watcher(username, email, notification)
return watcher
[docs]
def data(self, recurse: bool = False) -> Dict[str, str]:
data = {
"username": self.username,
"email": self.email,
"notification": str(self.notification),
}
return data
[docs]
def to_model(self) -> WatcherData:
return WatcherData.model_validate(self.data())