~therp-nl/server-env-tools/6.1-mass_editing-fix_dataloss

« back to all changes in this revision

Viewing changes to fetchmail_attach_from_folder/match_algorithm/email_exact.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 base import base
 
24
from openerp.tools.safe_eval import safe_eval
 
25
from openerp.addons.mail.mail_message import to_email
 
26
 
 
27
class email_exact(base):
 
28
    '''Search for exactly the mailadress as noted in the email'''
 
29
 
 
30
    name = 'Exact mailadress'
 
31
    required_fields = ['model_field', 'mail_field']
 
32
 
 
33
    def _get_mailaddresses(self, conf, mail_message):
 
34
        mailaddresses = []
 
35
        fields = conf.mail_field.split(',')
 
36
        for field in fields:
 
37
            if field in mail_message:
 
38
                mailaddresses += to_email(mail_message[field])
 
39
        return [ addr.lower() for addr in mailaddresses ]
 
40
 
 
41
    def _get_mailaddress_search_domain(
 
42
            self, conf, mail_message, operator='=', values=None):
 
43
        mailaddresses = values or self._get_mailaddresses(
 
44
                conf, mail_message)
 
45
        if not mailaddresses:
 
46
            return [(0, '=', 1)]
 
47
        search_domain = ((['|'] * (len(mailaddresses) - 1)) + [
 
48
                (conf.model_field, operator, addr) for addr in mailaddresses] +
 
49
                safe_eval(conf.domain or '[]'))
 
50
        return search_domain
 
51
 
 
52
    def search_matches(self, cr, uid, conf, mail_message, mail_message_org):
 
53
        conf_model = conf.pool.get(conf.model_id.model)
 
54
        search_domain = self._get_mailaddress_search_domain(conf, mail_message)
 
55
        return conf_model.search(
 
56
            cr, uid, search_domain, order=conf.model_order)