NDOJ/judge/migrations/0158_migrate_pagevote.py

71 lines
2.3 KiB
Python
Raw Normal View History

# Generated by Django 3.2.18 on 2023-08-03 07:40
from django.db import migrations, models
import django.db.models.deletion
from django.core.exceptions import ObjectDoesNotExist
def migrate_pagevote(apps, schema_editor):
PageVote = apps.get_model("judge", "PageVote")
Problem = apps.get_model("judge", "Problem")
Solution = apps.get_model("judge", "Solution")
BlogPost = apps.get_model("judge", "BlogPost")
Contest = apps.get_model("judge", "Contest")
for vote in PageVote.objects.all():
page = vote.page
try:
if page.startswith("p:"):
code = page[2:]
vote.linked_object = Problem.objects.get(code=code)
elif page.startswith("s:"):
code = page[2:]
vote.linked_object = Solution.objects.get(problem__code=code)
elif page.startswith("c:"):
key = page[2:]
vote.linked_object = Contest.objects.get(key=key)
elif page.startswith("b:"):
blog_id = page[2:]
vote.linked_object = BlogPost.objects.get(id=blog_id)
vote.save()
except ObjectDoesNotExist:
vote.delete()
class Migration(migrations.Migration):
dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("judge", "0157_auto_20230801_1145"),
]
operations = [
migrations.AddField(
model_name="pagevote",
name="content_type",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
),
),
migrations.AddField(
model_name="pagevote",
name="object_id",
field=models.PositiveIntegerField(default=None),
preserve_default=False,
),
migrations.AlterUniqueTogether(
name="pagevote",
unique_together={("content_type", "object_id")},
),
migrations.AddIndex(
model_name="pagevote",
index=models.Index(
fields=["content_type", "object_id"],
name="judge_pagev_content_ed8899_idx",
),
),
migrations.RunPython(migrate_pagevote, migrations.RunPython.noop, atomic=True),
]