NDOJ/judge/migrations/0171_update_notification.py
2023-10-10 17:38:48 -05:00

68 lines
2 KiB
Python

# Generated by Django 3.2.18 on 2023-10-10 21:17
from django.db import migrations, models
import django.db.models.deletion
from django.urls import reverse
from collections import defaultdict
# Run this in shell
def migrate_notif(apps, schema_editor):
Notification = apps.get_model("judge", "Notification")
Profile = apps.get_model("judge", "Profile")
NotificationProfile = apps.get_model("judge", "NotificationProfile")
unread_count = defaultdict(int)
for c in Notification.objects.all():
if c.comment:
c.html_link = (
f'<a href="{c.comment.get_absolute_url()}">{c.comment.page_title}</a>'
)
c.author = c.comment.author
c.save()
if c.read is False:
unread_count[c.author] += 1
for user in unread_count:
np = NotificationProfile(user=user)
np.unread_count = unread_count[user]
np.save()
class Migration(migrations.Migration):
dependencies = [
("judge", "0170_contests_summary"),
]
operations = [
migrations.AlterModelOptions(
name="contestssummary",
options={
"verbose_name": "contests summary",
"verbose_name_plural": "contests summaries",
},
),
migrations.CreateModel(
name="NotificationProfile",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("unread_count", models.IntegerField(default=0)),
(
"user",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE, to="judge.profile"
),
),
],
),
]