models: Add model for storing notifications

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-01-30 13:25:02 -08:00 committed by James Valleroy
parent f1c4b2235a
commit 5c514e91d3
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,61 @@
#
# This file is part of FreedomBox.
#
# 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/>.
#
#
# Generated by Django 2.2.6 on 2020-01-29 03:17
#
"""
Django migration for adding the notification model.
"""
from django.db import migrations, models
from plinth.models import JSONField
class Migration(migrations.Migration):
dependencies = [
('plinth', '0004_userprofile'),
]
operations = [
migrations.CreateModel(
name='StoredNotification',
fields=[
('id',
models.CharField(max_length=128, primary_key=True,
serialize=False)),
('app_id',
models.CharField(default=None, max_length=128, null=True)),
('severity', models.CharField(max_length=32)),
('title', models.CharField(max_length=256)),
('message', models.TextField(default=None, null=True)),
('actions', JSONField(default=list)),
('body_template',
models.CharField(max_length=128, null=True, default=None)),
('data', JSONField(default=dict)),
('created_time', models.DateTimeField(auto_now_add=True)),
('last_update_time', models.DateTimeField(auto_now=True)),
('user',
models.CharField(max_length=128, default=None, null=True)),
('group',
models.CharField(max_length=128, default=None, null=True)),
('dismissed', models.BooleanField(default=False)),
],
),
]

View File

@ -22,6 +22,7 @@ import json
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.dispatch import receiver from django.dispatch import receiver
@ -63,3 +64,41 @@ def _on_user_post_save(sender, instance, **kwargs):
instance.userprofile.save() instance.userprofile.save()
else: else:
UserProfile.objects.update_or_create(user=instance) UserProfile.objects.update_or_create(user=instance)
class JSONField(models.TextField):
"""Store and retrieve JSON data into a TextField."""
def to_python(self, value):
"""Deserialize a text string from form field to Python dict."""
if not value:
return self.default()
try:
return json.loads(value)
except json.decoder.JSONDecodeError:
raise ValidationError('Invalid JSON value')
def from_db_value(self, value, *args, **kwargs):
"""Deserialize a value from DB to Python dict."""
return self.to_python(value)
def get_prep_value(self, value):
"""Serialize the Python dict to text for form field."""
return json.dumps(value or self.default())
class StoredNotification(models.Model):
"""Model to store a user notification."""
id = models.CharField(primary_key=True, max_length=128)
app_id = models.CharField(max_length=128, null=True, default=None)
severity = models.CharField(max_length=32)
title = models.CharField(max_length=256)
message = models.TextField(null=True, default=None)
actions = JSONField(default=list)
body_template = models.CharField(max_length=128, null=True, default=None)
data = JSONField(default=dict)
created_time = models.DateTimeField(auto_now_add=True)
last_update_time = models.DateTimeField(auto_now=True)
user = models.CharField(max_length=128, null=True, default=None)
group = models.CharField(max_length=128, null=True, default=None)
dismissed = models.BooleanField(default=False)