-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpush_notification.py
More file actions
326 lines (251 loc) · 8.85 KB
/
push_notification.py
File metadata and controls
326 lines (251 loc) · 8.85 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# -*- coding: utf-8 -*-
from . import fields
from .base import Instance, Model
class DeviceBase(object):
"""
Base abstract class for GCM and APNS Devices;
"""
LINKS = (
{'type': 'detail', 'name': 'self'},
)
registration_id = fields.StringField(max_length=512, unique=True, primary_key=True)
device_id = fields.StringField(required=False)
is_active = fields.BooleanField(default=True)
label = fields.StringField(max_length=80)
user = fields.IntegerField(required=False)
links = fields.LinksField()
created_at = fields.DateTimeField(read_only=True, required=False)
updated_at = fields.DateTimeField(read_only=True, required=False)
class Meta:
abstract = True
def send_message(self, content):
"""
A method which allows to send message directly to the device;
:param contet: Message content structure - object like;
:return:
"""
send_message_path = self.links.send_message
data = {
'content': content
}
connection = self._get_connection()
response = connection.request('POST', send_message_path, data=data)
self.to_python(response)
return self
class GCMDevice(DeviceBase, Model):
"""
Model which handles the Google Cloud Message Device.
CORE supports only Create, Delete and Read;
Usage::
Create a new Device:
gcm_device = GCMDevice(
label='example label',
registration_id=86152312314401555,
user_id=u.id,
device_id='10000000001',
)
gcm_device.save()
Read:
gcm_device = GCMDevice.please.get(registration_id=86152312314401554)
Delete:
gcm_device.delete()
Update:
gcm_device.label = 'some new label'
gcm_device.save()
"""
class Meta:
parent = Instance
endpoints = {
'detail': {
'methods': ['delete', 'get', 'put', 'patch'],
'path': '/push_notifications/gcm/devices/{registration_id}/',
},
'list': {
'methods': ['post', 'get'],
'path': '/push_notifications/gcm/devices/',
}
}
class APNSDevice(DeviceBase, Model):
"""
Model which handles the Apple Push Notification Server Device.
CORE supports only Create, Delete and Read;
Usage::
Create a new Device:
apns_device = APNSDevice(
label='example label',
registration_id='4719084371920471208947120984731208947910827409128470912847120894',
user_id=u.id,
device_id='7189d7b9-4dea-4ecc-aa59-8cc61a20608a',
)
apns_device.save()
Read:
apns_device =
APNSDevice.please.get(registration_id='4719084371920471208947120984731208947910827409128470912847120894')
Delete:
apns_device.delete()
Update:
apns_device.label = 'some new label'
apns_device.save()
.. note::
Also note the different format (from GCM) of registration_id required by APNS; the device_id have different
format too.
"""
class Meta:
parent = Instance
endpoints = {
'detail': {
'methods': ['delete', 'get', 'put', 'patch'],
'path': '/push_notifications/apns/devices/{registration_id}/',
},
'list': {
'methods': ['post', 'get'],
'path': '/push_notifications/apns/devices/',
}
}
class MessageBase(object):
"""
Base abstract class for GCM and APNS Messages;
"""
status = fields.StringField(read_only=True)
content = fields.PushJSONField(default={})
result = fields.JSONField(default={}, read_only=True)
created_at = fields.DateTimeField(read_only=True, required=False)
updated_at = fields.DateTimeField(read_only=True, required=False)
class Meta:
abstract = True
class GCMMessage(MessageBase, Model):
"""
Model which handles the Google Cloud Messaging Message.
Only creating and reading is allowed.
Usage::
Create a new Message:
message = GCMMessage(
content={
'registration_ids': [gcm_device.registration_id], # maximum 1000 elements;
'data': {
'example_data_one': 1,
'example_data_two': 2,
}
}
)
message.save()
Read:
gcm_message = GCMMessage.please.get(id=1)
Debugging:
gcm_message.status - on of the (scheduled, error, partially_delivered, delivered)
gcm_message.result - a result from GCM server;
The data parameter is passed as-it-is to the GCM server; Base checking is made on syncano CORE;
For more details read the GCM documentation;
.. note::
Every save after initial one will raise an error;
.. note::
The altering of existing Message is not possible. It also not possible to delete message.
"""
class Meta:
parent = Instance
endpoints = {
'detail': {
'methods': ['delete', 'get'],
'path': '/push_notifications/gcm/messages/{id}/',
},
'list': {
'methods': ['get', 'post'],
'path': '/push_notifications/gcm/messages/',
}
}
class APNSMessage(MessageBase, Model):
"""
Model which handles the Apple Push Notification Server Message.
Only creating and reading is allowed.
Usage::
Create new Message:
apns_message = APNSMessage(
content={
'registration_ids': [gcm_device.registration_id],
'aps': {'alert': 'test alert'},
}
)
apns_message.save()
Read:
apns_message = APNSMessage.please.get(id=1)
Debugging:
apns_message.status - one of the following: scheduled, error, partially_delivered, delivered;
apns_message.result - a result from APNS server;
The 'aps' data is send 'as-it-is' to APNS, some validation is made on syncano CORE;
For more details read the APNS documentation;
.. note::
Every save after initial one will raise an error;
"""
class Meta:
parent = Instance
endpoints = {
'detail': {
'methods': ['delete', 'get'],
'path': '/push_notifications/apns/messages/{id}/',
},
'list': {
'methods': ['get', 'post'],
'path': '/push_notifications/apns/messages/',
}
}
class GCMConfig(Model):
"""
A model which stores information with GCM Push keys;
Usage::
Add (modify) new keys:
gcm_config = GCMConfig(production_api_key='ccc', development_api_key='ddd')
gcm_config.save()
or:
gcm_config = GCMConfig().please.get()
gcm_config.production_api_key = 'ccc'
gcm_config.development_api_key = 'ddd'
gcm_config.save()
"""
production_api_key = fields.StringField(required=False)
development_api_key = fields.StringField(required=False)
def is_new(self):
return False # this is predefined - never will be new
class Meta:
parent = Instance
endpoints = {
'list': {
'methods': ['get', 'put'],
'path': '/push_notifications/gcm/config/',
},
'detail': {
'methods': ['get', 'put'],
'path': '/push_notifications/gcm/config/',
},
}
class APNSConfig(Model):
"""
A model which stores information with APNS Push certificates;
Usage::
Add (modify) new keys:
cert_file = open('cert_file.p12', 'rb')
apns_config = APNSConfig(development_certificate=cert_file)
apns_config.save()
cert_file.close()
"""
production_certificate_name = fields.StringField(required=False)
production_certificate = fields.FileField(required=False)
production_bundle_identifier = fields.StringField(required=False)
production_expiration_date = fields.DateField(read_only=True)
development_certificate_name = fields.StringField(required=False)
development_certificate = fields.FileField(required=False)
development_bundle_identifier = fields.StringField(required=False)
development_expiration_date = fields.DateField(read_only=True)
def is_new(self):
return False # this is predefined - never will be new
class Meta:
parent = Instance
endpoints = {
'list': {
'methods': ['get', 'put'],
'path': '/push_notifications/apns/config/',
},
'detail': {
'methods': ['get', 'put'],
'path': '/push_notifications/apns/config/',
},
}