File size: 1,658 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
37
38
39
40
41
42
from dataclasses import dataclass
from typing import Optional
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[str] = 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
        }