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.osv import osv, fields
23 from openerp.tools import html2plaintext
25 class note_stage(osv.osv):
26 """ Category of Note """
28 _description = "Note Stage"
30 'name': fields.char('Stage Name', translate=True, required=True),
31 'sequence': fields.integer('Sequence', help="Used to order the note stages"),
32 'user_id': fields.many2one('res.users', 'Owner', help="Owner of the note stage.", required=True, ondelete='cascade'),
33 'fold': fields.boolean('Folded by Default'),
35 _order = 'sequence asc'
38 'user_id': lambda self, cr, uid, ctx: uid,
42 class note_tag(osv.osv):
44 _description = "Note Tag"
46 'name' : fields.char('Tag Name', required=True),
49 class note_note(osv.osv):
52 _inherit = ['mail.thread']
55 #writing method (no modification of values)
56 def name_create(self, cr, uid, name, context=None):
57 rec_id = self.create(cr, uid, {'memo': name}, context=context)
58 return self.name_get(cr, uid, [rec_id], context)[0]
60 #read the first line (convert hml into text)
61 def _get_note_first_line(self, cr, uid, ids, name="", args={}, context=None):
63 for note in self.browse(cr, uid, ids, context=context):
64 res[note.id] = (note.memo and html2plaintext(note.memo) or "").strip().replace('*','').split("\n")[0]
68 def onclick_note_is_done(self, cr, uid, ids, context=None):
69 return self.write(cr, uid, ids, {'open': False, 'date_done': fields.date.today()}, context=context)
71 def onclick_note_not_done(self, cr, uid, ids, context=None):
72 return self.write(cr, uid, ids, {'open': True}, context=context)
74 #used for undisplay the follower if it's the current user
75 def _get_my_current_partner(self, cr, uid, ids, name, args, context=None):
76 user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
77 pid = user.partner_id and user.partner_id.id or False
78 return dict.fromkeys(ids, pid)
80 #return the default stage for the uid user
81 def _get_default_stage_id(self,cr,uid,context=None):
82 ids = self.pool.get('note.stage').search(cr,uid,[('user_id','=',uid)], context=context)
83 return ids and ids[0] or False
85 def _set_stage_per_user(self, cr, uid, id, name, value, args=None, context=None):
86 note = self.browse(cr, uid, id, context=context)
87 if not value: return False
88 stage_ids = [value] + [stage.id for stage in note.stage_ids if stage.user_id.id != uid ]
89 return self.write(cr, uid, [id], {'stage_ids': [(6, 0, set(stage_ids))]}, context=context)
91 def _get_stage_per_user(self, cr, uid, ids, name, args, context=None):
92 result = dict.fromkeys(ids, False)
93 for record in self.browse(cr, uid, ids, context=context):
94 for stage in record.stage_ids:
95 if stage.user_id.id == uid:
96 result[record.id] = stage.id
100 'name': fields.function(_get_note_first_line,
101 string='Note Summary',
102 type='text', store=True),
103 'memo': fields.html('Note Content'),
104 'sequence': fields.integer('Sequence'),
105 'stage_id': fields.function(_get_stage_per_user,
106 fnct_inv=_set_stage_per_user,
109 relation='note.stage'),
110 'stage_ids': fields.many2many('note.stage','note_stage_rel','note_id','stage_id','Stages of Users'),
111 'open': fields.boolean('Active', track_visibility='onchange'),
112 'date_done': fields.date('Date done'),
113 'color': fields.integer('Color Index'),
114 'tag_ids' : fields.many2many('note.tag','note_tags_rel','note_id','tag_id','Tags'),
115 'current_partner_id' : fields.function(_get_my_current_partner, type="many2one", relation='res.partner', string="Owner"),
119 'stage_id' : _get_default_stage_id,
123 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
124 if groupby and groupby[0]=="stage_id":
127 current_stage_ids = self.pool.get('note.stage').search(cr,uid,[('user_id','=',uid)], context=context)
129 if current_stage_ids: #if the user have some stages
131 #dict of stages: map les ids sur les noms
132 stage_name = dict(self.pool.get('note.stage').name_get(cr, uid, current_stage_ids, context=context))
134 result = [{ #notes by stage for stages user
135 '__context': {'group_by': groupby[1:]},
136 '__domain': domain + [('stage_ids.id', '=', current_stage_id)],
137 'stage_id': (current_stage_id, stage_name[current_stage_id]),
138 'stage_id_count': self.search(cr,uid, domain+[('stage_ids', '=', current_stage_id)], context=context, count=True)
139 } for current_stage_id in current_stage_ids]
141 #note without user's stage
142 nb_notes_ws = self.search(cr,uid, domain+[('stage_ids', 'not in', current_stage_ids)], context=context, count=True)
144 result += [{ #notes for unknown stage and if stage_ids is not empty
145 '__context': {'group_by': groupby[1:]},
146 '__domain': domain + [('stage_ids', 'not in', current_stage_ids)],
147 'stage_id': (0, 'Unknown'),
148 'stage_id_count':nb_notes_ws
151 else: # if stage_ids is empty
153 #note without user's stage
154 nb_notes_ws = self.search(cr,uid, domain, context=context, count=True)
156 result = [{ #notes for unknown stage
157 '__context': {'group_by': groupby[1:]},
159 'stage_id': (0, 'Unknown'),
160 'stage_id_count':nb_notes_ws
167 return super(note_note, self).read_group(self, cr, uid, domain, fields, groupby,
168 offset=offset, limit=limit, context=context, orderby=orderby)
171 #upgrade config setting page to configure pad, fancy and tags mode
172 class note_base_config_settings(osv.osv_memory):
173 _inherit = 'base.config.settings'
175 'module_note_pad': fields.boolean('Use collaborative pads (etherpad)'),
176 'group_note_fancy': fields.boolean('Use fancy layouts for notes', implied_group='note.group_note_fancy'),
179 class res_users(osv.Model):
181 _inherit = ['res.users']
182 def create(self, cr, uid, data, context=None):
183 user_id = super(res_users, self).create(cr, uid, data, context=context)
184 user = self.browse(cr, uid, uid, context=context)
185 note_obj = self.pool.get('note.stage')
186 data_obj = self.pool.get('ir.model.data')
187 model_id = data_obj.get_object_reference(cr, uid, 'base', 'group_user') #Employee Group
188 group_id = model_id and model_id[1] or False
189 if group_id in [x.id for x in user.groups_id]:
190 for note_xml_id in ['note_stage_01','note_stage_02','note_stage_03','note_stage_04']:
191 data_id = data_obj._get_id(cr, uid, 'note', note_xml_id)
192 stage_id = data_obj.browse(cr, uid, data_id, context=context).res_id
193 note_obj.copy(cr, uid, stage_id, default = {
194 'user_id': user_id}, context=context)