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 19 additions and 26 deletions
Showing only changes of commit 16b1fa32b6 - Show all commits

View File

@ -423,6 +423,23 @@ class Extension(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Mod
if latest_version:
self.update_metadata_from_version(latest_version)
def update_is_listed(self):
should_be_listed = (
self.status == self.STATUSES.APPROVED
and self.versions.filter(file__status=File.STATUSES.APPROVED).count() > 0
)
if self.is_listed == should_be_listed:
return
self.is_listed = should_be_listed
update_fields = {'is_listed'}
if self.status == self.STATUSES.APPROVED and not should_be_listed:
self.status = self.STATUSES.DRAFT
update_fields.add('status')
self.save(update_fields=update_fields)
class VersionPermission(CreatedModifiedMixin, models.Model):
name = models.CharField(max_length=128, null=False, blank=False, unique=True)

View File

@ -57,29 +57,6 @@ def _record_changes(
instance.record_status_change(was_changed, old_state, **kwargs)
def extension_should_be_listed(extension):
return (
extension.status == extension.STATUSES.APPROVED
and extension.versions.filter(file__status=files.models.File.STATUSES.APPROVED).count() > 0
)
def update_is_listed(extension):
old_is_listed = extension.is_listed
new_is_listed = extension_should_be_listed(extension)
if old_is_listed == new_is_listed:
return
extension.is_listed = new_is_listed
update_fields = {'is_listed'}
if extension.status == extensions.models.Extension.STATUSES.APPROVED and not new_is_listed:
extension.status = extensions.models.Extension.STATUSES.DRAFT
update_fields.add('status')
extension.save(update_fields=update_fields)
# TODO? split this out into version.approve that would take care of updating file.status and
# recomputing extension.latest_version
@receiver(post_save, sender=files.models.File)
@ -95,7 +72,7 @@ def _update_version(
if hasattr(instance, 'version'):
extension = instance.version.extension
update_is_listed(extension)
extension.update_is_listed()
extension.update_latest_version()
@ -110,8 +87,7 @@ def _set_is_listed(
if raw:
return
extension = instance
update_is_listed(extension)
instance.update_is_listed()
@receiver(post_save, sender=extensions.models.Extension)