[IMP] barcodes: use builtin python sets when it makes sense
[odoo/odoo.git] / addons / note / note.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 openerp import SUPERUSER_ID
23 from openerp.osv import osv, fields
24 from openerp.tools import html2plaintext
25
26 class note_stage(osv.osv):
27     """ Category of Note """
28     _name = "note.stage"
29     _description = "Note Stage"
30     _columns = {
31         'name': fields.char('Stage Name', translate=True, required=True),
32         'sequence': fields.integer('Sequence', help="Used to order the note stages"),
33         'user_id': fields.many2one('res.users', 'Owner', help="Owner of the note stage.", required=True, ondelete='cascade'),
34         'fold': fields.boolean('Folded by Default'),
35     }
36     _order = 'sequence asc'
37     _defaults = {
38         'fold': 0,
39         'user_id': lambda self, cr, uid, ctx: uid,
40         'sequence' : 1,
41     }
42
43 class note_tag(osv.osv):
44     _name = "note.tag"
45     _description = "Note Tag"
46     _columns = {
47         'name' : fields.char('Tag Name', required=True),
48     }
49
50 class note_note(osv.osv):
51     """ Note """
52     _name = 'note.note'
53     _inherit = ['mail.thread']
54     _description = "Note"
55
56     #writing method (no modification of values)
57     def name_create(self, cr, uid, name, context=None):
58         rec_id = self.create(cr, uid, {'memo': name}, context=context)
59         return self.name_get(cr, uid, [rec_id], context)[0]
60
61     #read the first line (convert hml into text)
62     def _get_note_first_line(self, cr, uid, ids, name="", args={}, context=None):
63         res = {}
64         for note in self.browse(cr, uid, ids, context=context):
65             res[note.id] = (note.memo and html2plaintext(note.memo) or "").strip().replace('*','').split("\n")[0]
66
67         return res
68
69     def onclick_note_is_done(self, cr, uid, ids, context=None):
70         return self.write(cr, uid, ids, {'open': False, 'date_done': fields.date.today()}, context=context)
71
72     def onclick_note_not_done(self, cr, uid, ids, context=None):
73         return self.write(cr, uid, ids, {'open': True}, context=context)
74
75     #return the default stage for the uid user
76     def _get_default_stage_id(self,cr,uid,context=None):
77         ids = self.pool.get('note.stage').search(cr,uid,[('user_id','=',uid)], context=context)
78         return ids and ids[0] or False
79
80     def _set_stage_per_user(self, cr, uid, id, name, value, args=None, context=None):
81         note = self.browse(cr, uid, id, context=context)
82         if not value: return False
83         stage_ids = [value] + [stage.id for stage in note.stage_ids if stage.user_id.id != uid ]
84         return self.write(cr, uid, [id], {'stage_ids': [(6, 0, set(stage_ids))]}, context=context)
85
86     def _get_stage_per_user(self, cr, uid, ids, name, args, context=None):
87         result = dict.fromkeys(ids, False)
88         for record in self.browse(cr, uid, ids, context=context):
89             for stage in record.stage_ids:
90                 if stage.user_id.id == uid:
91                     result[record.id] = stage.id
92         return result
93
94     _columns = {
95         'name': fields.function(_get_note_first_line, 
96             string='Note Summary', 
97             type='text', store=True),
98         'user_id': fields.many2one('res.users', 'Owner'),
99         'memo': fields.html('Note Content'),
100         'sequence': fields.integer('Sequence'),
101         'stage_id': fields.function(_get_stage_per_user, 
102             fnct_inv=_set_stage_per_user, 
103             string='Stage', 
104             type='many2one', 
105             relation='note.stage'),
106         'stage_ids': fields.many2many('note.stage','note_stage_rel','note_id','stage_id','Stages of Users'),
107         'open': fields.boolean('Active', track_visibility='onchange'),
108         'date_done': fields.date('Date done'),
109         'color': fields.integer('Color Index'),
110         'tag_ids' : fields.many2many('note.tag','note_tags_rel','note_id','tag_id','Tags'),
111     }
112     _defaults = {
113         'user_id': lambda self, cr, uid, ctx=None: uid,
114         'open' : 1,
115         'stage_id' : _get_default_stage_id,
116     }
117     _order = 'sequence'
118
119     def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
120         if groupby and groupby[0]=="stage_id":
121
122             #search all stages
123             current_stage_ids = self.pool.get('note.stage').search(cr,uid,[('user_id','=',uid)], context=context)
124
125             if current_stage_ids: #if the user have some stages
126                 stages = self.pool['note.stage'].browse(cr, uid, current_stage_ids, context=context)
127
128                 result = [{ #notes by stage for stages user
129                         '__context': {'group_by': groupby[1:]},
130                         '__domain': domain + [('stage_ids.id', '=', stage.id)],
131                         'stage_id': (stage.id, stage.name),
132                         'stage_id_count': self.search(cr,uid, domain+[('stage_ids', '=', stage.id)], context=context, count=True),
133                         '__fold': stage.fold,
134                     } for stage in stages]
135
136                 #note without user's stage
137                 nb_notes_ws = self.search(cr,uid, domain+[('stage_ids', 'not in', current_stage_ids)], context=context, count=True)
138                 if nb_notes_ws:
139                     # add note to the first column if it's the first stage
140                     dom_not_in = ('stage_ids', 'not in', current_stage_ids)
141                     if result and result[0]['stage_id'][0] == current_stage_ids[0]:
142                         dom_in = result[0]['__domain'].pop()
143                         result[0]['__domain'] = domain + ['|', dom_in, dom_not_in]
144                         result[0]['stage_id_count'] += nb_notes_ws
145                     else:
146                         # add the first stage column
147                         result = [{
148                             '__context': {'group_by': groupby[1:]},
149                             '__domain': domain + [dom_not_in],
150                             'stage_id': (stages[0].id, stages[0].name),
151                             'stage_id_count':nb_notes_ws,
152                             '__fold': stages[0].name,
153                         }] + result
154
155             else: # if stage_ids is empty
156
157                 #note without user's stage
158                 nb_notes_ws = self.search(cr,uid, domain, context=context, count=True)
159                 if nb_notes_ws:
160                     result = [{ #notes for unknown stage
161                         '__context': {'group_by': groupby[1:]},
162                         '__domain': domain,
163                         'stage_id': False,
164                         'stage_id_count':nb_notes_ws
165                     }]
166                 else:
167                     result = []
168             return result
169
170         else:
171             return super(note_note, self).read_group(self, cr, uid, domain, fields, groupby, 
172                 offset=offset, limit=limit, context=context, orderby=orderby,lazy=lazy)
173
174
175 #upgrade config setting page to configure pad, fancy and tags mode
176 class note_base_config_settings(osv.osv_memory):
177     _inherit = 'base.config.settings'
178     _columns = {
179         'module_note_pad': fields.boolean('Use collaborative pads (etherpad)'),
180         'group_note_fancy': fields.boolean('Use fancy layouts for notes', implied_group='note.group_note_fancy'),
181     }
182
183 class res_users(osv.Model):
184     _name = 'res.users'
185     _inherit = ['res.users']
186     def create(self, cr, uid, data, context=None):
187         user_id = super(res_users, self).create(cr, uid, data, context=context)
188         note_obj = self.pool['note.stage']
189         data_obj = self.pool['ir.model.data']
190         is_employee = self.has_group(cr, user_id, 'base.group_user')
191         if is_employee:
192             for n in range(5):
193                 xmlid = 'note_stage_%02d' % (n,)
194                 try:
195                     _model, stage_id = data_obj.get_object_reference(cr, SUPERUSER_ID, 'note', xmlid)
196                 except ValueError:
197                     continue
198                 note_obj.copy(cr, SUPERUSER_ID, stage_id, default={'user_id': user_id}, context=context)
199         return user_id