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):
Extension = apps.get_model('extensions', 'Extension')
to_update = []
for extension in Extension.objects.prefetch_related(
'versions',
'versions__file',
@ -29,7 +30,8 @@ def populate_latest_version(apps, schema_editor):
latest_version = version
break
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):

View File

@ -409,7 +409,7 @@ class Extension(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Mod
@transaction.atomic
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')
Oleg-Komarov marked this conversation as resolved Outdated

select_related missing for .file

`select_related` missing for `.file`
latest_version = None
for version in versions:
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
)
# 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
Oleg-Komarov marked this conversation as resolved Outdated

"below"?

"below"?
if self.is_listed == should_be_listed:
return