Browse Source

[PEP8] fetchmail_attach_from_folder

pull/27/head
Sandy Carter 10 years ago
committed by Sandy Carter
parent
commit
d007be5060
  1. 2
      fetchmail_attach_from_folder/match_algorithm/base.py
  2. 1
      fetchmail_attach_from_folder/match_algorithm/email_domain.py
  3. 1
      fetchmail_attach_from_folder/match_algorithm/email_exact.py
  4. 15
      fetchmail_attach_from_folder/match_algorithm/openerp_standard.py
  5. 14
      fetchmail_attach_from_folder/model/fetchmail_server.py
  6. 33
      fetchmail_attach_from_folder/model/fetchmail_server_folder.py
  7. 45
      fetchmail_attach_from_folder/wizard/attach_mail_manually.py

2
fetchmail_attach_from_folder/match_algorithm/base.py

@ -20,6 +20,7 @@
#
##############################################################################
class base(object):
name = None
'''Name shown to the user'''
@ -30,7 +31,6 @@ class base(object):
readonly_fields = []
'''Fields on fetchmail_server folder that are readonly for this algorithm'''
def search_matches(self, cr, uid, conf, mail_message, mail_message_org):
'''Returns ids found for model with mail_message'''
return []

1
fetchmail_attach_from_folder/match_algorithm/email_domain.py

@ -22,6 +22,7 @@
from email_exact import email_exact
class email_domain(email_exact):
'''Search objects by domain name of email address.
Beware of match_first here, this is most likely to get it wrong (gmail)'''

1
fetchmail_attach_from_folder/match_algorithm/email_exact.py

@ -24,6 +24,7 @@ from base import base
from openerp.tools.safe_eval import safe_eval
from openerp.tools.mail import email_split
class email_exact(base):
'''Search for exactly the mailadress as noted in the email'''

15
fetchmail_attach_from_folder/match_algorithm/openerp_standard.py

@ -21,15 +21,21 @@
##############################################################################
from base import base
from openerp.tools.safe_eval import safe_eval
class openerp_standard(base):
'''No search at all. Use OpenERP's standard mechanism to attach mails to
mail.thread objects. Note that this algorithm always matches.'''
name = 'OpenERP standard'
readonly_fields = ['model_field', 'mail_field', 'match_first', 'domain',
'model_order', 'flag_nonmatching']
readonly_fields = [
'model_field',
'mail_field',
'match_first',
'domain',
'model_order',
'flag_nonmatching',
]
def search_matches(self, cr, uid, conf, mail_message, mail_message_org):
'''Always match. Duplicates will be fished out by message_id'''
@ -43,7 +49,8 @@ class openerp_standard(base):
folder.model_id.model, mail_message_org,
save_original=folder.server_id.original,
strip_attachments=(not folder.server_id.attach),
context=context)
context=context
)
if folder.delete_matching:
connection.store(msgid, '+FLAGS', '\\DELETED')

14
fetchmail_attach_from_folder/model/fetchmail_server.py

@ -23,12 +23,11 @@
import base64
import simplejson
from lxml import etree
from openerp.osv.orm import Model, except_orm, browse_null
from openerp.osv.orm import Model, except_orm
from openerp.tools.translate import _
from openerp.osv import fields
from openerp.addons.fetchmail.fetchmail import _logger as logger
from openerp.tools.misc import UnquoteEvalContext
from openerp.tools.safe_eval import safe_eval
class fetchmail_server(Model):
@ -95,8 +94,7 @@ class fetchmail_server(Model):
if connection.select(folder.path)[0] != 'OK':
logger.error(
'Could not open mailbox %s on %s' % (
folder.path, this.server))
'Could not open mailbox %s on %s' % (folder.path, this.server))
connection.select()
continue
result, msgids = this.get_msgids(connection)
@ -130,15 +128,15 @@ class fetchmail_server(Model):
if result != 'OK':
logger.error(
'Could not fetch %s in %s on %s' % (
msgid, folder.path, this.server))
'Could not fetch %s in %s on %s' % (msgid, folder.path, this.server))
continue
mail_message = self.pool.get('mail.thread').message_parse(
cr, uid, msgdata[0][1], save_original=this.original,
context=context)
if self.pool.get('mail.message').search(cr, uid, [
if self.pool.get('mail.message').search(
cr, uid, [
('message_id', '=', mail_message['message_id'])]):
continue
@ -156,7 +154,7 @@ class fetchmail_server(Model):
msgdata[0][1], msgid, context)
cr.execute('release savepoint apply_matching')
matched_object_ids += found_ids[:1]
except Exception, e:
except Exception:
cr.execute('rollback to savepoint apply_matching')
logger.exception(
"Failed to fetch mail %s from %s",

33
fetchmail_attach_from_folder/model/fetchmail_server_folder.py

@ -49,45 +49,55 @@ class fetchmail_server_folder(Model):
'path': fields.char(
'Path', size=256, help='The path to your mail '
"folder. Typically would be something like 'INBOX.myfolder'",
required=True),
required=True
),
'model_id': fields.many2one(
'ir.model', 'Model', required=True,
help='The model to attach emails to'),
help='The model to attach emails to'
),
'model_field': fields.char(
'Field (model)', size=128,
help='The field in your model that contains the field to match '
'against.\n'
'Examples:\n'
"'email' if your model is res.partner, or "
"'partner_id.email' if you're matching sale orders"),
"'partner_id.email' if you're matching sale orders"
),
'model_order': fields.char(
'Order (model)', size=128,
help='Fields to order by, this mostly useful in conjunction '
"with 'Use 1st match'"),
"with 'Use 1st match'"
),
'match_algorithm': fields.selection(
_get_match_algorithms_sel,
'Match algorithm', required=True, translate=True,
help='The algorithm used to determine which object an email '
'matches.'),
'matches.'
),
'mail_field': fields.char(
'Field (email)', size=128,
help='The field in the email used for matching. Typically '
"this is 'to' or 'from'"),
"this is 'to' or 'from'"
),
'server_id': fields.many2one('fetchmail.server', 'Server'),
'delete_matching': fields.boolean(
'Delete matches',
help='Delete matched emails from server'),
help='Delete matched emails from server'
),
'flag_nonmatching': fields.boolean(
'Flag nonmatching',
help="Flag emails in the server that don't match any object "
'in OpenERP'),
'in OpenERP'
),
'match_first': fields.boolean(
'Use 1st match',
help='If there are multiple matches, use the first one. If '
'not checked, multiple matches count as no match at all'),
'not checked, multiple matches count as no match at all'
),
'domain': fields.char(
'Domain', size=128, help='Fill in a search '
'filter to narrow down objects to match'),
'filter to narrow down objects to match'
),
'msg_state': fields.selection(
[
('sent', 'Sent'),
@ -95,7 +105,8 @@ class fetchmail_server_folder(Model):
],
'Message state',
help='The state messages fetched from this folder should be '
'assigned in OpenERP'),
'assigned in OpenERP'
),
}
_defaults = {

45
fetchmail_attach_from_folder/wizard/attach_mail_manually.py

@ -22,6 +22,8 @@
from openerp.osv import fields
from openerp.osv.orm import TransientModel
import logging
logger = logging.getLogger(__name__)
class attach_mail_manually(TransientModel):
@ -38,19 +40,22 @@ class attach_mail_manually(TransientModel):
if context is None:
context = {}
defaults = super(attach_mail_manually, self).default_get(cr, uid,
fields_list, context)
defaults = super(attach_mail_manually, self).default_get(
cr, uid, fields_list, context
)
for folder in self.pool.get('fetchmail.server.folder').browse(cr, uid,
for folder in self.pool.get('fetchmail.server.folder').browse(
cr, uid,
[context.get('default_folder_id')], context):
defaults['mail_ids'] = []
connection = folder.server_id.connect()
connection.select(folder.path)
result, msgids = connection.search(None,
result, msgids = connection.search(
None,
'FLAGGED' if folder.flag_nonmatching else 'UNDELETED')
if result != 'OK':
logger.error('Could not search mailbox %s on %s' % (
folder.path, this.server))
folder.path, folder.server_id.name))
continue
attach_mail_manually_mail._columns['object_id'].selection = [
(folder.model_id.model, folder.model_id.name)]
@ -58,12 +63,13 @@ class attach_mail_manually(TransientModel):
result, msgdata = connection.fetch(msgid, '(RFC822)')
if result != 'OK':
logger.error('Could not fetch %s in %s on %s' % (
msgid, folder.path, this.server))
msgid, folder.path, folder.server_id.name))
continue
mail_message = self.pool.get('mail.thread').message_parse(
cr, uid, msgdata[0][1],
save_original=folder.server_id.original,
context=context)
context=context
)
defaults['mail_ids'].append((0, 0, {
'msgid': msgid,
'subject': mail_message.get('subject', ''),
@ -82,7 +88,7 @@ class attach_mail_manually(TransientModel):
result, msgdata = connection.fetch(mail.msgid, '(RFC822)')
if result != 'OK':
logger.error('Could not fetch %s in %s on %s' % (
msgid, folder.path, this.server))
mail.msgid, this.folder_id.path, this.server))
continue
mail_message = self.pool.get('mail.thread').message_parse(
@ -90,12 +96,15 @@ class attach_mail_manually(TransientModel):
save_original=this.folder_id.server_id.original,
context=context)
this.folder_id.server_id.attach_mail(connection,
this.folder_id.server_id.attach_mail(
connection,
mail.object_id.id, this.folder_id, mail_message,
mail.msgid)
mail.msgid
)
connection.close()
return {'type': 'ir.actions.act_window_close'}
class attach_mail_manually_mail(TransientModel):
_name = 'fetchmail.attach.mail.manually.mail'
@ -105,10 +114,16 @@ class attach_mail_manually_mail(TransientModel):
'msgid': fields.char('Message id', size=16, readonly=True),
'subject': fields.char('Subject', size=128, readonly=True),
'date': fields.datetime('Date', readonly=True),
'object_id': fields.reference('Object',
selection=lambda self, cr, uid, context:
[(m.model, m.name) for m in
self.pool.get('ir.model').browse(cr, uid,
'object_id': fields.reference(
'Object',
selection=lambda self, cr, uid, context: [
(m.model, m.name)
for m in self.pool.get('ir.model').browse(
cr, uid,
self.pool.get('ir.model').search(cr, uid, []),
context)], size=128),
context
)
],
size=128,
),
}
Loading…
Cancel
Save