From dcfcc226be74379110773d2f761cb1724c95bc79 Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Mon, 11 Oct 2021 19:49:47 +0400 Subject: [PATCH 1/3] Move thumbnails to `MediaMixin` to provide the thumbnail field to audio sets --- .../api/migrations/0039_audio_set_thumbs.py | 18 ++++++++++++++++++ openverse_api/catalog/api/models/media.py | 4 ---- openverse_api/catalog/api/models/mixins.py | 5 +++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 openverse_api/catalog/api/migrations/0039_audio_set_thumbs.py diff --git a/openverse_api/catalog/api/migrations/0039_audio_set_thumbs.py b/openverse_api/catalog/api/migrations/0039_audio_set_thumbs.py new file mode 100644 index 000000000..83ff131c7 --- /dev/null +++ b/openverse_api/catalog/api/migrations/0039_audio_set_thumbs.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.7 on 2021-10-11 15:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0038_filetypes'), + ] + + operations = [ + migrations.AddField( + model_name='audioset', + name='thumbnail', + field=models.URLField(blank=True, help_text='The thumbnail for the media.', max_length=1000, null=True), + ), + ] diff --git a/openverse_api/catalog/api/models/media.py b/openverse_api/catalog/api/models/media.py index 415abbb46..6e52490bc 100644 --- a/openverse_api/catalog/api/models/media.py +++ b/openverse_api/catalog/api/models/media.py @@ -25,10 +25,6 @@ class AbstractMedia(IdentifierMixin, MediaMixin, OpenLedgerModel): information common to all media types indexed by Openverse. """ - thumbnail = models.URLField( - max_length=1000, blank=True, null=True, help_text="The thumbnail for the media." - ) - watermarked = models.BooleanField(blank=True, null=True) license = models.CharField(max_length=50) diff --git a/openverse_api/catalog/api/models/mixins.py b/openverse_api/catalog/api/models/mixins.py index 898d2094e..047f6c30f 100644 --- a/openverse_api/catalog/api/models/mixins.py +++ b/openverse_api/catalog/api/models/mixins.py @@ -53,6 +53,11 @@ class MediaMixin(models.Model): creator = models.CharField(max_length=2000, blank=True, null=True) creator_url = models.URLField(max_length=2000, blank=True, null=True) + # Because all forms of media have a thumbnail for visual representation + thumbnail = models.URLField( + max_length=1000, blank=True, null=True, help_text="The thumbnail for the media." + ) + class Meta: abstract = True From b28ac349c4cabff93ab37ac153f08f5d48f95ca7 Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Mon, 11 Oct 2021 23:06:34 +0400 Subject: [PATCH 2/3] Make URLs nullable to allow non-file media like audio-sets --- .../api/migrations/0040_nullable_urls.py | 28 +++++++++++++++++++ openverse_api/catalog/api/models/mixins.py | 6 +++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 openverse_api/catalog/api/migrations/0040_nullable_urls.py diff --git a/openverse_api/catalog/api/migrations/0040_nullable_urls.py b/openverse_api/catalog/api/migrations/0040_nullable_urls.py new file mode 100644 index 000000000..d884ea818 --- /dev/null +++ b/openverse_api/catalog/api/migrations/0040_nullable_urls.py @@ -0,0 +1,28 @@ +# Generated by Django 3.2.7 on 2021-10-11 15:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0039_audio_set_thumbs'), + ] + + operations = [ + migrations.AlterField( + model_name='audio', + name='url', + field=models.URLField(blank=True, help_text='The actual URL to the media file.', max_length=1000, null=True, unique=True), + ), + migrations.AlterField( + model_name='audioset', + name='url', + field=models.URLField(blank=True, help_text='The actual URL to the media file.', max_length=1000, null=True, unique=True), + ), + migrations.AlterField( + model_name='image', + name='url', + field=models.URLField(blank=True, help_text='The actual URL to the media file.', max_length=1000, null=True, unique=True), + ), + ] diff --git a/openverse_api/catalog/api/models/mixins.py b/openverse_api/catalog/api/models/mixins.py index 047f6c30f..5607bcbb3 100644 --- a/openverse_api/catalog/api/models/mixins.py +++ b/openverse_api/catalog/api/models/mixins.py @@ -69,7 +69,11 @@ class FileMixin(models.Model): """ url = models.URLField( - unique=True, max_length=1000, help_text="The actual URL to the media file." + unique=True, + max_length=1000, + help_text="The actual URL to the media file.", + blank=True, + null=True, ) filesize = models.IntegerField( blank=True, From c12360c8e76b6e9e46c6f2fd7eb168dbc175d3fa Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Mon, 11 Oct 2021 23:08:45 +0400 Subject: [PATCH 3/3] Update views that use the `url` field of audio sets --- openverse_api/catalog/api/views/audio_views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openverse_api/catalog/api/views/audio_views.py b/openverse_api/catalog/api/views/audio_views.py index 266626a7f..61ecc0bde 100644 --- a/openverse_api/catalog/api/views/audio_views.py +++ b/openverse_api/catalog/api/views/audio_views.py @@ -65,7 +65,7 @@ def thumbnail(self, request, *_, **__): image_url = None if thumbnail := audio.thumbnail: image_url = thumbnail - elif audio.audio_set and (thumbnail := audio.audio_set.url): + elif audio.audio_set and (thumbnail := audio.audio_set.thumbnail): image_url = thumbnail if not image_url: raise get_api_exception("Could not find artwork.", 404)