[imp] idea: simplify code
[odoo/odoo.git] / addons / idea / idea.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 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 ##############################################################################
22
23 from osv import osv, fields
24
25 VoteValues = [('-1','Not Voted'),('0','Very Bad'),('25', 'Bad'),('50','Normal'),('75','Good'),('100','Very Good') ]
26 DefaultVoteValue = '50'
27
28 class idea_category(osv.osv):
29     _name = "idea.category"
30     _description = "Category for an idea"
31     _columns = {
32         'name': fields.char('Category', size=64, required=True),
33         'summary': fields.text('Summary'),
34         'parent_id': fields.many2one('idea.category','Parent Categories', ondelete='set null'),
35         'child_ids': fields.one2many('idea.category','parent_id','Child Categories')
36     }
37     _sql_constraints = [
38         ('name', 'unique(parent_id,name)', 'The name of the category must be unique' )
39     ]
40     _order = 'parent_id,name asc'
41 idea_category()
42
43 class idea_idea(osv.osv):
44     _name = 'idea.idea'
45     _rec_name = 'title'
46
47     def _vote_avg_compute(self, cr, uid, ids, name, arg, context = None):
48         if not len(ids):
49             return {}
50
51         sql = """select i.id, avg(v.score::integer)
52                    from idea_idea i left outer join idea_vote v on i.id = v.idea_id
53                     where i.id in %s
54                     group by i.id
55                 """
56
57         cr.execute(sql, (tuple(ids),))
58         return dict(cr.fetchall())
59
60     def _vote_count(self,cr,uid,ids,name,arg,context=None):
61         if not len(ids):
62             return {}
63
64         sql = """select i.id, count(1)
65                    from idea_idea i left outer join idea_vote v on i.id = v.idea_id
66                     where i.id in %s
67                     group by i.id
68                 """
69
70         cr.execute(sql, (tuple(ids),))
71         return dict(cr.fetchall())
72
73     def _comment_count(self,cr,uid,ids,name,arg,context=None):
74         if not len(ids):
75             return {}
76
77         sql = """select i.id, count(1)
78                    from idea_idea i left outer join idea_comment c on i.id = c.idea_id
79                     where i.id in %s
80                     group by i.id
81                 """
82
83
84         cr.execute(sql, (tuple(ids),))
85         return dict(cr.fetchall())
86
87     def _vote_read(self, cr, uid, ids, name, arg, context = None):
88         res = {}
89         for id in ids:
90             res[id] = '-1'
91         vote_obj = self.pool.get('idea.vote')
92         votes_ids = vote_obj.search(cr, uid, [('idea_id', 'in', ids), ('user_id', '=', uid)])
93         for vote in vote_obj.browse(cr, uid, votes_ids, context):
94             res[vote.idea_id.id] = vote.score
95         return res
96
97     def _vote_save(self, cr, uid, id, field_name, field_value, arg, context = None):
98         vote_obj = self.pool.get('idea.vote')
99         vote = vote_obj.search(cr,uid,[('idea_id', '=', id),('user_id', '=', uid)])
100         textual_value = str(field_value)
101         if vote:
102             if int(field_value)>=0:
103                 vote_obj.write(cr,uid, vote, { 'score' : textual_value })
104             else:
105                 vote_obj.unlink(cr,uid, vote)
106         else:
107             if int(field_value)>=0:
108                 vote_obj.create(cr,uid, { 'idea_id' : id, 'user_id' : uid, 'score' : textual_value })
109
110
111     _columns = {
112         'user_id': fields.many2one('res.users', 'Creator', required=True, readonly=True),
113         'title': fields.char('Idea Summary', size=64, required=True),
114         'description': fields.text('Description', required=True, help='Content of the idea'),
115         'comment_ids': fields.one2many('idea.comment', 'idea_id', 'Comments'),
116         'create_date' : fields.datetime( 'Creation date', readonly=True),
117         'vote_ids' : fields.one2many('idea.vote', 'idea_id', 'Vote'),
118         'my_vote' : fields.function(_vote_read, fnct_inv = _vote_save, string="My Vote", method=True, type="selection", selection=VoteValues),
119         'vote_avg' : fields.function(_vote_avg_compute, method=True, string="Average Score", type="float"),
120         'count_votes' : fields.function(_vote_count, method=True, string="Count of votes", type="integer"),
121         'count_comments': fields.function(_comment_count, method=True, string="Count of comments", type="integer"),
122         'category_id': fields.many2one('idea.category', 'Category', required=True ),
123         'state': fields.selection([('draft','Draft'),('open','Opened'),('close','Accepted'),('cancel','Canceled')], 'Status', readonly=True),
124         'stat_vote_ids': fields.one2many('idea.vote.stat', 'idea_id', 'Statistics', readonly=True),
125     }
126
127     _defaults = {
128         'user_id': lambda self,cr,uid,context: uid,
129         'my_vote': lambda *a: '-1', 
130         'state': lambda *a: 'draft'
131     }
132     _order = 'id desc'
133
134     def idea_cancel(self, cr, uid, ids):
135         self.write(cr, uid, ids, { 'state' : 'cancel' })
136         return True
137
138     def idea_open(self, cr, uid, ids):
139         self.write(cr, uid, ids, { 'state' : 'open' })
140         return True
141
142     def idea_close(self, cr, uid, ids):
143         self.write(cr, uid, ids, { 'state' : 'close' })
144         return True
145
146     def idea_draft(self, cr, uid, ids):
147         self.write(cr, uid, ids, { 'state' : 'draft' })
148         return True
149 idea_idea()
150
151 class idea_comment(osv.osv):
152     _name = 'idea.comment'
153     _description = 'Comments'
154     _rec_name = 'content'
155     _columns = {
156         'idea_id': fields.many2one('idea.idea', 'Idea', required=True, ondelete='cascade' ),
157         'user_id': fields.many2one('res.users', 'User', required=True ),
158         'content': fields.text( 'Comment', required=True ),
159         'create_date' : fields.datetime( 'Creation date', readonly=True),
160     }
161     _defaults = {
162         'user_id': lambda self, cr, uid, context: uid
163     }
164     _order = 'id desc'
165 idea_comment()
166
167 class idea_vote(osv.osv):
168     _name = 'idea.vote'
169     _rec_name = 'score'
170     _columns = {
171         'user_id': fields.many2one( 'res.users', 'User'),
172         'idea_id': fields.many2one('idea.idea', 'Idea', required=True, ondelete='cascade'),
173         'score': fields.selection( VoteValues, 'Score', required=True)
174     }
175     _defaults = {
176         'score': lambda *a: DefaultVoteValue,
177     }
178 idea_vote()
179
180 class idea_vote_stat(osv.osv):
181     _name = 'idea.vote.stat'
182     _description = 'Idea Votes Statistics'
183     _auto = False
184     _rec_name = 'idea_id'
185     _columns = {
186         'idea_id': fields.many2one('idea.idea', 'Idea', readonly=True),
187         'score': fields.selection( VoteValues, 'Score', readonly=True),
188         'nbr': fields.integer('Number of Votes', readonly=True),
189     }
190     def init(self, cr):
191         """
192         initialize the sql view for the stats
193         
194         cr -- the cursor
195         """
196         cr.execute("""
197             create or replace view idea_vote_stat as (
198                 select
199                     min(v.id) as id,
200                     i.id as idea_id,
201                     v.score,
202                     count(1) as nbr
203                 from
204                     idea_vote v
205                     left join 
206                     idea_idea i on (v.idea_id=i.id)
207                 group by
208                     i.id, v.score, i.id
209         )""")
210 idea_vote_stat()
211
212 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
213