File size: 1,291 Bytes
25f22bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
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
} |