2021-03-08 16:49:10 +00:00
|
|
|
""" alert a user to activity """
|
2021-01-01 18:46:15 +00:00
|
|
|
from django.db import models
|
|
|
|
from .base_model import BookWyrmModel
|
|
|
|
|
|
|
|
|
|
|
|
NotificationType = models.TextChoices(
|
2021-03-08 16:49:10 +00:00
|
|
|
"NotificationType",
|
2021-03-16 19:57:23 +00:00
|
|
|
"FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT ADD REPORT",
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
|
2021-01-01 18:46:15 +00:00
|
|
|
|
|
|
|
class Notification(BookWyrmModel):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" you've been tagged, liked, followed, etc """
|
|
|
|
|
|
|
|
user = models.ForeignKey("User", on_delete=models.CASCADE)
|
|
|
|
related_book = models.ForeignKey("Edition", on_delete=models.CASCADE, null=True)
|
2021-01-01 18:46:15 +00:00
|
|
|
related_user = models.ForeignKey(
|
2021-03-08 16:49:10 +00:00
|
|
|
"User", on_delete=models.CASCADE, null=True, related_name="related_user"
|
|
|
|
)
|
|
|
|
related_status = models.ForeignKey("Status", on_delete=models.CASCADE, null=True)
|
|
|
|
related_import = models.ForeignKey("ImportJob", on_delete=models.CASCADE, null=True)
|
2021-02-10 21:15:12 +00:00
|
|
|
related_list_item = models.ForeignKey(
|
2021-03-08 16:49:10 +00:00
|
|
|
"ListItem", on_delete=models.CASCADE, null=True
|
|
|
|
)
|
2021-03-16 19:57:23 +00:00
|
|
|
related_report = models.ForeignKey("Report", on_delete=models.CASCADE, null=True)
|
2021-01-01 18:46:15 +00:00
|
|
|
read = models.BooleanField(default=False)
|
|
|
|
notification_type = models.CharField(
|
2021-03-08 16:49:10 +00:00
|
|
|
max_length=255, choices=NotificationType.choices
|
|
|
|
)
|
2021-01-01 18:46:15 +00:00
|
|
|
|
2021-02-11 00:21:29 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" save, but don't make dupes """
|
2021-02-11 00:21:29 +00:00
|
|
|
# there's probably a better way to do this
|
|
|
|
if self.__class__.objects.filter(
|
2021-03-08 16:49:10 +00:00
|
|
|
user=self.user,
|
|
|
|
related_book=self.related_book,
|
|
|
|
related_user=self.related_user,
|
|
|
|
related_status=self.related_status,
|
|
|
|
related_import=self.related_import,
|
|
|
|
related_list_item=self.related_list_item,
|
2021-03-16 19:57:23 +00:00
|
|
|
related_report=self.related_report,
|
2021-03-08 16:49:10 +00:00
|
|
|
notification_type=self.notification_type,
|
|
|
|
).exists():
|
2021-02-11 00:21:29 +00:00
|
|
|
return
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2021-01-01 18:46:15 +00:00
|
|
|
class Meta:
|
2021-03-08 16:49:10 +00:00
|
|
|
""" checks if notifcation is in enum list for valid types """
|
|
|
|
|
2021-01-01 18:46:15 +00:00
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(notification_type__in=NotificationType.values),
|
|
|
|
name="notification_type_valid",
|
|
|
|
)
|
|
|
|
]
|