[FIX] crm: fixed on_change_user that was crashing because
[odoo/odoo.git] / addons / crm_profiling / wizard / open_questionnaire.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23 from openerp.tools.translate import _
24
25 class open_questionnaire_line(osv.osv_memory):
26     _name = 'open.questionnaire.line'
27     _rec_name = 'question_id'
28     _columns = {
29         'question_id': fields.many2one('crm_profiling.question','Question', required=True),
30         'answer_id': fields.many2one('crm_profiling.answer', 'Answer'),
31         'wizard_id': fields.many2one('open.questionnaire', 'Questionnaire'),
32     }
33
34 open_questionnaire_line()
35
36 class open_questionnaire(osv.osv_memory):
37     _name = 'open.questionnaire'
38     _columns = {
39         'questionnaire_id': fields.many2one('crm_profiling.questionnaire', 'Questionnaire name'),
40         'question_ans_ids': fields.one2many('open.questionnaire.line', 'wizard_id', 'Question / Answers'),
41     }
42
43     def default_get(self, cr, uid, fields, context=None):
44         if context is None: context = {}
45         res = super(open_questionnaire, self).default_get(cr, uid, fields, context=context)
46         questionnaire_id = context.get('questionnaire_id', False)
47         if questionnaire_id and 'question_ans_ids' in fields:
48             query = """
49                 select question as question_id from profile_questionnaire_quest_rel where questionnaire = %s"""
50             cr.execute(query, (questionnaire_id,))
51             result = cr.dictfetchall()
52             res.update(question_ans_ids=result)
53         return res
54
55     def questionnaire_compute(self, cr, uid, ids, context=None):
56         """ Adds selected answers in partner form """
57         model = context.get('active_model')
58         answers = []
59         if model == 'res.partner':
60             data = self.browse(cr, uid, ids[0], context=context)
61             for d in data.question_ans_ids:
62                  if d.answer_id:
63                      answers.append(d.answer_id.id)
64             self.pool.get(model)._questionnaire_compute(cr, uid, answers, context=context)
65         return {'type': 'ir.actions.act_window_close'}
66
67
68     def build_form(self, cr, uid, ids, context=None):
69         """ Dynamically generates form according to selected questionnaire """
70         models_data = self.pool.get('ir.model.data')
71         result = models_data._get_id(cr, uid, 'crm_profiling', 'open_questionnaire_form')
72         res_id = models_data.browse(cr, uid, result, context=context).res_id
73         datas = self.browse(cr, uid, ids[0], context=context)
74         context.update({'questionnaire_id': datas.questionnaire_id.id})
75
76         return {
77             'name': _('Questionnaire'),
78             'view_type': 'form',
79             'view_mode': 'form',
80             'res_model': 'open.questionnaire',
81             'type': 'ir.actions.act_window',
82             'views': [(res_id,'form')],
83             'target': 'new',
84             'context': context
85         }
86
87 open_questionnaire()
88 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
89