~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to segmentation/segmentation.py

  • Committer: nel
  • Date: 2007-10-01 05:36:04 UTC
  • Revision ID: nel-96aa6366eff3c935db4b610739abba94e5d985f0
ref taken off

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from osv import fields,osv
2
 
from osv import orm
3
 
import pooler
4
 
 
5
 
 
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)
10
 
        temp = True
11
 
        for y_ans in yes_answers:
12
 
                if y_ans not in answers_ids:
13
 
                        temp = False
14
 
                        break
15
 
        if temp:
16
 
                for ans in answers_ids:
17
 
                        if ans in no_answers:
18
 
                                temp = False
19
 
                                break
20
 
        if temp:
21
 
                return True
22
 
        return False
23
 
 
24
 
 
25
 
def _recompute_profiles(self, cr, uid, pid, answers_ids):
26
 
        ok =  []
27
 
 
28
 
        cr.execute('''
29
 
                select id 
30
 
                from segmentation_profile
31
 
                order by id''')
32
 
 
33
 
        for prof_id in cr.fetchall():
34
 
                if test_prof(self, cr, uid, prof_id[0], pid, answers_ids):
35
 
                        ok.append(prof_id[0])
36
 
        return ok
37
 
 
38
 
class question(osv.osv):
39
 
        _name="segmentation.question"
40
 
        _description= "Question"
41
 
        _columns={
42
 
                'name': fields.char("Question",size=128, required=True),
43
 
                'answers_ids': fields.one2many("segmentation.answer","question_id","Avalaible answers",),
44
 
                }
45
 
question()
46
 
 
47
 
 
48
 
class answer(osv.osv):
49
 
        _name="segmentation.answer"
50
 
        _description="Answer"
51
 
        _columns={
52
 
                "name": fields.char("Answer",size=128, required=True),
53
 
                "question_id": fields.many2one('segmentation.question',"Question"),
54
 
                }
55
 
answer()
56
 
 
57
 
 
58
 
class questionnaire(osv.osv):
59
 
        _name="segmentation.questionnaire"
60
 
        _description= "Questionnaire"
61
 
        _columns={
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"),
65
 
                }
66
 
questionnaire()
67
 
 
68
 
 
69
 
class profile(osv.osv):
70
 
 
71
 
        def get_answers(self, cr, uid, ids):
72
 
                query = """
73
 
                select distinct(answer)
74
 
                from profile_question_yes_rel
75
 
                where profile in (%s)"""% ','.join([str(i) for i in ids ])
76
 
 
77
 
                cr.execute(query)
78
 
                ans_yes = [x[0] for x in cr.fetchall()]
79
 
 
80
 
                query = """
81
 
                select distinct(answer)
82
 
                from profile_question_no_rel
83
 
                where profile in (%s)"""% ','.join([str(i) for i in ids ])
84
 
 
85
 
                cr.execute(query)
86
 
                ans_no = [x[0] for x in cr.fetchall()]
87
 
 
88
 
                return [ans_yes, ans_no]
89
 
 
90
 
        def get_parents(self, cr, uid, ids):
91
 
                ids_to_check = ids
92
 
                cr.execute("""
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 ]))
97
 
 
98
 
                parent_ids = [x[0] for x in cr.fetchall()]
99
 
 
100
 
                trigger = False
101
 
                for x in parent_ids:
102
 
                        if x not in ids_to_check:
103
 
                                ids_to_check.append(x)
104
 
                                trigger = True
105
 
 
106
 
                if trigger:
107
 
                        ids_to_check = pooler.get_pool(cr.dbname).get('segmentation.profile').get_parents(cr, uid, ids_to_check)
108
 
 
109
 
                return ids_to_check
110
 
 
111
 
        def process_continue(self, cr, uid, ids, state=False):
112
 
                cr.execute('delete from partner_profile_rel where profile_id=%d', (ids[0],))
113
 
 
114
 
                cr.execute('select id from res_partner order by id ')
115
 
                partners = [x[0] for x in cr.fetchall()]
116
 
                to_remove_list=[]
117
 
                for pid in partners:
118
 
 
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)
123
 
 
124
 
                for pid in to_remove_list:
125
 
                        partners.remove(pid)
126
 
 
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))
129
 
                cr.commit()
130
 
 
131
 
                cr.commit()
132
 
                return True
133
 
 
134
 
        _name="segmentation.profile"
135
 
        _description="Profile"
136
 
        _columns={
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'),
142
 
                }
143
 
        _constraints = [
144
 
                (orm.orm.check_recursion, 'Error ! You can not create recursive profiles.', ['parent_id'])
145
 
        ]
146
 
profile()
147
 
 
148
 
 
149
 
class partner(osv.osv):
150
 
 
151
 
        def write(self, cr, uid, ids, vals, context=None):
152
 
                if not context:
153
 
                        context={}
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)
157
 
 
158
 
        _inherit="res.partner"
159
 
        _columns={
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"),
162
 
                }
163
 
partner()