[IMP]: crm*: Improvements for "Add note" wizard,
[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
9 class crm_add_note(osv.osv_memory):
10     """Adds a new note to the case."""
11     _name = 'crm.add.note'
12     _description = "Add New Note"
13
14     _columns = {
15         'body': fields.text('Note Body', required=True),
16         'state': fields.selection(AVAILABLE_STATES, string='Set New State To',
17                                   required=True), 
18         'attachment_ids' : fields.one2many('crm.send.mail.attachment', 'wizard_id'),
19     }
20
21     def action_add(self, cr, uid, ids, context=None):
22         if not context:
23             context = {}
24
25         if not context.get('active_model'):
26             raise osv.except_osv(_('Error'), _('Can not add note!'))
27
28         model = context.get('active_model')
29         case_pool = self.pool.get(model)
30
31         for obj in self.browse(cr, uid, ids, context=context):
32             case_list = case_pool.browse(cr, uid, context['active_ids'],
33                                          context=context)
34             case = case_list[0]
35             user_obj = self.pool.get('res.users')
36             user_name = user_obj.browse(cr, uid, [uid], context=context)[0].name
37             attach = [
38                 (x.name, base64.decodestring(x.binary)) for x in obj.attachment_ids
39             ]
40             case_pool.history(cr, uid, [case], _("Note"), history=False,
41                               details=obj.body, email_from=user_name, attach=attach)
42
43             if obj.state == 'unchanged':
44                 pass
45             elif obj.state == 'done':
46                 case_pool.case_close(cr, uid, [case.id])
47             elif obj.state == 'draft':
48                 case_pool.case_reset(cr, uid, [case.id])
49             elif obj.state in ['cancel', 'open', 'pending']:
50                 act = 'case_' + obj.state
51                 getattr(case_pool, act)(cr, uid, [case.id])
52
53         return {}
54
55     def default_get(self, cr, uid, fields, context=None):
56         """
57         This function gets default values
58         """
59         return {'state': u'unchanged'}
60
61
62 crm_add_note()