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

« back to all changes in this revision

Viewing changes to segmentation/segmentation.py

  • Committer: Fabien Pinckaers
  • Date: 2008-11-12 06:43:12 UTC
  • Revision ID: fp@tinyerp.com-20081112064312-fp85io97i1e95tuz
moved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
1
22
from osv import fields,osv
2
23
from osv import orm
3
24
import pooler
5
26
 
6
27
def test_prof(self, cr, uid, prof_id, pid, answers_ids):
7
28
#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
 
29
    ids_to_check = pooler.get_pool(cr.dbname).get('segmentation.profile').get_parents(cr, uid, [prof_id])
 
30
    [yes_answers, no_answers] = pooler.get_pool(cr.dbname).get('segmentation.profile').get_answers(cr, uid, ids_to_check)
 
31
    temp = True
 
32
    for y_ans in yes_answers:
 
33
        if y_ans not in answers_ids:
 
34
            temp = False
 
35
            break
 
36
    if temp:
 
37
        for ans in answers_ids:
 
38
            if ans in no_answers:
 
39
                temp = False
 
40
                break
 
41
    if temp:
 
42
        return True
 
43
    return False
23
44
 
24
45
 
25
46
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
 
47
    ok =  []
 
48
 
 
49
    cr.execute('''
 
50
        select id 
 
51
        from segmentation_profile
 
52
        order by id''')
 
53
 
 
54
    for prof_id in cr.fetchall():
 
55
        if test_prof(self, cr, uid, prof_id[0], pid, answers_ids):
 
56
            ok.append(prof_id[0])
 
57
    return ok
37
58
 
38
59
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
 
                }
 
60
    _name="segmentation.question"
 
61
    _description= "Question"
 
62
    _columns={
 
63
        'name': fields.char("Question",size=128, required=True),
 
64
        'answers_ids': fields.one2many("segmentation.answer","question_id","Avalaible answers",),
 
65
        }
45
66
question()
46
67
 
47
68
 
48
69
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
 
                }
 
70
    _name="segmentation.answer"
 
71
    _description="Answer"
 
72
    _columns={
 
73
        "name": fields.char("Answer",size=128, required=True),
 
74
        "question_id": fields.many2one('segmentation.question',"Question"),
 
75
        }
55
76
answer()
56
77
 
57
78
 
58
79
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
 
                }
 
80
    _name="segmentation.questionnaire"
 
81
    _description= "Questionnaire"
 
82
    _columns={
 
83
        'name': fields.char("Questionnaire",size=128, required=True),
 
84
        'description':fields.text("Description", required=True),
 
85
        'questions_ids': fields.many2many('segmentation.question','profile_questionnaire_quest_rel','questionnaire', 'question', "Questions"),
 
86
        }
66
87
questionnaire()
67
88
 
68
89
 
69
90
class profile(osv.osv):
70
91
 
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
 
        ]
 
92
    def get_answers(self, cr, uid, ids):
 
93
        query = """
 
94
        select distinct(answer)
 
95
        from profile_question_yes_rel
 
96
        where profile in (%s)"""% ','.join([str(i) for i in ids ])
 
97
 
 
98
        cr.execute(query)
 
99
        ans_yes = [x[0] for x in cr.fetchall()]
 
100
 
 
101
        query = """
 
102
        select distinct(answer)
 
103
        from profile_question_no_rel
 
104
        where profile in (%s)"""% ','.join([str(i) for i in ids ])
 
105
 
 
106
        cr.execute(query)
 
107
        ans_no = [x[0] for x in cr.fetchall()]
 
108
 
 
109
        return [ans_yes, ans_no]
 
110
 
 
111
    def get_parents(self, cr, uid, ids):
 
112
        ids_to_check = ids
 
113
        cr.execute("""
 
114
         select distinct(parent_id)
 
115
         from segmentation_profile
 
116
         where parent_id is not null
 
117
         and id in (%s)""" % ','.join([str(i) for i in ids ]))
 
118
 
 
119
        parent_ids = [x[0] for x in cr.fetchall()]
 
120
 
 
121
        trigger = False
 
122
        for x in parent_ids:
 
123
            if x not in ids_to_check:
 
124
                ids_to_check.append(x)
 
125
                trigger = True
 
126
 
 
127
        if trigger:
 
128
            ids_to_check = pooler.get_pool(cr.dbname).get('segmentation.profile').get_parents(cr, uid, ids_to_check)
 
129
 
 
130
        return ids_to_check
 
131
 
 
132
    def process_continue(self, cr, uid, ids, state=False):
 
133
        cr.execute('delete from partner_profile_rel where profile_id=%d', (ids[0],))
 
134
 
 
135
        cr.execute('select id from res_partner order by id ')
 
136
        partners = [x[0] for x in cr.fetchall()]
 
137
        to_remove_list=[]
 
138
        for pid in partners:
 
139
 
 
140
            cr.execute('select distinct(answer) from partner_question_rel where partner=%d' % pid)
 
141
            answers_ids = [x[0] for x in cr.fetchall()]
 
142
            if (not test_prof(self, cr, uid, ids[0], pid, answers_ids)):
 
143
                to_remove_list.append(pid)
 
144
 
 
145
        for pid in to_remove_list:
 
146
            partners.remove(pid)
 
147
 
 
148
        for partner_id in partners:
 
149
            cr.execute('insert into partner_profile_rel (profile_id,partner_id) values (%d,%d)', (ids[0],partner_id))
 
150
        cr.commit()
 
151
 
 
152
        cr.commit()
 
153
        return True
 
154
 
 
155
    _name="segmentation.profile"
 
156
    _description="Profile"
 
157
    _columns={
 
158
        "name": fields.char("Description",size=128, required=True),
 
159
        "answer_yes": fields.many2many("segmentation.answer","profile_question_yes_rel","profile","answer","Inclued Answers"),
 
160
        "answer_no": fields.many2many("segmentation.answer","profile_question_no_rel","profile","answer","Excluded Answers"),
 
161
        'parent_id': fields.many2one('segmentation.profile', 'Parent Profile'),
 
162
        'child_ids': fields.one2many('segmentation.profile', 'parent_id', 'Childs Profile'),
 
163
        }
 
164
    _constraints = [
 
165
        (orm.orm.check_recursion, 'Error ! You can not create recursive profiles.', ['parent_id'])
 
166
    ]
146
167
profile()
147
168
 
148
169
 
149
170
class partner(osv.osv):
150
171
 
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)
 
172
    def write(self, cr, uid, ids, vals, context=None):
 
173
        if not context:
 
174
            context={}
 
175
        if 'answers_ids' in vals:
 
176
            vals['profiles_ids']=[[6, 0, _recompute_profiles(self, cr, uid, ids[0],vals['answers_ids'][0][2])]]
 
177
        return super(partner, self).write(cr, uid, ids, vals, context=context)
157
178
 
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
 
                }
 
179
    _inherit="res.partner"
 
180
    _columns={
 
181
        "answers_ids": fields.many2many("segmentation.answer","partner_question_rel","partner","answer","Answers"),
 
182
        "profiles_ids":fields.many2many("segmentation.profile","partner_profile_rel","partner_id","profile_id","Matching Profiles", readonly=True, select="2"),
 
183
        }
163
184
partner()
 
185
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
186