[Mpuls-commits] r2105 - in wasko/branches/2.0: jmdweb/controllers mpulsweb/controllers

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Mar 24 14:09:29 CET 2010


Author: torsten
Date: 2010-03-24 14:09:29 +0100 (Wed, 24 Mar 2010)
New Revision: 2105

Added:
   wasko/branches/2.0/mpulsweb/controllers/evaluate.py
Removed:
   wasko/branches/2.0/jmdweb/controllers/evaluate.py
Log:
Moved controller for evaluation to mpulsweb


Deleted: wasko/branches/2.0/jmdweb/controllers/evaluate.py
===================================================================
--- wasko/branches/2.0/jmdweb/controllers/evaluate.py	2010-03-24 13:08:41 UTC (rev 2104)
+++ wasko/branches/2.0/jmdweb/controllers/evaluate.py	2010-03-24 13:09:29 UTC (rev 2105)
@@ -1,286 +0,0 @@
-import os
-import logging
-import formencode
-import traceback
-import pylons
-
-from pylons import session, g, c
-from pylons.i18n import _ 
-from libmpuls.evaluation.config import EvaluationConfig
-from libmpuls.evaluation.evaluation import EvaluationSet
-from libmpuls.evaluation.export import EvaluationExportXML, EvaluationExportCSV, EvaluationExportHTML
-from mpulsweb.lib.base import BaseController, render, request, response
-from mpulsweb.lib.db import db
-from mpulsweb.lib.helpers import format_date, get_phasesuccessors 
-from mpulsweb.lib.validators import EvaluationFormValidator
-
-log = logging.getLogger(__name__)
-
-def get_configfile(id):
-    for enabled_eval in g.mpuls_config.get('evaluations', 'enabled'):
-        if enabled_eval.get('id') == str(id):
-            filename = enabled_eval.get('config')
-            for d in pylons.config.get('pylons.paths').get('static_files'):
-                path = os.path.join(d, 'xml', filename)
-                if os.path.exists(path):
-                    return path
-    return None
-
-def get_phase_dates(id):
-    '''Helper function to get start and end dates of phases ready to use in
-    SQL-Statements for evaluation based on the configuration. Note that
-    enddates will have a coalesce clause.'''
-    sdate, edate = None, None
-    log.debug('Phase: %s.' % id)
-    pair = g.mpuls_config.get('phases', 'pairs')[0].get(id) 
-    log.debug('Getting start and end date for phase %s.' % pair)
-    for d in g.mpuls_config.get('phases', 'dates'):
-        for k,v in d.iteritems():
-            if k == pair[0]:
-                sdate = v
-            elif k == pair[1]:
-                edate = "coalesce(%s, now())" % v
-    log.debug('sdate: %s, edate: %s' % (sdate, edate))
-    return sdate, edate
-
-
-def get_search_options(soptions=None, id=None):
-    options = {}
-    options['id'] = id
-    if soptions:
-
-        options['start_date'] = soptions.get('start_date') or \
-            g.mpuls_config.get('search', 'default-start-date')
-        options['end_date'] = soptions.get('end_date') or \
-            g.mpuls_config.get('search', 'default-end-date')
-        phase = [int(p) for p in soptions.get('phase')]
-        
-        if len(phase) == 1 and -1 in phase:
-            options['start_date_field'] = None
-            options['end_date_field'] = None
-        else:
-            options['start_date_field'] = get_phase_dates(min(phase))[0]
-            options['end_date_field'] = get_phase_dates(max(phase))[1]
-        options['phase'] = max(phase) #TODO: Why max?
-        options['sqlwhere'] = soptions.get('sqlwhere') or None 
-        options['sql'] =  soptions.get('sql') or None
-    else:
-        # set default evaluation options.
-        options['phase'] = g.mpuls_config.get('evaluations', 'default-phases') 
-        options['start_date'] = g.mpuls_config.get('search', 'default-start-date')
-        options['end_date'] = g.mpuls_config.get('search', 'default-end-date')
-    options['typelist'] = c.evalconfig.get_evaluations()
-
-    #phases = []
-    #for p in g.mpuls_config.get('search', 'phases'):
-    #    pt = [t for t in p.values() if int(t) >= 0]
-    #    phases.extend(pt)
-    #options['phase'] =  phases
-
-    # convert dates to locale
-    options['start_date'] = format_date(options['start_date']) 
-    options['end_date']   = format_date(options['end_date']) 
-    return options
-
-class EvaluateController(BaseController):
-
-    def index(self):
-        return self.evaluate()
-
-    def evaluate(self, id=None):
-        conn, cur = None, None
-        try:
-            try:
-                config_file = get_configfile(id)
-                conn = db.getConnection()
-                c.evalconfig   = EvaluationConfig(
-                                config_file,
-                                conn,
-                                None,
-                                None,
-                                None,
-                                None,
-                                None,
-                                None,
-                                None)
-            except:
-                print 'Error: Evaluation failed %s' % traceback.print_exc()
-                log.error(_('Error: Evaluation failed'))
-        finally:
-            db.recycleConnection(conn, cur)
-
-        c.evaloptions = get_search_options(session.get('evaluation.options'), id)
-
-        # If user selects adele-evaluation render page with disabled configuration elements. 
-        # Change default params
-        if id == '0':
-            c.evaloptions['phase'] = g.mpuls_config.get('evaluations', 'adele-phases') 
-            form = render('/evaluation/evaluate_adele.mako')
-        else:
-            form = render('/evaluation/evaluate.mako')
-        return formencode.htmlfill.render(form, \
-                defaults=c.evaloptions, \
-                errors={}, \
-                auto_insert_errors=False)
-
-    def _get_evalparams(self, form_result):
-        params = {}
-        params['id'] = form_result['id']
-        params['start_date'] = str(form_result['start_date'])
-        params['end_date'] = str(form_result['end_date'])
-        params['typelist'] = form_result['typelist']
-        params['phase']    = form_result['phase']
-        params['ending']    = form_result['type_ending']
-
-        # Dates
-        # Build timeframes bases on selected phases.
-        dates = []
-        all_phases = []
-        tdates = []
-        phase = params.get('phase')
-        dates.append('( ')
-        if params.get('start_date') != 'None' and params.get('end_date') != 'None':
-            if phase:
-                for p in phase:
-                    if int(p) >= 0: # phase is unknown -> has no start and endphase
-                        sdf, edf = get_phase_dates(p)
-                        suc = get_phasesuccessors(p)
-                        all_phases.extend(p)
-                        tdates.append("('%s'::date <= %s AND '%s'::date >= %s AND phase IN (%s) )" % (params.get('start_date'), edf, params.get('end_date'), sdf, ",".join(["%s" % s for s in suc])))
-                    else:
-                        tdates.append('phase IN (-1)')
-                dates.append(" OR ".join(tdates))
-            else:
-                dates.append("FALSE")
-        else:
-            dates.append("TRUE")
-        dates.append(' )')
-
-        params['sql'] = "SELECT %%(fields)s from master_tbl_eval_total_view m WHERE %s " % ("".join(dates))
-
-        #Datefields
-        if all_phases:
-            params['start_date_field'] = get_phase_dates(min(all_phases))[0]
-            params['end_date_field'] = get_phase_dates(max(all_phases))[1]
-        else:
-            params['start_date_field'] = None
-            params['end_date_field'] =  None
-        return params
-
-
-    def evaluateAction(self):
-            '''Return an HTML file containing the result of one or more evaluations.'''
-            params = formencode.variabledecode.variable_decode(request.params)
-            #params = request.params
-            validator    = EvaluationFormValidator() 
-            form_result  = {}
-            form_errors  = {}
-
-            # Check values
-            conn, cur = None, None
-            conn = db.getConnection()
-            id = int(params['id'])
-            c.evalconfig   = EvaluationConfig(
-                            get_configfile(id),
-                            conn,
-                            None,
-                            None,
-                            None,
-                            None,
-                            None,
-                            None,
-                            None)
-            try:
-                form_result  = validator.to_python(params)
-            except formencode.Invalid, error:
-                form_result = error.value
-                form_errors = error.error_dict or {}
-                c.evaloptions = get_search_options(session.get('evaluation.options'), id)
-                form = render('/evaluation/evaluate.mako')
-                return formencode.htmlfill.render(form, \
-                        defaults=form_result, \
-                        errors=form_errors, \
-                        auto_insert_errors=False)
-
-            # Build evaluation
-            eval_params = self._get_evalparams(form_result)
-            try:
-                try:
-                    evalconfig   = EvaluationConfig(
-                                    get_configfile(form_result['id']),
-                                    conn,
-                                    eval_params['start_date'], 
-                                    eval_params['end_date'],
-                                    eval_params['start_date_field'], 
-                                    eval_params['end_date_field'], 
-                                    None,
-                                    eval_params['sql'], 
-                                    eval_params['typelist'])
-                    evalset      = EvaluationSet(evalconfig, True)
-                    evalset.evaluate()
-                    c.result = evalset.export(EvaluationExportHTML(show_percent=form_result['show_percent']))
-                    session['evaluation.params'] = eval_params
-                    session.save()
-                    form = render('/evaluation/result.mako')
-                    return formencode.htmlfill.render(form, \
-                        defaults=form_result, \
-                        errors=form_errors, \
-                        auto_insert_errors=False)
-                except:
-                    log.error(_('Error: Evaluation failed'))
-                    log.error(traceback.print_exc())
-            finally:
-                db.recycleConnection(conn, cur)
-
-    def exportXML(self):
-        '''Return an XML file containing the result of one or more evaluations.'''
-        response.headers['Content-Type'] = 'application/xml; charset=utf8'
-        response.headers['Content-Disposition'] = 'attachment; filename=evaluation-export.xml'
-        form_result = session.get('evaluation.params')
-        try:
-            try:
-                conn, cur = db.getConnection(), None
-                evalconfig   = EvaluationConfig(
-                                get_configfile(form_result['id']),
-                                conn,
-                                form_result['start_date'], 
-                                form_result['end_date'],
-                                form_result['start_date_field'], 
-                                form_result['end_date_field'], 
-                                None,
-                                form_result['sql'], 
-                                form_result['typelist'])
-                evalset      = EvaluationSet(evalconfig)
-                evalset.evaluate()
-                return evalset.export(EvaluationExportXML())
-            except:
-                log.exception(_('XML Export failed'))
-        finally:
-            db.recycleConnection(conn, cur)
-
-
-    def exportCSV(self):
-        '''Return an CSV file containing the result of one or more evaluations.'''
-        response.headers['Content-Type'] = 'application/csv; charset=utf8'
-        response.headers['Content-Disposition'] = 'attachment; filename=evaluation-export.csv'
-        form_result = session.get('evaluation.params')
-        try:
-            try:
-                conn, cur = db.getConnection(), None
-                evalconfig   = EvaluationConfig(
-                                get_configfile(form_result['id']),
-                                conn,
-                                form_result['start_date'], 
-                                form_result['end_date'],
-                                form_result['start_date_field'], 
-                                form_result['end_date_field'], 
-                                None,
-                                form_result['sql'], 
-                                form_result['typelist'])
-                evalset      = EvaluationSet(evalconfig)
-                evalset.evaluate()
-                return evalset.export(EvaluationExportCSV())
-            except:
-                log.exception(_('CSV Export failed'))
-        finally:
-            db.recycleConnection(conn, cur)

Copied: wasko/branches/2.0/mpulsweb/controllers/evaluate.py (from rev 2103, wasko/branches/2.0/jmdweb/controllers/evaluate.py)


Property changes on: wasko/branches/2.0/mpulsweb/controllers/evaluate.py
___________________________________________________________________
Name: svn:mergeinfo
   + 



More information about the Mpuls-commits mailing list