From 402022819e15a8dbc1f4c54053cd8cbfc8662ac6 Mon Sep 17 00:00:00 2001 From: SaadYousaf Date: Wed, 26 Apr 2023 13:12:37 +0500 Subject: [PATCH] feat: add user notification preference model --- .../core/djangoapps/notifications/admin.py | 7 ++- .../migrations/0002_notificationpreference.py | 35 ++++++++++++++ .../core/djangoapps/notifications/models.py | 48 +++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 openedx/core/djangoapps/notifications/migrations/0002_notificationpreference.py diff --git a/openedx/core/djangoapps/notifications/admin.py b/openedx/core/djangoapps/notifications/admin.py index 9c778c09f2..a4797e0701 100644 --- a/openedx/core/djangoapps/notifications/admin.py +++ b/openedx/core/djangoapps/notifications/admin.py @@ -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) diff --git a/openedx/core/djangoapps/notifications/migrations/0002_notificationpreference.py b/openedx/core/djangoapps/notifications/migrations/0002_notificationpreference.py new file mode 100644 index 0000000000..e1b409c174 --- /dev/null +++ b/openedx/core/djangoapps/notifications/migrations/0002_notificationpreference.py @@ -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, + }, + ), + ] diff --git a/openedx/core/djangoapps/notifications/models.py b/openedx/core/djangoapps/notifications/models.py index dea1873ca2..8d8d3fa2cd 100644 --- a/openedx/core/djangoapps/notifications/models.py +++ b/openedx/core/djangoapps/notifications/models.py @@ -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