[silva.security.addables][Sylvain Viollon] Add an installer that...

[email protected] Mon, 04 Nov 2013 16:44:28 +0100
Newsgroups gmane.comp.web.zope.silva.cvs
Message-ID <[email protected]>
author:    Sylvain Viollon
date:      Mon Nov 04 16:44:22 2013 +0100
revision:  35:e4a42c7e3b4f in silva.security.addables
branch:    2.4
details:   https://hg.infrae.com/silva.security.addables?cmd=changeset;node=e4a42c7e3b4f
modified:  src/silva/security/addables/AddablesPermissions.py src/silva/security/addables/AddablesPermissions_templates/configurepermissions.cpt src/silva/security/addables/AddablesPermissions_templates/managepermissions.pt src/silva/security/addables/__init__.py src/silva/security/addables/www/addablesPermissionsServiceEdit.zpt
added:     src/silva/security/addables/AddablesPermissions_templates/configurepermissions.cpt src/silva/security/addables/AddablesPermissions_templates/managepermissions.pt
removed:   src/silva/security/addables/www/addablesPermissionsServiceEdit.zpt
log:       Add an installer that create the service for the user. Refactor ZMI
	screen to use a view. Add a view to configure the service from the
	SMI too.


diffstat:

 src/silva/security/addables/AddablesPermissions.py                                 |  57 +++++++--
 src/silva/security/addables/AddablesPermissions_templates/configurepermissions.cpt |  58 +++++++++
 src/silva/security/addables/AddablesPermissions_templates/managepermissions.pt     |  60 ++++++++++
 src/silva/security/addables/__init__.py                                            |  28 ++++-
 src/silva/security/addables/www/addablesPermissionsServiceEdit.zpt                 |  51 --------
 5 files changed, 187 insertions(+), 67 deletions(-)

diffs (309 lines):

diff -r 4453a305a85c -r e4a42c7e3b4f src/silva/security/addables/AddablesPermissions.py
--- a/src/silva/security/addables/AddablesPermissions.py	Mon Sep 24 16:47:56 2012 +0200
+++ b/src/silva/security/addables/AddablesPermissions.py	Mon Nov 04 16:44:22 2013 +0100
@@ -7,15 +7,22 @@
 from App.class_init import InitializeClass
 
 from Products.Silva import roleinfo
-from Products.PageTemplates.PageTemplateFile import PageTemplateFile
+from zope.component import getUtility
 
 from five import grok
 from silva.core import conf as silvaconf
+from silva.core.interfaces import ISilvaConfigurableService
+from silva.core.interfaces import ISilvaLocalService, IAddableContents
+from silva.core.messages.interfaces import IMessageService
 from silva.core.services.base import SilvaService
-from silva.core.interfaces import ISilvaLocalService, IAddableContents
+from silva.translations import translate as _
+from silva.core.views import views as silvaviews
+from silva.ui import rest
 
 
-class IAddablesPermissionsService(ISilvaLocalService):
+class IAddablesPermissionsService(
+    ISilvaLocalService,
+    ISilvaConfigurableService):
     pass
 
 
@@ -33,16 +40,9 @@
     security = ClassSecurityInfo()
 
     manage_options = (
-        {'label':'Edit', 'action':'manage_editForm'},
+        {'label':'Edit', 'action':'manage_permissions'},
         ) + SilvaService.manage_options
 
-    security.declareProtected('View management screens',
-                              'manage_editForm')
-    manage_editForm = PageTemplateFile(
-        'www/addablesPermissionsServiceEdit', globals(),
-        __name__='manage_editForm')
-
-
     security.declareProtected(
         'View management screens', 'manage_editAddablesPermissions')
     @postonly
@@ -88,11 +88,42 @@
                     if winner is None:
                         winner = role
                 elif not (winner is None): # Sanaty check
-                        msg = 'Invalid permission settings. %s can add %s but %s cannot.'
-                        raise ValueError, msg % (winner, metatype, role)
+                    msg = 'Invalid permission settings. %s can add %s but %s cannot.'
+                    raise ValueError, msg % (winner, metatype, role)
 
             settings[metatype] = winner
         return settings
 
 
 InitializeClass(AddablesPermissionsService)
+
+
+class ManagePermissions(silvaviews.ZMIView):
+    grok.context(AddablesPermissionsService)
+    grok.name('manage_permissions')
+    grok.require('zope2.ViewManagementScreens')
+
+    def update(self):
+        super(ManagePermissions, self).update()
+        self.status = None
+        if 'update' in self.request:
+            self.context.manage_editAddablesPermissions(self.request)
+            self.status = _("Permissions updated.")
+
+
+class ConfigurePermissions(rest.FormWithTemplateREST):
+    grok.adapts(rest.Screen, AddablesPermissionsService)
+    grok.name('admin')
+    grok.require('zope2.ViewManagementScreens')
+
+    def get_menu_title(self):
+        return _('Addables permissions configuration')
+
+    def update(self):
+        super(ConfigurePermissions, self).update()
+        if 'update' in self.request:
+            self.context.manage_editAddablesPermissions(self.request)
+            service = getUtility(IMessageService)
+            service.send(_(u"Permissions updated."),
+                         self.request,
+                         namespace='feedback')
diff -r 4453a305a85c -r e4a42c7e3b4f src/silva/security/addables/AddablesPermissions_templates/configurepermissions.cpt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/silva/security/addables/AddablesPermissions_templates/configurepermissions.cpt	Mon Nov 04 16:44:22 2013 +0100
@@ -0,0 +1,58 @@
+<tal:configure i18n:domain="silva">
+
+  <div class="form-head">
+    <h3 i18n:translate="">
+      Configure Addables Permissions
+    </h3>
+    <p i18n:translate="">
+      Select the lowest role which should be able to add each content.
+    </p>
+
+  <form class="form-fields-container">
+    <table class="form-table">
+      <thead>
+        <tr>
+          <th></th>
+          <th tal:repeat="role context.manageableRoles()"
+              tal:content="role">
+            ROLE
+          </th>
+        </tr>
+      </thead>
+      <tbody tal:define="settings context.currentAddablesPermissions()">
+        <tal:line tal:repeat="content sorted(settings.keys())">
+          <tr class="#"
+              tal:define="odd repeat.content.odd"
+              tal:attributes="class odd and 'odd' or 'even'">
+            <td tal:content="content">
+              CONTENT
+            </td>
+            <td tal:repeat="role context.manageableRoles()">
+              <input type="radio" value="#" name="#"
+                     tal:attributes="name content;
+                                     value role;
+                                     checked python:role == settings[content] and 'checked'" />
+            </td>
+          </tr>
+        </tal:line>
+      </tbody>
+    </table>
+
+    <div class="form-footer">
+      <div class="form-controls">
+        <a class="form-button open-screen" rel="admin" href="."
+           data-form-shortcut="ctrl+z" title="Go back to the site preferences"
+           i18n:attributes="title">
+          <ins class="ui-icon ui-icon-arrowreturnthick-1-w"></ins>
+          Cancel
+        </a>
+        <a class="form-control" name="update" data-form-shortcut="ctrl+u"
+           i18n:translate="">
+          <ins class="icon form_check"></ins>
+           Update permissions
+        </a>
+      </div>
+    </div>
+  </form>
+
+</tal:configure>
diff -r 4453a305a85c -r e4a42c7e3b4f src/silva/security/addables/AddablesPermissions_templates/managepermissions.pt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/silva/security/addables/AddablesPermissions_templates/managepermissions.pt	Mon Nov 04 16:44:22 2013 +0100
@@ -0,0 +1,60 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:metal="http://xml.zope.org/namespaces/metal"
+      xmlns:tal="http://xml.zope.org/namespaces/tal"
+      xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+      metal:use-macro="context/@@standard_macros/page"
+      i18n:domain="silva">
+  <body>
+    <div metal:fill-slot="body">
+
+      <div class="system-msg"
+           i18n:translate=""
+           tal:condition="view/status"
+           tal:content="view/status">
+        Form status summary
+      </div>
+
+      <h2>
+        Manage Addables Permissions
+      </h2>
+
+      <p>Select the lowest role which should be able to add each content.</p>
+
+      <form action="#" method="post" class="form-text"
+            tal:attributes="action request/URL">
+
+        <table>
+          <thead>
+            <tr>
+              <th></th>
+              <th tal:repeat="role here/manageableRoles"
+                  tal:content="role">
+                ROLE
+              </th>
+            </tr>
+          </thead>
+          <tbody tal:define="settings here/currentAddablesPermissions">
+            <tr tal:repeat="content python:sorted(settings.keys())">
+              <td tal:content="content">
+                CONTENT
+              </td>
+              <td style="text-align: center"
+                  tal:repeat="role here/manageableRoles">
+                <input type="radio" value="#" name="#"
+                       tal:attributes="name content;
+                                       value role;
+                                       checked python:role == settings[content] and 'checked'" />
+              </td>
+            </tr>
+          </tbody>
+        </table>
+
+        <input type="submit"
+               name="update"
+               value="Change Settings"
+               class="form-element" />
+      </form>
+
+    </div>
+  </body>
+</html>
diff -r 4453a305a85c -r e4a42c7e3b4f src/silva/security/addables/__init__.py
--- a/src/silva/security/addables/__init__.py	Mon Sep 24 16:47:56 2012 +0200
+++ b/src/silva/security/addables/__init__.py	Mon Nov 04 16:44:22 2013 +0100
@@ -3,7 +3,29 @@
 # See also LICENSE.txt
 
 from silva.core import conf as silvaconf
+from silva.core.conf.installer import DefaultInstaller
+from zope.interface import Interface
 
-silvaconf.extensionName('silva.security.addables')
-silvaconf.extensionTitle('Silva Security Addables')
-silvaconf.extensionSystem()
+silvaconf.extension_name('silva.security.addables')
+silvaconf.extension_title('Silva Security Addables')
+
+
+class Installer(DefaultInstaller):
+    service_id = 'service_addablespermissions'
+
+    def install_custom(self, root):
+        if self.service_id not in root.objectIds():
+            factory = root.manage_addProduct['silva.security.addables']
+            factory.manage_addAddablesPermissionsService(self.service_id)
+
+    def uninstall_custom(self, root):
+        if self.service_id in root.objectIds():
+            root.manage_delObjects([self.service_id])
+
+
+class IExtension(Interface):
+    """Marker interface for our extension.
+    """
+
+
+install = Installer("silva.security.addables", IExtension)
diff -r 4453a305a85c -r e4a42c7e3b4f src/silva/security/addables/www/addablesPermissionsServiceEdit.zpt
--- a/src/silva/security/addables/www/addablesPermissionsServiceEdit.zpt	Mon Sep 24 16:47:56 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-<tal:block tal:replace="nothing">
-Copyright (c) 2008 Infrae. All rights reserved.
-See also LICENSE.txt
-</tal:block><h1 tal:replace="structure here/manage_page_header">Header</h1>
-
-<h2 tal:define="manage_tabs_message options/manage_tabs_message | nothing"
-    tal:replace="structure here/manage_tabs">Tabs</h2>
-
-<h4><a href="../manage_services">Silva Services</a></h4>
-
-<h2>
-  Manage Addables Permissions
-</h2>
-
-<p>Select the lowest role which should be able to add each content.</p>
-
-<form action="." method="post" class="form-text">
-
-  <table>
-    <thead>
-      <tr>
-        <th></th>
-        <th tal:repeat="role here/manageableRoles"
-            tal:content="role">
-          ROLE
-        </th>
-      </tr>
-    </thead>
-    <tbody tal:define="settings here/currentAddablesPermissions">
-      <tr tal:repeat="content settings">
-        <td tal:content="content">
-          CONTENT
-        </td>
-        <td style="text-align: center" 
-            tal:repeat="role here/manageableRoles">
-          <input type="radio" value="#" name="#"
-                 tal:attributes="name content;
-                                 value role;
-                                 checked python:role == settings[content] and 'checked'" />
-        </td>
-      </tr>
-    </tbody>
-  </table>
-
-  <input type="submit" 
-         name="manage_editAddablesPermissions:method" 
-         value="Change Settings" 
-         class="form-element" />
-</form>
-
-<h1 tal:replace="structure here/manage_page_footer">Footer</h1>