mirror of
https://github.com/DMOJ/online-judge.git
synced 2024-11-25 16:32:37 +08:00
Fix comment revision count
This commit is contained in:
parent
724af7ef77
commit
d2e9fac6a9
39
judge/migrations/0146_comment_revision_count_v2.py
Normal file
39
judge/migrations/0146_comment_revision_count_v2.py
Normal file
@ -0,0 +1,39 @@
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def populate_revisions(apps, schema_editor):
|
||||
db_alias = schema_editor.connection.alias
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
try:
|
||||
content_type = ContentType.objects.get(app_label='judge', model='Comment')
|
||||
except ObjectDoesNotExist:
|
||||
# If you don't have content types, then you obviously haven't had any edited comments.
|
||||
# Therefore, it's safe to all revision counts as zero.
|
||||
pass
|
||||
else:
|
||||
schema_editor.execute("""\
|
||||
UPDATE `judge_comment` INNER JOIN (
|
||||
SELECT CAST(`reversion_version`.`object_id` AS INT) AS `id`, COUNT(*) AS `count`
|
||||
FROM `reversion_version`
|
||||
WHERE `reversion_version`.`content_type_id` = %s AND
|
||||
`reversion_version`.`db` = %s
|
||||
GROUP BY 1
|
||||
) `versions` ON (`judge_comment`.`id` = `versions`.`id`)
|
||||
SET `judge_comment`.`revisions` = `versions`.`count`;
|
||||
""", (content_type.id, db_alias))
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('judge', '0145_site_data_batch_prerequisites'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='comment',
|
||||
name='revisions',
|
||||
field=models.IntegerField(default=1, verbose_name='revisions'),
|
||||
),
|
||||
migrations.RunPython(populate_revisions, migrations.RunPython.noop, atomic=False, elidable=True),
|
||||
]
|
@ -33,7 +33,7 @@ class Comment(MPTTModel):
|
||||
hidden = models.BooleanField(verbose_name=_('hidden'), default=0)
|
||||
parent = TreeForeignKey('self', verbose_name=_('parent'), null=True, blank=True, related_name='replies',
|
||||
on_delete=CASCADE)
|
||||
revisions = models.IntegerField(verbose_name=_('revisions'), default=0)
|
||||
revisions = models.IntegerField(verbose_name=_('revisions'), default=1)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('comment')
|
||||
|
Loading…
Reference in New Issue
Block a user