1
from osv import fields,osv
6
def test_prof(self, cr, uid, prof_id, pid, answers_ids):
7
#return True if the partner pid fetch the profile rule prof_id
8
ids_to_check = pooler.get_pool(cr.dbname).get('segmentation.profile').get_parents(cr, uid, [prof_id])
9
[yes_answers, no_answers] = pooler.get_pool(cr.dbname).get('segmentation.profile').get_answers(cr, uid, ids_to_check)
11
for y_ans in yes_answers:
12
if y_ans not in answers_ids:
16
for ans in answers_ids:
25
def _recompute_profiles(self, cr, uid, pid, answers_ids):
30
from segmentation_profile
33
for prof_id in cr.fetchall():
34
if test_prof(self, cr, uid, prof_id[0], pid, answers_ids):
38
class question(osv.osv):
39
_name="segmentation.question"
40
_description= "Question"
42
'name': fields.char("Question",size=128, required=True),
43
'answers_ids': fields.one2many("segmentation.answer","question_id","Avalaible answers",),
48
class answer(osv.osv):
49
_name="segmentation.answer"
52
"name": fields.char("Answer",size=128, required=True),
53
"question_id": fields.many2one('segmentation.question',"Question"),
58
class questionnaire(osv.osv):
59
_name="segmentation.questionnaire"
60
_description= "Questionnaire"
62
'name': fields.char("Questionnaire",size=128, required=True),
63
'description':fields.text("Description", required=True),
64
'questions_ids': fields.many2many('segmentation.question','profile_questionnaire_quest_rel','questionnaire', 'question', "Questions"),
69
class profile(osv.osv):
71
def get_answers(self, cr, uid, ids):
73
select distinct(answer)
74
from profile_question_yes_rel
75
where profile in (%s)"""% ','.join([str(i) for i in ids ])
78
ans_yes = [x[0] for x in cr.fetchall()]
81
select distinct(answer)
82
from profile_question_no_rel
83
where profile in (%s)"""% ','.join([str(i) for i in ids ])
86
ans_no = [x[0] for x in cr.fetchall()]
88
return [ans_yes, ans_no]
90
def get_parents(self, cr, uid, ids):
93
select distinct(parent_id)
94
from segmentation_profile
95
where parent_id is not null
96
and id in (%s)""" % ','.join([str(i) for i in ids ]))
98
parent_ids = [x[0] for x in cr.fetchall()]
102
if x not in ids_to_check:
103
ids_to_check.append(x)
107
ids_to_check = pooler.get_pool(cr.dbname).get('segmentation.profile').get_parents(cr, uid, ids_to_check)
111
def process_continue(self, cr, uid, ids, state=False):
112
cr.execute('delete from partner_profile_rel where profile_id=%d', (ids[0],))
114
cr.execute('select id from res_partner order by id ')
115
partners = [x[0] for x in cr.fetchall()]
119
cr.execute('select distinct(answer) from partner_question_rel where partner=%d' % pid)
120
answers_ids = [x[0] for x in cr.fetchall()]
121
if (not test_prof(self, cr, uid, ids[0], pid, answers_ids)):
122
to_remove_list.append(pid)
124
for pid in to_remove_list:
127
for partner_id in partners:
128
cr.execute('insert into partner_profile_rel (profile_id,partner_id) values (%d,%d)', (ids[0],partner_id))
134
_name="segmentation.profile"
135
_description="Profile"
137
"name": fields.char("Description",size=128, required=True),
138
"answer_yes": fields.many2many("segmentation.answer","profile_question_yes_rel","profile","answer","Inclued Answers"),
139
"answer_no": fields.many2many("segmentation.answer","profile_question_no_rel","profile","answer","Excluded Answers"),
140
'parent_id': fields.many2one('segmentation.profile', 'Parent Profile'),
141
'child_ids': fields.one2many('segmentation.profile', 'parent_id', 'Childs Profile'),
144
(orm.orm.check_recursion, 'Error ! You can not create recursive profiles.', ['parent_id'])
149
class partner(osv.osv):
151
def write(self, cr, uid, ids, vals, context=None):
154
if 'answers_ids' in vals:
155
vals['profiles_ids']=[[6, 0, _recompute_profiles(self, cr, uid, ids[0],vals['answers_ids'][0][2])]]
156
return super(partner, self).write(cr, uid, ids, vals, context=context)
158
_inherit="res.partner"
160
"answers_ids": fields.many2many("segmentation.answer","partner_question_rel","partner","answer","Answers"),
161
"profiles_ids":fields.many2many("segmentation.profile","partner_profile_rel","partner_id","profile_id","Matching Profiles", readonly=True, select="2"),