src/pyams_media/skin/video.py
changeset 118 0ebdb35ceaed
child 119 4cdacd1e34c7
equal deleted inserted replaced
117:a498a17a34c4 118:0ebdb35ceaed
       
     1 #
       
     2 # Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 from pyramid.renderers import render
       
    16 from zope.interface import Interface
       
    17 
       
    18 from pyams_file.interfaces import IFile
       
    19 from pyams_template.template import get_view_template
       
    20 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
       
    21 from pyams_utils.interfaces.tales import ITALESExtension
       
    22 
       
    23 
       
    24 @adapter_config(name='video',
       
    25                 context=(Interface, Interface, Interface),
       
    26                 provides=ITALESExtension)
       
    27 class VideoTALESExtension(ContextRequestViewAdapter):
       
    28     """Video TALES adapter
       
    29 
       
    30     This TALES adapter can be used to create a 'video' HTML tag
       
    31     embedding all video attributes.
       
    32     """
       
    33 
       
    34     _render = get_view_template()
       
    35 
       
    36     def render(self, context=None, *args, **kwargs):
       
    37         """Video renderer
       
    38 
       
    39         Method signature accepts unused *args and **kwargs to match 'picture' TALES extension.
       
    40         """
       
    41         if context is None:
       
    42             context = self.context
       
    43         return render('templates/video.pt', {
       
    44             'video': context
       
    45         }, request=self.request)
       
    46 
       
    47 
       
    48 @adapter_config(name='media',
       
    49                 context=(Interface, Interface, Interface),
       
    50                 provides=ITALESExtension)
       
    51 class MediaTALESExtension(ContextRequestViewAdapter):
       
    52     """Media TALES adapter
       
    53 
       
    54     This TALES adapter can be used to create any media HTML tag.
       
    55     Actually images and videos are supported, and the right renderer will be used according
       
    56     to context content type.
       
    57     """
       
    58 
       
    59     def render(self, context=None, lg_thumb='lg', lg_width=12, md_thumb='md', md_width=12,
       
    60                sm_thumb='sm', sm_width=12, xs_thumb='xs', xs_width=12, alt='', css_class=''):
       
    61         if context is None:
       
    62             context = self.context
       
    63         if not IFile.providedBy(context):
       
    64             return ''
       
    65         registry = self.request.registry
       
    66         if context.content_type.startswith('video/'):
       
    67             extension = registry.queryMultiAdapter((context, self.request, self.view), ITALESExtension, name='video')
       
    68         elif context.content_type.startswith('image/'):
       
    69             extension = registry.queryMultiAdapter((context, self.request, self.view), ITALESExtension, name='picture')
       
    70         else:
       
    71             extension = None
       
    72         if extension is not None:
       
    73             return extension.render(context, lg_thumb, lg_width, md_thumb, md_width, sm_thumb, sm_width, xs_thumb,
       
    74                                     xs_width, alt, css_class)
       
    75         else:
       
    76             return ''