[Zope-CVS] SVN: zope.webdev/trunk/browser/ Okay, some refactoring.

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Jan 30 13:31:56 EST 2006


Log message for revision 41503:
  Okay, some refactoring.
  

Changed:
  U   zope.webdev/trunk/browser/base.py
  U   zope.webdev/trunk/browser/content.py
  U   zope.webdev/trunk/browser/content.zcml
  U   zope.webdev/trunk/browser/content_package_overview.pt
  U   zope.webdev/trunk/browser/package.py
  U   zope.webdev/trunk/browser/page.py

-=-
Modified: zope.webdev/trunk/browser/base.py
===================================================================
--- zope.webdev/trunk/browser/base.py	2006-01-30 17:48:12 UTC (rev 41502)
+++ zope.webdev/trunk/browser/base.py	2006-01-30 18:31:56 UTC (rev 41503)
@@ -16,11 +16,18 @@
 $Id$
 """
 __docformat__ = "reStructuredText"
+import datetime
+import pytz
+import zope.event
+
 from zope.formlib import form
+from zope.interface.common import idatetime
 from zope.app import component
 from zope.app import zapi
 from zope.app.pagetemplate import ViewPageTemplateFile
 
+from zope.webdev.interfaces import _
+
 class UtilityAddFormBase(form.AddForm):
     """Add form for utilities."""
 
@@ -44,3 +51,65 @@
 
     def nextURL(self):
         return zapi.absoluteURL(self.context.context, self.request)
+
+
+def haveEditFlag(form, action):
+    if 'doEdit' in form.request:
+        return True
+    return False
+
+class EditFormBase(form.EditForm):
+    '''A base class for display forms that are also edit forms.'''
+
+    def fixUpWidgets(self):
+        pass
+
+    def setUpWidgets(self, ignore_request=False):
+        for_display = True
+        if 'doEdit' in self.request:
+            for_display = False
+
+        self.adapters = {}
+        self.widgets = form.setUpEditWidgets(
+            self.form_fields, self.prefix, self.context, self.request,
+            adapters=self.adapters, for_display=for_display,
+            ignore_request=ignore_request
+            )
+        if not for_display:
+            self.fixUpWidgets()
+
+    @form.action(_("Edit"), condition=lambda *args: not haveEditFlag(*args))
+    def handleStartEditAction(self, action, data):
+        self.request.form['doEdit'] = True
+        self.setUpWidgets()
+
+    @form.action(_("Apply"), condition=haveEditFlag)
+    def handleEditAction(self, action, data):
+        del self.request.form['doEdit']
+
+        if form.applyChanges(self.context, self.form_fields,
+                             data, self.adapters):
+            zope.event.notify(
+                zope.app.event.objectevent.ObjectModifiedEvent(self.context))
+            formatter = self.request.locale.dates.getFormatter(
+                'dateTime', 'medium')
+
+            try:
+                time_zone = idatetime.ITZInfo(self.request)
+            except TypeError:
+                time_zone = pytz.UTC
+
+            status = _("Updated on ${date_time}",
+                       mapping={'date_time':
+                                formatter.format(
+                                   datetime.datetime.now(time_zone)
+                                   )
+                        }
+                       )
+            self.status = status
+        else:
+            self.status = _('No changes')
+
+    @form.action(_("Cancel"), condition=haveEditFlag)
+    def handleCancelAction(self, action, data):
+        del self.request.form['doEdit']

Modified: zope.webdev/trunk/browser/content.py
===================================================================
--- zope.webdev/trunk/browser/content.py	2006-01-30 17:48:12 UTC (rev 41502)
+++ zope.webdev/trunk/browser/content.py	2006-01-30 18:31:56 UTC (rev 41503)
@@ -18,9 +18,14 @@
 __docformat__ = "reStructuredText"
 
 import zope.interface
+import zope.schema
+from zope.security import proxy, checker
 from zope.formlib import form
 from zope.app import apidoc
 from zope.app import zapi
+from zope.app.pagetemplate import ViewPageTemplateFile
+from zope.app.form import utility
+from zope.app.form.interfaces import IInputWidget
 
 from zope.webdev import interfaces, content
 from zope.webdev.interfaces import _
@@ -39,6 +44,111 @@
         return content.ContentComponentDefinition(**data)
 
 
+class Overview(base.EditFormBase):
+    """Page Overview."""
+    form_fields = form.Fields(interfaces.IContentComponentDefinition).select(
+        'name', 'schema')
+    template = ViewPageTemplateFile('contentdefinition_overview.pt')
+
+
+class Permissions(form.FormBase):
+    """Page Overview."""
+
+    template = ViewPageTemplateFile('contentdefinition_permission.pt')
+
+    # XXX
+    def validate(self, action, data):
+        pass
+
+    def setUpWidgets(self, ignore_request=False):
+        schema = proxy.removeSecurityProxy(self.context.schema)
+        for name, field in zope.schema.getFieldsInOrder(schema):
+
+            # get the permissions and then the permission id.
+            # We can't deal in dropdownboxes with permission itself.
+            # There is no way to compare the permission to the
+            # "Permission" or "Permission Id" vocabulary for to
+            # get the SELECTED state.
+            if self.context.permissions.has_key(name):
+                get_perm, set_perm = self.context.permissions[name]
+                try:
+                    get_perm_id = get_perm.id
+                    print 'get_perm.id = %s' % get_perm.id
+                except:
+                    get_perm_id = None
+                try:
+                    set_perm_id = set_perm.id
+                    print 'set_perm.id = %s' % set_perm.id
+                except:
+                    set_perm_id = None
+            else:
+                get_perm_id, set_perm_id = None, None
+
+            # Create the Accessor Permission Widget for this field
+            permField = zope.schema.Choice(
+                __name__=name+'_get_perm',
+                title=u"Accessor Permission",
+                default=checker.CheckerPublic,
+                vocabulary="Permission Ids",
+                required=False)
+            utility.setUpWidget(self, name+'_get_perm', permField, IInputWidget,
+                        value=get_perm_id, ignoreStickyValues=True)
+
+            # Create the Mutator Permission Widget for this field
+            permField = zope.schema.Choice(
+                __name__=name+'_set_perm',
+                title=u"Mutator Permission",
+                default=checker.CheckerPublic,
+                vocabulary="Permission Ids",
+                required=False)
+            utility.setUpWidget(self, name+'_set_perm', permField, IInputWidget,
+                        value=set_perm_id, ignoreStickyValues=True)
+
+
+    def getPermissionWidgets(self):
+        schema = proxy.removeSecurityProxy(self.context.schema)
+        info = []
+        for name, field in zope.schema.getFieldsInOrder(schema):
+            field = proxy.removeSecurityProxy(field)
+            info.append(
+                {'fieldName': name,
+                 'fieldTitle': field.title,
+                 'getter': getattr(self, name+'_get_perm_widget'),
+                 'setter': getattr(self, name+'_set_perm_widget')} )
+        return info
+
+
+    @form.action(_("Apply"))
+    def handleEditAction(self, action, data):
+        schema = self.context.schema
+        perms = proxy.removeSecurityProxy(self.context.permissions)
+        for name, field in zope.schema.getFieldsInOrder(schema):
+            getPermWidget = getattr(self, name+'_get_perm_widget')
+            setPermWidget = getattr(self, name+'_set_perm_widget')
+
+            # get the selected permission id from the from request
+            get_perm_id = getPermWidget.getInputValue()
+            set_perm_id = setPermWidget.getInputValue()
+
+            # get the right permission from the given id
+            get_perm = zapi.getUtility(IPermission, get_perm_id)
+            set_perm = zapi.getUtility(IPermission, set_perm_id)
+
+            # set the permission back to the instance
+            perms[name] = (get_perm, set_perm)
+
+            # update widget ohterwise we see the old value
+            getPermWidget.setRenderedValue(get_perm_id)
+            setPermWidget.setRenderedValue(set_perm_id)
+
+        self.status = _('Fields permissions mapping updated.')
+
+
+    @form.action(_("Cancel"))
+    def handleCancelAction(self, action, data):
+        pass
+
+
 class PackageOverview(object):
     """A pagelet that serves as the overview of the content component
     definitions in the package overview."""
@@ -53,6 +163,7 @@
         """Return PT-friendly info dictionaries for all definitions."""
         return [
             {'name': value.name,
-             'schema': apidoc.utilities.getPythonPath(value.schema)}
+             'schema': apidoc.utilities.getPythonPath(value.schema),
+             'url': zapi.absoluteURL(value, self.request)}
             for value in self.context.values()
             if interfaces.IContentComponentDefinition.providedBy(value)]

Modified: zope.webdev/trunk/browser/content.zcml
===================================================================
--- zope.webdev/trunk/browser/content.zcml	2006-01-30 17:48:12 UTC (rev 41502)
+++ zope.webdev/trunk/browser/content.zcml	2006-01-30 18:31:56 UTC (rev 41503)
@@ -46,20 +46,31 @@
 
   <!-- Definition Overview -->
 
-  <!--page
+  <page
       for="..interfaces.IContentComponentDefinition"
       permission="webdev.ViewContentComponentDefinition"
       name="overview.html"
       class=".content.Overview"
-      template="content_overview.pt"
       layer="zope.webdev.browser.skin.webdev"
       menu="webdev_views"
       title="Overview"
-      /-->
+      />
 
   <defaultView
       for="..interfaces.IContentComponentDefinition"
       name="overview.html"
       />
 
+  <!-- Definition Permissions -->
+
+  <page
+      for="..interfaces.IContentComponentDefinition"
+      permission="webdev.ManageContentComponentDefinition"
+      name="permissions.html"
+      class=".content.Permissions"
+      layer="zope.webdev.browser.skin.webdev"
+      menu="webdev_views"
+      title="Permissions"
+      />
+
 </configure>

Modified: zope.webdev/trunk/browser/content_package_overview.pt
===================================================================
--- zope.webdev/trunk/browser/content_package_overview.pt	2006-01-30 17:48:12 UTC (rev 41502)
+++ zope.webdev/trunk/browser/content_package_overview.pt	2006-01-30 18:31:56 UTC (rev 41503)
@@ -2,6 +2,7 @@
   <li tal:repeat="definition view/definitions">
     <div class="name">
       <a href=""
+         tal:attributes="href definition/url"
          tal:content="definition/name">
         Person
       </a>

Modified: zope.webdev/trunk/browser/package.py
===================================================================
--- zope.webdev/trunk/browser/package.py	2006-01-30 17:48:12 UTC (rev 41502)
+++ zope.webdev/trunk/browser/package.py	2006-01-30 18:31:56 UTC (rev 41503)
@@ -16,29 +16,17 @@
 $Id$
 """
 __docformat__ = "reStructuredText"
-import datetime
-import pytz
-
-import zope.event
 import zope.interface
 import zope.schema
 import zope.app.event.objectevent
 from zope import viewlet
 from zope.formlib import form
-from zope.interface.common import idatetime
 from zope.app import zapi
 from zope.app.pagetemplate import ViewPageTemplateFile
 from zope.webdev import interfaces
-from zope.webdev.browser import pagelet
+from zope.webdev.browser import pagelet, base
 
-from zope.webdev.interfaces import _
-
-def haveEditFlag(form, action):
-    if 'doEdit' in form.request:
-        return True
-    return False
-
-class Overview(form.EditForm):
+class Overview(base.EditFormBase):
     """Package Overview."""
 
     form_fields = form.Fields(interfaces.IPackage).select(
@@ -48,57 +36,7 @@
     def fixUpWidgets(self):
         self.widgets.get('docstring').height = 3
 
-    def setUpWidgets(self, ignore_request=False):
-        for_display = True
-        if 'doEdit' in self.request:
-            for_display = False
 
-        self.adapters = {}
-        self.widgets = form.setUpEditWidgets(
-            self.form_fields, self.prefix, self.context, self.request,
-            adapters=self.adapters, for_display=for_display,
-            ignore_request=ignore_request
-            )
-        if not for_display:
-            self.fixUpWidgets()
-
-    @form.action(_("Edit"), condition=lambda *args: not haveEditFlag(*args))
-    def handleStartEditAction(self, action, data):
-        self.request.form['doEdit'] = True
-        self.setUpWidgets()
-
-    @form.action(_("Apply"), condition=haveEditFlag)
-    def handleEditAction(self, action, data):
-        del self.request.form['doEdit']
-
-        if form.applyChanges(self.context, self.form_fields,
-                             data, self.adapters):
-            zope.event.notify(
-                zope.app.event.objectevent.ObjectModifiedEvent(self.context))
-            formatter = self.request.locale.dates.getFormatter(
-                'dateTime', 'medium')
-
-            try:
-                time_zone = idatetime.ITZInfo(self.request)
-            except TypeError:
-                time_zone = pytz.UTC
-
-            status = _("Updated on ${date_time}",
-                       mapping={'date_time':
-                                formatter.format(
-                                   datetime.datetime.now(time_zone)
-                                   )
-                        }
-                       )
-            self.status = status
-        else:
-            self.status = _('No changes')
-
-    @form.action(_("Cancel"), condition=haveEditFlag)
-    def handleCancelAction(self, action, data):
-        del self.request.form['doEdit']
-
-
 class IPackageOverviewManager(pagelet.IPageletManager):
     """A pagelet manager to display contents of a package."""
 

Modified: zope.webdev/trunk/browser/page.py
===================================================================
--- zope.webdev/trunk/browser/page.py	2006-01-30 17:48:12 UTC (rev 41502)
+++ zope.webdev/trunk/browser/page.py	2006-01-30 18:31:56 UTC (rev 41503)
@@ -33,13 +33,6 @@
 from zope.interface.common import idatetime
 
 
-
-def haveEditFlag(form, action):
-    if 'doEdit' in form.request:
-        return True
-    return False
-
-
 class AddForm(base.UtilityAddFormBase):
 
     label = _('Page')
@@ -81,64 +74,10 @@
         return pages
 
 
-class Overview(form.EditForm):
+class Overview(base.EditFormBase):
     """Page Overview."""
-
     form_fields = form.Fields(interfaces.IPage).select(
         'name', 'for_', 'layer', 'permission','templateSource',
         'moduleSource','className')
     template = ViewPageTemplateFile('package_overview.pt')
 
-#    def fixUpWidgets(self):
-#        self.widgets.get('docstring').height = 3
-
-    def setUpWidgets(self, ignore_request=False):
-        for_display = True
-        if 'doEdit' in self.request:
-            for_display = False
-
-        self.adapters = {}
-        self.widgets = form.setUpEditWidgets(
-            self.form_fields, self.prefix, self.context, self.request,
-            adapters=self.adapters, for_display=for_display,
-            ignore_request=ignore_request
-            )
-#        if not for_display:
-#            self.fixUpWidgets()
-
-    @form.action(_("Edit"), condition=lambda *args: not haveEditFlag(*args))
-    def handleStartEditAction(self, action, data):
-        self.request.form['doEdit'] = True
-        self.setUpWidgets()
-
-    @form.action(_("Apply"), condition=haveEditFlag)
-    def handleEditAction(self, action, data):
-        del self.request.form['doEdit']
-
-        if form.applyChanges(self.context, self.form_fields,
-                             data, self.adapters):
-            zope.event.notify(
-                zope.app.event.objectevent.ObjectModifiedEvent(self.context))
-            formatter = self.request.locale.dates.getFormatter(
-                'dateTime', 'medium')
-
-            try:
-                time_zone = idatetime.ITZInfo(self.request)
-            except TypeError:
-                time_zone = pytz.UTC
-
-            status = _("Updated on ${date_time}",
-                       mapping={'date_time':
-                                formatter.format(
-                                   datetime.datetime.now(time_zone)
-                                   )
-                        }
-                       )
-            self.status = status
-        else:
-            self.status = _('No changes')
-
-    @form.action(_("Cancel"), condition=haveEditFlag)
-    def handleCancelAction(self, action, data):
-        del self.request.form['doEdit']
-



More information about the Zope-CVS mailing list