~camptocamp/server-env-tools/pending-merge-7.0

« back to all changes in this revision

Viewing changes to fetchmail_attach_from_folder/wizard/attach_mail_manually.py

  • Committer: Alexandre Fayolle
  • Author(s): hbrunn at therp
  • Date: 2013-04-26 07:56:43 UTC
  • mfrom: (25.2.9 6.1-fetchmail_attach_from_folder)
  • Revision ID: alexandre.fayolle@camptocamp.com-20130426075643-xczdulm1x6scnkxg
[MRG][ADD] fetchmail_attach_from_folder

Adds the possibility to attach emails from a certain IMAP folder to objects,
ie partners. Matching is done via several algorithms, ie email address.

This gives a simple possibility to archive emails in OpenERP without a mail
client integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    This module copyright (C) 2013 Therp BV (<http://therp.nl>)
 
6
#    All Rights Reserved
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from openerp.osv import fields
 
24
from openerp.osv.orm import TransientModel
 
25
 
 
26
 
 
27
class attach_mail_manually(TransientModel):
 
28
    _name = 'fetchmail.attach.mail.manually'
 
29
 
 
30
    _columns = {
 
31
            'folder_id': fields.many2one('fetchmail.server.folder', 'Folder',
 
32
                readonly=True),
 
33
            'mail_ids': fields.one2many(
 
34
                'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails'),
 
35
            }
 
36
 
 
37
    def default_get(self, cr, uid, fields_list, context=None):
 
38
        if context is None:
 
39
            context = {}
 
40
 
 
41
        defaults = super(attach_mail_manually, self).default_get(cr, uid, 
 
42
                                fields_list, context)
 
43
 
 
44
        for folder in self.pool.get('fetchmail.server.folder').browse(cr, uid,
 
45
                [context.get('default_folder_id')], context):
 
46
            defaults['mail_ids']=[]
 
47
            connection = folder.server_id.connect()
 
48
            connection.select(folder.path)
 
49
            result, msgids = connection.search(None, 
 
50
                    'FLAGGED' if folder.flag_nonmatching else 'UNDELETED')
 
51
            if result != 'OK':
 
52
                logger.error('Could not search mailbox %s on %s' % (
 
53
                    folder.path, this.server))
 
54
                continue
 
55
            attach_mail_manually_mail._columns['object_id'].selection=[
 
56
                    (folder.model_id.model, folder.model_id.name)]
 
57
            for msgid in msgids[0].split():
 
58
                result, msgdata = connection.fetch(msgid, '(RFC822)')
 
59
                if result != 'OK':
 
60
                    logger.error('Could not fetch %s in %s on %s' % (
 
61
                        msgid, folder.path, this.server))
 
62
                    continue
 
63
                mail_message = self.pool.get('mail.message').parse_message(
 
64
                        msgdata[0][1])
 
65
                defaults['mail_ids'].append((0, 0, {
 
66
                    'msgid': msgid,
 
67
                    'subject': mail_message.get('subject', ''),
 
68
                    'date': mail_message.get('date', ''),
 
69
                    'object_id': folder.model_id.model+',False'
 
70
                    }))
 
71
            connection.close()
 
72
 
 
73
        return defaults
 
74
 
 
75
    def attach_mails(self, cr, uid, ids, context=None):
 
76
        for this in self.browse(cr, uid, ids, context):
 
77
            for mail in this.mail_ids:
 
78
                connection = this.folder_id.server_id.connect()
 
79
                connection.select(this.folder_id.path)
 
80
                result, msgdata = connection.fetch(mail.msgid, '(RFC822)') 
 
81
                if result != 'OK': 
 
82
                    logger.error('Could not fetch %s in %s on %s' % ( 
 
83
                        msgid, folder.path, this.server)) 
 
84
                    continue 
 
85
                
 
86
                mail_message = self.pool.get('mail.message').parse_message( 
 
87
                        msgdata[0][1], this.folder_id.server_id.original)
 
88
 
 
89
                this.folder_id.server_id.attach_mail(connection, 
 
90
                        mail.object_id.id, this.folder_id, mail_message, 
 
91
                        mail.msgid)
 
92
                connection.close()
 
93
        return {'type': 'ir.actions.act_window_close'}
 
94
 
 
95
class attach_mail_manually_mail(TransientModel):
 
96
    _name = 'fetchmail.attach.mail.manually.mail'
 
97
 
 
98
    _columns = {
 
99
            'wizard_id': fields.many2one('fetchmail.attach.mail.manually', 
 
100
                readonly=True),
 
101
            'msgid': fields.char('Message id', size=16, readonly=True),
 
102
            'subject': fields.char('Subject', size=128, readonly=True),
 
103
            'date': fields.datetime('Date', readonly=True),
 
104
            'object_id': fields.reference('Object', 
 
105
                selection=lambda self, cr, uid, context: 
 
106
                    [(m.model, m.name) for m in 
 
107
                        self.pool.get('ir.model').browse(cr, uid,
 
108
                            self.pool.get('ir.model').search(cr, uid, []),
 
109
                            context)], size=128),
 
110
            }