-
Notifications
You must be signed in to change notification settings - Fork 28
/
models.py
54 lines (38 loc) · 1.64 KB
/
models.py
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
import logging
from django.db.models.query import QuerySet
from django.db import models
from fcm.utils import FCMMessage
logger = logging.getLogger(__name__)
class DeviceQuerySet(QuerySet):
def send_message(self, data, notification=None, **kwargs):
if self:
registration_ids = list(self.values_list("reg_id", flat=True))
return FCMMessage().send(
data, notification, registration_ids=registration_ids, **kwargs)
class DeviceManager(models.Manager):
def get_queryset(self):
return DeviceQuerySet(self.model)
class AbstractDevice(models.Model):
dev_id = models.CharField(verbose_name=("Device ID"), max_length=50, unique=True,)
reg_id = models.CharField(verbose_name=("Registration ID"), max_length=255, unique=True)
name = models.CharField(verbose_name=("Name"), max_length=255, blank=True, null=True)
is_active = models.BooleanField(verbose_name=("Is active?"), default=False)
objects = DeviceManager()
def __str__(self):
return self.dev_id
class Meta:
abstract = True
verbose_name = ("Device")
verbose_name_plural = ("Devices")
def send_message(self, data, **kwargs):
return FCMMessage().send(
registration_ids=[self.reg_id], data=data, **kwargs)
def mark_inactive(self, **kwargs):
self.is_active = False
self.save()
if kwargs.get('error_message'):
logger.debug("Device %s (%s) marked inactive due to error: %s",
self.dev_id, self.name, kwargs['error_message'])
class Device(AbstractDevice):
class Meta:
swappable = 'FCM_DEVICE_MODEL'