Use a materialized Extension.latest_version field instead of a dynamic property #152
@ -3,6 +3,34 @@
|
|||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
import django.db.models.deletion
|
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
|
|||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
@ -16,4 +44,5 @@ class Migration(migrations.Migration):
|
|||||||
name='latest_version',
|
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'),
|
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),
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user
Extension.objects.bulk_update(.., fields={'latest_version'})