Lin / backend /models /source.py
Zelyanoth's picture
fff
25f22bf
raw
history blame
1.29 kB
from dataclasses import dataclass
from typing import Optional
from datetime import datetime
@dataclass
class Source:
"""Source model representing an RSS source."""
id: str
user_id: str
source: str
category: Optional[str] = None
last_update: Optional[datetime] = None
created_at: Optional[datetime] = None
@classmethod
def from_dict(cls, data: dict):
"""Create a Source instance from a dictionary."""
return cls(
id=data['id'],
user_id=data['user_id'],
source=data['source'],
category=data.get('category'),
last_update=datetime.fromisoformat(data['last_update'].replace('Z', '+00:00')) if data.get('last_update') else None,
created_at=datetime.fromisoformat(data['created_at'].replace('Z', '+00:00')) if data.get('created_at') else None
)
def to_dict(self):
"""Convert Source instance to dictionary."""
return {
'id': self.id,
'user_id': self.user_id,
'source': self.source,
'category': self.category,
'last_update': self.last_update.isoformat() if self.last_update else None,
'created_at': self.created_at.isoformat() if self.created_at else None
}