Skip to content

Commit

Permalink
Add chapters to media items (#1726)
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 22, 2024
1 parent cfa7e7f commit c3038ff
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions etna/api/tests/expected_results/article.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
"date": null,
"description": "<p>Description</p>",
"transcript": "<p>Transcript</p>",
"chapters": [],
"width": 1920,
"height": 1080,
"duration": 10.0
Expand Down
9 changes: 9 additions & 0 deletions etna/media/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def get_api_representation(self, value, context=None):
We use expand_db_html to get any rich text fields as useful HTML,
rather than the raw database representation.
"""
chapters = [
{
"time": int(chapter.value["time"]),
"heading": chapter.value["heading"],
"transcript": expand_db_html(chapter.value["transcript"].source),
}
for chapter in value.chapters
]
return {
"id": value.id,
"file": value.url,
Expand All @@ -34,6 +42,7 @@ def get_api_representation(self, value, context=None):
"date": value.date,
"description": expand_db_html(value.description),
"transcript": expand_db_html(value.transcript),
"chapters": sorted(chapters, key=lambda x: x["time"]),
"width": value.width,
"height": value.height,
"duration": value.duration,
Expand Down
47 changes: 47 additions & 0 deletions etna/media/migrations/0002_etnamedia_chapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 5.0.8 on 2024-10-21 10:27

import django.core.validators
import wagtail.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("media", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="etnamedia",
name="chapters",
field=wagtail.fields.StreamField(
[("chapter", 3)],
blank=True,
block_lookup={
0: (
"wagtail.blocks.IntegerBlock",
(),
{
"blank": True,
"default": 0,
"label": "Time in seconds",
"validators": [django.core.validators.MinValueValidator(0)],
},
),
1: ("wagtail.blocks.CharBlock", (), {"max_length": 20}),
2: (
"etna.core.blocks.paragraph.APIRichTextBlock",
(),
{"features": ["bold", "italic"], "required": False},
),
3: (
"wagtail.blocks.StructBlock",
[[("time", 0), ("heading", 1), ("transcript", 2)]],
{},
),
},
null=True,
),
),
]
29 changes: 28 additions & 1 deletion etna/media/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import mimetypes

from django.conf import settings
from django.core.validators import MinValueValidator
from django.db import models

from wagtail import blocks
from wagtail.api import APIField
from wagtail.fields import RichTextField
from wagtail.fields import RichTextField, StreamField

from wagtailmedia.models import AbstractMedia

from etna.core.blocks.paragraph import APIRichTextBlock
from etna.core.serializers import RichTextSerializer


class MediaChapterSectionBlock(blocks.StructBlock):
time = blocks.IntegerBlock(
blank=True,
default=0,
validators=[MinValueValidator(0)],
label="Time in seconds",
)
heading = blocks.CharBlock(max_length=20)
transcript = APIRichTextBlock(required=False, features=["bold", "italic"])

class Meta:
label = "Chapter"


class EtnaMedia(AbstractMedia):
"""
Extend wagtailmedia model.
Expand All @@ -24,6 +41,14 @@ class EtnaMedia(AbstractMedia):
blank=True, null=True, features=settings.INLINE_RICH_TEXT_FEATURES
)

chapters = StreamField(
[
("chapter", MediaChapterSectionBlock()),
],
blank=True,
null=True,
)

# Added full_url to be sent to the frontend via the Wagtail API
@property
def full_url(self):
Expand All @@ -33,6 +58,7 @@ def full_url(self):
"title",
"date",
"file",
"chapters",
"collection",
"description",
"duration",
Expand All @@ -50,6 +76,7 @@ def mime(self):
APIField("type"),
APIField("url"),
APIField("mime"),
APIField("chapters"),
APIField("description", serializer=RichTextSerializer()),
APIField("transcript", serializer=RichTextSerializer()),
]

0 comments on commit c3038ff

Please sign in to comment.