Commit 5c30deb9 authored by Seb's avatar Seb
Browse files

Improved video tag, added ability for extra attributes

parent c70af40e
......@@ -149,6 +149,10 @@ class AbstractVideo(CollectionMember, TagSearchable):
def filename(self):
return os.path.basename(self.file.name)
@property
def file_ext(self):
return os.path.splitext(self.filename)[1][1:]
def is_editable_by_user(self, user):
from wagtail.wagtailimages.permissions import permission_policy
return permission_policy.user_has_permission_for_instance(user, 'change', self)
......
from __future__ import absolute_import, unicode_literals
from django import template
from django.forms.widgets import flatatt
from django.template import resolve_variable
from django.utils.text import mark_safe
from wagtailvideos.models import MediaFormats, Video
register = template.Library()
# {% video self.intro_video html5(optional) %}
# {% video self.intro_video html5(optional) extra_att extra_att %}
@register.tag(name="video")
def video(parser, token):
contents = token.split_contents()
template_params = token.split_contents()[1:] # Everything after 'video'
video_expr = template_params[0]
extra_attrs = {}
html5 = False
# Everyting after video expression
if(len(template_params) > 1):
for param in template_params[1:]:
if param == 'html5':
html5 = True
else:
try:
video_field = contents[1] # A Video object should be the first variable
name, value = param.split('=')
extra_attrs[name] = value
except ValueError:
raise template.TemplateSyntaxError("video tag requires a Video as the first option")
if len(contents) > 2:
return VideoNode(video_field, contents[2] == 'html5')
else:
return VideoNode(video_field)
extra_attrs[param] = '' # attributes without values e.g. autoplay, controls
return VideoNode(video_expr, html5, extra_attrs)
class VideoNode(template.Node):
def __init__(self, video, html5=False):
def __init__(self, video, html5=False, attrs={}):
self.video = template.Variable(video)
self.html5 = html5
self.attrs = attrs
def render(self, context):
video = self.video.resolve(context)
if not self.html5:
return mark_safe("<video controls><source src='{0}' type='{1}'></video>"
.format(video.url, 'video/mp4'))
return mark_safe("<video {0}><source src='{1}' type='video/{2}'></video>"
.format(flatatt(self.attrs), video.url, video.file_ext)) # FIXME get mimetype properly
else:
return ''
# TODO
# https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailimages/models.py#L500
transcodes = []
for media_format in MediaFormats:
transcode = video.get_transcode(media_format) # FIXME this is blocking
transcodes.append("<source src='{0}' type='video/{1}' >".format(transcode.url, transcode.media_format.name))
return mark_safe(
"<video {0}>{1}</video".format(flatatt(self.attrs), "\n".join(transcodes)))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment