[IMP] mail: rename body to body_text in msg_dict too, for consistency, correct typo
[odoo/odoo.git] / addons / crm / wizard / crm_add_note.py
1 from crm import crm
2 from osv import fields, osv
3 from tools.translate import _
4 import base64
5
6 AVAILABLE_STATES = crm.AVAILABLE_STATES + [('unchanged', 'Unchanged')]
7
8 class crm_add_note_email_attachment(osv.osv_memory):
9     _name = 'crm.add.note.email.attachment'
10
11     _columns = {
12         'binary' : fields.binary('Attachment', required=True),
13         'name' : fields.char('Name', size=128, required=True),
14         'wizard_id' : fields.many2one('crm.add.note', 'Wizard', required=True),
15     }
16
17 crm_add_note_email_attachment()
18
19 class crm_add_note(osv.osv_memory):
20     """Adds a new note to the case."""
21     _name = 'crm.add.note'
22     _description = "Add Internal Note"
23
24     _columns = {
25         'body': fields.text('Note Body', required=True),
26         'state': fields.selection(AVAILABLE_STATES, string='Set New State To',
27                                   required=True),
28         'attachment_ids' : fields.one2many('crm.add.note.email.attachment', 'wizard_id'),
29     }
30
31     def action_add(self, cr, uid, ids, context=None):
32         if context is None:
33             context = {}
34
35         if not context.get('active_model'):
36             raise osv.except_osv(_('Error'), _('Can not add note!'))
37
38         model = context.get('active_model')
39         case_pool = self.pool.get(model)
40
41         for obj in self.browse(cr, uid, ids, context=context):
42             case_list = case_pool.browse(cr, uid, context['active_ids'],
43                                          context=context)
44             case = case_list[0]
45             user_obj = self.pool.get('res.users')
46             user_name = user_obj.browse(cr, uid, [uid], context=context)[0].name
47             attach = dict(
48                 (x.name, base64.decodestring(x.binary)) for x in obj.attachment_ids
49             )
50             case_pool.history(cr, uid, [case], self.pool.get('email.message').truncate_data(cr, uid, obj.body, context=context), history=False,
51                               details=obj.body, email_from=user_name, attach=attach)
52
53             if obj.state == 'unchanged':
54                 pass
55             elif obj.state == 'done':
56                 case_pool.case_close(cr, uid, [case.id])
57             elif obj.state == 'draft':
58                 case_pool.case_reset(cr, uid, [case.id])
59             elif obj.state in ['cancel', 'open', 'pending']:
60                 act = 'case_' + obj.state
61                 getattr(case_pool, act)(cr, uid, [case.id])
62
63         return {'type': 'ir.actions.act_window_close'}
64
65     def default_get(self, cr, uid, fields, context=None):
66         """
67         This function gets default values
68         """
69         return {'state': u'unchanged'}
70
71
72 crm_add_note()