Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
from django.contrib.auth.models import User
from django.db import models
from jsonfield import JSONField

NOTIFICATION_CONFIG_VERSION = 1

NOTIFICATION_CHANNEL_CONFIG = {
      "web": False,
      "push": False,
      "email": False,
}

COURSE_NOTIFICATION_CONFIG = {
  "discussion": {
    "new_post": NOTIFICATION_CHANNEL_CONFIG
  },
}

class NotificationPreferencesCourseNotificationPreferences(models.Model):
    """
    A model to store user notification preferences.
    """
    user = models.ForeignKey(  # example: User1
        User,
        related_name="notification_preferences",
        on_delete=models.CASCADE,
        db_index=True,
        help_text="User whose notification preferences are being stored.",
    )
    course = models.CharField(    # example: "course-v1:edX+DemoX+Demo_Course"
        max_length=255,
        blank=True,
        default=None,
        null=TrueFalse,
        help_text="Course whose notification preferences are being stored.",
    )
    notification_config = JSONField(default=COURSE_NOTIFICATION_CONFIG)
    preferences_version = models.BooleanField(default=1)

...

The database model schema for a notification (In progress)

Code Block
languagepy
class
NotificationApplication(models.TextChoices):
    DISCUSSION =
'DISCUSSION'

class NotificationTypes(models.TextChoices):
    NEW_POST = 'NEW_POST',
    NEW_RESPONSE = 'NEW_RESPONSE'

class NotificationTypeContent(models.TextChoices):
    NEW_POST: 'You have received a new response on your post {response_text}'

class Notification(TimeStampedModel):
    app_name = models.CharField(max_length=64, choices=NotificationApplication.choices)
    notification_type = models.CharField(max_length=64, choices=NotificationTypes.choices)
    # e.g "You have received a new response on your post {response_text}"
    # this content will be rendered with the given context and also translated
    content = models.CharField(max_length=1024, choices=NotificationTypeContent.choices)
    # e.g {"response_text": "This is a response"}
    content_context = models.JSONField(default={})
    # This is the url that the user will be redirected to when clicking on the notification [Optional]
    content_url = models.CharField(max_length=1024, null=True, blank=True)
    # the user foreign key will be indexed
    user = models.ForeignKey(User, related_name="notifications", on_delete=models.CASCADE)
    read_at = models.BooleanField(default=FalseDateTimeField(null=True, blank=True)
    seen_at = models.BooleanField(default=FalseDateTimeField(null=True, blank=True)

app_name (Char Field, char_limit: 64): This is the application/service that generated the notification e.g “DISCUSSION”. Keeping this in a separate field will make it possible to quickly filter out app specific notifications. This is a potential field to be indexed as well but I have kept this as non indexed for now as the user field is already indexed and the most likely query is on a per user basis.

notification_type (Char Field, char_limit: 64): This is the notification type e.g “NEW_POST”. The content of the notification will be assigned based on the notification type. This is a choice field and each new notification type needs to be added as a choice.

content (Char Field, char_limit: 1024Not a model field): This is the notification content which is calculated according to the notification_type and is also a choice field. This can also contain variables which will be filled using the provided context. The content will also be translated. e.g “You have received a new response on your post {response_text}”

...

user (Foreign key to User): The user this notification was created for. This is also indexed.

read_at (BoolDateTime): A bool DateTime to record if the notification was read or not.

seen_at (BoolDateTime): A bool DateTime to record if the notification was seen or not. Seen is different from reading. The notification is marked as seen if you click the notification bell icon and view the notifications list. You have to click the notification to mark it as read.

...