feat: add platform notification app and models

This commit is contained in:
SaadYousaf
2023-04-04 16:53:11 +05:00
committed by Saad Yousaf
parent d7f9fc3023
commit dc63e525f8
8 changed files with 122 additions and 1 deletions

View File

@@ -0,0 +1,13 @@
"""
Django Admin for Notifications
"""
from django.contrib import admin
from .models import Notification
class NotificationAdmin(admin.ModelAdmin):
pass
admin.site.register(Notification, NotificationAdmin)

View File

@@ -0,0 +1,38 @@
# Generated by Django 3.2.18 on 2023-04-12 23:07
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import model_utils.fields
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Notification',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')),
('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
('app_name', models.CharField(choices=[('DISCUSSION', 'Discussion')], max_length=64)),
('notification_type', models.CharField(choices=[('NEW_CONTRIBUTION', 'New Contribution')], max_length=64)),
('content', models.CharField(max_length=1024)),
('content_context', models.JSONField(default={})),
('content_url', models.URLField(blank=True, null=True)),
('last_read', models.DateTimeField(blank=True, null=True)),
('last_seen', models.DateTimeField(blank=True, null=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
},
),
]

View File

@@ -0,0 +1,64 @@
"""
Models for notifications
"""
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.db import models
from model_utils.models import TimeStampedModel
class NotificationApplication(models.TextChoices):
"""
Application choices where notifications are generated from
"""
DISCUSSION = 'DISCUSSION'
class NotificationType(models.TextChoices):
"""
Notification type choices
"""
NEW_CONTRIBUTION = 'NEW_CONTRIBUTION'
class NotificationTypeContent:
"""
Notification type content
"""
NEW_CONTRIBUTION_NOTIFICATION_CONTENT = 'There is a new contribution. {new_contribution}'
class Notification(TimeStampedModel):
"""
Model to store notifications for users
.. no_pii:
"""
user = models.ForeignKey(User, related_name="notifications", on_delete=models.CASCADE)
app_name = models.CharField(max_length=64, choices=NotificationApplication.choices)
notification_type = models.CharField(max_length=64, choices=NotificationType.choices)
content = models.CharField(max_length=1024)
content_context = models.JSONField(default={})
content_url = models.URLField(null=True, blank=True)
last_read = models.DateTimeField(null=True, blank=True)
last_seen = models.DateTimeField(null=True, blank=True)
def __str__(self):
return f'{self.user.username} - {self.app_name} - {self.notification_type} - {self.content}'
def get_content(self):
return self.content
def get_content_url(self):
return self.content_url
def get_notification_type(self):
return self.notification_type
def get_app_name(self):
return self.app_name
def get_content_context(self):
return self.content_context
def get_user(self):
return self.user