[IMP]: Added log for crm modules and document
[odoo/odoo.git] / addons / mail_gateway / mail_gateway.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 osv import osv, fields
23 import time
24 import  base64
25
26 class one2many_domain(fields.one2many):
27     def set(self, cr, obj, id, field, values, user=None, context=None):
28         if not values:
29             return
30         return super(one2many_domain, self).set(cr, obj, id, field, values, 
31                                             user=user, context=context)
32
33     def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
34         if context is None:
35             context = {}
36         res = {}
37         msg_obj = obj.pool.get('mailgate.message')
38         for thread in obj.browse(cr, user, ids, context=context):
39             final = msg_obj.search(cr, user, self._domain + [('thread_id', '=', thread.id)], context=context)
40             res[thread.id] = final
41         return res
42         
43         
44 class mailgate_thread(osv.osv):
45     '''
46     Mailgateway Thread
47     '''
48     _name = 'mailgate.thread'
49     _description = 'Mailgateway Thread'
50     _rec_name = 'thread' 
51
52     _columns = {
53         'thread': fields.char('Thread', size=124, required=False), 
54         'message_ids': one2many_domain('mailgate.message', 'thread_id', 'Messages', domain=[('history', '=', True)], required=False), 
55         'log_ids': one2many_domain('mailgate.message', 'thread_id', 'Logs', domain=[('history', '=', False)], required=False),
56         'model': fields.char('Model Name', size=64, required=False),  
57         'res_id': fields.integer('Resource ID'), 
58         }
59         
60     def __history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, email_from=False, message_id=False, attach=[], context={}):
61         """
62         @param self: The object pointer
63         @param cr: the current row, from the database cursor,
64         @param uid: the current user’s ID for security checks,
65         @param cases: a browse record list
66         @param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
67         @param history: Value True/False, If True it makes entry in case History otherwise in Case Log
68         @param email: Email address if any
69         @param details: Details of case history if any 
70         @param atach: Attachment sent in email
71         @param context: A standard dictionary for contextual values"""
72         if context is None:
73             context = {}
74         # The mailgate sends the ids of the cases and not the object list
75         if all(isinstance(case_id, (int, long)) for case_id in cases) and context.get('model'):
76             cases = self.pool.get(context['model']).browse(cr, uid, cases, context=context)
77
78         att_obj = self.pool.get('ir.attachment')
79         obj = self.pool.get('mailgate.message')
80
81         for case in cases:
82             data = {
83                 'name': keyword, 
84                 'user_id': uid, 
85                 'model' : case._name, 
86                 'res_id': case.id, 
87                 'date': time.strftime('%Y-%m-%d %H:%M:%S'), 
88                 'thread_id': case.thread_id.id, 
89                 'message_id': message_id, 
90             }
91             attachments = []
92             if history:
93                 for att in attach:
94                     attachments.append(att_obj.create(cr, uid, {'name': att[0], 'datas': base64.encodestring(att[1])}))
95                 
96                 data = {
97                         'name': subject or 'History', 
98                         'history': True, 
99                         'user_id': uid, 
100                         'model' : case._name, 
101                         'res_id': case.id,
102                         'date': time.strftime('%Y-%m-%d %H:%M:%S'), 
103                         'description': details or (hasattr(case, 'description') and case.description or False), 
104                         'email_to': email or \
105                                 (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
106                                     case.user_id.address_id.email) or tools.config.get('email_from', False), 
107                         'email_from': email_from or \
108                                 (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
109                                     case.user_id.address_id.email) or tools.config.get('email_from', False), 
110                         'partner_id': hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False, 
111                         'thread_id': case.thread_id.id, 
112                         'message_id': message_id, 
113                         'attachment_ids': [(6, 0, attachments)]
114                         }
115             res = obj.create(cr, uid, data, context)
116             case._table.log(cr, uid, case.id, case._description + " '" + case.name + "': " + keyword, context=context)
117         return True
118     
119     _history = __history
120     history = __history
121     
122
123 mailgate_thread()
124
125 class mailgate_message(osv.osv):
126     '''
127     Mailgateway Message
128     '''
129     _name = 'mailgate.message'
130     _description = 'Mailgateway Message'
131     _order = 'date desc'
132     _log_create=True
133
134     _columns = {
135         'name':fields.char('Message', size=64), 
136         'thread_id':fields.many2one('mailgate.thread', 'Thread'), 
137         'date': fields.datetime('Date'), 
138         'history': fields.boolean('Is History?', required=False), 
139         'user_id': fields.many2one('res.users', 'User Responsible', readonly=True), 
140         'message': fields.text('Description'), 
141         'email_from': fields.char('Email From', size=84), 
142         'email_to': fields.char('Email To', size=84), 
143         'email_cc': fields.char('Email CC', size=84), 
144         'email_bcc': fields.char('Email BCC', size=84), 
145         'message_id': fields.char('Message Id', size=1024, readonly=True, help="Message Id on Email Server.", select=True), 
146         'description': fields.text('Description'), 
147         'partner_id': fields.many2one('res.partner', 'Partner', required=False), 
148         'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments'),
149         'model': fields.char('Model Name', size=64, required=False),  
150         'res_id': fields.integer('Resource ID'), 
151     }
152
153 mailgate_message()
154
155
156 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: