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