Skip to content

Commit

Permalink
Also categorize direct messages and add logging to migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mathjazz committed Dec 3, 2024
1 parent 2a75927 commit 928e52f
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pontoon/base/migrations/0069_notification_categories.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import logging
import re

from django.db import migrations


log = logging.getLogger(__name__)


def get_category(notification):
verb = notification.verb
desc = notification.description
Expand All @@ -23,7 +27,7 @@ def get_category(notification):
if verb == "":
return "unreviewed_suggestion"

if re.match(r"has reviewed suggestions", verb):
if verb == "has reviewed suggestions":
# Review actions on own suggestions notifications
if desc.startswith("Your suggestions have been reviewed"):
return "review"
Expand All @@ -32,19 +36,32 @@ def get_category(notification):
if desc.startswith("<a href="):
return "new_contributor"

if verb == "has sent a message in" or verb == "has sent you a message":
return "direct_message"

return None


def store_notification_categories(apps, schema_editor):
Notification = apps.get_model("notifications", "Notification")
notifications = Notification.objects.filter(data__isnull=True)
notifications = Notification.objects.all()
unchanged = []

for notification in notifications:
category = get_category(notification)

if category:
if category == "direct_message":
notification.data["category"] = category
elif category:
notification.data = {"category": category}
else:
unchanged.append(notification)

Notification.objects.bulk_update(notifications, ["data"], batch_size=2000)

log.info(f"Notifications categorized: {len(notifications) - len(unchanged)}.")
log.info(f"Notifications left unchanged: {len(unchanged)}.")


class Migration(migrations.Migration):
dependencies = [
Expand Down

0 comments on commit 928e52f

Please sign in to comment.