mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-02-02 04:12:20 +00:00
Moves list add notification into notification model
Right now notifications are a mix of post-save signals and clauses in the save function of the model. I'm not actually sure which is better, but I'm moving them to signals where it's straightforward to be consistent.
This commit is contained in:
parent
736d29ea20
commit
41f42e33ed
2 changed files with 31 additions and 23 deletions
|
@ -1,7 +1,6 @@
|
|||
""" make a list of books!! """
|
||||
import uuid
|
||||
|
||||
from django.apps import apps
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
|
@ -152,32 +151,11 @@ class ListItem(CollectionItemMixin, BookWyrmModel):
|
|||
|
||||
def save(self, *args, **kwargs):
|
||||
"""create a notification too"""
|
||||
created = not bool(self.id)
|
||||
super().save(*args, **kwargs)
|
||||
# tick the updated date on the parent list
|
||||
self.book_list.updated_date = timezone.now()
|
||||
self.book_list.save(broadcast=False, update_fields=["updated_date"])
|
||||
|
||||
list_owner = self.book_list.user
|
||||
model = apps.get_model("bookwyrm.Notification", require_ready=True)
|
||||
# create a notification if somoene ELSE added to a local user's list
|
||||
if created and list_owner.local and list_owner != self.user:
|
||||
model.objects.create(
|
||||
user=list_owner,
|
||||
related_user=self.user,
|
||||
related_list_item=self,
|
||||
notification_type=model.ADD,
|
||||
)
|
||||
|
||||
if self.book_list.group:
|
||||
for membership in self.book_list.group.memberships.all():
|
||||
if membership.user != self.user:
|
||||
model.objects.create(
|
||||
user=membership.user,
|
||||
related_user=self.user,
|
||||
related_list_item=self,
|
||||
notification_type=model.ADD,
|
||||
)
|
||||
|
||||
def raise_not_deletable(self, viewer):
|
||||
"""the associated user OR the list owner can delete"""
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
from django.db import models, transaction
|
||||
from django.dispatch import receiver
|
||||
from .base_model import BookWyrmModel
|
||||
from . import Boost, Favorite, GroupMemberInvitation, ImportJob, Report, Status, User
|
||||
from . import Boost, Favorite, GroupMemberInvitation, ImportJob, ListItem, Report
|
||||
from . import Status, User
|
||||
|
||||
|
||||
class Notification(BookWyrmModel):
|
||||
|
@ -219,3 +220,32 @@ def notify_user_on_group_invite(sender, instance, *args, **kwargs):
|
|||
related_group=instance.group,
|
||||
notification_type=Notification.INVITE,
|
||||
)
|
||||
|
||||
|
||||
@receiver(models.signals.post_save, sender=ListItem)
|
||||
@transaction.atomic
|
||||
# pylint: disable=unused-argument
|
||||
def notify_user_on_list_item_add(sender, instance, created, *args, **kwargs):
|
||||
"""Someone added to your list"""
|
||||
if not created:
|
||||
return
|
||||
|
||||
list_owner = instance.book_list.user
|
||||
# create a notification if somoene ELSE added to a local user's list
|
||||
if list_owner.local and list_owner != instance.user:
|
||||
Notification.notify(
|
||||
list_owner,
|
||||
instance.user,
|
||||
related_list_item=instance,
|
||||
notification_type=Notification.ADD
|
||||
)
|
||||
|
||||
if instance.book_list.group:
|
||||
for membership in instance.book_list.group.memberships.all():
|
||||
if membership.user != instance.user:
|
||||
Notification.notify(
|
||||
membership.user,
|
||||
instance.user,
|
||||
related_list_item=instance,
|
||||
notification_type=Notification.ADD,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue