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 ##############################################################################
22 from openerp import SUPERUSER_ID
23 from openerp.osv import osv, fields
24 from openerp.tools import html2plaintext
26 class note_stage(osv.osv):
27 """ Category of Note """
29 _description = "Note Stage"
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'),
36 _order = 'sequence asc'
39 'user_id': lambda self, cr, uid, ctx: uid,
43 class note_tag(osv.osv):
45 _description = "Note Tag"
47 'name' : fields.char('Tag Name', required=True),
50 class note_note(osv.osv):
53 _inherit = ['mail.thread']
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]
61 #read the first line (convert hml into text)
62 def _get_note_first_line(self, cr, uid, ids, name="", args={}, context=None):
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]
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)
72 def onclick_note_not_done(self, cr, uid, ids, context=None):
73 return self.write(cr, uid, ids, {'open': True}, context=context)
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
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)
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
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,
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'),
113 'user_id': lambda self, cr, uid, ctx=None: uid,
115 'stage_id' : _get_default_stage_id,
119 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
120 if groupby and groupby[0]=="stage_id":
123 current_stage_ids = self.pool.get('note.stage').search(cr,uid,[('user_id','=',uid)], context=context)
125 if current_stage_ids: #if the user have some stages
127 #dict of stages: map les ids sur les noms
128 stage_name = dict(self.pool.get('note.stage').name_get(cr, uid, current_stage_ids, context=context))
130 result = [{ #notes by stage for stages user
131 '__context': {'group_by': groupby[1:]},
132 '__domain': domain + [('stage_ids.id', '=', current_stage_id)],
133 'stage_id': (current_stage_id, stage_name[current_stage_id]),
134 'stage_id_count': self.search(cr,uid, domain+[('stage_ids', '=', current_stage_id)], context=context, count=True)
135 } for current_stage_id in current_stage_ids]
137 #note without user's stage
138 nb_notes_ws = self.search(cr,uid, domain+[('stage_ids', 'not in', current_stage_ids)], context=context, count=True)
140 # add note to the first column if it's the first stage
141 dom_not_in = ('stage_ids', 'not in', current_stage_ids)
142 if result and result[0]['stage_id'][0] == current_stage_ids[0]:
143 dom_in = result[0]['__domain'].pop()
144 result[0]['__domain'] = domain + ['|', dom_in, dom_not_in]
145 result[0]['stage_id_count'] += nb_notes_ws
147 # add the first stage column
149 '__context': {'group_by': groupby[1:]},
150 '__domain': domain + [dom_not_in],
151 'stage_id': (current_stage_ids[0], stage_name[current_stage_ids[0]]),
152 'stage_id_count':nb_notes_ws
155 else: # if stage_ids is empty
157 #note without user's stage
158 nb_notes_ws = self.search(cr,uid, domain, context=context, count=True)
160 result = [{ #notes for unknown stage
161 '__context': {'group_by': groupby[1:]},
164 'stage_id_count':nb_notes_ws
171 return super(note_note, self).read_group(self, cr, uid, domain, fields, groupby,
172 offset=offset, limit=limit, context=context, orderby=orderby)
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'
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'),
183 class res_users(osv.Model):
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')
193 xmlid = 'note_stage_%02d' % (n,)
195 _model, stage_id = data_obj.get_object_reference(cr, SUPERUSER_ID, 'note', xmlid)
198 note_obj.copy(cr, SUPERUSER_ID, stage_id, default={'user_id': user_id}, context=context)