Skip to content

Commit

Permalink
Add videochooser block + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-b committed Jan 22, 2021
1 parent 92dafd2 commit 7324ab7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 9 deletions.
20 changes: 20 additions & 0 deletions tests/app/migrations/0002_testpage_video_streamfield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.1.5 on 2021-01-21 22:54

from django.db import migrations
import wagtail.core.fields
import wagtailvideos.blocks


class Migration(migrations.Migration):

dependencies = [
('app', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='testpage',
name='video_streamfield',
field=wagtail.core.fields.StreamField([('video', wagtailvideos.blocks.VideoChooserBlock())], blank=True),
),
]
13 changes: 9 additions & 4 deletions tests/app/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from __future__ import unicode_literals

from django.db import models
from wagtail.core.models import Page
from wagtail.core.fields import StreamField
from wagtail.admin.edit_handlers import StreamFieldPanel

from wagtailvideos.edit_handlers import VideoChooserPanel

from wagtailvideos.blocks import VideoChooserBlock

class TestPage(Page):
video_field = models.ForeignKey(
'wagtailvideos.Video', related_name='+', null=True, blank=True, on_delete=models.SET_NULL)

video_streamfield = StreamField([
('video', VideoChooserBlock())
], blank=True)

content_panels = Page.content_panels + [
VideoChooserPanel('video_field')
VideoChooserPanel('video_field'),
StreamFieldPanel('video_streamfield'),
]
3 changes: 1 addition & 2 deletions tests/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',

'wagtail.core.middleware.SiteMiddleware',
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': ['tests/templates'],
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
Expand Down
41 changes: 41 additions & 0 deletions tests/app/test_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from wagtail.core.models import Page, Site
from wagtail.tests.utils import WagtailPageTests
from wagtail.tests.utils.form_data import nested_form_data, streamfield

from tests.app.models import TestPage
from tests.utils import create_test_video_file
from wagtailvideos.models import Video


class TestVideoBlock(WagtailPageTests):
def setUp(self):
super().setUp()
self.root_page = Page.objects.get(pk=1)
self.video = Video.objects.create(
title="Test Video",
file=create_test_video_file()
)

def test_block_admin(self):
self.assertCanCreate(self.root_page, TestPage, nested_form_data({
'title': 'VideoPage',
'video_streamfield': streamfield([
('video', self.video.id)
])
}))

def test_block_basic_render(self):
page = self.root_page.add_child(instance=TestPage(
title='Test',
slug='vidtest',
video_streamfield=[
('video', self.video)
]
))
Site.objects.create(
hostname='localhost', port=8080, root_page=page,
site_name='Test Site', is_default_site=True
)
response = self.client.get(page.full_url)

self.assertContains(response, self.video.video_tag(attrs={"controls": True}))
8 changes: 8 additions & 0 deletions tests/templates/app/test_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% load wagtailvideos_tags %}
<h1>{{ page.title }}</h1>

{% if self.video_field %}
{% video self.video_field controls %}
{% endif %}

{{ self.video_streamfield }}
4 changes: 1 addition & 3 deletions tests/test_admin_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import unicode_literals

import json

from django.contrib.auth import get_user_model
Expand All @@ -9,10 +7,10 @@
from django.test import TestCase, override_settings
from django.urls import reverse
from mock import patch
from tests.utils import create_test_video_file
from wagtail.core.models import Collection, GroupCollectionPermission
from wagtail.tests.utils import WagtailTestUtils

from tests.utils import create_test_video_file
from wagtailvideos.models import Video


Expand Down
22 changes: 22 additions & 0 deletions wagtailvideos/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from wagtail.core.blocks import ChooserBlock
from django.utils.functional import cached_property

class VideoChooserBlock(ChooserBlock):
@cached_property
def target_model(self):
from wagtailvideos.models import Video
return Video

@cached_property
def widget(self):
from wagtailvideos.widgets import AdminVideoChooser
return AdminVideoChooser

def render_basic(self, value, context=None):
if value:
return value.video_tag(attrs={"controls": True})
else:
return ""

class Meta:
icon = 'media'

0 comments on commit 7324ab7

Please sign in to comment.