summaryrefslogtreecommitdiff
path: root/calendar_entry.py
blob: 1066f8d2890675b1b97e265f8a6a8f31a5827f0b (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# calendar_entry.py
import uuid
from datetime import datetime

class CalendarEntry:
    """Represents a single calendar entry with all its properties"""
    
    def __init__(self, start_date, end_date, keyword, entry_id=None, 
                 corrected_start_date=None, corrected_end_date=None, 
                 time_period=None, commentary: str = ""):
        self.id = entry_id if entry_id else str(uuid.uuid4())
        
        # Convert string dates to datetime if necessary
        self.start_date = (
            datetime.fromisoformat(start_date) 
            if isinstance(start_date, str) 
            else start_date
        )
        self.end_date = (
            datetime.fromisoformat(end_date) 
            if isinstance(end_date, str) 
            else end_date
        )
        
        self.keyword = keyword
        self.corrected_start_date = corrected_start_date
        self.corrected_end_date = corrected_end_date
        self.time_period = time_period
        self.commentary = commentary
    
    def to_dict(self):
        """Convert entry to dictionary for serialization"""
        return {
            'id': self.id,
            'start_date': self.start_date.isoformat(),
            'end_date': self.end_date.isoformat(),
            'keyword': self.keyword,
            'corrected_start_date': self.corrected_start_date.isoformat() if self.corrected_start_date else None,
            'corrected_end_date': self.corrected_end_date.isoformat() if self.corrected_end_date else None,
            'commentary': self.commentary,         
        }
    
    @classmethod
    def from_dict(cls, data):
        """Create entry from dictionary"""
        return cls(
            start_date=datetime.fromisoformat(data['start_date']),
            end_date=datetime.fromisoformat(data['end_date']),
            keyword=data['keyword'],
            entry_id=data['id'],
            corrected_start_date=datetime.fromisoformat(data['corrected_start_date']) if data.get('corrected_start_date') else None,
            corrected_end_date=datetime.fromisoformat(data['corrected_end_date']) if data.get('corrected_end_date') else None,
            commentary=data.get('commentary', ""),
        )

    def __repr__(self):
        return (f"CalendarEntry(id={self.id}, start_date={self.start_date}, "
                f"end_date={self.end_date}, keyword='{self.keyword}', "
                f"corrected_start_date={self.corrected_start_date}, corrected_end_date={self.corrected_end_date})")