Skip to content

Users & Friending ~ Notification Model Documentation

Omar Elkashef edited this page Nov 10, 2023 · 1 revision

Overview

A Notification represents a notification to a user. It includes information about the type of notification, the sender (actor), the receiver, the timestamp and various methods to manage the notification's status.

Attributes

  • receiver (models.ForeignKey(User)): The user who will receive the notification.

  • first_sent (models.DateTimeField): The date and time when the notification was first sent.

  • last_sent (models.DateTimeField): The date and time when the notification was last sent (i.e. a user resend a friendship invitation).

  • type: The type of notification. Possible values are FRIEND_REQUEST, REMINDER, UPCOMING_MATCH, MATCH_PROPOSAL. These types will grow to include more types of invitations according to the needs of ChiGame

  • read (models.BooleanField): Indicates whether the notification has been read by the user. This is by default false.

  • visible (models.BooleanField): Indicates whether the notification is visible (This is by default True until the receiver marks the Notification deleted and it never gets deleted from the database, but only becomes invisible.

  • actor_content_type (models.ForeignKey(ContentType)): The model of the sender. Also, the ContentType of the actor (sender).

  • actor_object_id (models.PositiveIntegerField): Object ID of the actor (sender). This is the primary key of the model instance that invoked the notification.

  • actor (GenericForeignKey("actor_content_type", "actor_object_id")): The actor (sender). It is the model instance that invoked the notification.

  • message (models.CharField): An optional message associated with the notification. It has a max length of 255 characters

Methods:

  • mark_as_read(self): Marks the notification as read.
  • mark_as_unread(self): Marks the notification as unread.
  • mark_as_deleted(self): Marks the notification as deleted.
  • renew_notification(self): Updates the last_sent attribute to the current date and time. This should be called whenever the same instance of a Notification gets resent i.e. when a user resends a friend invitation to a receiver.

NotificationQuerySet

A custom queryset manager for the Notification model, providing additional filtering and retrieval methods.

Methods:

  • filter_by_actor(self, actor, **kwargs): Filters notifications by actor (sender). As the name suggests, it uses fiter() in its internal implementation
  • get_by_actor(self, actor, **kwargs): Gets a notification by actor (sender). It uses get() in its internal implementation

Usage Examples

Sending Friend Invitation

friend_request = FriendInvitation.objects.create(sender=sender, receiver=receiver)
notification = Notification.objects.create(
    actor=friend_request, receiver=receiver, type=Notification.FRIEND_REQUEST
)

Cancelling Friend Invitation

notification = Notification.objects.get_by_actor(friendship)
notification.mark_as_deleted()

Resending Friend Invitation

friend_request = FriendInvitation.objects.get(sender=sender, receiver=receiver)
notification = Notification.objects.get_by_actor(friend_request, receiver=receiver)
notification.renew_notification()
Clone this wiki locally