Added small videos conversions dashboard
authorThierry Florac <thierry.florac@onf.fr>
Tue, 08 Sep 2015 16:21:33 +0200
changeset 2 5410acbcfb77
parent 1 1ade73c9f2c0
child 3 4fe9ea97da5a
Added small videos conversions dashboard
src/pyams_media/zmi/media.py
src/pyams_media/zmi/templates/media-conversions.pt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_media/zmi/media.py	Tue Sep 08 16:21:33 2015 +0200
@@ -0,0 +1,98 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+from pyams_file.interfaces import IMediaFile, IVideo, IThumbnail
+from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager
+from pyams_media.ffbase import FFmpeg
+from pyams_media.ffdocument import FFDocument
+from pyams_media.interfaces import IMediaConversionUtility, VIDEO_FRAME_SIZE, IMediaConverter, IMediaConversions
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.interfaces.viewlet import IContextActions
+from pyams_skin.layer import IPyAMSLayer
+from pyams_skin.viewlet.toolbar import ToolbarMenuItem
+from pyams_template.template import template_config
+from pyams_utils.property import cached_property
+from pyams_utils.registry import query_utility
+from pyams_viewlet.viewlet import viewlet_config, Viewlet
+from pyams_zmi.form import AdminDialogDisplayForm
+from pyams_zmi.layer import IAdminLayer
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+
+# import packages
+from z3c.form import field
+from zope.interface import Interface
+
+from pyams_media import _
+
+
+@viewlet_config(name='media.conversions.action', context=IVideo, layer=IPyAMSLayer, view=Interface,
+                manager=IContextActions, permission='manage', weight=15)
+class MediaConversionsAction(ToolbarMenuItem):
+    """Media conversions action"""
+
+    label = _("Media conversions...")
+    label_css_class = 'fa fa-fw fa-compress'
+
+    url = 'media-conversions.html'
+    modal_target = True
+
+
+@pagelet_config(name='media-conversions.html', context=IVideo, layer=IPyAMSLayer, permission='manage')
+class MediaConversionsDisplayForm(AdminDialogDisplayForm):
+    """Media conversions display form"""
+
+    legend = _("Selected media conversions")
+    icon_css_class = 'fa fa-fw fa-compress'
+
+    fields = field.Fields(Interface)
+
+    @property
+    def title(self):
+        return self.context.title or self.context.filename
+
+
+@viewlet_config(name='media-conversions-prefix', context=IVideo, layer=IAdminLayer,
+                view=MediaConversionsDisplayForm, manager=IWidgetsPrefixViewletsManager)
+@template_config(template='templates/media-conversions.pt')
+class MediaConversionsViewletsPrefix(Viewlet):
+    """Media conversions display form viewlets prefix"""
+
+    @property
+    def formats(self):
+        utility = query_utility(IMediaConversionUtility)
+        if utility is not None:
+            for format in utility.video_frame_size:
+                yield format, VIDEO_FRAME_SIZE.get(format)
+
+    @property
+    def converters(self):
+        utility = query_utility(IMediaConversionUtility)
+        if utility is not None:
+            for converter_name in utility.video_formats:
+                converter = query_utility(IMediaConverter, name=converter_name)
+                if converter is not None:
+                    yield converter_name, converter
+
+    @property
+    def original_frame_size(self):
+        return IThumbnail(self.context).get_image_size()
+
+    def has_conversion(self, converter, frame_size):
+        conversions = IMediaConversions(self.context)
+        name = 'video{width}.{extension}'.format(width='-{0}'.format(frame_size) if frame_size else '',
+                                                 extension=converter.format)
+        return name in conversions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_media/zmi/templates/media-conversions.pt	Tue Sep 08 16:21:33 2015 +0200
@@ -0,0 +1,43 @@
+<table class="" i18n:domain="pyams_media"
+		tal:define="original_size view.original_frame_size">
+	<tr>
+		<th>&nbsp;</th>
+		<th class="text-center padding-x-10">
+			<i18n:var translate="">Original size</i18n:var><br />
+			(<tal:var content="'{0[0]}x{0[1]}'.format(original_size)" />)
+		</th>
+		<tal:loop repeat="format view.formats">
+			<th class="text-center padding-x-10" tal:define="format_name format[0];">
+				<tal:var content="format_name" /><br />
+				(<tal:var content="'{0[0]}x{0[1]}'.format(format[1])" />)
+			</th>
+		</tal:loop>
+	</tr>
+	<tal:loop repeat="converter_util view.converters">
+		<tr tal:define="converter_name converter_util[0];
+						converter converter_util[1];">
+			<td tal:content="converter.label" i18n:translate=""></td>
+			<td tal:define="converted view.has_conversion(converter, None)"
+				class="text-align-center">
+				<span tal:condition="converted"
+					  class="txt-color-green" i18n:translate="">OK</span>
+				<span tal:condition="not converted"
+					  class="txt-color-orange" i18n:translate="">Waiting...</span>
+			</td>
+			<tal:loop repeat="format view.formats">
+				<td class="text-center padding-x-10"
+					tal:define="frame_width format[1][0];
+								maybe frame_width < original_size[0];
+								converted view.has_conversion(converter, format);">
+					<span tal:condition="not maybe">N/A</span>
+					<tal:if condition="maybe">
+						<span tal:condition="converted"
+							  class="txt-color-green" i18n:translate="">OK</span>
+						<span tal:condition="not converted"
+							  class="txt-color-orange" i18n:translate="">Waiting...</span>
+					</tal:if>
+				</td>
+			</tal:loop>
+		</tr>
+	</tal:loop>
+</table>