1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# This module copyright (C) 2013 Therp BV (<http://therp.nl>)
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.
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.
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/>.
21
##############################################################################
23
from openerp.osv import fields
24
from openerp.osv.orm import TransientModel
27
class attach_mail_manually(TransientModel):
28
_name = 'fetchmail.attach.mail.manually'
31
'folder_id': fields.many2one('fetchmail.server.folder', 'Folder',
33
'mail_ids': fields.one2many(
34
'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails'),
37
def default_get(self, cr, uid, fields_list, context=None):
41
defaults = super(attach_mail_manually, self).default_get(cr, uid,
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')
52
logger.error('Could not search mailbox %s on %s' % (
53
folder.path, this.server))
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)')
60
logger.error('Could not fetch %s in %s on %s' % (
61
msgid, folder.path, this.server))
63
mail_message = self.pool.get('mail.message').parse_message(
65
defaults['mail_ids'].append((0, 0, {
67
'subject': mail_message.get('subject', ''),
68
'date': mail_message.get('date', ''),
69
'object_id': folder.model_id.model+',False'
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)')
82
logger.error('Could not fetch %s in %s on %s' % (
83
msgid, folder.path, this.server))
86
mail_message = self.pool.get('mail.message').parse_message(
87
msgdata[0][1], this.folder_id.server_id.original)
89
this.folder_id.server_id.attach_mail(connection,
90
mail.object_id.id, this.folder_id, mail_message,
93
return {'type': 'ir.actions.act_window_close'}
95
class attach_mail_manually_mail(TransientModel):
96
_name = 'fetchmail.attach.mail.manually.mail'
99
'wizard_id': fields.many2one('fetchmail.attach.mail.manually',
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),