[Mpuls-commits] r2002 - wasko/branches/2.0/mpulsweb/controllers
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Thu Mar 18 12:45:16 CET 2010
Author: torsten
Date: 2010-03-18 12:45:15 +0100 (Thu, 18 Mar 2010)
New Revision: 2002
Added:
wasko/branches/2.0/mpulsweb/controllers/case_bundle.py
Log:
* mpulsweb/controllers/case_bundle.py: New controller to handle
bundled actions
Added: wasko/branches/2.0/mpulsweb/controllers/case_bundle.py
===================================================================
--- wasko/branches/2.0/mpulsweb/controllers/case_bundle.py 2010-03-18 11:43:32 UTC (rev 2001)
+++ wasko/branches/2.0/mpulsweb/controllers/case_bundle.py 2010-03-18 11:45:15 UTC (rev 2002)
@@ -0,0 +1,409 @@
+# -*- coding: utf-8 -*-
+import re
+from cgi import escape
+import logging
+
+import formencode
+
+from pylons import app_globals as g
+
+from mpulsweb.lib.base import c, h, render, request, session, BaseController
+from mpulsweb.lib.security import checkRole
+from mpulsweb.lib.validators import BundleActionForm, SetBundleEditorForm, \
+ SetBundleStandinForm
+from mpulsweb.lib.helper.filters import NA
+from mpulsweb.model.user import UserGroupList, UserListObject, UserObject
+from mpulsweb.model.case import MpulsCaseBundle, MpulsCaseOverview
+from mpulsweb.model.phase import ConsistenceCheckException
+from mpulsweb.controllers.case_overview import parseSearchOptions
+
+log = logging.getLogger(__name__)
+
+
+BUNDLE_NOTIFICATION_NOACTION_HEADER = u"""\
+Keine Aktion für die Auswahl angegeben!"""
+BUNDLE_NOTIFICATION_NOACTION = u"""\
+Sie haben keine Aktion für die Auswahl angegeben. Bitte klicken Sie auf
+OK, um zur Fallaktenübersicht zu gelangen und dort eine Aktion aus dem
+Auswahlfeld auszuwählen."""
+
+BUNDLE_NOTIFICATION_EMPTYLIST_HEADER = u"""\
+Keine gültigen Fälle in der Auswahl!"""
+BUNDLE_NOTIFICATION_EMPTYLIST = u"""\
+Die gewünschte Aktion kann für keine der in der Auswahl befindlichen
+Fallakten durchgeführt werden. Vergewissern Sie sich, dass sich die
+Fallakten in einem Zustand befinden, aus dem Sie die Aktion ausführen
+können."""
+DIALOG_CONFIRM_DELETE = u'''Fallakten löschen?'''
+DIALOG_CONFIRM_EXPLAINATION = u'''\
+Wollen Sie wirklich die folgenden Fallakten löschen?%s'''
+MARKDELETE_SUCCESS_HEADER = u"""Fallakten gelöscht!"""
+MARKDELETE_SUCCESS_TEXT = u"""\
+Es wurden %s Fallakten erfolgreich zum Löschen freigegeben. Bitte
+klicken Sie auf "OK", um fortzufahren."""
+DELETE_SUCCESS_HEADER = u"""Fallakten gelöscht!"""
+DELETE_SUCCESS_TEXT = u"""\
+Es wurden %s Fallakten erfolgreich gelöscht. Bitte klicken Sie auf "OK",
+um fortzufahren."""
+
+DIALOG_CONFIRM_MARKANONYMIZE = u'''Fallakten zur Anonymisierung freigeben?'''
+DIALOG_CONFIRM_MARKANONYMIZE_EXPLAINATION = u'''\
+Wollen Sie wirklich die folgenden Fallakten zum Anonymisieren freigeben?
+Die Fallakten stehen Ihnen danach nicht mehr zur Bearbeitung zur
+Verfügung und werden der Administration zur Anonymisierung vorgelegt.%s'''
+MARKANONYMIZE_SUCCESS_HEADER = u"""\
+Fallakte wurde zur Anonymisierung freigegeben!"""
+MARKANONYMIZE_SUCCESS_TEXT = u"""\
+Es wurden %s Fallakten zur Anonymisierung freigegeben. Sie liegen der
+Administration zur endgültigen Anonymisierung vor. Bitte klicken Sie auf "Ok",
+um fortzufahren."""
+ANONYMIZE_SUCCESS_HEADER = u"""Fallakten anonymisiert!"""
+ANONYMIZE_SUCCESS_TEXT = u"""\
+Es wurden %s Fallakten erfolgreich anonymisiert. Bitte klicken Sie auf "Ok",
+um fortzufahren."""
+
+DIALOG_CONFIRM_RESTORE = u'''Fallakten wiederherstellen?'''
+DIALOG_CONFIRM_RESTORE_EXPLAINATION = u'''\
+Wollen Sie wirklich die folgenden Fallakten wiederherstellen? Die
+Fallakten stehen danach wieder zur Bearbeitung zur Verfügung.%s'''
+RESTORE_SUCCESS_HEADER = u"""Fallakten wiederhergestellt!"""
+RESTORE_SUCCESS_TEXT = u"""\
+Es wurden %s Fallakten erfolgreich wiederhergestellt. Bitte klicken Sie
+auf "OK", um fortzufahren."""
+
+TRANSFER_SUCCESS_HEADER = u"""Bearbeitung übertragen!"""
+TRANSFER_SUCCESS_TEXT = u"""\
+Es wurden %s Fallakten erfolgreich an %s %s übertragen. Bitte klicken
+Sie auf "OK", um fortzufahren."""
+STANDIN_SUCCESS_HEADER = u"""Vertretung zugewiesen!"""
+STANDIN_SUCCESS_TEXT = u"""\
+Es wurden für %s Fallakten erfolgreich die Vertretung zugewiesen. Bitte
+klicken Sie auf "OK", um fortzufahren."""
+
+def no_action(confirmed=False):
+ c.dialog_title= BUNDLE_NOTIFICATION_NOACTION_HEADER
+ c.dialog_text = BUNDLE_NOTIFICATION_NOACTION
+ return render('/casemanagement/dialogs/bundle_noselection.mako')
+
+ACTION_MAPPING = {'no_action': no_action}
+
+class CaseBundleController(BaseController):
+
+ def index(self):
+ return "Hello world."
+
+ def bundleAction(self):
+ validator = BundleActionForm()
+ log.debug(request.params)
+ try:
+ # Check submitted fields
+ form_result = validator.to_python(request.params)
+ log.debug(form_result)
+
+ # Check if the user has selected any cases
+ if len(form_result.get('case_id', [])) <= 0 and \
+ not form_result.get('all_cases'):
+ return self._empty_caselist()
+
+ # Build case bundle
+ case_bundle = self._build_casebundle(form_result)
+
+ # Delegate to concrete action
+ action = form_result.get('action')
+ # No action
+ if action == 'no_action':
+ return self.no_action()
+ if action == 'markdelete':
+ # Fiter out case which are not open(1) or inactive (2)
+ case_bundle = self._filterBundle(case_bundle, allowedstates=(1, 2))
+ if case_bundle.isEmpty():
+ return self._empty_caselist()
+ session['casebundle'] = case_bundle
+ session.save()
+ return self.markDelete(0)
+ if action == 'delete':
+ # Fiter out case which are not marked for deletion
+ case_bundle = self._filterBundle(case_bundle, allowedstates=(3,))
+ if case_bundle.isEmpty():
+ return self._empty_caselist()
+ session['casebundle'] = case_bundle
+ session.save()
+ return self.delete(0)
+ if action == 'markanonymize':
+ # Fiter out case which are not open(1) or inactive (2) or not anonymizable
+ case_bundle = self._filterBundle(case_bundle,
+ allowedstates=(1, 2),
+ filter_notanonymizable=True)
+ if case_bundle.isEmpty():
+ return self._empty_caselist()
+ session['casebundle'] = case_bundle
+ session.save()
+ return self.markAnonymize(0)
+ if action == 'restore':
+ # Filter out cases which aren't marked for anonymization (4),
+ # or marked for deletion (3)
+ case_bundle = self._filterBundle(case_bundle,
+ allowedstates=(3, 4))
+ if case_bundle.isEmpty():
+ return self._emptyListError()
+ session['casebundle'] = case_bundle
+ session.save()
+ return self.restore(0)
+ if action == 'standin':
+ # filter out cases where the user is not the editor and
+ # therefor is not allowed to set the standin.
+ user = session['USER_AUTHORIZED']
+ filtered = []
+ for case in case_bundle.getCases():
+ if (case.getEditor().id != user.id
+ and not user.isAdmin()):
+ filtered.append(case.id)
+
+ # Fiter out case which are not open(1) or inavtice (2)
+ case_bundle = self._filterBundle(case_bundle, (1, 2), filtered)
+ if case_bundle.isEmpty():
+ return self._emptyListError()
+ session['casebundle'] = case_bundle
+ session.save()
+
+ group_list = UserGroupList()
+ user_list = UserListObject()
+ group_filter = [u.gid for u in user_list.getAdminList()]
+ group_filter.append(user.gid)
+ c.group_list = group_list.getGroupList(group_filter)
+ c.case_list = case_bundle.getCases()
+ c.current_user = user
+
+ return render('/casemanagement/bundle_standin.mako')
+ if action == 'transfer':
+ # Fiter out case which are not open(1) or inavtice (2)
+ user = session['USER_AUTHORIZED']
+ case_bundle = self._filterBundle(case_bundle, (1, 2))
+ if case_bundle.isEmpty():
+ return self._emptyListError()
+ session['casebundle'] = case_bundle
+ session.save()
+
+ user_list = UserListObject()
+ user_filter = [u.id for u in user_list.getAdminList()]
+ user_filter.append(user.id)
+ c.user_list = user_list.getUserList(user_filter)
+ c.case_list = case_bundle.getCases()
+
+ return render('/casemanagement/bundle_editor.mako')
+
+ except formencode.Invalid, error:
+ log.exception(error)
+ raise
+ except Exception, e:
+ log.exception(e)
+ raise
+ return "bundleAction"
+
+ def _filterBundle(self, case_bundle, allowedstates, filtered_cases=None, filter_notanonymizable=False):
+ """Returns a filtered CaseBundle. Filtering can be done on
+ states and on case_ids.
+ """
+ filtered = []
+ for case in case_bundle.getCases(filtered_cases):
+ if filter_notanonymizable:
+ try:
+ case.check_anonymizeability()
+ except ConsistenceCheckException, e:
+ continue
+ state = case.getState()
+ if state.getState() in allowedstates:
+ filtered.append(case.id)
+ case_bundle = MpulsCaseBundle(filtered)
+ return case_bundle
+
+ def _buildCaseList(self, bundle):
+ list = ["<ul>"]
+ for case in bundle.getCases():
+ list.append("<li>%s, %s</li>" % (escape(NA(case.first_name)),
+ escape(NA(case.last_name))))
+ list.append("</ul>")
+ return list
+
+ def _build_casebundle(self, options):
+ user = session['USER_AUTHORIZED']
+ if options.get('all_cases') > 0:
+ # Narf! Load ids of all cases with the last search
+ # option again as they are not included in the submitted
+ # data.
+
+ # If the user is admin then he should see only cases
+ # which are marked for deletion or anonymisation
+ if user.isAdmin():
+ FORM_DEFAULTS = g.mpuls_config.get('search', 'admin_defaults')[0]
+ else:
+ FORM_DEFAULTS = g.mpuls_config.get('search', 'cm_defaults')[0]
+
+ search_options = (session.get('CASE_OVERVIEW_SEARCHOPTIONS')
+ or FORM_DEFAULTS)
+ search_str = ";".join(parseSearchOptions(search_options))
+ case_bundle = MpulsCaseBundle([case.id for case in
+ MpulsCaseOverview().search(search_str)])
+ else:
+ case_bundle = MpulsCaseBundle(options.get('case_id'))
+ return case_bundle
+
+ def _empty_caselist(self):
+ c.url_ok = "/case_overview"
+ c.dialog_title = BUNDLE_NOTIFICATION_EMPTYLIST_HEADER
+ c.dialog_text = BUNDLE_NOTIFICATION_EMPTYLIST
+ return render('/casemanagement/dialogs/bundle_noselection.mako')
+
+ #
+ # ACTIONS
+ #
+ def no_action(self):
+ c.dialog_title= BUNDLE_NOTIFICATION_NOACTION_HEADER
+ c.dialog_text = BUNDLE_NOTIFICATION_NOACTION
+ return render('/casemanagement/dialogs/bundle_noselection.mako')
+
+ # DELETE
+ @checkRole(('cm'))
+ def markDelete(self, confirmed):
+ confirmed = self._checkBool(confirmed)
+ c.context = "../main.mako"
+ case_bundle = session.get('casebundle')
+ if confirmed == 1:
+ num = case_bundle.markDelete()
+ c.dialog_title = MARKDELETE_SUCCESS_HEADER
+ c.dialog_text = MARKDELETE_SUCCESS_TEXT % str(num)
+ c.url_ok = "/case_overview"
+ del session['casebundle']
+ return render ('/casemanagement/dialogs/success_markdelete.mako')
+ else:
+ c.context = "../main.mako"
+ c.dialog_title = DIALOG_CONFIRM_DELETE
+ c.dialog_text = (DIALOG_CONFIRM_EXPLAINATION
+ % "\n".join(self._buildCaseList(case_bundle)))
+ c.url_yes = "/case_bundle/markDelete/1"
+ c.url_no = "/case_overview/"
+ return render('/casemanagement/dialogs/confirm.mako')
+
+ @checkRole(('admin'))
+ def delete(self, confirmed):
+ confirmed = self._checkBool(confirmed)
+ c.context = "../main.mako"
+ case_bundle = session.get('casebundle')
+ if confirmed == 1:
+ num = case_bundle.delete()
+ c.dialog_title = DELETE_SUCCESS_HEADER
+ c.dialog_text = DELETE_SUCCESS_TEXT % str(num)
+ c.url_ok = "/case_overview"
+ del session['casebundle']
+ return render('/casemanagement/dialogs/success_delete_admin.mako')
+ else:
+ c.dialog_title = DIALOG_CONFIRM_DELETE
+ c.dialog_text = (DIALOG_CONFIRM_EXPLAINATION
+ % "\n".join(self._buildCaseList(case_bundle)))
+ c.url_yes = "/case_bundle/delete/1"
+ c.url_no = "/case_overview/"
+ return render('/casemanagement/dialogs/confirm.mako')
+
+ # ANONYMIZE
+ @checkRole(('cm'))
+ def markAnonymize(self, confirmed):
+ confirmed = self._checkBool(confirmed)
+ c.context = "../main.mako"
+ case_bundle = session.get('casebundle')
+ if confirmed == 1:
+ num = case_bundle.markAnonymize()
+ c.success_for = MARKANONYMIZE_SUCCESS_HEADER
+ c.success_text = MARKANONYMIZE_SUCCESS_TEXT % str(num)
+ c.url_ok = "/case_overview"
+ del session['casebundle']
+ return render ('/casemanagement/dialogs/success_bundle_anonymize.mako')
+ else:
+ c.dialog_title = DIALOG_CONFIRM_MARKANONYMIZE
+ c.dialog_text = (DIALOG_CONFIRM_MARKANONYMIZE_EXPLAINATION
+ % "\n".join(self._buildCaseList(case_bundle)))
+ c.url_yes = "/case_bundle/markAnonymize/1"
+ c.url_no = "/case_overview/"
+ return render('/casemanagement/dialogs/confirm_bundle_anonymize.mako')
+
+ @checkRole(('admin'))
+ def anonymize(self, confirmed):
+ confirmed = self._checkBool(confirmed)
+ c.context = "../main.mako"
+ case_bundle = session.get('casebundle')
+ if confirmed == 1:
+ num = case_bundle.anonymize()
+ c.dialog_title = ANONYMIZE_SUCCESS_HEADER
+ c.dialog_text = ANONYMIZE_SUCCESS_TEXT % str(num)
+ c.url_ok = "/case_overview"
+ del session['casebundle']
+ return render ('/casemanagement/dialogs/success_bundle_anonymize.mako')
+ else:
+ c.dialog_title = DIALOG_CONFIRM_ANONYMIZE
+ c.dialog_text = (DIALOG_CONFIRM_ANONYMIZE_EXPLAINATION
+ % "\n".join(self._buildCaseList(case_bundle)))
+ c.url_yes = "/case_overview/anonymize/1"
+ c.url_no = "/case_overview/"
+ return render('/casemanagement/dialogs/confirm_bundle_anonymize.mako')
+
+ # RESTORE
+ @checkRole(('admin'))
+ def restore(self, confirmed):
+ confirmed = self._checkBool(confirmed)
+ c.context = "../main.mako"
+ case_bundle = session.get('casebundle')
+ if confirmed == 1:
+ num = case_bundle.restore()
+ c.dialog_title = RESTORE_SUCCESS_HEADER
+ c.dialog_text = RESTORE_SUCCESS_TEXT % str(num)
+ c.url_ok = "/case_overview"
+ del session['casebundle']
+ return render ('/casemanagement/dialogs/success_restore.mako')
+ else:
+ c.dialog_title = DIALOG_CONFIRM_RESTORE
+ c.dialog_text = (DIALOG_CONFIRM_RESTORE_EXPLAINATION
+ % "\n".join(self._buildCaseList(case_bundle)))
+ c.url_yes = "/case_bundle/restore/1"
+ c.url_no = "/case_overview/"
+ return render('/casemanagement/dialogs/confirm_restore.mako')
+
+ # ORGANISATION
+ @checkRole(('admin'))
+ def set_editor(self):
+ c.context = "../main.mako"
+ case_bundle = session.get('casebundle')
+ validator = SetBundleEditorForm()
+ try:
+ form_result = validator.to_python(request.params)
+ num = case_bundle.setEditor(form_result.get('editor'))
+ user = UserObject(form_result.get('editor'))
+ c.dialog_title = TRANSFER_SUCCESS_HEADER
+ c.dialog_text= (TRANSFER_SUCCESS_TEXT
+ % (str(num), user.first_name, user.last_name))
+ c.url_ok = "/case_overview"
+ del session['casebundle']
+ return render('/casemanagement/dialogs/notificate_bundle_seteditor.mako')
+ except formencode.Invalid, error:
+ c.form_result = error.value
+ c.form_errors = error.error_dict or {}
+ return render('/casemanagement/bundle_editor.mako')
+
+ @checkRole(('admin', 'cm'))
+ def set_standin(self):
+ c.context = "../main.mako"
+ case_bundle = session.get('casebundle')
+ validator = SetBundleStandinForm()
+ try:
+ form_result = validator.to_python(request.params)
+ num = case_bundle.setStandin(form_result.get('standin'))
+ c.dialog_title = STANDIN_SUCCESS_HEADER
+ c.dialog_text = STANDIN_SUCCESS_TEXT % str(num)
+ c.url_ok = "/case_overview"
+ del session['casebundle']
+ return render('/casemanagement/dialogs/notificate_bundle_setstandin.mako')
+ except formencode.Invalid, error:
+ c.form_result = error.value
+ c.form_errors = error.error_dict or {}
+ return render('/casemanagement/bundle_standin.mako')
+
+
+# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
More information about the Mpuls-commits
mailing list