From 5c514e91d3d248272e357c2b729ac8571e13d68b Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 30 Jan 2020 13:25:02 -0800 Subject: [PATCH] models: Add model for storing notifications Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/migrations/0005_storednotification.py | 61 ++++++++++++++++++++ plinth/models.py | 39 +++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 plinth/migrations/0005_storednotification.py diff --git a/plinth/migrations/0005_storednotification.py b/plinth/migrations/0005_storednotification.py new file mode 100644 index 000000000..2e62dac37 --- /dev/null +++ b/plinth/migrations/0005_storednotification.py @@ -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 . +# + +# +# 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)), + ], + ), + ] diff --git a/plinth/models.py b/plinth/models.py index cd449af01..9e12d6c46 100644 --- a/plinth/models.py +++ b/plinth/models.py @@ -22,6 +22,7 @@ import json from django.conf import settings from django.contrib.auth.models import User +from django.core.exceptions import ValidationError from django.db import models from django.dispatch import receiver @@ -63,3 +64,41 @@ def _on_user_post_save(sender, instance, **kwargs): instance.userprofile.save() else: 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)