1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 from osv import fields
24 from tools.translate import _
27 VoteValues = [('-1', 'Not Voted'), ('0', 'Very Bad'), ('25', 'Bad'), \
28 ('50', 'Normal'), ('75', 'Good'), ('100', 'Very Good') ]
29 DefaultVoteValue = '50'
31 class idea_category(osv.osv):
32 """ Category of Idea """
34 _name = "idea.category"
35 _description = "Idea Category"
38 'name': fields.char('Category', size=64, required=True),
39 'summary': fields.text('Summary'),
40 'parent_id': fields.many2one('idea.category', 'Parent Categories', ondelete='set null'),
41 'child_ids': fields.one2many('idea.category', 'parent_id', 'Child Categories'),
42 'visibility':fields.boolean('Open Idea?', required=False, help="If True creator of the idea will be visible to others"),
45 ('name', 'unique(parent_id,name)', 'The name of the category must be unique' )
47 _order = 'parent_id,name asc'
51 class idea_idea(osv.osv):
56 def _vote_avg_compute(self, cr, uid, ids, name, arg, context=None):
58 """ compute average for voting
59 @param cr: the current row, from the database cursor,
60 @param uid: the current user’s ID for security checks,
61 @param ids: List of voting’s IDs
62 @return: dictionay of Idea """
67 sql = """SELECT i.id, avg(v.score::integer)
68 FROM idea_idea i LEFT OUTER JOIN idea_vote v ON i.id = v.idea_id
73 cr.execute(sql, (tuple(ids),))
74 return dict(cr.fetchall())
76 def _vote_count(self, cr, uid, ids, name, arg, context=None):
78 """ count number of vote
79 @param cr: the current row, from the database cursor,
80 @param uid: the current user’s ID for security checks,
81 @param ids: List of voting count’s IDs
82 @return: dictionay of Idea """
87 sql = """SELECT i.id, COUNT(1)
88 FROM idea_idea i LEFT OUTER JOIN idea_vote v ON i.id = v.idea_id
93 cr.execute(sql, (tuple(ids),))
94 return dict(cr.fetchall())
96 def _comment_count(self, cr, uid, ids, name, arg, context=None):
98 """ count number of comment
99 @param cr: the current row, from the database cursor,
100 @param uid: the current user’s ID for security checks,
101 @param ids: List of comment’s IDs
102 @return: dictionay of Idea """
107 sql = """SELECT i.id, COUNT(1)
108 FROM idea_idea i LEFT OUTER JOIN idea_comment c ON i.id = c.idea_id
113 cr.execute(sql, (tuple(ids),))
114 return dict(cr.fetchall())
116 def _vote_read(self, cr, uid, ids, name, arg, context=None):
119 @param cr: the current row, from the database cursor,
120 @param uid: the current user’s ID for security checks,
121 @param ids: List of vote read’s IDs """
126 vote_obj = self.pool.get('idea.vote')
127 votes_ids = vote_obj.search(cr, uid, [('idea_id', 'in', ids), ('user_id', '=', uid)])
128 vote_obj_id = vote_obj.browse(cr, uid, votes_ids, context=context)
130 for vote in vote_obj_id:
131 res[vote.idea_id.id] = vote.score
134 def _vote_save(self, cr, uid, id, field_name, field_value, arg, context=None):
137 @param cr: the current row, from the database cursor,
138 @param uid: the current user’s ID for security checks,
139 @param ids: List of vote save’s IDs """
141 vote_obj = self.pool.get('idea.vote')
142 vote = vote_obj.search(cr, uid, [('idea_id', '=', id), ('user_id', '=', uid)])
143 textual_value = str(field_value)
146 if int(field_value) >= 0:
147 vote_obj.write(cr, uid, vote, {'score': textual_value })
149 vote_obj.unlink(cr, uid, vote)
151 if int(field_value) >= 0:
152 vote_obj.create(cr, uid, {'idea_id': id, 'user_id': uid, 'score': textual_value })
155 'user_id': fields.many2one('res.users', 'Creator', required=True, readonly=True),
156 'name': fields.char('Idea Summary', size=64, required=True, readonly=True, oldname='title', states={'draft':[('readonly',False)]}),
157 'description': fields.text('Description', help='Content of the idea', readonly=True, states={'draft':[('readonly',False)]}),
158 'comment_ids': fields.one2many('idea.comment', 'idea_id', 'Comments'),
159 'created_date': fields.datetime('Creation date', readonly=True),
160 'open_date': fields.datetime('Open date', readonly=True, help="Date when an idea opened"),
161 'vote_ids': fields.one2many('idea.vote', 'idea_id', 'Vote'),
162 'my_vote': fields.function(_vote_read, fnct_inv = _vote_save, string="My Vote", method=True, type="selection", selection=VoteValues),
163 'vote_avg': fields.function(_vote_avg_compute, method=True, string="Average Score", type="float"),
164 'count_votes': fields.function(_vote_count, method=True, string="Count of votes", type="integer"),
165 'count_comments': fields.function(_comment_count, method=True, string="Count of comments", type="integer"),
166 'category_id': fields.many2one('idea.category', 'Category', required=True, readonly=True, states={'draft':[('readonly',False)]}),
167 'state': fields.selection([('draft', 'Draft'),
169 ('close', 'Accepted'),
170 ('cancel', 'Refused')],
171 'State', readonly=True,
172 help='When the Idea is created the state is \'Draft\'.\n It is \
173 opened by the user, the state is \'Opened\'.\
174 \nIf the idea is accepted, the state is \'Accepted\'.'
176 'visibility':fields.boolean('Open Idea?', required=False),
177 'stat_vote_ids': fields.one2many('idea.vote.stat', 'idea_id', 'Statistics', readonly=True),
178 'vote_limit': fields.integer('Maximum Vote per User',
179 help="Set to one if you require only one Vote per user"),
183 'user_id': lambda self,cr,uid,context: uid,
184 'my_vote': lambda *a: '-1',
185 'state': lambda *a: 'draft',
186 'vote_limit': lambda * a: 1,
187 'created_date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
188 'visibility': lambda *a: True,
192 def create(self, cr, user, vals, context=None):
194 Create a new record for a model idea_idea
195 @param cr: A database cursor
196 @param user: ID of the user currently logged in
197 @param vals: provides data for new record
198 @param context: context arguments, like lang, time zone
200 @return: Returns an id of the new record
204 if vals.get('category_id', False):
205 category_pool = self.pool.get('idea.category')
206 category = category_pool.browse(cr, user, vals.get('category_id'), context=context)
207 visibility = category.visibility
210 'visibility':visibility
213 res_id = super(idea_idea, self).create(cr, user, vals, context=context)
216 def copy(self, cr, uid, id, default={}, context=None):
218 Create the new record in idea_idea model from existing one
219 @param cr: A database cursor
220 @param user: ID of the user currently logged in
221 @param id: list of record ids on which copy method executes
222 @param default: dict type contains the values to be overridden during copy of object
223 @param context: context arguments, like lang, time zone
225 @return: Returns the id of the new record
231 'stat_vote_ids':False
234 res_id = super(idea_idea, self).copy(cr, uid, id, default, context=context)
237 def write(self, cr, user, ids, vals, context=None):
239 Update redord(s) exist in {ids}, with new value provided in {vals}
241 @param cr: A database cursor
242 @param user: ID of the user currently logged in
243 @param ids: list of record ids to update
244 @param vals: dict of new values to be set
245 @param context: context arguments, like lang, time zone
247 @return: Returns True on success, False otherwise
249 state = self.browse(cr, user, ids[0], context=context).state
251 if vals.get('my_vote', False):
252 if vals.get('state', state) != 'open':
253 raise osv.except_osv(_("Warning !"), _("Draft/Accepted/Cancelled ideas Could not be voted"))
255 res = super(idea_idea, self).write(cr, user, ids, vals, context=context)
258 def idea_cancel(self, cr, uid, ids):
259 self.write(cr, uid, ids, { 'state': 'cancel' })
262 def idea_open(self, cr, uid, ids):
263 self.write(cr, uid, ids, { 'state': 'open' ,'open_date': time.strftime('%Y-%m-%d %H:%M:%S')})
266 def idea_close(self, cr, uid, ids):
267 self.write(cr, uid, ids, { 'state': 'close' })
270 def idea_draft(self, cr, uid, ids):
271 self.write(cr, uid, ids, { 'state': 'draft' })
276 class idea_comment(osv.osv):
277 """ Apply Idea for Comment """
279 _name = 'idea.comment'
280 _description = 'Comment'
281 _rec_name = 'content'
284 'idea_id': fields.many2one('idea.idea', 'Idea', required=True, ondelete='cascade'),
285 'user_id': fields.many2one('res.users', 'User', required=True),
286 'content': fields.text('Comment', required=True),
287 'create_date': fields.datetime('Creation date', readonly=True),
291 'user_id': lambda self, cr, uid, context: uid
299 class idea_vote(osv.osv):
300 """ Apply Idea for Vote """
303 _description = 'Idea Vote'
307 'user_id': fields.many2one('res.users', 'User', readonly="True"),
308 'idea_id': fields.many2one('idea.idea', 'Idea', readonly="True", ondelete='cascade'),
309 'score': fields.selection(VoteValues, 'Vote Status', readonly="True"),
310 'date': fields.datetime('Date', readonly="True"),
311 'comment': fields.text('Comment', readonly="True"),
314 'score': lambda *a: DefaultVoteValue,
315 'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
320 class idea_vote_stat(osv.osv):
321 """ Idea votes Statistics """
323 _name = 'idea.vote.stat'
324 _description = 'Idea Votes Statistics'
326 _rec_name = 'idea_id'
329 'idea_id': fields.many2one('idea.idea', 'Idea', readonly=True),
330 'score': fields.selection(VoteValues, 'Score', readonly=True),
331 'nbr': fields.integer('Number of Votes', readonly=True),
336 initialize the sql view for the stats
341 CREATE OR REPLACE VIEW idea_vote_stat AS (
349 LEFT JOIN idea_idea i ON (v.idea_id = i.id)
351 i.id, v.score, i.id )
356 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: