diff --git a/etna/api/tests/expected_results/article.json b/etna/api/tests/expected_results/article.json index 29b88c4b1..02bac6faa 100644 --- a/etna/api/tests/expected_results/article.json +++ b/etna/api/tests/expected_results/article.json @@ -224,7 +224,11 @@ "chapters": [], "width": 1920, "height": 1080, - "duration": 10.0 + "duration": 10.0, + "subtitles_file": null, + "subtitles_file_full_url": null, + "chapters_file": null, + "chapters_file_full_url": null } }, "id": "6e6816e0-6634-46cc-bf75-0c7d737a4cb2" diff --git a/etna/media/blocks.py b/etna/media/blocks.py index 765c00cf1..2e6821ed9 100644 --- a/etna/media/blocks.py +++ b/etna/media/blocks.py @@ -46,6 +46,10 @@ def get_api_representation(self, value, context=None): "width": value.width, "height": value.height, "duration": value.duration, + "subtitles_file": value.subtitles_file_url, + "subtitles_file_full_url": value.subtitles_file_full_url, + "chapters_file": value.chapters_file_url, + "chapters_file_full_url": value.chapters_file_full_url, } diff --git a/etna/media/migrations/0003_etnamedia_chapters_file_etnamedia_subtitles_file.py b/etna/media/migrations/0003_etnamedia_chapters_file_etnamedia_subtitles_file.py new file mode 100644 index 000000000..1880dd345 --- /dev/null +++ b/etna/media/migrations/0003_etnamedia_chapters_file_etnamedia_subtitles_file.py @@ -0,0 +1,27 @@ +# Generated by Django 5.0.9 on 2024-10-24 15:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("media", "0002_etnamedia_chapters"), + ] + + operations = [ + migrations.AddField( + model_name="etnamedia", + name="chapters_file", + field=models.FileField( + blank=True, null=True, upload_to="media", verbose_name="chapters file" + ), + ), + migrations.AddField( + model_name="etnamedia", + name="subtitles_file", + field=models.FileField( + blank=True, null=True, upload_to="media", verbose_name="subtitles file" + ), + ), + ] diff --git a/etna/media/models.py b/etna/media/models.py index 57b36d19d..ed917b40f 100644 --- a/etna/media/models.py +++ b/etna/media/models.py @@ -1,7 +1,7 @@ import mimetypes from django.conf import settings -from django.core.validators import MinValueValidator +from django.core.validators import FileExtensionValidator, MinValueValidator from django.db import models from wagtail import blocks @@ -40,6 +40,12 @@ class EtnaMedia(AbstractMedia): transcript = RichTextField( blank=True, null=True, features=settings.INLINE_RICH_TEXT_FEATURES ) + subtitles_file = models.FileField( + blank=True, null=True, upload_to="media", verbose_name="subtitles file" + ) + chapters_file = models.FileField( + blank=True, null=True, upload_to="media", verbose_name="chapters file" + ) chapters = StreamField( [ @@ -49,11 +55,40 @@ class EtnaMedia(AbstractMedia): null=True, ) + def clean(self, *args, **kwargs): + super().clean(*args, **kwargs) + if self.subtitles_file: + validate = FileExtensionValidator(["vtt"]) + validate(self.subtitles_file) + if self.chapters_file: + validate = FileExtensionValidator(["vtt"]) + validate(self.chapters_file) + # Added full_url to be sent to the frontend via the Wagtail API @property def full_url(self): return settings.WAGTAILADMIN_BASE_URL + self.file.url + @property + def subtitles_file_url(self): + if self.subtitles_file and hasattr(self.subtitles_file, "url"): + return self.subtitles_file.url + + @property + def subtitles_file_full_url(self): + if self.subtitles_file and hasattr(self.subtitles_file, "url"): + return settings.WAGTAILADMIN_BASE_URL + self.subtitles_file.url + + @property + def chapters_file_url(self): + if self.chapters_file and hasattr(self.chapters_file, "url"): + return self.chapters_file.url + + @property + def chapters_file_full_url(self): + if self.chapters_file and hasattr(self.chapters_file, "url"): + return settings.WAGTAILADMIN_BASE_URL + self.chapters_file.url + admin_form_fields = ( "title", "date", @@ -66,6 +101,8 @@ def full_url(self): "height", "thumbnail", "transcript", + "subtitles_file", + "chapters_file", "tags", ) @@ -79,4 +116,6 @@ def mime(self): APIField("chapters"), APIField("description", serializer=RichTextSerializer()), APIField("transcript", serializer=RichTextSerializer()), + APIField("subtitles_file"), + APIField("chapters_file"), ]