Skip to content

Commit

Permalink
Add VTT subtitles and chapters fields to media items (#1736)
Browse files Browse the repository at this point in the history
Co-authored-by: James Biggs <[email protected]>
  • Loading branch information
ahosgood and jamesbiggs authored Oct 28, 2024
1 parent 115ae95 commit f3ea5ff
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
6 changes: 5 additions & 1 deletion etna/api/tests/expected_results/article.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions etna/media/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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"
),
),
]
41 changes: 40 additions & 1 deletion etna/media/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
[
Expand All @@ -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",
Expand All @@ -66,6 +101,8 @@ def full_url(self):
"height",
"thumbnail",
"transcript",
"subtitles_file",
"chapters_file",
"tags",
)

Expand All @@ -79,4 +116,6 @@ def mime(self):
APIField("chapters"),
APIField("description", serializer=RichTextSerializer()),
APIField("transcript", serializer=RichTextSerializer()),
APIField("subtitles_file"),
APIField("chapters_file"),
]

0 comments on commit f3ea5ff

Please sign in to comment.