Added action to restart media conversions
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Sep 2017 13:46:31 +0200
changeset 47 44889107d310
parent 46 55d5fde71e8f
child 48 463872a6656a
Added action to restart media conversions
src/pyams_media/zmi/interfaces.py
src/pyams_media/zmi/media.py
src/pyams_media/zmi/video.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_media/zmi/interfaces.py	Mon Sep 11 13:46:31 2017 +0200
@@ -0,0 +1,25 @@
+#
+# 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.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+
+# import packages
+from zope.interface import Interface
+
+
+class IMediaConversionsForm(Interface):
+    """Medias conversions form marker interface"""
--- a/src/pyams_media/zmi/media.py	Mon Sep 11 13:44:23 2017 +0200
+++ b/src/pyams_media/zmi/media.py	Mon Sep 11 13:46:31 2017 +0200
@@ -16,34 +16,61 @@
 # import standard library
 
 # import interfaces
-from pyams_file.interfaces import IVideo, IThumbnail, IAudio
-from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager
+from pyams_file.interfaces import IVideo, IThumbnail, IAudio, IMediaFile
+from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager, IFormHelp
 from pyams_media.interfaces import IMediaConversionUtility, VIDEO_FRAME_SIZE, IMediaConverter, IMediaConversions
+from pyams_media.zmi.interfaces import IMediaConversionsForm
 from pyams_skin.interfaces.viewlet import IContextActions
 from pyams_skin.layer import IPyAMSLayer
 from pyams_zmi.layer import IAdminLayer
+from transaction.interfaces import ITransactionManager
+from z3c.form.interfaces import DISPLAY_MODE
 
 # import packages
+from pyams_form.form import AJAXEditForm
+from pyams_form.help import FormHelp
+from pyams_form.schema import CloseButton
+from pyams_media.media import check_media_conversion
 from pyams_pagelet.pagelet import pagelet_config
-from pyams_skin.viewlet.toolbar import ToolbarMenuItem
+from pyams_skin.viewlet.toolbar import ToolbarActionItem
 from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config
 from pyams_utils.interfaces import MANAGE_PERMISSION
 from pyams_utils.registry import query_utility
 from pyams_viewlet.viewlet import viewlet_config, Viewlet
-from pyams_zmi.form import AdminDialogDisplayForm
-from z3c.form import field
-from zope.interface import Interface
+from pyams_zmi.form import AdminDialogEditForm
+from pyramid.view import view_config
+from z3c.form import field, button
+from zope.interface import implementer, Interface
 
 from pyams_media import _
 
 
+class IMediaConversionsFormButtons(Interface):
+    """Media conversions form buttons"""
+
+    close = CloseButton(name='close', title=_("Cancel"))
+    convert = button.Button(name='convert', title=_("Restart missing conversions"))
+
+
+@adapter_config(context=(IMediaFile, IAdminLayer, IMediaConversionsForm), provides=IFormHelp)
+class MediaConversionsFormHelpAdapter(FormHelp):
+    """Medias conversions form help adapter"""
+
+    message = _("""Several formats are required to handle different browsers and screen resolutions.
+    
+Conversion process may require some time based on media format and duration; this dashboard gives
+you indications about required conversions.""")
+    message_format = 'rest'
+
+
 #
 # Video conversions
 #
 
 @viewlet_config(name='video.conversions.action', context=IVideo, layer=IPyAMSLayer, view=Interface,
                 manager=IContextActions, permission=MANAGE_PERMISSION, weight=15)
-class VideoConversionsAction(ToolbarMenuItem):
+class VideoConversionsAction(ToolbarActionItem):
     """Video conversions action"""
 
     label = _("Video conversions...")
@@ -54,21 +81,49 @@
 
 
 @pagelet_config(name='video-conversions.html', context=IVideo, layer=IPyAMSLayer, permission=MANAGE_PERMISSION)
-class VideoConversionsDisplayForm(AdminDialogDisplayForm):
+@implementer(IMediaConversionsForm)
+class VideoConversionsEditForm(AdminDialogEditForm):
     """Video conversions display form"""
 
     legend = _("Selected video conversions")
     icon_css_class = 'fa fa-fw fa-compress'
 
     fields = field.Fields(Interface)
+    buttons = button.Buttons(IMediaConversionsFormButtons)
+    edit_permission = MANAGE_PERMISSION
+    ajax_handler = 'restart-video-conversions.json'
 
     @property
     def title(self):
         return self.context.title or self.context.filename
 
+    def check_mode(self):
+        if not self.request.has_permission(self.edit_permission, self.getContent()):
+            self.mode = DISPLAY_MODE
+
+    def updateActions(self):
+        super(VideoConversionsEditForm, self).updateActions()
+        if 'convert' in self.actions:
+            self.actions['convert'].addClass('btn-primary')
+
+    def applyChanges(self, data):
+        media = self.getContent()
+        ITransactionManager(media).get().addAfterCommitHook(check_media_conversion, kws={'media': media})
+        return True
+
+
+@view_config(name='restart-video-conversions.json', context=IVideo, request_type=IPyAMSLayer,
+             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+class VideoConversionsAJAXEditForm(AJAXEditForm, VideoConversionsEditForm):
+    """Video conversions edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        return {'status': 'success',
+                'message': self.request.localizer.translate(_("Media conversions restarted!"))}
+
 
 @viewlet_config(name='video-conversions-prefix', context=IVideo, layer=IAdminLayer,
-                view=VideoConversionsDisplayForm, manager=IWidgetsPrefixViewletsManager)
+                view=VideoConversionsEditForm, manager=IWidgetsPrefixViewletsManager)
 @template_config(template='templates/video-conversions.pt')
 class VideoConversionsViewletsPrefix(Viewlet):
     """Video conversions display form viewlets prefix"""
@@ -112,7 +167,7 @@
 
 @viewlet_config(name='audio.conversions.action', context=IAudio, layer=IPyAMSLayer, view=Interface,
                 manager=IContextActions, permission=MANAGE_PERMISSION, weight=15)
-class AudioConversionsAction(ToolbarMenuItem):
+class AudioConversionsAction(ToolbarActionItem):
     """Audio conversions action"""
 
     label = _("Audio conversions...")
@@ -123,21 +178,49 @@
 
 
 @pagelet_config(name='audio-conversions.html', context=IAudio, layer=IPyAMSLayer, permission=MANAGE_PERMISSION)
-class AudioConversionsDisplayForm(AdminDialogDisplayForm):
+@implementer(IMediaConversionsForm)
+class AudioConversionsEditForm(AdminDialogEditForm):
     """Audio conversions display form"""
 
     legend = _("Selected audio conversions")
     icon_css_class = 'fa fa-fw fa-compress'
 
     fields = field.Fields(Interface)
+    buttons = button.Buttons(IMediaConversionsFormButtons)
+    edit_permission = MANAGE_PERMISSION
+    ajax_handler = 'restart-audio-conversions.json'
 
     @property
     def title(self):
         return self.context.title or self.context.filename
 
+    def check_mode(self):
+        if not self.request.has_permission(self.edit_permission, self.getContent()):
+            self.mode = DISPLAY_MODE
+
+    def updateActions(self):
+        super(AudioConversionsEditForm, self).updateActions()
+        if 'convert' in self.actions:
+            self.actions['convert'].addClass('btn-primary')
+
+    def applyChanges(self, data):
+        media = self.getContent()
+        ITransactionManager(media).get().addAfterCommitHook(check_media_conversion, kws={'media': media})
+        return True
+
+
+@view_config(name='restart-audio-conversions.json', context=IAudio, request_type=IPyAMSLayer,
+             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+class AudioConversionsAJAXEditForm(AJAXEditForm, AudioConversionsEditForm):
+    """Audio conversions edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        return {'status': 'success',
+                'message': self.request.localizer.translate(_("Media conversions restarted!"))}
+
 
 @viewlet_config(name='audio-conversions-prefix', context=IAudio, layer=IAdminLayer,
-                view=AudioConversionsDisplayForm, manager=IWidgetsPrefixViewletsManager)
+                view=AudioConversionsEditForm, manager=IWidgetsPrefixViewletsManager)
 @template_config(template='templates/audio-conversions.pt')
 class AudioConversionsViewletsPrefix(Viewlet):
     """Audio conversions display form viewlets prefix"""
--- a/src/pyams_media/zmi/video.py	Mon Sep 11 13:44:23 2017 +0200
+++ b/src/pyams_media/zmi/video.py	Mon Sep 11 13:46:31 2017 +0200
@@ -30,7 +30,7 @@
 from pyams_form.schema import CloseButton
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.layer import IPyAMSLayer
-from pyams_skin.viewlet.toolbar import ToolbarMenuDivider, ToolbarMenuItem
+from pyams_skin.viewlet.toolbar import ToolbarActionItem
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 from pyams_utils.schema import DottedDecimalField
@@ -43,19 +43,13 @@
 from pyams_media import _
 
 
-@viewlet_config(name='video.thumbnail.divider', context=IVideo, layer=IPyAMSLayer, view=Interface,
-                manager=IContextActions, permission=MANAGE_PERMISSION, weight=19)
-class VideoDividerAction(ToolbarMenuDivider):
-    """Video divider action"""
-
-
 #
 # Video thumbnail
 #
 
 @viewlet_config(name='video.thumbnail.action', context=IVideo, layer=IPyAMSLayer, view=Interface,
                 manager=IContextActions, permission=MANAGE_PERMISSION, weight=20)
-class VideoThumbnailAction(ToolbarMenuItem):
+class VideoThumbnailAction(ToolbarActionItem):
     """Video thumbnail selection action"""
 
     label = _("Select thumbnail...")