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
2 changed files with 5 additions and 3 deletions
Showing only changes of commit 6f6f5d7959 - Show all commits

View File

@ -17,6 +17,7 @@ def valid_file_statuses(self):
def populate_latest_version(apps, schema_editor): def populate_latest_version(apps, schema_editor):
Extension = apps.get_model('extensions', 'Extension') Extension = apps.get_model('extensions', 'Extension')
to_update = []
for extension in Extension.objects.prefetch_related( for extension in Extension.objects.prefetch_related(
'versions', 'versions',
'versions__file', 'versions__file',
@ -29,7 +30,8 @@ def populate_latest_version(apps, schema_editor):
latest_version = version latest_version = version
break break
extension.latest_version = latest_version extension.latest_version = latest_version
Oleg-Komarov marked this conversation as resolved
Review

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

`Extension.objects.bulk_update(.., fields={'latest_version'})`
extension.save(update_fields={'latest_version'}) to_update.append(extension)
Extension.objects.bulk_update(to_update, ['latest_version'])
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -409,7 +409,7 @@ class Extension(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Mod
@transaction.atomic @transaction.atomic
def update_latest_version(self, skip_version=None): def update_latest_version(self, skip_version=None):
versions = self.versions.all().order_by('-date_created') versions = self.versions.select_related('file').all().order_by('-date_created')
latest_version = None latest_version = None
for version in versions: for version in versions:
if skip_version and version == skip_version: if skip_version and version == skip_version:
@ -429,7 +429,7 @@ class Extension(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Mod
and self.versions.filter(file__status=File.STATUSES.APPROVED).count() > 0 and self.versions.filter(file__status=File.STATUSES.APPROVED).count() > 0
) )
# this method is called from post_save signal, this early return above should prevent a loop # this method is called from post_save signal, this early return should prevent a loop
if self.is_listed == should_be_listed: if self.is_listed == should_be_listed:
return return