[Mpuls-commits] r1941 - wasko/branches/2.0/mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Mar 15 11:32:56 CET 2010


Author: torsten
Date: 2010-03-15 11:32:53 +0100 (Mon, 15 Mar 2010)
New Revision: 1941

Added:
   wasko/branches/2.0/mpulsweb/lib/dialogs.py
Log:
* mpulsweb/lib/dialogs.py: New. Includes checkPrivacy-Decorator and
	future logic for confirm-dialogs.


Added: wasko/branches/2.0/mpulsweb/lib/dialogs.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/dialogs.py	2010-03-15 09:09:46 UTC (rev 1940)
+++ wasko/branches/2.0/mpulsweb/lib/dialogs.py	2010-03-15 10:32:53 UTC (rev 1941)
@@ -0,0 +1,115 @@
+#-*- coding: utf-8 -*-
+
+import logging
+from decorator import decorator
+
+from paste.httpexceptions import HTTPNotFound
+
+from pylons import request, session, tmpl_context as c, app_globals as g
+from pylons.i18n import _
+
+from mpulsweb.lib.base import render
+from mpulsweb.lib.helpers import url_for 
+from mpulsweb.model.appointment import CaseAppointmentFactory
+
+CONFIRM_DEFAULT_HEADER = _('Default Header')
+CONFIRM_DEFAULT_TEXT   = _('Default Text')
+CONFIRM_DEFAULT_URL_ABORT = '/index'
+
+DIALOGS_ENABLED = True
+
+log = logging.getLogger(__name__)
+
+# MISSING STATEMENT DIALOG
+# ------------------------
+# If the statement of the declaration of consent is missing on openening a case
+# we will open a dialog which informs the user about this fact
+
+def checkPrivacyStatement(context='select'):
+    '''Decorator for the methods in the case controller'''
+    def validate(func, self, *args, **kwargs):
+        #load case and check if the statement is signed
+        if context == "showAppointment":
+            try:
+                app_id = int(args[0])
+                confirmed = int(args[1])
+            except:
+                raise HTTPNotFound()
+            app_factory = CaseAppointmentFactory()
+            app = app_factory.loadById(app_id)
+            case_id = app.case_id
+            c.targeturl = url_for(controller="/case",
+                                    action="showAppointment",
+                                    id=app_id, confirmed=1)
+        else:
+            try:
+                case_id = int(args[0])
+                confirmed = int(args[1])
+            except:
+                raise HTTPNotFound()
+            c.targeturl = url_for(controller="/case", action=context,
+                                    id=case_id, confirmed=1)
+
+        case = g.case_factory.loadById(case_id)
+
+        statement = case.getPrivacyStatement()
+        # Check if there is a "Einverstaendniserklaerung" None if it is
+        # not either signed or declined
+        if (not statement.isSigned() is None) or (confirmed == 1):
+            return func(self, *args, **kwargs)
+        else:
+            c.ds_id = case_id
+            return render('/privacy/dialogs/missing_statement.mako')
+    return decorator(validate)
+
+
+# FUTURE CONFIRM DIALOG
+# ---------------------
+# Not used yet. Use this confirm decaorator in future to set up general
+# confirm dialog. Not that this means to loose information in the navigation
+# 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
+
+def render_confirm(header=None,text=None,url_ok=None,url_abort=None,dyntext=None):
+    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 
+    return render('/dialogs/confirm_%s.mako' % get_dialog_template())
+
+def confirm(header=None, text=None, url_ok=None, url_abort=None, dynfunc=None):
+    def validate(func, self, *args, **kwargs):
+        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__)
+                return func(self, *args, **kwargs)
+            else: # show dialog
+                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()
+
+                # Use function to include dynamic content to the confirm
+                # 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)
+        else: # No dialogs at all
+           return func(self, *args, **kwargs)
+    return decorator(validate)
+
+# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8:



More information about the Mpuls-commits mailing list