src/pyams_content/shared/site/zmi/manager.py
changeset 277 9649f8ce3b1c
child 279 c31da1985a3b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/site/zmi/manager.py	Tue Nov 14 14:05:30 2017 +0100
@@ -0,0 +1,127 @@
+#
+# 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
+from pyams_content.interfaces import MANAGE_SITE_ROOT_PERMISSION
+from pyams_content.root.interfaces import ISiteRoot
+from pyams_content.shared.site.interfaces import ISiteManager
+from pyams_content.zmi.interfaces import IUserAddingsMenuLabel
+from pyams_i18n.interfaces import II18n, INegotiator
+from pyams_skin.interfaces.container import ITableElementEditor
+from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
+from pyams_skin.layer import IPyAMSLayer
+from pyams_zmi.layer import IAdminLayer
+from z3c.form.interfaces import IDataExtractedEvent
+
+# import packages
+from pyams_content.shared.site.manager import SiteManager
+from pyams_content.shared.zmi.sites import SiteTreeTable
+from pyams_form.form import AJAXAddForm
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.table import DefaultElementEditorAdapter
+from pyams_skin.viewlet.toolbar import ToolbarMenuItem
+from pyams_utils.adapter import adapter_config, ContextRequestAdapter
+from pyams_utils.registry import query_utility
+from pyams_utils.unicode import translate_string
+from pyams_utils.url import absolute_url
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi.form import AdminDialogAddForm
+from pyramid.events import subscriber
+from pyramid.view import view_config
+from z3c.form import field
+from zope.interface import Invalid
+
+from pyams_content import _
+
+
+@adapter_config(context=(ISiteManager, IAdminLayer), provides=IUserAddingsMenuLabel)
+class SiteManageruserAddingsMenuLabelAdapter(ContextRequestAdapter):
+    """Site manager user addings menu label adapter"""
+
+    @property
+    def label(self):
+        return '{content} ({blog})'.format(
+            content=self.request.localizer.translate(self.context.shared_content_factory.content_class.content_name),
+            blog=II18n(self.context).query_attribute('title', request=self.request))
+
+
+@viewlet_config(name='add-site-manager.menu', context=ISiteRoot, layer=IAdminLayer,
+                view=SiteTreeTable, manager=IToolbarAddingMenu, permission=MANAGE_SITE_ROOT_PERMISSION)
+class SiteManagerAddMenu(ToolbarMenuItem):
+    """Site manager add menu"""
+
+    label = _("Add site manager")
+    label_css_class = 'fa fa-fw fa-sitemap'
+    url = 'add-site-manager.html'
+    modal_target = True
+
+
+@pagelet_config(name='add-site-manager.html', context=ISiteRoot, layer=IPyAMSLayer,
+                permission=MANAGE_SITE_ROOT_PERMISSION)
+class SiteManagerAddForm(AdminDialogAddForm):
+    """Site manager add form"""
+
+    title = _("Site manager")
+    legend = _("Add site manager")
+    icon_css_class = 'fa fa-fw fa-sitemap'
+
+    fields = field.Fields(ISiteManager).select('title', 'short_name')
+    ajax_handler = 'add-site-manager.json'
+    edit_permission = None
+
+    def create(self, data):
+        return SiteManager()
+
+    def add(self, object):
+        short_name = II18n(object).query_attribute('short_name', request=self.request)
+        name = translate_string(short_name, force_lower=True, spaces='-')
+        self.context[name] = object
+
+    def nextURL(self):
+        return absolute_url(self.context, self.request, 'site-tree.html')
+
+
+@subscriber(IDataExtractedEvent, form_selector=SiteManagerAddForm)
+def handle_new_site_manager_data_extraction(event):
+    """Handle new site manager data extraction"""
+    container = event.form.context
+    negotiator = query_utility(INegotiator)
+    short_name = event.data['short_name'].get(negotiator.server_language)
+    if not short_name:
+        event.form.widgets.errors += (Invalid(_("You must provide a short name for default server language!")),)
+        return
+    name = translate_string(short_name, force_lower=True, spaces='-')
+    if name in container:
+        event.form.widgets.errors += (Invalid(_("Specified site manager name is already used!")),)
+        return
+    site = query_utility(ISiteManager, name=short_name)
+    if site is not None:
+        event.form.widgets.errors += (Invalid(_("A site manager is already registered with this name!!")),)
+
+
+@view_config(name='add-site-manager.json', context=ISiteRoot, request_type=IPyAMSLayer,
+             permission=MANAGE_SITE_ROOT_PERMISSION, renderer='json', xhr=True)
+class SiteManagerAJAXAddForm(AJAXAddForm, SiteManagerAddForm):
+    """Site manager add form, JSOn renderer"""
+
+
+@adapter_config(context=(ISiteManager, IAdminLayer, SiteTreeTable), provides=ITableElementEditor)
+class SiteManagerTableElementEditor(DefaultElementEditorAdapter):
+    """Site tree table element editor"""
+
+    view_name = 'admin'
+    modal_target = False