| from dataclasses import dataclass |
| from typing import Optional, Union |
| from datetime import datetime |
|
|
| @dataclass |
| class Post: |
| """Post model representing a social media post.""" |
| id: str |
| social_account_id: str |
| Text_content: str |
| is_published: bool = True |
| sched: Optional[str] = None |
| image_content_url: Optional[Union[str, bytes]] = None |
| created_at: Optional[datetime] = None |
| scheduled_at: Optional[datetime] = None |
| |
| @classmethod |
| def from_dict(cls, data: dict): |
| """Create a Post instance from a dictionary.""" |
| return cls( |
| id=data['id'], |
| social_account_id=data['social_account_id'], |
| Text_content=data['Text_content'], |
| is_published=data.get('is_published', False), |
| sched=data.get('sched'), |
| image_content_url=data.get('image_content_url'), |
| created_at=datetime.fromisoformat(data['created_at'].replace('Z', '+00:00')) if data.get('created_at') else None, |
| scheduled_at=datetime.fromisoformat(data['scheduled_at'].replace('Z', '+00:00')) if data.get('scheduled_at') else None |
| ) |
| |
| def to_dict(self): |
| """Convert Post instance to dictionary.""" |
| return { |
| 'id': self.id, |
| 'social_account_id': self.social_account_id, |
| 'Text_content': self.Text_content, |
| 'is_published': self.is_published, |
| 'sched': self.sched, |
| 'image_content_url': self.image_content_url, |
| 'created_at': self.created_at.isoformat() if self.created_at else None, |
| 'scheduled_at': self.scheduled_at.isoformat() if self.scheduled_at else None |
| } |