1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-Today OpenERP S.A. (<http://openerp.com>).
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 ##############################################################################
22 from openerp.osv import osv
23 from openerp.osv import fields
26 class IdeaCategory(osv.Model):
27 """ Category of Idea """
28 _name = "idea.category"
29 _description = "Idea Category"
34 'name': fields.char('Category Name', size=64, required=True),
38 ('name', 'unique(name)', 'The name of the category must be unique')
42 class IdeaIdea(osv.Model):
43 """ Model of an Idea """
45 _description = 'Propose and Share your Ideas'
50 def _get_state_list(self, cr, uid, context=None):
51 return [('draft', 'New'),
52 ('open', 'In discussion'),
53 ('close', 'Accepted'),
54 ('cancel', 'Refused')]
56 def _get_color(self, cr, uid, ids, fields, args, context=None):
57 res = dict.fromkeys(ids, 3)
58 for idea in self.browse(cr, uid, ids, context=context):
59 if idea.priority == 'low':
61 elif idea.priority == 'high':
66 'user_id': fields.many2one('res.users', 'Responsible', required=True),
67 'name': fields.char('Summary', required=True, readonly=True,
68 states={'draft': [('readonly', False)]},
70 'description': fields.text('Description', required=True,
71 states={'draft': [('readonly', False)]},
72 help='Content of the idea'),
73 'category_ids': fields.many2many('idea.category', string='Tags'),
74 'state': fields.selection(_get_state_list, string='Status', required=True),
75 'priority': fields.selection([('low', 'Low'), ('normal', 'Normal'), ('high', 'High')],
76 string='Priority', required=True),
77 'color': fields.function(_get_color, type='integer', string='Color Index'),
81 ('name', 'unique(name)', 'The name of the idea must be unique')
85 'user_id': lambda self, cr, uid, ctx=None: uid,
86 'state': lambda self, cr, uid, ctx=None: self._get_state_list(cr, uid, ctx)[0][0],
90 #------------------------------------------------------
92 #------------------------------------------------------
94 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
95 """ Override read_group to always display all states. """
96 if groupby and groupby[0] == "state":
97 # Default result structure
98 states = self._get_state_list(cr, uid, context=context)
99 read_group_all_states = [{
100 '__context': {'group_by': groupby[1:]},
101 '__domain': domain + [('state', '=', state_value)],
102 'state': state_value,
104 } for state_value, state_name in states]
105 # Get standard results
106 read_group_res = super(IdeaIdea, self).read_group(cr, uid, domain, fields, groupby, offset, limit, context, orderby, lazy)
107 # Update standard results with default results
109 for state_value, state_name in states:
110 res = filter(lambda x: x['state'] == state_value, read_group_res)
112 res = filter(lambda x: x['state'] == state_value, read_group_all_states)
113 res[0]['state'] = [state_value, state_name]
114 result.append(res[0])
117 return super(IdeaIdea, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby, lazy=lazy)
119 #------------------------------------------------------
121 #------------------------------------------------------
123 def idea_set_low_priority(self, cr, uid, ids, context=None):
124 return self.write(cr, uid, ids, {'priority': 'low'}, context=context)
126 def idea_set_normal_priority(self, cr, uid, ids, context={}):
127 return self.write(cr, uid, ids, {'priority': 'normal'}, context=context)
129 def idea_set_high_priority(self, cr, uid, ids, context={}):
130 return self.write(cr, uid, ids, {'priority': 'high'}, context=context)