File size: 4,502 Bytes
550665c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from gcsa.calendar import Calendar, CalendarListEntry
from .base_serializer import BaseSerializer
from .reminder_serializer import ReminderSerializer


class CalendarSerializer(BaseSerializer):
    type_ = Calendar

    def __init__(self, calendar):
        super().__init__(calendar)

    @staticmethod
    def _to_json(calendar: Calendar):
        data = {
            "id": calendar.calendar_id,
            "summary": calendar.summary,
            "description": calendar.description,
            "location": calendar.location,
            "timeZone": calendar.timezone,
        }
        if calendar.allowed_conference_solution_types:
            data["conferenceProperties"] = {
                "allowedConferenceSolutionTypes": calendar.allowed_conference_solution_types
            }

        data = CalendarSerializer._remove_empty_values(data)

        return data

    @staticmethod
    def _to_object(json_calendar):
        conference_properties = json_calendar.get('conferenceProperties', {})
        allowed_conference_solution_types = conference_properties.get('allowedConferenceSolutionTypes')
        return Calendar(
            summary=json_calendar.get('summary'),
            calendar_id=json_calendar.get('id'),
            description=json_calendar.get('description'),
            location=json_calendar.get('location'),
            timezone=json_calendar.get('timeZone'),
            allowed_conference_solution_types=allowed_conference_solution_types
        )


class CalendarListEntrySerializer(BaseSerializer):
    type_ = CalendarListEntry

    def __init__(self, calendar_list_entry):
        super().__init__(calendar_list_entry)

    @staticmethod
    def _to_json(calendar: CalendarListEntry):
        data = {
            "id": calendar.calendar_id,
            "summaryOverride": calendar.summary_override,
            "colorId": calendar.color_id,
            "backgroundColor": calendar.background_color,
            "foregroundColor": calendar.foreground_color,
            "hidden": calendar.hidden,
            "selected": calendar.selected,

        }
        if calendar.default_reminders:
            data["defaultReminders"] = [ReminderSerializer.to_json(r) for r in calendar.default_reminders]

        if calendar.notification_types:
            data["notificationSettings"] = {
                "notifications": [
                    {
                        "type": notification_type,
                        "method": "email"
                    }
                    for notification_type in calendar.notification_types
                ]
            }

        data = CalendarListEntrySerializer._remove_empty_values(data)

        return data

    @staticmethod
    def _to_object(json_calendar):
        conference_properties = json_calendar.pop('conferenceProperties', {})
        allowed_conference_solution_types = conference_properties.pop('allowedConferenceSolutionTypes', None)

        reminders_json = json_calendar.pop('defaultReminders', [])
        default_reminders = [ReminderSerializer.to_object(r) for r in reminders_json] if reminders_json else None

        notifications = json_calendar.pop('notificationSettings', {}).pop('notifications', None)
        notification_types = [n['type'] for n in notifications] if notifications else None

        return CalendarListEntry(
            calendar_id=json_calendar.pop('id'),
            summary_override=json_calendar.pop('summaryOverride', None),
            color_id=json_calendar.pop('colorId', None),
            background_color=json_calendar.pop('backgroundColor', None),
            foreground_color=json_calendar.pop('foregroundColor', None),
            hidden=json_calendar.pop('hidden', False),
            selected=json_calendar.pop('selected', False),
            default_reminders=default_reminders,
            notification_types=notification_types,
            _summary=json_calendar.pop('summary', None),
            _description=json_calendar.pop('description', None),
            _location=json_calendar.pop('location', None),
            _timezone=json_calendar.pop('timeZone', None),
            _allowed_conference_solution_types=allowed_conference_solution_types,
            _access_role=json_calendar.pop('accessRole', None),
            _primary=json_calendar.pop('primary', False),
            _deleted=json_calendar.pop('deleted', False)
        )