This repository was archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathnotification.py
More file actions
222 lines (173 loc) · 5.86 KB
/
notification.py
File metadata and controls
222 lines (173 loc) · 5.86 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""Notification class."""
import json
class Notification():
"""Notification class."""
SEGMENTS_MODE = 'segments'
DEVICES_MODE = 'devices'
FILTERS_MODE = 'filters'
DEFAULT_LANGUAGE = 'en'
NOTIFICATION_MODES = [SEGMENTS_MODE, DEVICES_MODE, FILTERS_MODE]
IOS_BADGE_TYPE_NONE = 'None'
IOS_BADGE_TYPE_SETTO = 'SetTo'
IOS_BADGE_TYPE_INCREASE = 'Increase'
IOS_BADGES_TYPES = [
IOS_BADGE_TYPE_NONE, IOS_BADGE_TYPE_SETTO, IOS_BADGE_TYPE_INCREASE
]
# Mode Settings
@property
def mode(self):
return self._mode
@mode.setter
def mode(self, value):
if value not in self.NOTIFICATION_MODES:
raise ValueError('Unknown operation mode.')
self._mode = value
# Device mode properties
@property
def include_player_ids(self):
return self._include_player_ids
@include_player_ids.setter
def include_player_ids(self, value):
if self.mode != self.DEVICES_MODE:
raise TypeError('Mode should be set to device.')
if not isinstance(value, list):
raise TypeError('Value must be a list.')
self._include_player_ids = value
# Common Parameters - App
@property
def app_id(self):
return self._app_id
@app_id.setter
def app_id(self, value):
self._app_id = value
# Common Parameters - Content & Language
@property
def contents(self):
return json.loads(self._contents)
@contents.setter
def contents(self, value):
self._validate_content_dict(value)
self._contents = json.dumps(value)
@property
def headings(self):
return json.loads(self._headings)
@headings.setter
def headings(self, value):
self._validate_content_dict(value)
self._headings = json.dumps(value)
@property
def subtitle(self):
return json.loads(self._subtitle)
@subtitle.setter
def subtitle(self, value):
self._validate_content_dict(value)
self._subtitle = json.dumps(value)
# Common Parameters - Attachments
@property
def data(self):
return json.loads(self._data)
@data.setter
def data(self, value):
if isinstance(value, str):
value = json.loads(value)
if not isinstance(value, dict):
raise TypeError('Value must be a dict.')
self._data = json.dumps(value)
# Common Parameters - Appearance
@property
def small_icon(self):
return self._small_icon
@small_icon.setter
def small_icon(self, value):
self._small_icon = str(value) if value is not None else None
@property
def large_icon(self):
return self._large_icon
@large_icon.setter
def large_icon(self, value):
self._large_icon = str(value) if value is not None else None
@property
def ios_badge_type(self):
return self._ios_badge_type
@ios_badge_type.setter
def ios_badge_type(self, value):
if value not in self.IOS_BADGES_TYPES:
raise TypeError('Unknown badge type.')
self._ios_badge_type = value
@property
def ios_badge_count(self):
return self._ios_badge_count
@ios_badge_count.setter
def ios_badge_count(self, value):
self._ios_badge_count = int(value)
def __init__(self, app_id, mode=SEGMENTS_MODE):
self.app_id = app_id
self.mode = mode
# Device defaults
self._include_player_ids = []
# Common defaults
self.contents = {'en': 'Default message.'}
self.headings = {}
self.subtitle = {}
self.send_after = None
self.url = None
self.data = {}
self.small_icon = None
self.large_icon = None
self.ios_badge_type = self.IOS_BADGE_TYPE_NONE
self.ios_badge_count = 0
self.image = None
def _validate_content_dict(self, value):
"""
Validates dicts used for content properties.
Ex: headings, subtitle, contents.
"""
if isinstance(value, str):
value = json.loads(value)
if not isinstance(value, dict):
raise TypeError('Value must be a dict.')
if len(value) > 0 and not value.get(self.DEFAULT_LANGUAGE, False):
raise KeyError('Default language (%s) must be included.' % (
self.DEFAULT_LANGUAGE))
return True
def get_payload_for_request(self):
"""
Get the JSON data to be sent to /notifications post.
"""
payload = {
'app_id': self.app_id,
# Should change when template/content_available support be done
'contents': self.contents,
'android_accent_color': 'FFE42D1F',
}
# Mode related settings
if self.mode == self.DEVICES_MODE:
payload.update({'include_player_ids': self.include_player_ids})
# Common parameters
payload.update({'data': self.data})
if len(self.headings) > 0:
payload.update({'headings': self.headings})
if len(self.subtitle) > 0:
payload.update({'subtitle': self.subtitle})
if self.send_after:
payload.update({'send_after': self.send_after})
if self.url:
payload.update({'url': self.url})
if self.small_icon:
payload.update({'small_icon': self.small_icon})
if self.large_icon:
payload.update({'large_icon': self.large_icon})
if self.image:
payload.update({
'mutable_content': True,
'ios_attachments': {
'id': self.image
},
'big_picture': self.image
})
if self.ios_badge_count > 0:
payload.update({
'ios_badgeType': self.ios_badge_type,
'ios_badgeCount': self.ios_badge_count
})
return payload