feat: add user notification preference model

This commit is contained in:
SaadYousaf
2023-04-26 13:12:37 +05:00
committed by Saad Yousaf
parent 2d08a2a731
commit 402022819e
3 changed files with 89 additions and 1 deletions

View File

@@ -4,10 +4,15 @@ Django Admin for Notifications
from django.contrib import admin
from .models import Notification
from .models import Notification, NotificationPreference
class NotificationAdmin(admin.ModelAdmin):
pass
class NotificationPreferenceAdmin(admin.ModelAdmin):
pass
admin.site.register(Notification, NotificationAdmin)
admin.site.register(NotificationPreference, NotificationPreferenceAdmin)

View File

@@ -0,0 +1,35 @@
# Generated by Django 3.2.18 on 2023-05-03 09:05
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import model_utils.fields
import opaque_keys.edx.django.models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('notifications', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='NotificationPreference',
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')),
('course_id', opaque_keys.edx.django.models.CourseKeyField(blank=True, default=None, max_length=255)),
('notification_preference_config', models.JSONField(default={'discussion': {'new_post': {'email': False, 'push': False, 'web': False}}})),
('config_version', models.IntegerField(blank=True, default=1)),
('is_active', models.BooleanField(default=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notification_preferences', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
},
),
]

View File

@@ -5,6 +5,22 @@ from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imp
from django.db import models
from model_utils.models import TimeStampedModel
from opaque_keys.edx.django.models import CourseKeyField
# When notification preferences are updated, we need to update the CONFIG_VERSION.
NOTIFICATION_PREFERENCE_CONFIG = {
"discussion": {
"new_post": {
"web": False,
"push": False,
"email": False,
},
},
}
# Update this version when NOTIFICATION_PREFERENCE_CONFIG is updated.
CONFIG_VERSION = 1
class NotificationApplication(models.TextChoices):
"""
@@ -62,3 +78,35 @@ class Notification(TimeStampedModel):
def get_user(self):
return self.user
class NotificationPreference(TimeStampedModel):
"""
Model to store notification preferences for users
.. no_pii:
"""
user = models.ForeignKey(User, related_name="notification_preferences", on_delete=models.CASCADE)
course_id = CourseKeyField(max_length=255, blank=True, default=None)
notification_preference_config = models.JSONField(default=NOTIFICATION_PREFERENCE_CONFIG)
# This version indicates the current version of this notification preference.
config_version = models.IntegerField(blank=True, default=1)
is_active = models.BooleanField(default=True)
def __str__(self):
return f'{self.user.username} - {self.course_id} - {self.notification_preference_config}'
def get_user(self):
return self.user
def get_course_id(self):
return self.course_id
def get_notification_preference_config(self):
return self.notification_preference_config
def get_config_version(self):
return self.config_version
def get_is_active(self):
return self.is_active