From a2a04da49356165e53cbcd557ad2b9ad2a1e75e1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 9 Apr 2022 09:41:10 -0700 Subject: [PATCH] Adds many to many related items to notifications --- .../migrations/0150_auto_20220408_2236.py | 128 ++++++++++++++++++ bookwyrm/models/notification.py | 41 ++---- 2 files changed, 142 insertions(+), 27 deletions(-) create mode 100644 bookwyrm/migrations/0150_auto_20220408_2236.py diff --git a/bookwyrm/migrations/0150_auto_20220408_2236.py b/bookwyrm/migrations/0150_auto_20220408_2236.py new file mode 100644 index 000000000..bf1c30487 --- /dev/null +++ b/bookwyrm/migrations/0150_auto_20220408_2236.py @@ -0,0 +1,128 @@ +# Generated by Django 3.2.12 on 2022-04-08 22:36 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0149_remove_notification_related_book"), + ] + + operations = [ + migrations.AddField( + model_name="notification", + name="related_groups", + field=models.ManyToManyField( + related_name="notifications", to="bookwyrm.Group" + ), + ), + migrations.AddField( + model_name="notification", + name="related_list_items", + field=models.ManyToManyField( + related_name="notifications", to="bookwyrm.ListItem" + ), + ), + migrations.AddField( + model_name="notification", + name="related_reports", + field=models.ManyToManyField(to="bookwyrm.Report"), + ), + migrations.AddField( + model_name="notification", + name="related_statuses", + field=models.ManyToManyField( + related_name="notifications", to="bookwyrm.Status" + ), + ), + migrations.AddField( + model_name="notification", + name="related_users", + field=models.ManyToManyField( + related_name="notifications", to=settings.AUTH_USER_MODEL + ), + ), + migrations.AlterField( + model_name="notification", + name="related_group", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="notifications_temp", + to="bookwyrm.group", + ), + ), + migrations.AlterField( + model_name="notification", + name="related_list_item", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="notifications_tmp", + to="bookwyrm.listitem", + ), + ), + migrations.AlterField( + model_name="notification", + name="related_report", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="notifications_tmp", + to="bookwyrm.report", + ), + ), + migrations.RunSQL( + sql=""" + INSERT INTO bookwyrm_notification_related_statuses (notification_id, status_id) + SELECT id, related_status_id + FROM bookwyrm_notification + WHERE bookwyrm_notification.related_status_id IS NOT NULL; + + INSERT INTO bookwyrm_notification_related_users (notification_id, user_id) + SELECT id, related_user_id + FROM bookwyrm_notification + WHERE bookwyrm_notification.related_user_id IS NOT NULL; + + INSERT INTO bookwyrm_notification_related_groups (notification_id, group_id) + SELECT id, related_group_id + FROM bookwyrm_notification + WHERE bookwyrm_notification.related_group_id IS NOT NULL; + + INSERT INTO bookwyrm_notification_related_list_items (notification_id, listitem_id) + SELECT id, related_list_item_id + FROM bookwyrm_notification + WHERE bookwyrm_notification.related_list_item_id IS NOT NULL; + + INSERT INTO bookwyrm_notification_related_reports (notification_id, report_id) + SELECT id, related_report_id + FROM bookwyrm_notification + WHERE bookwyrm_notification.related_report_id IS NOT NULL; + + """, + reverse_sql=migrations.RunSQL.noop, + ), + migrations.RemoveField( + model_name="notification", + name="related_group", + ), + migrations.RemoveField( + model_name="notification", + name="related_list_item", + ), + migrations.RemoveField( + model_name="notification", + name="related_report", + ), + migrations.RemoveField( + model_name="notification", + name="related_status", + ), + migrations.RemoveField( + model_name="notification", + name="related_user", + ), + ] diff --git a/bookwyrm/models/notification.py b/bookwyrm/models/notification.py index 28c5b803e..7b8059e0f 100644 --- a/bookwyrm/models/notification.py +++ b/bookwyrm/models/notification.py @@ -15,38 +15,25 @@ class Notification(BookWyrmModel): """you've been tagged, liked, followed, etc""" user = models.ForeignKey("User", on_delete=models.CASCADE) - related_user = models.ForeignKey( - "User", on_delete=models.CASCADE, null=True, related_name="related_user" - ) - related_group = models.ForeignKey( - "Group", on_delete=models.CASCADE, null=True, related_name="notifications" - ) - related_status = models.ForeignKey("Status", on_delete=models.CASCADE, null=True) - related_import = models.ForeignKey("ImportJob", on_delete=models.CASCADE, null=True) - related_list_item = models.ForeignKey( - "ListItem", on_delete=models.CASCADE, null=True - ) - related_report = models.ForeignKey("Report", on_delete=models.CASCADE, null=True) read = models.BooleanField(default=False) notification_type = models.CharField( max_length=255, choices=NotificationType.choices ) - def save(self, *args, **kwargs): - """save, but don't make dupes""" - # there's probably a better way to do this - if self.__class__.objects.filter( - user=self.user, - related_user=self.related_user, - related_group=self.related_group, - related_status=self.related_status, - related_import=self.related_import, - related_list_item=self.related_list_item, - related_report=self.related_report, - notification_type=self.notification_type, - ).exists(): - return - super().save(*args, **kwargs) + related_users = models.ManyToManyField( + "User", symmetrical=False, related_name="notifications" + ) + related_groups = models.ManyToManyField( + "Group", symmetrical=False, related_name="notifications" + ) + related_statuses = models.ManyToManyField( + "Status", symmetrical=False, related_name="notifications" + ) + related_import = models.ForeignKey("ImportJob", on_delete=models.CASCADE, null=True) + related_list_items = models.ManyToManyField( + "ListItem", symmetrical=False, related_name="notifications" + ) + related_reports = models.ManyToManyField("Report", symmetrical=False) class Meta: """checks if notifcation is in enum list for valid types"""