Skip to content

Commit

Permalink
Add chapters to media items
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Oct 11, 2024
1 parent c876d2e commit 3f13556
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
3 changes: 3 additions & 0 deletions etna/media/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def get_api_representation(self, value, context=None):
"date": value.date,
"description": expand_db_html(value.description),
"transcript": expand_db_html(value.transcript),
"chapters": value.chapters.stream_block.get_api_representation(
value.chapters, context
),
"width": value.width,
"height": value.height,
"duration": value.duration,
Expand Down
46 changes: 46 additions & 0 deletions etna/media/migrations/0002_etnamedia_chapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 5.0.8 on 2024-10-11 11:42

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)],
block_lookup={
0: (
"wagtail.blocks.DecimalBlock",
(),
{
"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)]],
{},
),
},
default=None,
),
),
]
33 changes: 32 additions & 1 deletion etna/media/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
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.DecimalBlock(
blank=True,
default=0,
validators=[MinValueValidator(0)],
label="Time in seconds",
)
heading = blocks.CharBlock(max_length=20)
transcript = APIRichTextBlock(required=False, features=["bold", "italic"])

def get_api_representation(self, value, context=None):
representation = super().get_api_representation(value, context)
representation["time"] = int(representation["time"])
return representation

class Meta:
label = "Chapter"


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

chapters = StreamField(
[
("chapter", MediaChapterSectionBlock()),
],
default=None,
)

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

0 comments on commit 3f13556

Please sign in to comment.