| 1 |
from __future__ import unicode_literals |
| 2 |
from django.db import models |
| 3 |
from django.utils.encoding import python_2_unicode_compatible |
| 4 |
from django.utils.translation import ugettext_lazy as _ |
| 5 |
|
| 6 |
from .fields import HexIntegerField |
| 7 |
from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS |
| 8 |
|
| 9 |
|
| 10 |
CLOUD_MESSAGE_TYPES = ( |
| 11 |
("FCM", "Firebase Cloud Message"), |
| 12 |
("GCM", "Google Cloud Message"), |
| 13 |
) |
| 14 |
|
| 15 |
BROWSER_TYPES = ( |
| 16 |
("CHROME", "Chrome"), |
| 17 |
("FIREFOX", "Firefox"), |
| 18 |
("OPERA", "Opera"), |
| 19 |
) |
| 20 |
|
| 21 |
@python_2_unicode_compatible |
| 22 |
class Device(models.Model): |
| 23 |
name = models.CharField(max_length=255, verbose_name=_("Name"), blank=True, null=True) |
| 24 |
active = models.BooleanField( |
| 25 |
verbose_name=_("Is active"), default=True, |
| 26 |
help_text=_("Inactive devices will not be sent notifications") |
| 27 |
) |
| 28 |
user = models.ForeignKey( |
| 29 |
SETTINGS["USER_MODEL"], blank=True, null=True, on_delete=models.CASCADE |
| 30 |
) |
| 31 |
date_created = models.DateTimeField( |
| 32 |
verbose_name=_("Creation date"), auto_now_add=True, null=True |
| 33 |
) |
| 34 |
application_id = models.CharField( |
| 35 |
max_length=64, verbose_name=_("Application ID"), |
| 36 |
help_text=_( |
| 37 |
"Opaque application identity, should be filled in for multiple" |
| 38 |
" key/certificate access" |
| 39 |
), |
| 40 |
blank=True, null=True |
| 41 |
) |
| 42 |
|
| 43 |
class Meta: |
| 44 |
abstract = True |
| 45 |
|
| 46 |
def __str__(self): |
| 47 |
return ( |
| 48 |
self.name or str(self.device_id or "") or |
| 49 |
"%s for %s" % (self.__class__.__name__, self.user or "unknown user") |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
class GCMDeviceManager(models.Manager): |
| 54 |
def get_queryset(self): |
| 55 |
return GCMDeviceQuerySet(self.model) |
| 56 |
|
| 57 |
|
| 58 |
class GCMDeviceQuerySet(models.query.QuerySet): |
| 59 |
def send_message(self, message, **kwargs): |
| 60 |
if self: |
| 61 |
from .gcm import send_message as gcm_send_message |
| 62 |
|
| 63 |
data = kwargs.pop("extra", {}) |
| 64 |
if message is not None: |
| 65 |
data["message"] = message |
| 66 |
|
| 67 |
app_ids = self.filter(active=True).order_by( |
| 68 |
"application_id" |
| 69 |
).values_list("application_id", flat=True).distinct() |
| 70 |
response = [] |
| 71 |
for cloud_type in ("FCM", "GCM"): |
| 72 |
for app_id in app_ids: |
| 73 |
reg_ids = list( |
| 74 |
self.filter( |
| 75 |
active=True, cloud_message_type=cloud_type, application_id=app_id).values_list( |
| 76 |
"registration_id", flat=True |
| 77 |
) |
| 78 |
) |
| 79 |
if reg_ids: |
| 80 |
r = gcm_send_message(reg_ids, data, cloud_type, application_id=app_id, **kwargs) |
| 81 |
response.append(r) |
| 82 |
|
| 83 |
return response |
| 84 |
|
| 85 |
|
| 86 |
class GCMDevice(Device): |
| 87 |
# device_id cannot be a reliable primary key as fragmentation between different devices |
| 88 |
# can make it turn out to be null and such: |
| 89 |
# http://android-developers.blogspot.co.uk/2011/03/identifying-app-installations.html |
| 90 |
device_id = HexIntegerField( |
| 91 |
verbose_name=_("Device ID"), blank=True, null=True, db_index=True, |
| 92 |
help_text=_("ANDROID_ID / TelephonyManager.getDeviceId() (always as hex)") |
| 93 |
) |
| 94 |
registration_id = models.TextField(verbose_name=_("Registration ID")) |
| 95 |
cloud_message_type = models.CharField( |
| 96 |
verbose_name=_("Cloud Message Type"), max_length=3, |
| 97 |
choices=CLOUD_MESSAGE_TYPES, default="GCM", |
| 98 |
help_text=_("You should choose FCM or GCM") |
| 99 |
) |
| 100 |
objects = GCMDeviceManager() |
| 101 |
|
| 102 |
class Meta: |
| 103 |
verbose_name = _("GCM device") |
| 104 |
|
| 105 |
def send_message(self, message, **kwargs): |
| 106 |
from .gcm import send_message as gcm_send_message |
| 107 |
|
| 108 |
data = kwargs.pop("extra", {}) |
| 109 |
if message is not None: |
| 110 |
data["message"] = message |
| 111 |
|
| 112 |
return gcm_send_message( |
| 113 |
self.registration_id, data, self.cloud_message_type, |
| 114 |
application_id=self.application_id, **kwargs |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
class APNSDeviceManager(models.Manager): |
| 119 |
def get_queryset(self): |
| 120 |
return APNSDeviceQuerySet(self.model) |
| 121 |
|
| 122 |
|
| 123 |
class APNSDeviceQuerySet(models.query.QuerySet): |
| 124 |
def send_message(self, message, certfile=None, **kwargs): |
| 125 |
if self: |
| 126 |
from .apns import apns_send_bulk_message |
| 127 |
|
| 128 |
app_ids = self.filter(active=True).order_by("application_id")\ |
| 129 |
.values_list("application_id", flat=True).distinct() |
| 130 |
res = [] |
| 131 |
for app_id in app_ids: |
| 132 |
reg_ids = list(self.filter(active=True, application_id=app_id).values_list( |
| 133 |
"registration_id", flat=True) |
| 134 |
) |
| 135 |
r = apns_send_bulk_message( |
| 136 |
registration_ids=reg_ids, alert=message, application_id=app_id, |
| 137 |
certfile=certfile, **kwargs |
| 138 |
) |
| 139 |
if hasattr(r, "keys"): |
| 140 |
res += [r] |
| 141 |
elif hasattr(r, "__getitem__"): |
| 142 |
res += r |
| 143 |
return res |
| 144 |
|
| 145 |
|
| 146 |
class APNSDevice(Device): |
| 147 |
device_id = models.UUIDField( |
| 148 |
verbose_name=_("Device ID"), blank=True, null=True, db_index=True, |
| 149 |
help_text="UDID / UIDevice.identifierForVendor()" |
| 150 |
) |
| 151 |
registration_id = models.CharField( |
| 152 |
verbose_name=_("Registration ID"), max_length=200, unique=True |
| 153 |
) |
| 154 |
|
| 155 |
objects = APNSDeviceManager() |
| 156 |
|
| 157 |
class Meta: |
| 158 |
verbose_name = _("APNS device") |
| 159 |
|
| 160 |
def send_message(self, message, certfile=None, **kwargs): |
| 161 |
from .apns import apns_send_message |
| 162 |
|
| 163 |
return apns_send_message( |
| 164 |
registration_id=self.registration_id, |
| 165 |
alert=message, |
| 166 |
application_id=self.application_id, certfile=certfile, |
| 167 |
**kwargs |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
class WNSDeviceManager(models.Manager): |
| 172 |
def get_queryset(self): |
| 173 |
return WNSDeviceQuerySet(self.model) |
| 174 |
|
| 175 |
|
| 176 |
class WNSDeviceQuerySet(models.query.QuerySet): |
| 177 |
def send_message(self, message, **kwargs): |
| 178 |
if self: |
| 179 |
from .wns import wns_send_bulk_message |
| 180 |
|
| 181 |
app_ids = self.filter(active=True).order_by("application_id")\ |
| 182 |
.values_list("application_id", flat=True).distinct() |
| 183 |
res = [] |
| 184 |
for app_id in app_ids: |
| 185 |
reg_ids = list(self.filter(active=True, application_id=app_id).values_list( |
| 186 |
"registration_id", flat=True |
| 187 |
)) |
| 188 |
r = wns_send_bulk_message(uri_list=reg_ids, message=message, **kwargs) |
| 189 |
if hasattr(r, "keys"): |
| 190 |
res += [r] |
| 191 |
elif hasattr(r, "__getitem__"): |
| 192 |
res += r |
| 193 |
return res |
| 194 |
|
| 195 |
|
| 196 |
class WNSDevice(Device): |
| 197 |
device_id = models.UUIDField( |
| 198 |
verbose_name=_("Device ID"), blank=True, null=True, db_index=True, |
| 199 |
help_text=_("GUID()") |
| 200 |
) |
| 201 |
registration_id = models.TextField(verbose_name=_("Notification URI")) |
| 202 |
|
| 203 |
objects = WNSDeviceManager() |
| 204 |
|
| 205 |
class Meta: |
| 206 |
verbose_name = _("WNS device") |
| 207 |
|
| 208 |
def send_message(self, message, **kwargs): |
| 209 |
from .wns import wns_send_message |
| 210 |
|
| 211 |
return wns_send_message( |
| 212 |
uri=self.registration_id, message=message, application_id=self.application_id, **kwargs |
| 213 |
) |
| 214 |
|
| 215 |
|
| 216 |
class WebPushDeviceManager(models.Manager): |
| 217 |
def get_queryset(self): |
| 218 |
return WebPushDeviceQuerySet(self.model) |
| 219 |
|
| 220 |
|
| 221 |
class WebPushDeviceQuerySet(models.query.QuerySet): |
| 222 |
def send_message(self, message, **kwargs): |
| 223 |
devices = self.filter(active=True).order_by("application_id").distinct() |
| 224 |
res = [] |
| 225 |
for device in devices: |
| 226 |
res.append(device.send_message(message)) |
| 227 |
return res |
| 228 |
|
| 229 |
|
| 230 |
class WebPushDevice(Device): |
| 231 |
registration_id = models.TextField(verbose_name=_("Registration ID")) |
| 232 |
p256dh = models.CharField( |
| 233 |
verbose_name=_("User public encryption key"), |
| 234 |
max_length=88) |
| 235 |
auth = models.CharField( |
| 236 |
verbose_name=_("User auth secret"), |
| 237 |
max_length=24) |
| 238 |
browser = models.CharField( |
| 239 |
verbose_name=_("Browser"), max_length=10, |
| 240 |
choices=BROWSER_TYPES, default=BROWSER_TYPES[0][0], |
| 241 |
help_text=_("Currently only support to Chrome, Firefox and Opera browsers") |
| 242 |
) |
| 243 |
objects = WebPushDeviceManager() |
| 244 |
|
| 245 |
class Meta: |
| 246 |
verbose_name = _("WebPush device") |
| 247 |
|
| 248 |
|
| 249 |
def send_message(self, message, **kwargs): |
| 250 |
from .webpush import webpush_send_message |
| 251 |
|
| 252 |
return webpush_send_message( |
| 253 |
uri=self.registration_id, message=message, browser=self.browser, |
| 254 |
auth=self.auth, p256dh=self.p256dh, application_id=self.application_id, **kwargs) |
| 255 |
|
| 256 |
@property |
| 257 |
def device_id(self): |
| 258 |
return None |