Refactor Extension and Version: explicit constructors from File #191

Merged
Oleg-Komarov merged 6 commits from refactor-models into main 2024-06-20 14:40:41 +02:00
4 changed files with 15 additions and 16 deletions
Showing only changes of commit 77fa0f9b05 - Show all commits

View File

@ -92,7 +92,6 @@ class Command(BaseCommand):
size_bytes=FILE_SOURCES["blender-kitsu"]["size"], size_bytes=FILE_SOURCES["blender-kitsu"]["size"],
source=FILE_SOURCES["blender-kitsu"]["file"], source=FILE_SOURCES["blender-kitsu"]["file"],
status=File.STATUSES.APPROVED, status=File.STATUSES.APPROVED,
type=File.TYPES.BPY,
), ),
) )
for preview in [ for preview in [
@ -108,8 +107,8 @@ class Command(BaseCommand):
for i in range(10): for i in range(10):
type = random.choice(Extension.TYPES)[0] type = random.choice(Extension.TYPES)[0]
version = create_approved_version( version = create_approved_version(
status=File.STATUSES.APPROVED,
metadata__tags=random.sample(tags[type], k=1), metadata__tags=random.sample(tags[type], k=1),
status=File.STATUSES.APPROVED,
type=type, type=type,
) )
for preview in [ for preview in [
@ -126,9 +125,9 @@ class Command(BaseCommand):
for i in range(5): for i in range(5):
type = random.choice(Extension.TYPES)[0] type = random.choice(Extension.TYPES)[0]
version = create_version( version = create_version(
metadata__tags=random.sample(tags[type], k=1),
status=random.choice((File.STATUSES.DISABLED, File.STATUSES.DISABLED_BY_AUTHOR)), status=random.choice((File.STATUSES.DISABLED, File.STATUSES.DISABLED_BY_AUTHOR)),
type=type, type=type,
metadata__tags=random.sample(tags[type], k=1),
) )
for i in range(random.randint(1, len(LICENSES))): for i in range(random.randint(1, len(LICENSES))):
version.licenses.add(LICENSES[i]) version.licenses.add(LICENSES[i])

View File

@ -7,7 +7,7 @@ import factory.fuzzy
from files.models import File from files.models import File
class FileMetadataFactory(factory.DictFactory): class ManifestFactory(factory.DictFactory):
name = factory.Faker('name') name = factory.Faker('name')
id = factory.Faker('slug') id = factory.Faker('slug')
support = factory.Faker('url') support = factory.Faker('url')
@ -28,29 +28,27 @@ class FileFactory(DjangoModelFactory):
class Meta: class Meta:
model = File model = File
original_name = factory.LazyAttribute(lambda x: x.source)
original_hash = factory.Faker('lexify', text='fakehash:??????????????????', letters='deadbeef')
hash = factory.Faker('lexify', text='fakehash:??????????????????', letters='deadbeef') hash = factory.Faker('lexify', text='fakehash:??????????????????', letters='deadbeef')
metadata = factory.SubFactory(ManifestFactory)
original_hash = factory.Faker('lexify', text='fakehash:??????????????????', letters='deadbeef')
original_name = factory.LazyAttribute(lambda x: x.source)
size_bytes = factory.Faker('random_int', min=1234) size_bytes = factory.Faker('random_int', min=1234)
source = factory.Faker('file_name', extension='zip') source = factory.Faker('file_name', extension='zip')
type = File.TYPES.BPY type = File.TYPES.BPY
user = factory.SubFactory('common.tests.factories.users.UserFactory') user = factory.SubFactory('common.tests.factories.users.UserFactory')
metadata = factory.SubFactory(FileMetadataFactory)
class ImageFactory(FileFactory): class ImageFactory(FileFactory):
metadata = {}
original_name = factory.Faker('file_name', extension='png') original_name = factory.Faker('file_name', extension='png')
size_bytes = 1234
source = 'images/de/deadbeef.png' source = 'images/de/deadbeef.png'
type = File.TYPES.IMAGE type = File.TYPES.IMAGE
size_bytes = 1234
metadata = {}
class VideoFactory(FileFactory): class VideoFactory(FileFactory):
metadata = {}
original_name = factory.Faker('file_name', extension='mp4') original_name = factory.Faker('file_name', extension='mp4')
size_bytes = 12345678
source = 'images/be/beefcafe.mp4' source = 'images/be/beefcafe.mp4'
type = File.TYPES.VIDEO type = File.TYPES.VIDEO
size_bytes = 12345678
metadata = {}

View File

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

View File

@ -18,11 +18,11 @@ class FileTest(TestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.file = FileFactory( self.file = FileFactory(
status=File.STATUSES.AWAITING_REVIEW,
original_name='test.zip',
hash='foobar', hash='foobar',
size_bytes=7149,
metadata={}, metadata={},
original_name='test.zip',
size_bytes=7149,
status=File.STATUSES.AWAITING_REVIEW,
) )
self.assertEqual(entries_for(self.file).count(), 0) self.assertEqual(entries_for(self.file).count(), 0)
self.assertIsNone(self.file.date_approved) self.assertIsNone(self.file.date_approved)