Skip to content.

Etria Lists

 

[quills-dev] [Collective-checkins] r32310 - Quills/trunk/browser

quills-dev at lists.etria.com quills-dev at lists.etria.com
Wed Oct 25 15:09:20 UTC 2006


Author: tomster
Date: Wed Oct 25 15:09:19 2006
New Revision: 32310

Added:
   Quills/trunk/browser/controllerView.py
Log:
A basic form controller designed for maximum compatibility with CMFFormController
i.e. changes to the existing templates are minimised.
n.b. this should probably be phased out again at some time by using formlib
but for the time being it's rather useful to have.
Written by Andreas Zeidler a.k.a. witsch based on the `FormControllerView` in membrane
(http://dev.plone.org/collective/browser/membrane/trunk/browser/tool_zmi_views.py?rev=21197)


Added: Quills/trunk/browser/controllerView.py
==============================================================================
--- (empty file)
+++ Quills/trunk/browser/controllerView.py	Wed Oct 25 15:09:19 2006
@@ -0,0 +1,125 @@
+from Products.Quills.browser.QuillsBaseView import QuillsBaseView
+from Products.CMFCore.utils import getToolByName
+
+# A basic form controller designed for maximum compatibility with CMFFormController
+# i.e. changes to the existing templates are minimised.
+# n.b. this should probably be phased out again at some time by using formlib
+# but for the time being it's rather useful to have.
+# Written by Andreas Zeidler a.k.a. witsch based on the `FormControllerView` in membrane
+# (http://dev.plone.org/collective/browser/membrane/trunk/browser/tool_zmi_views.py?rev=21197)
+
+class FormControllerViewState:
+    """ fake formcontroller's state class for page templates """
+    
+    def __init__(self, errors):
+        self.errors = errors
+    
+    def getErrors(self, *args, **kw):
+
+        return self.errors
+    
+
+class FormControllerView(QuillsBaseView):
+    """ a very basic formcontroller-like view class """
+    
+    def __call__(self, *args, **kw):
+        self.args = args
+        self.kw = kw
+        errors = {}
+        if self.request.get('submitted', None) or self.request.get('form.submitted', None):
+            errors = self.validate()
+            if not errors:
+                errors = self.control()
+                if not errors:
+                    return self.action(*args, **kw)
+            elif type(errors) == type(''):      # a redirection was given...
+                return self.callByName(errors)
+        return self.callIndex(errors, *args, **kw)
+    
+    def validate(self):
+        """ performs validation and returns an errors dictionary """
+        return {}
+    
+    def control(self):
+        """ performs the actions after a successful validation possibly
+            returning an errors dictionary """
+        return {}
+    
+    def action(self, *args, **kw):
+        """ this can be called to return some output or redirect to another
+            page after the control action was successfully called """
+        return self.callIndex(*args, **kw)
+    
+    def translate(self, msg):
+        ltool = getToolByName(self.context, 'portal_languages')
+        lang = ltool.getPreferredLanguage()
+        panel = getToolByName(self.context, 'Control_Panel')
+        return panel.TranslationService.utranslate(msgid=msg,
+            domain='LabelFinderAGX', target_language=lang, context=self.context)
+    
+    def translateErrors(self, errors):
+        translated = {}
+        for field, msg in errors.items():
+            if type(msg) == type(()):
+                if len(msg) == 2:
+                    msgstr, msgid = msg
+                else:
+                    msgstr, msgid = msg[0], msg[0]
+                msg = self.translate(msgid)
+                if msg == msgid:
+                    msg = self.translate(msgstr)
+            translated[field] = msg
+        return translated
+    
+    def callIndex(self, errors={}, *args, **kw):
+        """ call the original template with some additional parameters """
+        errors = self.translateErrors(errors)
+        return self.index(errors=errors, 
+            state=FormControllerViewState(errors), *args, **kw)
+            
+    def callByName(self, name, errors={}):
+        """ call the original template with some additional parameters """
+        obj = getattr(self.context, name)
+        errors = self.translateErrors(errors)
+        return obj(errors=errors, 
+            state=FormControllerViewState(errors), *self.args, **self.kw) 
+
+    def setMessage(self, msg):
+        self.request.set('portal_status_message', msg)
+    
+    def getToolByName(self, name):
+        """ helper function to return the named portal tool """
+        return getToolByName(self.context, name, None)
+    
+    def getFormVar(self, name):
+        """ return the value of a request variable or None """
+        var = self.request.get(name, None)
+        if var is not None and type(var) == type(''):
+            var = var.strip()
+        return var
+        
+    def test(self, expr, positive, negative):
+        """ helper function since the 'test' function in zpts seems to be gone;
+            TODO: ask on #zope about that... """
+        if expr:
+            return positive
+        else:
+            return negative
+    
+
+class FormControllerTestView(FormControllerView):
+    """ view class for unit-testing the formcontrollerview """
+    
+    def validate(self):
+        """ performs validation and returns an errors dictionary """
+        errors = {}
+        if not self.request.get('answer', None) == 'yes, please':
+            errors['answer'] = "Sorry, but that won't do...", 'sorry'
+        return errors
+    
+    def action(self):
+        """ performs the actions after a successful validation possibly
+            returning an errors dictionary """
+        return '<html><body><p> Thank you! </p></body></html>'
+    
+

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Collective-checkins mailing list
Collective-checkins at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/collective-checkins



More information about the quills-dev mailing list