Adds many to many related items to notifications

This commit is contained in:
Mouse Reeve 2022-04-09 09:41:10 -07:00
parent 8d266fda4d
commit a2a04da493
2 changed files with 142 additions and 27 deletions

View file

@ -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",
),
]

View file

@ -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"""