[CMF-checkins] SVN: CMF/branches/tseaver-viewification/CMFDefault/browser/ - an other viewification approach: tests and interfaces are still missing, but view class and template should give you an idea how I'd do it

Yvo Schubbe y.2005- at wcm-solutions.de
Sun Oct 23 14:08:31 EDT 2005


Log message for revision 39566:
  - an other viewification approach: tests and interfaces are still missing, but view class and template should give you an idea how I'd do it

Changed:
  UU  CMF/branches/tseaver-viewification/CMFDefault/browser/configure.zcml
  A   CMF/branches/tseaver-viewification/CMFDefault/browser/documentviews.py
  A   CMF/branches/tseaver-viewification/CMFDefault/browser/templates/document_edit.pt
  A   CMF/branches/tseaver-viewification/CMFDefault/browser/templates/form_widgets.pt
  A   CMF/branches/tseaver-viewification/CMFDefault/browser/utils.py

-=-
Modified: CMF/branches/tseaver-viewification/CMFDefault/browser/configure.zcml
===================================================================
--- CMF/branches/tseaver-viewification/CMFDefault/browser/configure.zcml	2005-10-23 15:24:14 UTC (rev 39565)
+++ CMF/branches/tseaver-viewification/CMFDefault/browser/configure.zcml	2005-10-23 18:08:30 UTC (rev 39566)
@@ -48,4 +48,21 @@
     layer="cmf"
     />
 
+  <browser:page
+      for="Products.CMFDefault.interfaces.IMutableDocument"
+      name="document_edit_form"
+      class=".documentviews.DocumentEditView"
+      template="templates/document_edit.pt"
+      permission="cmf.ModifyPortalContent"
+      layer="cmf"
+      />
+
+  <browser:page
+      for="*"
+      name="form_widget"
+      template="templates/form_widgets.pt"
+      permission="zope2.View"
+      layer="cmf"
+      />
+
 </configure>


Property changes on: CMF/branches/tseaver-viewification/CMFDefault/browser/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: CMF/branches/tseaver-viewification/CMFDefault/browser/documentviews.py
===================================================================
--- CMF/branches/tseaver-viewification/CMFDefault/browser/documentviews.py	2005-10-23 15:24:14 UTC (rev 39565)
+++ CMF/branches/tseaver-viewification/CMFDefault/browser/documentviews.py	2005-10-23 18:08:30 UTC (rev 39566)
@@ -0,0 +1,53 @@
+from Products.CMFDefault.exceptions import EditingConflict
+from Products.CMFDefault.exceptions import IllegalHTML
+from Products.CMFDefault.exceptions import ResourceLockedError
+from Products.CMFDefault.utils import MessageID as _
+from Products.CMFDefault.utils import scrubHTML
+
+from utils import FormViewBase
+
+
+class DocumentEditView(FormViewBase):
+
+    """ Edit view for IMutableDocument.
+    """
+
+    # XXX: _BUTTONS this should become configurable
+    _BUTTONS = ({'name': 'change',
+                 'value': _('Change'),
+                 'transform': ('validateTextFile', 'validateHTML', 'update'),
+                 'redirect': ('context', 'object/edit')},
+                {'name': 'change_and_view',
+                 'value': _('Change and View'),
+                 'transform': ('validateTextFile', 'validateHTML', 'update'),
+                 'redirect': ('context', 'object/view')})
+
+    def validateTextFile(self, file='', **kw):
+        try:
+            upload = file.read()
+        except AttributeError:
+            return self.setStatus(True)
+        else:
+            if upload:
+                return self.setStatus(True, text=upload)
+            else:
+                return self.setStatus(True)
+
+    def validateHTML(self, text, description='', **kw):
+        try:
+            description = scrubHTML(description)
+            text = scrubHTML(text)
+            return self.setStatus(True, text=text, description=description)
+        except IllegalHTML, errmsg:
+            return self.setStatus(False, errmsg)
+
+    def update(self, text_format, text, SafetyBelt='', **kw):
+        context = self.context
+        if text_format != context.text_format or text != context.text:
+            try:
+                context.edit(text_format, text, safety_belt=SafetyBelt)
+                return self.setStatus(True, _('Document changed.'))
+            except (ResourceLockedError, EditingConflict), errmsg:
+                return self.setStatus(False, errmsg)
+        else:
+            return self.setStatus(False, _('Nothing to change.'))


Property changes on: CMF/branches/tseaver-viewification/CMFDefault/browser/documentviews.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: CMF/branches/tseaver-viewification/CMFDefault/browser/templates/document_edit.pt
===================================================================
--- CMF/branches/tseaver-viewification/CMFDefault/browser/templates/document_edit.pt	2005-10-23 15:24:14 UTC (rev 39565)
+++ CMF/branches/tseaver-viewification/CMFDefault/browser/templates/document_edit.pt	2005-10-23 18:08:30 UTC (rev 39566)
@@ -0,0 +1,66 @@
+<html metal:use-macro="context/@@standard_macros/page">
+<body>
+
+<metal:slot metal:fill-slot="header" i18n:domain="cmf_default">
+<h1 i18n:translate="">Edit: <tal:span
+    tal:content="context/Title" i18n:name="obj_title">Title</tal:span></h1>
+</metal:slot>
+
+<metal:slot metal:fill-slot="body" i18n:domain="cmf_default">
+<div class="Desktop">
+
+<form action="document_edit_form" method="post" enctype="multipart/form-data"
+   tal:attributes="action request/ACTUAL_URL">
+ <input type="hidden" name="SafetyBelt" value=""
+        tal:attributes="value context/SafetyBelt" />
+<table class="FormLayout">
+ <tr>
+  <th i18n:translate="">Title</th>
+  <td tal:content="context/Title">Title</td>
+ </tr>
+ <tr>
+  <th i18n:translate="">Description</th>
+  <td tal:content="context/Description">Description</td>
+ </tr>
+ <tr>
+  <th i18n:translate="">Format</th>
+  <td>
+   <input type="radio" name="text_format" value="structured-text" id="cb_stx"
+      tal:attributes="
+             checked python:path('context/text_format')=='structured-text'" />
+   <label for="cb_stx" i18n:translate="">structured-text</label>
+   <input type="radio" name="text_format" value="plain" id="cb_plain"
+      tal:attributes="checked python:path('context/text_format')=='plain'" />
+   <label for="cb_plain" i18n:translate="">plain text</label>
+   <input type="radio" name="text_format" value="html" id="cb_html"
+      tal:attributes="checked python:path('context/text_format')=='html'" />
+   <label for="cb_html" i18n:translate="">html</label>
+  </td>
+ </tr>
+ <tr>
+  <th i18n:translate="">Upload</th>
+  <td>
+   <input type="file" name="file" size="25" />
+  </td>
+ </tr>
+ <tr>
+  <th class="TextField" i18n:translate="">Edit</th>
+  <td class="TextField">
+   <textarea name="text:text" rows="20" cols="80" wrap="soft"
+             tal:content="context/EditableBody"></textarea>
+  </td>
+ </tr>
+ <tr>
+  <td>&nbsp;</td>
+  <td>
+   <metal:macro metal:use-macro="context/@@form_widget/buttons" />
+  </td>
+ </tr>
+</table>
+</form>
+
+</div>
+</metal:slot>
+
+</body>
+</html>

Added: CMF/branches/tseaver-viewification/CMFDefault/browser/templates/form_widgets.pt
===================================================================
--- CMF/branches/tseaver-viewification/CMFDefault/browser/templates/form_widgets.pt	2005-10-23 15:24:14 UTC (rev 39565)
+++ CMF/branches/tseaver-viewification/CMFDefault/browser/templates/form_widgets.pt	2005-10-23 18:08:30 UTC (rev 39566)
@@ -0,0 +1,19 @@
+<html>
+<body>
+
+<metal:macro metal:define-macro="hidden_vars">
+ <tal:loop tal:repeat="hidden_var view/listHiddenVarInfos"
+ ><input type="hidden" name="HiddenVarName" value=""
+     tal:attributes="name hidden_var/name; value hidden_var/value" /></tal:loop
+></metal:macro>
+
+ <metal:macro metal:define-macro="buttons"
+><div class="FormButtons">
+  <tal:loop tal:repeat="button view/listButtonInfos"
+  ><input type="submit" name="ButtonName" value="ButtonValue"
+      tal:attributes="name button/name; value button/value"
+      i18n:attributes="value" /></tal:loop></div
+></metal:macro>
+
+</body>
+</html>

Added: CMF/branches/tseaver-viewification/CMFDefault/browser/utils.py
===================================================================
--- CMF/branches/tseaver-viewification/CMFDefault/browser/utils.py	2005-10-23 15:24:14 UTC (rev 39565)
+++ CMF/branches/tseaver-viewification/CMFDefault/browser/utils.py	2005-10-23 18:08:30 UTC (rev 39566)
@@ -0,0 +1,53 @@
+from ZTUtils import make_query
+
+from Products.CMFCore.utils import getToolByName
+
+
+class FormViewBase:
+
+    def __call__(self, change='', change_and_view=''):
+        form = self.request.form
+        for button in self._BUTTONS:
+            if button['name'] in form:
+                for transform in button['transform']:
+                    if not getattr(self, transform)(**form):
+                        return self.index()
+                if self.setRedirect(*button['redirect']):
+                    return
+        return self.index()
+
+    def listButtonInfos(self):
+        return self._BUTTONS
+
+    def setStatus(self, success, message='', **kw):
+        if message:
+            self.request.other['portal_status_message'] = message
+        if kw:
+            for k, v in kw.items():
+                self.request.form[k] = v
+
+        return success
+
+    def setRedirect(self, provider_id, action_path, **kw):
+        utool = getToolByName(self.context, 'portal_url')
+        portal_url = utool()
+
+        if provider_id == 'context':
+            provider = self.context
+        else:
+            provider = getToolByName(self.context, provider_id)
+        try:
+            target = provider.getActionInfo(action_path)['url']
+        except ValueError:
+            target = portal_url
+
+        message = self.request.other.get('portal_status_message', '')
+        kw['portal_status_message'] = message
+        for k, v in kw.items():
+            if not v:
+                del kw[k]
+
+        query = kw and ( '?%s' % make_query(kw) ) or ''
+        self.request.RESPONSE.redirect( '%s%s' % (target, query) )
+
+        return True


Property changes on: CMF/branches/tseaver-viewification/CMFDefault/browser/utils.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the CMF-checkins mailing list