Esempio n. 1
0
 def evaluate(self, cr, uid, ids, input_text, question=None, context=None):
     def norm(validation):
         return "((%s))" % ") or\n (".join([ l.strip() for l in validation.strip().split("\n") if l.strip() != ''])
     local_dict = tools.local_dict(input_text, question)
     r = {}
     for f in self.browse(cr, uid, ids):
         is_valid = eval(norm(f.validation), local_dict)
         formated = eval(norm(f.formating), dict(local_dict, is_valid=is_valid))
         message = ';'.join(
             [ m.name for m in f.message_ids if eval(norm(m.condition), dict(local_dict, is_valid=is_valid, formated=formated))]
         )
         r[f.id] = dict(
             is_valid = is_valid,
             formated = formated,
             message = message,
         )
     return r
Esempio n. 2
0
    def evaluate(self, cr, uid, ids, input_text, question=None, context=None):
        def norm(validation):
            return "((%s))" % ") or\n (".join([
                l.strip()
                for l in validation.strip().split("\n") if l.strip() != ''
            ])

        local_dict = tools.local_dict(input_text, question)
        r = {}
        for f in self.browse(cr, uid, ids):
            is_valid = eval(norm(f.validation), local_dict)
            formated = eval(norm(f.formating),
                            dict(local_dict, is_valid=is_valid))
            message = ';'.join([
                m.name for m in f.message_ids
                if eval(norm(m.condition),
                        dict(local_dict, is_valid=is_valid, formated=formated))
            ])
            r[f.id] = dict(
                is_valid=is_valid,
                formated=formated,
                message=message,
            )
        return r
Esempio n. 3
0
    def onchange_input(self, cr, uid, ids, input_text, fields, context=None):
        """
        Esta función toma el cambio que ocurre en una celda input y
        actualiza el estado del próximo campo según lo que indique las
        condiciones del "next_enable" o próximo campo a habilitar.
        También verifica que mensaje tiene que enviarse al dataentry.
        """
        context = context or None
        value = {}
        complete_place = False

        answer_obj = self.pool.get("survey_methodology.answer")
        question_obj = self.pool.get("survey_methodology.question")
        question_ids = question_obj.search(cr, uid, [("complete_place", "=", fields)])

        # answer_id = answer_obj.search(cr, uid, [('complete_place','=',fields),('questionnaire_id','=',ids)])

        # Iterate over all hierarchical branch questions.
        # child_ids = question_ids
        # parent_ids = question_obj.search(cr, uid, [('child_ids', 'in', child_ids)])
        # while len(parent_ids)>0:
        #    child_ids = child_ids + parent_ids
        #    parent_ids = question_obj.search(cr, uid, [('child_ids', 'in', parent_ids)])
        # question_ids = child_ids

        for question in question_obj.browse(cr, uid, question_ids):

            # Habilitación o deshabilitación de preguntas.
            if question.next_enable == False:
                _logger.warning("Question %s no enable any other question" % question.complete_name)
            else:
                _logger.debug("Next enable: %s" % (question.next_enable))
                for lines in question.next_enable.split("\n"):
                    if lines.strip():
                        parsed = re.search(
                            r"(?P<condition>[^:]*):(?P<to_enable>[^:]*)(:(?P<to_disable>.*))?", lines
                        ).groupdict()
                        if parsed["condition"] and eval(parsed["condition"], tools.local_dict(input_text, question)):
                            to_enable = filter(lambda i: i != "", (parsed["to_enable"] or "").split(","))
                            to_disable = filter(lambda i: i != "", (parsed["to_disable"] or "").split(","))
                            to_enable = [to if " / " in to else to.replace("_", " / ") for to in to_enable]
                            to_disable = [to if " / " in to else to.replace("_", " / ") for to in to_disable]
                            _logger.debug("Searching to enable: %s" % (",".join(to_enable)))
                            _logger.debug("Searching to disable: %s" % (",".join(to_disable)))
                            next_dict = dict(
                                [
                                    (qid, "enabled")
                                    for qid in question_obj.search(
                                        cr,
                                        uid,
                                        [("survey_id", "=", question.survey_id.id), ("complete_name", "in", to_enable)],
                                    )
                                ]
                                + [
                                    (qid, "disabled")
                                    for qid in question_obj.search(
                                        cr,
                                        uid,
                                        [
                                            ("survey_id", "=", question.survey_id.id),
                                            ("complete_name", "in", to_disable),
                                        ],
                                    )
                                ]
                            )
                            _logger.debug("Found: %s" % (next_dict))
                            next_field_code = question_obj.read(
                                cr, uid, next_dict.keys(), ["complete_place", "complete_name"]
                            )
                            for item in next_field_code:
                                complete_place = item["complete_place"]
                                value["sta_%s" % complete_place] = next_dict[item["id"]]
                                it_ids = answer_obj.search(cr, uid, [("complete_place", "=", complete_place)])
                                if it_ids == []:
                                    q_ids = question_obj.search(
                                        cr,
                                        uid,
                                        [
                                            ("survey_id", "=", question.survey_id.id),
                                            ("complete_place", "=", complete_place),
                                        ],
                                    )
                                    for nq in question_obj.browse(cr, uid, q_ids):
                                        v = {
                                            "name": nq.question,
                                            "complete_place": nq.complete_place,
                                            "code": nq.name,
                                            "input": False,
                                            "formated": False,
                                            "message": False,
                                            "valid": False,
                                            "questionnaire_id": ids[0],
                                            "question_id": nq.id,
                                        }
                                        it_ids.append(answer_obj.create(cr, uid, v))
                                    if it_ids == []:
                                        raise osv.except_osv(
                                            "Inestable Questionary",
                                            "Not answer associated to the next field. Communicate with the administrator.",
                                        )
                                answer_obj.write(cr, uid, it_ids, {"state": next_dict[item["id"]]})
                                _logger.debug("Change %s(%s) to %s" % (complete_place, it_ids, next_dict[item["id"]]))

            # Evaluamos el formato
            format_obj = question.format_id
            format_res = format_obj.evaluate(input_text, question)[format_obj.id]

            # Mensajes según pregunta.
            value["msg_%s" % fields] = format_res["message"]
            value["vms_%s" % fields] = format_res["message"]
            value["for_%s" % fields] = format_res["formated"]
            value["vfo_%s" % fields] = format_res["formated"]
            value["val_%s" % fields] = format_res["is_valid"]

        r = {"value": value}
        if complete_place:
            r.update(grab_focus="inp_%s" % complete_place)

        return r
Esempio n. 4
0
    def onchange_input(self, cr, uid, ids, input_text, fields, context=None):
        """
        Esta función toma el cambio que ocurre en una celda input y
        actualiza el estado del próximo campo según lo que indique las
        condiciones del "next_enable" o próximo campo a habilitar.
        También verifica que mensaje tiene que enviarse al dataentry.
        """
        context = context or None
        value={}
        complete_place = False

        answer_obj = self.pool.get('sondaggio.answer')
        question_obj = self.pool.get('sondaggio.node')
        question_ids = question_obj.search(cr, uid, [('complete_place','=',fields)])

        #answer_id = answer_obj.search(cr, uid, [('complete_place','=',fields),('questionnaire_id','=',ids)])

        # Iterate over all hierarchical branch questions.
        #child_ids = question_ids
        #parent_ids = question_obj.search(cr, uid, [('child_ids', 'in', child_ids)])
        #while len(parent_ids)>0:
        #    child_ids = child_ids + parent_ids
        #    parent_ids = question_obj.search(cr, uid, [('child_ids', 'in', parent_ids)])
        #question_ids = child_ids

        for question in question_obj.browse(cr, uid, question_ids):

            # Habilitación o deshabilitación de preguntas.
            if question.next_enable == False:
                _logger.warning('Question %s no enable any other question' % question.complete_name)
            else:
                _logger.debug('Next enable: %s' % (question.next_enable))
                for lines in question.next_enable.split('\n'):
                    if lines.strip():
                        parsed = re.search(r'(?P<condition>[^:]*):(?P<to_enable>[^:]*)(:(?P<to_disable>.*))?', lines).groupdict()
                        if parsed['condition'] and eval(parsed['condition'], tools.local_dict(input_text, question)):
                            to_enable = filter(lambda i: i!='', (parsed['to_enable'] or '').split(','))
                            to_disable = filter(lambda i: i!='', (parsed['to_disable'] or '').split(','))
                            to_enable = [ to if ' / ' in to else to.replace('_', ' / ') for to in to_enable ]
                            to_disable = [ to if ' / ' in to else to.replace('_', ' / ') for to in to_disable ]
                            _logger.debug('Searching to enable: %s' % (','.join(to_enable)))
                            _logger.debug('Searching to disable: %s' % (','.join(to_disable)))
                            next_dict = dict(
                                [ (qid, 'enabled') for qid in question_obj.search(cr, uid, [
                                    ('survey_id','=',question.survey_id.id),
                                    ('complete_name', 'in', to_enable)
                                ]) ] +
                                [ (qid, 'disabled') for qid in question_obj.search(cr, uid, [
                                    ('survey_id','=',question.survey_id.id),
                                    ('complete_name', 'in', to_disable)
                                ]) ])
                            _logger.debug('Found: %s' % (next_dict))
                            next_field_code = question_obj.read(cr, uid, next_dict.keys(), ['complete_place', 'complete_name'])
                            for item in next_field_code:
                                complete_place = item['complete_place']
                                value['sta_%s' % complete_place] = next_dict[item['id']]
                                it_ids = answer_obj.search(cr, uid, [('complete_place','=',complete_place)])
                                if it_ids == []:
                                    q_ids = question_obj.search(cr, uid, [('survey_id','=',question.survey_id.id),('complete_place','=',complete_place)])
                                    for nq in question_obj.browse(cr, uid, q_ids):
                                        v = {
                                            'name': nq.question,
                                            'complete_place': nq.complete_place,
                                            'code': nq.name,
                                            'input': False,
                                            'formated': False,
                                            'message': False,
                                            'valid': False,
                                            'questionnaire_id': ids[0],
                                            'question_id': nq.id,
                                        }
                                        it_ids.append(answer_obj.create(cr, uid, v))
                                    if it_ids == []:
                                        raise osv.except_osv("Inestable Questionary", "Not answer associated to the next field. Communicate with the administrator.")
                                answer_obj.write(cr, uid, it_ids, {'state': next_dict[item['id']]})
                                _logger.debug('Change %s(%s) to %s' % (complete_place, it_ids, next_dict[item['id']]))

            # Evaluamos el formato
            format_obj = question.format_id
            format_res = format_obj.evaluate(input_text, question)[format_obj.id]

            # Mensajes según pregunta.
            value['msg_%s' % fields] = format_res['message']
            value['vms_%s' % fields] = format_res['message']
            value['for_%s' % fields] = format_res['formated']
            value['vfo_%s' % fields] = format_res['formated']
            value['val_%s' % fields] = format_res['is_valid']

        r = { 'value': value }
        if complete_place:
            r.update(grab_focus='inp_%s' % complete_place)

        return r