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 7 additions and 11 deletions
Showing only changes of commit c18e423c18 - Show all commits

View File

@ -715,13 +715,7 @@ class Version(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Model
message=f'uploaded new version: {self.version}', message=f'uploaded new version: {self.version}',
).save() ).save()
# we assume that self is the latest created version self.extension.update_latest_version()
# if two versions are saved at the same time, we have a data race
if self.file.status in self.extension.valid_file_statuses:
self.extension.latest_version = self
self.extension.save(update_fields={'latest_version'})
self.extension.update_metadata_from_version(self)
@transaction.atomic @transaction.atomic
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):

View File

@ -4,6 +4,7 @@ import logging
from actstream.actions import follow, unfollow from actstream.actions import follow, unfollow
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from django.db import transaction
from django.db.models.signals import m2m_changed, pre_save, post_save, pre_delete from django.db.models.signals import m2m_changed, pre_save, post_save, pre_delete
from django.dispatch import receiver from django.dispatch import receiver
@ -57,8 +58,8 @@ def _record_changes(
instance.record_status_change(was_changed, old_state, **kwargs) instance.record_status_change(was_changed, old_state, **kwargs)
# TODO? split this out into version.approve that would take care of updating file.status and # TODO? move this out into version.approve that would take care of updating file.status and
# recomputing extension.latest_version # recomputing extension's is_listed and latest_version fields
@receiver(post_save, sender=files.models.File) @receiver(post_save, sender=files.models.File)
def _update_version( def _update_version(
sender: object, sender: object,
@ -72,6 +73,7 @@ def _update_version(
if hasattr(instance, 'version'): if hasattr(instance, 'version'):
extension = instance.version.extension extension = instance.version.extension
with transaction.atomic():
extension.update_is_listed() extension.update_is_listed()
extension.update_latest_version() extension.update_latest_version()