Version: drop file field, use files instead #193

Merged
Oleg-Komarov merged 6 commits from version-drop-file into main 2024-06-21 11:29:22 +02:00
6 changed files with 15 additions and 14 deletions
Showing only changes of commit bb926d8ed6 - Show all commits

View File

@ -5,7 +5,7 @@ from django.contrib import admin
from . import models
from common.admin import NoAddDeleteMixin
from extensions.models import Extension, Maintainer, Tag, Version, VersionFiles
from extensions.models import Extension, Maintainer, Tag, Version, VersionFile
log = logging.getLogger(__name__)
@ -32,7 +32,7 @@ class VersionInline(NoAddDeleteMixin, admin.TabularInline):
extra = 0
class VersionFilesInline(NoAddDeleteMixin, admin.TabularInline):
class VersionFileInline(NoAddDeleteMixin, admin.TabularInline):
model = Version.files.through
autocomplete_fields = ('file',)
extra = 0
@ -208,7 +208,7 @@ class VersionAdmin(admin.ModelAdmin):
},
),
)
inlines = (VersionFilesInline,)
inlines = (VersionFileInline,)
def get_urls(self):
def wrap(view):
@ -248,8 +248,8 @@ class TagAdmin(admin.ModelAdmin):
return ()
class VersionFilesAdmin(admin.ModelAdmin):
model = VersionFiles
class VersionFileAdmin(admin.ModelAdmin):
model = VersionFile
list_display = 'file'
readonly_fields = 'file'

View File

@ -19,6 +19,7 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RenameModel('VersionFiles', 'VersionFile'),
# this alter is only needed to make a reverse migration possible
migrations.AlterField(
model_name='version',

View File

@ -551,7 +551,7 @@ class Version(CreatedModifiedMixin, TrackChangesMixin, models.Model):
extension = models.ForeignKey(Extension, related_name='versions', on_delete=models.CASCADE)
files = models.ManyToManyField(
'files.File',
through='VersionFiles',
through='VersionFile',
related_name='version',
)
@ -771,7 +771,7 @@ class Version(CreatedModifiedMixin, TrackChangesMixin, models.Model):
super().delete(*args, **kwargs)
class VersionFiles(CreatedModifiedMixin, models.Model):
class VersionFile(CreatedModifiedMixin, models.Model):
version = models.ForeignKey(Version, on_delete=models.CASCADE)
file = models.OneToOneField(File, on_delete=models.CASCADE)

View File

@ -29,18 +29,18 @@ def _log_deletion(
instance.record_deletion()
@receiver(post_delete, sender=extensions.models.VersionFiles)
@receiver(post_delete, sender=extensions.models.VersionFile)
def _delete_versionfiles_file(
sender: object, instance: extensions.models.VersionFiles, **kwargs: object
sender: object, instance: extensions.models.VersionFile, **kwargs: object
) -> None:
# **N.B.**: this isn't part of an overloaded `VersionFiles.delete()` method because
# **N.B.**: this isn't part of an overloaded `VersionFile.delete()` method because
# that method isn't called when `Extension.delete()` cascades to deleting the versions:
#
# delete() method for an object is not necessarily called ... as a result of a cascading delete
# https://docs.djangoproject.com/en/4.2/topics/db/models/#overriding-predefined-model-methods
file = instance.file
logger.info('Deleting File pk=%s of VersionFiles pk=%s', file.pk, instance.pk)
logger.info('Deleting File pk=%s of VersionFile pk=%s', file.pk, instance.pk)
file.delete()
if instance.version.files.count() == 0:

View File

@ -65,7 +65,7 @@ class DeleteTest(TestCase):
)
# FIXME? version is listed twice, because unfortunately we generate duplicate log entry:
# 1. it happens due to Extension deleting its Version objects via CASCADE
# 2. it happens in post_delete for VersionFiles when the check for the last file is done
# 2. it happens in post_delete for VersionFile when the check for the last file is done
object_reprs = list(
map(
repr,
@ -102,7 +102,7 @@ class DeleteTest(TestCase):
self.assertIsNone(extensions.models.Extension.objects.filter(pk=extension.pk).first())
self.assertIsNone(extensions.models.Version.objects.filter(pk=version.pk).first())
self.assertIsNone(
extensions.models.VersionFiles.objects.filter(version__pk=version.pk).first()
extensions.models.VersionFile.objects.filter(version__pk=version.pk).first()
)
self.assertIsNone(files.models.File.objects.filter(pk=version_file.pk).first())
self.assertIsNotNone(files.models.File.objects.filter(pk=preview_file.pk).first())

View File

@ -244,7 +244,7 @@ class NewVersionFinalizeView(LoginRequiredMixin, OwnsFileMixin, CreateView):
def get_form_kwargs(self):
form_kwargs = super().get_form_kwargs()
# this lookup via VersionFiles ManyToManyManager returns the version that was created on
# this lookup via VersionFile ManyToManyManager returns the version that was created on
# the previous step by create_version_from_file
form_kwargs['instance'] = self.file.version.first()
return form_kwargs