Skip to content

Commit

Permalink
Add VTT subtitles and chapters fields to media items
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Oct 24, 2024
1 parent 115ae95 commit 7b3c50c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
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 7b3c50c

Please sign in to comment.