Skip to content

Commit 54c1eb8

Browse files
authored
Merge pull request #67 from thenewguy/issue-65
Fix issue 65
2 parents 0a408e8 + aff3240 commit 54c1eb8

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ Here is an example:
113113
from __future__ import unicode_literals
114114

115115
from django.db import models
116-
from django.utils.html import format_html
116+
from django.forms.utils import flatatt
117+
from django.utils.html import format_html, format_html_join
117118

118119
from wagtail.wagtailcore.models import Page
119120
from wagtail.wagtailcore.fields import StreamField
@@ -132,7 +133,7 @@ class TestMediaBlock(AbstractMediaChooserBlock):
132133
player_code = '''
133134
<div>
134135
<video width="320" height="240" controls>
135-
<source src="{0}" type="video/mp4">
136+
{0}
136137
Your browser does not support the video tag.
137138
</video>
138139
</div>
@@ -141,13 +142,16 @@ class TestMediaBlock(AbstractMediaChooserBlock):
141142
player_code = '''
142143
<div>
143144
<audio controls>
144-
<source src="{0}" type="audio/mpeg">
145+
{0}
145146
Your browser does not support the audio element.
146147
</audio>
147148
</div>
148149
'''
149150

150-
return format_html(player_code, value.file.url)
151+
return format_html(player_code, format_html_join(
152+
'\n', "<source{0}>",
153+
[[flatatt(s)] for s in value.sources]
154+
))
151155

152156

153157
class BlogPage(Page):

wagtailmedia/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import mimetypes
34
import os.path
45

56
from django.conf import settings
@@ -80,6 +81,13 @@ def file_extension(self):
8081
def url(self):
8182
return self.file.url
8283

84+
@property
85+
def sources(self):
86+
return [{
87+
'src': self.url,
88+
'type': mimetypes.guess_type(self.filename)[0],
89+
}]
90+
8391
def get_usage(self):
8492
return get_object_usage(self)
8593

wagtailmedia/tests/test_models.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import unicode_literals
22

3+
from django.core.files import File
4+
from django.core.files.base import ContentFile
35
from django.test import TestCase
46

7+
from six import b
8+
59
from wagtailmedia import models
610
from wagtailmedia.forms import get_media_form
711

@@ -53,3 +57,25 @@ def test_audio_form_presents_thumbnail(self):
5357
MediaForm = get_media_form(models.Media)
5458
media = models.Media.objects.create(title="Test media file", type='audio', duration=100)
5559
self.assertIn('thumbnail', MediaForm(instance=media).fields.keys())
60+
61+
62+
class TestAbstractMediaInterfaceModel(TestCase):
63+
def test_sources_mp4_type(self):
64+
fake_file = ContentFile(b("A boring example movie"))
65+
fake_file.name = 'movie.mp4'
66+
media = models.Media()
67+
media.file = File(fake_file)
68+
self.assertEqual(media.sources, [{
69+
'src': '/media/movie.mp4',
70+
'type': 'video/mp4',
71+
}])
72+
73+
def test_sources_unknown_type(self):
74+
fake_file = ContentFile(b("A boring example movie"))
75+
fake_file.name = 'movie'
76+
media = models.Media()
77+
media.file = File(fake_file)
78+
self.assertEqual(media.sources, [{
79+
'src': '/media/movie',
80+
'type': None,
81+
}])

0 commit comments

Comments
 (0)