-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
90 lines (71 loc) · 3.02 KB
/
models.py
File metadata and controls
90 lines (71 loc) · 3.02 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
# -*- encoding: utf-8 -*-
# from __future__ import unicode_literals
from django.db import models
from common.models import BaseModel
from notification.settings import USER_MODEL as User
from notification.choices import ServiceType, PlatformType, DeviceStatus
from notification.service import ServiceAgent
# Create your models here.
class Device(BaseModel):
member = models.ForeignKey(User, null=True, blank=True)
name = models.CharField(max_length=100, null=True, default='')
status = models.IntegerField(
choices=DeviceStatus.choices, default=DeviceStatus.ACTIVE)
token = models.CharField(max_length=200)
udid = models.CharField(max_length=200)
platform = models.IntegerField(
choices=PlatformType.choices, default=PlatformType.IOS)
last_login = models.DateTimeField(null=True)
lang_code = models.CharField(max_length=200, blank=True, null=True)
# def __unicode__(self):
# return self.name or u''
def __unicode__(self):
return '{} - {}'.format(self.name or '', self.get_platform_display())
def __str__(self):
if self.member is not None:
return self.member.email
return '{} - {}'.format(self.name or '', self.get_platform_display())
class Meta:
verbose_name = verbose_name_plural = u'Device'
class Notification(BaseModel):
device = models.ForeignKey(Device)
title = models.CharField(max_length=200)
content = models.TextField()
is_success = models.BooleanField(default=False)
class Meta:
verbose_name = verbose_name_plural = u'Notification'
def __unicode__(self):
return self.title
def __str__(self):
return self.__unicode__()
def device_member(self):
return self.device.member.email
device_member.short_description = 'Device User'
@staticmethod
def notify(member, title, content):
#####################################
# push the message title and content
# to user's all devices
#####################################
devices = Device.enables.filter(member=member).order_by('-created_at')
for device in devices:
services = ServiceAgent.get_services_by_platform(device.platform)
is_success = False
for service in services:
is_success = service.push(device, title, content)
if is_success:
break
Notification.objects.create(
device=device, title=title, content=content, is_success=is_success)
@staticmethod
def notify_device(device, title, content):
"""
push the notification message direct to the specified device
"""
services = ServiceAgent.get_services_by_platform(device.platform)
is_success = False
for service in services:
is_success = service.push(device, title, content)
if is_success:
break
Notification.objects.create(device=device, title=title, content=content, is_success=is_success)