[Mpuls-commits] r5329 - base/trunk/mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Sep 14 13:52:26 CEST 2011


Author: bh
Date: 2011-09-14 13:52:25 +0200 (Wed, 14 Sep 2011)
New Revision: 5329

Modified:
   base/trunk/mpulsweb/lib/dialogs.py
Log:
Port dialog functions from WASKU to mpulsweb.

This changes some of the functions in mpulsweb/lib/dialogs.py and adds
new ones. The functions that are change were not used anywhere yet. The
semantics of the new version is a little different from the old version
in mpulsweb, because the dialogs are so far only used for the
meta-server controllers and they have been modified for their
needs. They may have to be adapted for other controllers.

The code in the new and modified functions is now identical to the code
from the dialogs module of WASKU rev. 391:e20e98117f34.


Modified: base/trunk/mpulsweb/lib/dialogs.py
===================================================================
--- base/trunk/mpulsweb/lib/dialogs.py	2011-09-14 11:52:09 UTC (rev 5328)
+++ base/trunk/mpulsweb/lib/dialogs.py	2011-09-14 11:52:25 UTC (rev 5329)
@@ -77,46 +77,100 @@
 # path, as the dialogs do not have any information on this
 
 def get_dialog_template():
-    '''
+    """
     Depending of the called url this function will return the context
     (global/case) in which the dialog will be displayed
-    '''
+    """
     # TODO: Parse url and return context
-    return "main" # All dialogs are shown in main context for now
+    return "case" # All dialogs are shown in case context for now
 
-def render_confirm(header=None,text=None,url_ok=None,url_abort=None,dyntext=None):
+
+def render_confirm(header=None, text=None, url_ok=None, url_abort=None,
+                   dyntext=None):
+    """Returns the rendered confirm dialog"""
     c.dialog_title = header or CONFIRM_DEFAULT_HEADER
     if dyntext and text:
         c.dialog_text = text % dyntext
     else:
         c.dialog_text = text or CONFIRM_DEFAULT_TEXT
-    c.url_ok    = url_ok or "%s?confirmed=1" % (request.environ.get('PATH_INFO'))
-    #c.url_abort = url_abort or "%s" % session['history'][-1] 
-    c.url_abort = url_abort or CONFIRM_DEFAULT_URL_ABORT 
+    c.url_ok = url_ok
+    c.url_abort = url_abort or url_for(controller="meta", action="index")
     return render('/dialogs/confirm_%s.mako' % get_dialog_template())
 
+
+def render_success(header=None, text=None, url_ok=None, dyntext=None):
+    """Returns the rendered success dialog"""
+    c.dialog_title = header or CONFIRM_DEFAULT_HEADER
+    if dyntext and text:
+        c.dialog_text = text % dyntext
+    else:
+        c.dialog_text = text or CONFIRM_DEFAULT_TEXT
+    c.url_ok = url_ok
+    return render('/dialogs/success_%s.mako' % get_dialog_template())
+
+
+def render_error(header=None, text=None, url_ok=None, dyntext=None):
+    """Returns the rendered error dialog"""
+    c.dialog_title = header or CONFIRM_DEFAULT_HEADER
+    if dyntext and text:
+        c.dialog_text = text % dyntext
+    else:
+        c.dialog_text = text or CONFIRM_DEFAULT_TEXT
+    c.url_ok = url_ok
+    return render('/dialogs/error_%s.mako' % get_dialog_template())
+
+
 def confirm(header=None, text=None, url_ok=None, url_abort=None, dynfunc=None):
+    """Decorator to be used for actions that need to be confirmed by the user"""
+
     def validate(func, self, *args, **kwargs):
+        """Checks if the confirmed paramenter is set to 1 and call the origin
+        function. If the call is not confirmed, then render a confirm dialog"""
         if DIALOGS_ENABLED:
-            # Check if the GET parameters include a parameter 'confirmed' which is set to '1'
-            if request.params.get('confirmed') == '1':
-                log.debug('calling function %s is confirmed. Proceeding.' % func.__name__)
+            # Check if the url include a parameter 'confirmed' which is set to
+            # '1'
+            url_dict = request.environ.get('pylons.routes_dict')
+            if url_dict.get('confirmed') == '1':
+                log.debug('calling function %s is confirmed. Proceeding.'
+                          % func.__name__)
                 return func(self, *args, **kwargs)
             else: # show dialog
-                log.debug('calling function %s is NOT confirmed. Redirecting.' % func.__name__)
+                # Build url for ok
+                url_ok = url_for(controller=url_dict.get('controller'),
+                                 action=url_dict.get('action'),
+                                 id=url_dict.get('id'),
+                                 confirmed=1)
+                log.debug('calling function %s is NOT confirmed. Redirecting.'
+                          % func.__name__)
                 # Save request.params in a session. Otherwise they will get
                 # lost after the user confirms the action (new request).
-                session['request.params'] = request.params
-                session.save()
+                #session['request.params'] = request.params
+                #session.save()
 
                 # Use function to include dynamic content to the confirm
-                # dialog, function will be called with the id of the origin call 
+                # dialog, function will be called with the id of the origin
+                # call
                 dyntext = None
                 if dynfunc:
                     dyntext = dynfunc(args[0])
-                return render_confirm(header, text, url_ok, url_abort, dyntext)
+                return render_confirm(_(header), _(text), url_ok, url_abort,
+                                      dyntext)
         else: # No dialogs at all
-           return func(self, *args, **kwargs)
+            return func(self, *args, **kwargs)
+
     return decorator(validate)
 
+def success(header=None, text=None, url_ok=None, dynfunc=None):
+    """Simple function which can be called after an action was successfully
+    done. It returns a rendered success-dialog"""
+    url_ok = url_ok or url_for(controller="case", action="digest")
+    return render_success(header, text, url_ok, dynfunc)
+
+def error(header=None, text=None, url_ok=None, dynfunc=None):
+    """Simple function which can be called after an action was  done with
+    errors. It returns a rendered error-dialog"""
+    url_ok = url_ok or url_for(controller="case", action="digest")
+    return render_error(header, text, url_ok, dynfunc)
+
+
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8:



More information about the Mpuls-commits mailing list