Use new file modifier action and permission checker
authorThierry Florac <thierry.florac@onf.fr>
Wed, 12 Jul 2017 12:06:48 +0200
changeset 43 1a33309aef31
parent 42 63b5882984e2
child 44 6ccd68cd7379
Use new file modifier action and permission checker
src/pyams_file/zmi/file.py
src/pyams_file/zmi/image.py
--- a/src/pyams_file/zmi/file.py	Wed Jul 12 12:05:13 2017 +0200
+++ b/src/pyams_file/zmi/file.py	Wed Jul 12 12:06:48 2017 +0200
@@ -16,37 +16,38 @@
 # import standard library
 
 # import interfaces
-from pyams_file.interfaces import IFile, IFileInfo
+from pyams_file.interfaces import IFile, IFileInfo, IFileModifierForm
 from pyams_skin.interfaces.viewlet import IContextActions
 from pyams_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import MANAGE_PERMISSION, VIEW_PERMISSION
+from pyams_utils.interfaces import VIEW_PERMISSION
 
 # import packages
+from pyams_file.zmi import FileModifierAction
 from pyams_form.form import AJAXEditForm
 from pyams_pagelet.pagelet import pagelet_config
-from pyams_skin.viewlet.toolbar import ToolbarMenuItem
 from pyams_viewlet.viewlet import viewlet_config
 from pyams_zmi.form import AdminDialogEditForm
 from pyramid.view import view_config
 from z3c.form import field
-from zope.interface import Interface
+from zope.interface import implementer, Interface
 
 from pyams_file import _
 
 
 @viewlet_config(name='file.properties.action', context=IFile, layer=IPyAMSLayer, view=Interface,
-                manager=IContextActions, permission=VIEW_PERMISSION, weight=1)
-class FilePropertiesAction(ToolbarMenuItem):
+                manager=IContextActions, weight=1)
+class FilePropertiesAction(FileModifierAction):
     """File properties action"""
 
     label = _("Properties...")
-    label_css_class = 'fa fa-fw fa-edit'
+    label_css_class = 'fa fa-fw-md fa-edit'
 
     url = 'properties.html'
     modal_target = True
 
 
 @pagelet_config(name='properties.html', context=IFile, layer=IPyAMSLayer, permission=VIEW_PERMISSION)
+@implementer(IFileModifierForm)
 class FilePropertiesEditForm(AdminDialogEditForm):
     """File properties edit form"""
 
@@ -55,6 +56,7 @@
 
     fields = field.Fields(IFileInfo)
     ajax_handler = 'properties.json'
+    edit_permission = None
 
     @property
     def title(self):
@@ -67,7 +69,7 @@
 
 
 @view_config(name='properties.json', context=IFile, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class FilePropertiesAJAXEditForm(AJAXEditForm, FilePropertiesEditForm):
     """File properties edit form, AJAX renderer"""
 
@@ -79,3 +81,4 @@
                     'smallbox_status': 'success'}
         else:
             return super(FilePropertiesAJAXEditForm, self).get_ajax_output(changes)
+
--- a/src/pyams_file/zmi/image.py	Wed Jul 12 12:05:13 2017 +0200
+++ b/src/pyams_file/zmi/image.py	Wed Jul 12 12:06:48 2017 +0200
@@ -16,20 +16,20 @@
 # import standard library
 
 # import interfaces
-from pyams_file.interfaces import IImage, IThumbnail, IResponsiveImage, IImageWidget
+from pyams_file.interfaces import IImage, IThumbnail, IResponsiveImage, IImageWidget, IFileModifierForm
 from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager, IFormHelp
 from pyams_skin.interfaces.viewlet import IContextActions
 from pyams_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import MANAGE_PERMISSION
+from pyams_utils.interfaces import VIEW_PERMISSION
 from pyams_zmi.layer import IAdminLayer
 
 # import packages
 from pyams_file.image import ThumbnailGeometry
+from pyams_file.zmi import FileModifierAction
 from pyams_form.form import AJAXEditForm
 from pyams_form.help import FormHelp
 from pyams_form.schema import CloseButton
 from pyams_pagelet.pagelet import pagelet_config
-from pyams_skin.viewlet.toolbar import ToolbarMenuItem, ToolbarMenuDivider
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 from pyams_viewlet.viewlet import viewlet_config, Viewlet
@@ -43,23 +43,17 @@
 from pyams_file import _
 
 
-@viewlet_config(name='image.resize.divider', context=IImage, layer=IPyAMSLayer, view=IImageWidget,
-                manager=IContextActions, permission=MANAGE_PERMISSION, weight=19)
-class ImageDividerAction(ToolbarMenuDivider):
-    """Image divider action"""
-
-
 #
 # Image resize
 #
 
 @viewlet_config(name='image.resize.action', context=IImage, layer=IPyAMSLayer, view=IImageWidget,
-                manager=IContextActions, permission=MANAGE_PERMISSION, weight=20)
-class ImageResizeAction(ToolbarMenuItem):
+                manager=IContextActions, weight=20)
+class ImageResizeAction(FileModifierAction):
     """Image resize action"""
 
     label = _("Resize image...")
-    label_css_class = 'fa fa-fw fa-compress'
+    label_css_class = 'fa fa-fw-md fa-compress'
 
     url = 'resize.html'
     modal_target = True
@@ -104,7 +98,8 @@
     return info
 
 
-@pagelet_config(name='resize.html', context=IImage, layer=IPyAMSLayer, permission=MANAGE_PERMISSION)
+@pagelet_config(name='resize.html', context=IImage, layer=IPyAMSLayer, permission=VIEW_PERMISSION)
+@implementer(IFileModifierForm)
 class ImageResizeForm(AdminDialogEditForm):
     """Image resize form"""
 
@@ -114,6 +109,7 @@
     fields = field.Fields(IImageResizeInfo)
     buttons = button.Buttons(IResizeFormButtons)
     ajax_handler = 'resize.json'
+    edit_permission = None
 
     @property
     def title(self):
@@ -126,7 +122,7 @@
 
 
 @view_config(name='resize.json', context=IImage, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class ImageResizeAJAXForm(AJAXEditForm, ImageResizeForm):
     """Image resize form, AJAX renderer"""
 
@@ -154,12 +150,12 @@
 #
 
 @viewlet_config(name='image.crop.action', context=IImage, layer=IPyAMSLayer, view=IImageWidget,
-                manager=IContextActions, permission=MANAGE_PERMISSION, weight=21)
-class ImageCropAction(ToolbarMenuItem):
+                manager=IContextActions, weight=21)
+class ImageCropAction(FileModifierAction):
     """Image crop action"""
 
     label = _("Crop image...")
-    label_css_class = 'fa fa-fw fa-crop'
+    label_css_class = 'fa fa-fw-md fa-crop'
 
     url = 'crop.html'
     modal_target = True
@@ -172,7 +168,8 @@
     crop = button.Button(name='crop', title=_("Crop"))
 
 
-@pagelet_config(name='crop.html', context=IImage, layer=IPyAMSLayer, permission=MANAGE_PERMISSION)
+@pagelet_config(name='crop.html', context=IImage, layer=IPyAMSLayer, permission=VIEW_PERMISSION)
+@implementer(IFileModifierForm)
 class ImageCropForm(AdminDialogEditForm):
     """Image crop form"""
 
@@ -183,6 +180,7 @@
     fields = field.Fields(Interface)
     buttons = button.Buttons(ICropFormButtons)
     ajax_handler = 'crop.json'
+    edit_permission = None
 
     @property
     def title(self):
@@ -195,7 +193,7 @@
 
 
 @view_config(name='crop.json', context=IImage, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class ImageCropAJAXForm(AJAXEditForm, ImageCropForm):
     """Image crop form, AJAX renderer"""
 
@@ -234,12 +232,6 @@
 # Image square thumbnail selection
 #
 
-@viewlet_config(name='image.thumb.divider', context=IImage, layer=IPyAMSLayer, view=IImageWidget,
-                manager=IContextActions, permission=MANAGE_PERMISSION, weight=30)
-class ImageThumbnailsDividerAction(ToolbarMenuDivider):
-    """Image divider action"""
-
-
 class IThumbnailFormButtons(Interface):
     """Image crop form buttons"""
 
@@ -248,18 +240,19 @@
 
 
 @viewlet_config(name='image.thumb.square.action', context=IImage, layer=IPyAMSLayer, view=IImageWidget,
-                manager=IContextActions, permission=MANAGE_PERMISSION, weight=31)
-class ImageSquareThumbnailAction(ToolbarMenuItem):
+                manager=IContextActions, weight=31)
+class ImageSquareThumbnailAction(FileModifierAction):
     """Square thumbnail image selection"""
 
     label = _("Select square thumbnail...")
-    label_css_class = 'fa fa-fw fa-instagram'
+    label_css_class = 'fa fa-fw-md fa-instagram'
 
     url = 'square-thumbnail.html'
     modal_target = True
 
 
-@pagelet_config(name='square-thumbnail.html', context=IImage, layer=IPyAMSLayer, permission=MANAGE_PERMISSION)
+@pagelet_config(name='square-thumbnail.html', context=IImage, layer=IPyAMSLayer, permission=VIEW_PERMISSION)
+@implementer(IFileModifierForm)
 class ImageSquareThumbnailEditForm(AdminDialogEditForm):
     """Image square thumbnail edit form"""
 
@@ -270,6 +263,7 @@
     fields = field.Fields(Interface)
     buttons = button.Buttons(IThumbnailFormButtons)
     ajax_handler = 'square-thumbnail.json'
+    edit_permission = None
 
     @property
     def title(self):
@@ -282,7 +276,7 @@
 
 
 @view_config(name='square-thumbnail.json', context=IImage, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class ImageSquareThumbnailAJAXEditForm(AJAXEditForm, ImageSquareThumbnailEditForm):
     """Image square thumbnail edit form, AJAX renderer"""
 
@@ -312,18 +306,19 @@
 #
 
 @viewlet_config(name='image.thumb.pano.action', context=IImage, layer=IAdminLayer, view=IImageWidget,
-                manager=IContextActions, permission=MANAGE_PERMISSION, weight=32)
-class ImagePanoThumbnailAction(ToolbarMenuItem):
+                manager=IContextActions, weight=32)
+class ImagePanoThumbnailAction(FileModifierAction):
     """Panoramic thumbnail image selection"""
 
     label = _("Select panoramic thumbnail...")
-    label_css_class = 'fa fa-fw fa-youtube-play'
+    label_css_class = 'fa fa-fw-md fa-youtube-play'
 
     url = 'pano-thumbnail.html'
     modal_target = True
 
 
-@pagelet_config(name='pano-thumbnail.html', context=IImage, layer=IPyAMSLayer, permission=MANAGE_PERMISSION)
+@pagelet_config(name='pano-thumbnail.html', context=IImage, layer=IPyAMSLayer, permission=VIEW_PERMISSION)
+@implementer(IFileModifierForm)
 class ImagePanoThumbnailEditForm(AdminDialogEditForm):
     """Image panoramic thumbnail edit form"""
 
@@ -334,6 +329,7 @@
     fields = field.Fields(Interface)
     buttons = button.Buttons(IThumbnailFormButtons)
     ajax_handler = 'pano-thumbnail.json'
+    edit_permission = None
 
     @property
     def title(self):
@@ -346,7 +342,7 @@
 
 
 @view_config(name='pano-thumbnail.json', context=IImage, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class ImagePanoThumbnailAJAXEditForm(AJAXEditForm, ImagePanoThumbnailEditForm):
     """Image panoramic thumbnail edit form, AJAX renderer"""
 
@@ -375,12 +371,6 @@
 # Image responsive selections
 #
 
-@viewlet_config(name='responsive-image.selection.divider', context=IResponsiveImage, layer=IPyAMSLayer,
-                view=IImageWidget, manager=IContextActions, permission=MANAGE_PERMISSION, weight=40)
-class ResponsiveImageSelectionDividerAction(ToolbarMenuDivider):
-    """Image divider action"""
-
-
 class IResponsiveImageSelectionFormButtons(Interface):
     """Responsive image selection form buttons"""
 
@@ -388,6 +378,7 @@
     select = button.Button(name='select', title=_("Select image"))
 
 
+@implementer(IFileModifierForm)
 class ResponsiveImageSelectionForm(AdminDialogEditForm):
     """Base responsive image selection edit form"""
 
@@ -395,6 +386,7 @@
 
     fields = field.Fields(Interface)
     buttons = button.Buttons(IResponsiveImageSelectionFormButtons)
+    edit_permission = None
 
     @property
     def title(self):
@@ -435,19 +427,19 @@
 #
 
 @viewlet_config(name='responsive-image.selection.xs.action', context=IResponsiveImage, layer=IAdminLayer,
-                view=IImageWidget, manager=IContextActions, permission=MANAGE_PERMISSION, weight=41)
-class ResponsiveImageXsSelectionAction(ToolbarMenuItem):
+                view=IImageWidget, manager=IContextActions, weight=41)
+class ResponsiveImageXsSelectionAction(FileModifierAction):
     """Responsive image XS selection"""
 
     label = _("Select responsive XS image...")
-    label_css_class = 'fa fa-fw fa-mobile'
+    label_css_class = 'fa fa-fw-md fa-mobile'
 
     url = 'selection-xs.html'
     modal_target = True
 
 
 @pagelet_config(name='selection-xs.html', context=IResponsiveImage, layer=IPyAMSLayer,
-                permission=MANAGE_PERMISSION)
+                permission=VIEW_PERMISSION)
 class ResponsiveImageXsSelectionForm(ResponsiveImageSelectionForm):
     """Responsive image XS selection edit form"""
 
@@ -459,7 +451,7 @@
 
 
 @view_config(name='selection-xs.json', context=IResponsiveImage, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class ResponsiveImageXsSelectionAJAXEditForm(ResponsiveImageSelectionAJAXForm, ResponsiveImageXsSelectionForm):
     """Responsive image XS selection edit form, JSON renderer"""
 
@@ -469,19 +461,19 @@
 #
 
 @viewlet_config(name='responsive-image.selection.sm.action', context=IResponsiveImage, layer=IAdminLayer,
-                view=IImageWidget, manager=IContextActions, permission=MANAGE_PERMISSION, weight=42)
-class ResponsiveImageSmSelectionAction(ToolbarMenuItem):
+                view=IImageWidget, manager=IContextActions, weight=42)
+class ResponsiveImageSmSelectionAction(FileModifierAction):
     """Responsive image SM selection"""
 
     label = _("Select responsive SM image...")
-    label_css_class = 'fa fa-fw fa-tablet'
+    label_css_class = 'fa fa-fw-md fa-tablet'
 
     url = 'selection-sm.html'
     modal_target = True
 
 
 @pagelet_config(name='selection-sm.html', context=IResponsiveImage, layer=IPyAMSLayer,
-                permission=MANAGE_PERMISSION)
+                permission=VIEW_PERMISSION)
 class ResponsiveImageSmSelectionForm(ResponsiveImageSelectionForm):
     """Responsive image SM selection edit form"""
 
@@ -493,7 +485,7 @@
 
 
 @view_config(name='selection-sm.json', context=IResponsiveImage, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class ResponsiveImageSmSelectionAJAXEditForm(ResponsiveImageSelectionAJAXForm, ResponsiveImageSmSelectionForm):
     """Responsive image SM selection edit form, JSON renderer"""
 
@@ -503,19 +495,19 @@
 #
 
 @viewlet_config(name='responsive-image.selection.md.action', context=IResponsiveImage, layer=IAdminLayer,
-                view=IImageWidget, manager=IContextActions, permission=MANAGE_PERMISSION, weight=43)
-class ResponsiveImageMdSelectionAction(ToolbarMenuItem):
+                view=IImageWidget, manager=IContextActions, weight=43)
+class ResponsiveImageMdSelectionAction(FileModifierAction):
     """Responsive image MD selection"""
 
     label = _("Select responsive MD image...")
-    label_css_class = 'fa fa-fw fa-desktop'
+    label_css_class = 'fa fa-fw-md fa-desktop'
 
     url = 'selection-md.html'
     modal_target = True
 
 
 @pagelet_config(name='selection-md.html', context=IResponsiveImage, layer=IPyAMSLayer,
-                permission=MANAGE_PERMISSION)
+                permission=VIEW_PERMISSION)
 class ResponsiveImageMdSelectionForm(ResponsiveImageSelectionForm):
     """Responsive image MD selection edit form"""
 
@@ -527,7 +519,7 @@
 
 
 @view_config(name='selection-md.json', context=IResponsiveImage, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class ResponsiveImageMdSelectionAJAXEditForm(ResponsiveImageSelectionAJAXForm, ResponsiveImageMdSelectionForm):
     """Responsive image MD selection edit form, JSON renderer"""
 
@@ -537,19 +529,19 @@
 #
 
 @viewlet_config(name='responsive-image.selection.lg.action', context=IResponsiveImage, layer=IAdminLayer,
-                view=IImageWidget, manager=IContextActions, permission=MANAGE_PERMISSION, weight=44)
-class ResponsiveImageLgSelectionAction(ToolbarMenuItem):
+                view=IImageWidget, manager=IContextActions, weight=44)
+class ResponsiveImageLgSelectionAction(FileModifierAction):
     """Responsive image LG selection"""
 
     label = _("Select responsive LG image...")
-    label_css_class = 'fa fa-fw fa-television'
+    label_css_class = 'fa fa-fw-md fa-television'
 
     url = 'selection-lg.html'
     modal_target = True
 
 
 @pagelet_config(name='selection-lg.html', context=IResponsiveImage, layer=IPyAMSLayer,
-                permission=MANAGE_PERMISSION)
+                permission=VIEW_PERMISSION)
 class ResponsiveImageLgSelectionForm(ResponsiveImageSelectionForm):
     """Responsive image LG selection edit form"""
 
@@ -561,6 +553,6 @@
 
 
 @view_config(name='selection-lg.json', context=IResponsiveImage, request_type=IPyAMSLayer,
-             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+             permission=VIEW_PERMISSION, renderer='json', xhr=True)
 class ResponsiveImageLgSelectionAJAXEditForm(ResponsiveImageSelectionAJAXForm, ResponsiveImageLgSelectionForm):
     """Responsive image LG selection edit form, JSON renderer"""