Use a materialized Extension.latest_version field instead of a dynamic property #152

Merged
Oleg-Komarov merged 15 commits from latest-version-field into main 2024-05-27 17:58:56 +02:00
Showing only changes of commit 047e703a3c - Show all commits

View File

@ -3,6 +3,34 @@
from django.db import migrations, models
import django.db.models.deletion
from constants.base import (
EXTENSION_STATUS_CHOICES,
FILE_STATUS_CHOICES,
)
def valid_file_statuses(self):
if self.status == EXTENSION_STATUS_CHOICES.APPROVED:
return [FILE_STATUS_CHOICES.APPROVED]
return [FILE_STATUS_CHOICES.AWAITING_REVIEW, FILE_STATUS_CHOICES.APPROVED]
def populate_latest_version(apps, schema_editor):
Extension = apps.get_model('extensions', 'Extension')
for extension in Extension.objects.prefetch_related(
'versions',
'versions__file',
).all():
versions = extension.versions.all().order_by('-date_created')
latest_version = None
for version in versions:
if version.file.status not in valid_file_statuses(extension):
continue
latest_version = version
break
extension.latest_version = latest_version
extension.save(update_fields={'latest_version'})
Oleg-Komarov marked this conversation as resolved
Review

Extension.objects.bulk_update(.., fields={'latest_version'})

`Extension.objects.bulk_update(.., fields={'latest_version'})`
class Migration(migrations.Migration):
@ -16,4 +44,5 @@ class Migration(migrations.Migration):
name='latest_version',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='latest_version_of', to='extensions.version'),
),
migrations.RunPython(populate_latest_version, reverse_code=migrations.RunPython.noop),
]