combo-plugin-gnm/combo_plugin_gnm/models.py

78 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
# combo-plugin-gnm - Combo WebPush App
# Copyright (C) 2018 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.db import models
# TODO use signals to hook Notification model create or update ?
from combo.apps.notifications.models import Notification
from webpush.models import PushInformation
class WebPushRecord(models.Model):
DEFAULT_STATUS = 'NEW'
ERR_STATUS = 'ERR'
OK_STATUS = 'SENT'
STATUS = (
(DEFAULT_STATUS, 'Not-sent notification'),
(OK_STATUS, 'Sent notification'),
(ERR_STATUS, 'Invalid notification'),
)
subscription = models.ForeignKey(PushInformation, null=False)
notification = models.ForeignKey(Notification, null=False)
creation_date = models.DateTimeField(blank=True, auto_now_add=True)
status = models.CharField(blank=True, max_length=4,
choices=STATUS, default=DEFAULT_STATUS)
class Meta:
unique_together = ('subscription', 'notification')
@property
def ttl(self):
delta = self.notification.end_timestamp - self.notification.start_timestamp
return int(delta.total_seconds())
@property
def payload(self):
'''
For example, we could extend later this data to such JSON supported by Chrome for Android
{
"body": "Did you make a $1,000,000 purchase at Dr. Evil...",
"icon": "images/ccard.png",
"vibrate": [200, 100, 200, 100, 200, 100, 400],
"tag": "request",
"actions": [
{ "action": "yes", "title": "Yes", "icon": "images/yes.png" },
{ "action": "no", "title": "No", "icon": "images/no.png" }
]
}
'''
return {
'head': self.notification.summary,
'body': self.notification.body,
'url': self.notification.url,
}
def set_status_ok(self):
self.status = self.OK_STATUS
self.save()
def set_status_err(self):
self.status = self.ERR_STATUS
self.save()