[IMP] Changed all module categories, limited number of categories
[odoo/odoo.git] / addons / idea / idea.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import osv
23 from osv import fields
24 from tools.translate import _
25 import time
26
27 VoteValues = [('-1', 'Not Voted'), ('0', 'Very Bad'), ('25', 'Bad'), \
28               ('50', 'Normal'), ('75', 'Good'), ('100', 'Very Good') ]
29 DefaultVoteValue = '50'
30
31 class idea_category(osv.osv):
32     """ Category of Idea """
33
34     _name = "idea.category"
35     _description = "Idea Category"
36
37     _columns = {
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"),
43     }
44     _sql_constraints = [
45         ('name', 'unique(parent_id,name)', 'The name of the category must be unique' )
46     ]
47     _order = 'parent_id,name asc'
48
49 idea_category()
50
51 class idea_idea(osv.osv):
52     """ Idea """
53     _name = 'idea.idea'
54     _rec_name = 'name'
55
56     def _vote_avg_compute(self, cr, uid, ids, name, arg, context=None):
57
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 """
63
64         if not ids:
65             return {}
66
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
69             WHERE i.id IN %s
70             GROUP BY i.id
71         """
72
73         cr.execute(sql, (tuple(ids),))
74         return dict(cr.fetchall())
75
76     def _vote_count(self, cr, uid, ids, name, arg, context=None):
77
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 """
83
84         if not ids:
85             return {}
86
87         sql = """SELECT i.id, COUNT(1)
88            FROM idea_idea i LEFT OUTER JOIN idea_vote v ON i.id = v.idea_id
89             WHERE i.id IN %s
90             GROUP BY i.id
91         """
92
93         cr.execute(sql, (tuple(ids),))
94         return dict(cr.fetchall())
95
96     def _comment_count(self, cr, uid, ids, name, arg, context=None):
97
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 """
103
104         if not ids:
105             return {}
106
107         sql = """SELECT i.id, COUNT(1)
108            FROM idea_idea i LEFT OUTER JOIN idea_comment c ON i.id = c.idea_id
109             WHERE i.id IN %s
110             GROUP BY i.id
111         """
112
113         cr.execute(sql, (tuple(ids),))
114         return dict(cr.fetchall())
115
116     def _vote_read(self, cr, uid, ids, name, arg, context=None):
117
118         """ Read Vote
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 """
122
123         res = {}
124         for id in ids:
125             res[id] = '-1'
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)
129
130         for vote in vote_obj_id:
131             res[vote.idea_id.id] = vote.score
132         return res
133
134     def _vote_save(self, cr, uid, id, field_name, field_value, arg, context=None):
135
136         """ save Vote
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 """
140
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)
144
145         if vote:
146             if int(field_value) >= 0:
147                 vote_obj.write(cr, uid, vote, {'score': textual_value })
148             else:
149                 vote_obj.unlink(cr, uid, vote)
150         else:
151             if int(field_value) >= 0:
152                 vote_obj.create(cr, uid, {'idea_id': id, 'user_id': uid, 'score': textual_value })
153
154     _columns = {
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'),
168             ('open', 'Opened'),
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\'.'
175         ),
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"),
180     }
181
182     _defaults = {
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,
189     }
190     _order = 'id desc'
191
192     def create(self, cr, user, vals, context=None):
193         """
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
199
200         @return: Returns an id of the new record
201         """
202         visibility = False
203
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
208
209         vals.update({
210             'visibility':visibility
211         })
212
213         res_id = super(idea_idea, self).create(cr, user, vals, context=context)
214         return res_id
215
216     def copy(self, cr, uid, id, default={}, context=None):
217         """
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
224
225         @return: Returns the id of the new record
226         """
227
228         default.update({
229             'comment_ids':False,
230             'vote_ids':False,
231             'stat_vote_ids':False
232
233         })
234         res_id = super(idea_idea, self).copy(cr, uid, id, default, context=context)
235         return res_id
236
237     def write(self, cr, user, ids, vals, context=None):
238         """
239         Update redord(s) exist in {ids}, with new value provided in {vals}
240
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
246
247         @return: Returns True on success, False otherwise
248         """
249         state = self.browse(cr, user, ids[0], context=context).state
250
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"))
254
255         res = super(idea_idea, self).write(cr, user, ids, vals, context=context)
256         return res
257
258     def idea_cancel(self, cr, uid, ids):
259         self.write(cr, uid, ids, { 'state': 'cancel' })
260         return True
261
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')})
264         return True
265
266     def idea_close(self, cr, uid, ids):
267         self.write(cr, uid, ids, { 'state': 'close' })
268         return True
269
270     def idea_draft(self, cr, uid, ids):
271         self.write(cr, uid, ids, { 'state': 'draft' })
272         return True
273 idea_idea()
274
275
276 class idea_comment(osv.osv):
277     """ Apply Idea for Comment """
278
279     _name = 'idea.comment'
280     _description = 'Comment'
281     _rec_name = 'content'
282
283     _columns = {
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),
288     }
289
290     _defaults = {
291         'user_id': lambda self, cr, uid, context: uid
292     }
293
294     _order = 'id desc'
295
296 idea_comment()
297
298
299 class idea_vote(osv.osv):
300     """ Apply Idea for Vote """
301
302     _name = 'idea.vote'
303     _description = 'Idea Vote'
304     _rec_name = 'score'
305
306     _columns = {
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"),
312     }
313     _defaults = {
314         'score': lambda *a: DefaultVoteValue,
315         'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
316     }
317
318 idea_vote()
319
320 class idea_vote_stat(osv.osv):
321     """ Idea votes Statistics """
322
323     _name = 'idea.vote.stat'
324     _description = 'Idea Votes Statistics'
325     _auto = False
326     _rec_name = 'idea_id'
327
328     _columns = {
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),
332     }
333
334     def init(self, cr):
335         """
336         initialize the sql view for the stats
337
338         cr -- the cursor
339         """
340         cr.execute("""
341             CREATE OR REPLACE VIEW idea_vote_stat AS (
342                 SELECT
343                     MIN(v.id) AS id,
344                     i.id AS idea_id,
345                     v.score,
346                     COUNT(1) AS nbr
347                 FROM
348                     idea_vote v
349                     LEFT JOIN idea_idea i ON (v.idea_id = i.id)
350                 GROUP BY
351                     i.id, v.score, i.id )
352         """)
353
354 idea_vote_stat()
355
356 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
357