1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
23 from osv import osv, fields
25 VoteValues = [('-1','Not Voted'),('0','Very Bad'),('25', 'Bad'),('50','Normal'),('75','Good'),('100','Very Good') ]
26 DefaultVoteValue = '50'
28 class idea_category(osv.osv):
29 _name = "idea.category"
30 _description = "Category for an idea"
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')
38 ('name', 'unique(parent_id,name)', 'The name of the category must be unique' )
40 _order = 'parent_id,name asc'
43 class idea_idea(osv.osv):
47 def _vote_avg_compute(self, cr, uid, ids, name, arg, context = None):
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
57 cr.execute(sql, (tuple(ids),))
58 return dict(cr.fetchall())
60 def _vote_count(self,cr,uid,ids,name,arg,context=None):
64 sql = """select i.id, count(1)
65 from idea_idea i left outer join idea_vote v on i.id = v.idea_id
70 cr.execute(sql, (tuple(ids),))
71 return dict(cr.fetchall())
73 def _comment_count(self,cr,uid,ids,name,arg,context=None):
77 sql = """select i.id, count(1)
78 from idea_idea i left outer join idea_comment c on i.id = c.idea_id
84 cr.execute(sql, (tuple(ids),))
85 return dict(cr.fetchall())
87 def _vote_read(self, cr, uid, ids, name, arg, context = None):
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
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)
102 if int(field_value)>=0:
103 vote_obj.write(cr,uid, vote, { 'score' : textual_value })
105 vote_obj.unlink(cr,uid, vote)
107 if int(field_value)>=0:
108 vote_obj.create(cr,uid, { 'idea_id' : id, 'user_id' : uid, 'score' : textual_value })
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),
128 'user_id': lambda self,cr,uid,context: uid,
129 'my_vote': lambda *a: '-1',
130 'state': lambda *a: 'draft'
134 def idea_cancel(self, cr, uid, ids):
135 self.write(cr, uid, ids, { 'state' : 'cancel' })
138 def idea_open(self, cr, uid, ids):
139 self.write(cr, uid, ids, { 'state' : 'open' })
142 def idea_close(self, cr, uid, ids):
143 self.write(cr, uid, ids, { 'state' : 'close' })
146 def idea_draft(self, cr, uid, ids):
147 self.write(cr, uid, ids, { 'state' : 'draft' })
151 class idea_comment(osv.osv):
152 _name = 'idea.comment'
153 _description = 'Comments'
154 _rec_name = 'content'
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),
162 'user_id': lambda self, cr, uid, context: uid
167 class idea_vote(osv.osv):
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)
176 'score': lambda *a: DefaultVoteValue,
180 class idea_vote_stat(osv.osv):
181 _name = 'idea.vote.stat'
182 _description = 'Idea Votes Statistics'
184 _rec_name = 'idea_id'
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),
192 initialize the sql view for the stats
197 create or replace view idea_vote_stat as (
206 idea_idea i on (v.idea_id=i.id)
212 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: