2013-09-12 11:17:22 +02:00
|
|
|
# This file is part of csv_import_getmail module for Tryton.
|
|
|
|
# The COPYRIGHT file at the top level of this repository contains
|
|
|
|
# the full copyright notices and license terms.
|
|
|
|
from trytond.pool import Pool, PoolMeta
|
2016-03-29 12:00:21 +02:00
|
|
|
from trytond.model import ModelSQL, fields
|
2014-01-22 16:57:21 +01:00
|
|
|
from email.utils import parseaddr
|
2013-09-12 11:17:22 +02:00
|
|
|
import logging
|
|
|
|
|
2013-12-02 19:49:43 +01:00
|
|
|
__all__ = ['CSVProfile', 'CSVProfileParty']
|
2013-09-12 11:17:22 +02:00
|
|
|
|
2015-08-12 01:28:41 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-09-12 11:17:22 +02:00
|
|
|
|
2016-03-29 12:00:21 +02:00
|
|
|
class CSVProfile:
|
|
|
|
__metaclass__ = PoolMeta
|
2013-11-18 17:13:22 +01:00
|
|
|
__name__ = 'csv.profile'
|
|
|
|
parties = fields.Many2Many('csv.profile-party.party',
|
|
|
|
'profile', 'party', 'Parties')
|
|
|
|
|
2013-09-12 11:17:22 +02:00
|
|
|
@classmethod
|
2015-12-15 16:50:19 +01:00
|
|
|
def getmail(self, server, messages):
|
2013-09-12 11:17:22 +02:00
|
|
|
pool = Pool()
|
|
|
|
GetMail = pool.get('getmail.server')
|
|
|
|
CSVArchive = pool.get('csv.archive')
|
|
|
|
CSVProfile = pool.get('csv.profile')
|
|
|
|
|
2015-01-12 18:52:22 +01:00
|
|
|
for message in messages:
|
|
|
|
msgeid = str(message.uid)
|
2013-12-02 19:49:43 +01:00
|
|
|
if not message.attachments:
|
2015-08-12 01:28:41 +02:00
|
|
|
logger.info('Not attachments. Continue')
|
2013-12-03 09:08:32 +01:00
|
|
|
continue
|
2014-01-24 10:46:11 +01:00
|
|
|
if not message.from_addr:
|
2015-08-12 01:28:41 +02:00
|
|
|
logger.info('Not from address email. Continue')
|
2013-12-13 13:40:04 +01:00
|
|
|
continue
|
2013-09-12 11:17:22 +02:00
|
|
|
|
2014-01-24 10:46:11 +01:00
|
|
|
sender = parseaddr(message.from_addr)[1]
|
2013-09-23 09:09:07 +02:00
|
|
|
party, _ = GetMail.get_party_from_email(sender)
|
2013-09-12 11:17:22 +02:00
|
|
|
if not party:
|
2015-08-12 01:28:41 +02:00
|
|
|
logger.info('Not party from email %s' % sender)
|
2013-09-12 11:17:22 +02:00
|
|
|
continue
|
2013-11-21 13:19:56 +01:00
|
|
|
csv_profiles = CSVProfile.search([('parties', 'in', [party.id])])
|
2013-09-12 11:17:22 +02:00
|
|
|
if not csv_profiles:
|
2015-08-12 01:28:41 +02:00
|
|
|
logger.info('Not profile from party %s' % party.name)
|
2013-09-12 11:17:22 +02:00
|
|
|
continue
|
|
|
|
csv_profile = csv_profiles[0]
|
|
|
|
|
2015-08-12 01:28:41 +02:00
|
|
|
logger.info('Process email: %s' % (msgeid))
|
2013-09-12 11:17:22 +02:00
|
|
|
|
2013-12-03 08:54:20 +01:00
|
|
|
attch = None
|
2013-09-12 11:17:22 +02:00
|
|
|
for attachment in message.attachments:
|
2013-09-18 08:49:56 +02:00
|
|
|
if attachment[0][-3:].upper() == 'CSV':
|
2013-12-03 08:54:20 +01:00
|
|
|
attch = True
|
2015-08-12 01:28:41 +02:00
|
|
|
logger.info('Process import CSV: %s' % (msgeid))
|
2013-09-18 08:49:56 +02:00
|
|
|
csv_archive = CSVArchive()
|
|
|
|
csv_archive.profile = csv_profile
|
|
|
|
csv_archive.data = attachment[1]
|
2015-08-28 11:10:01 +02:00
|
|
|
csv_archive.on_change_profile()
|
2013-09-18 08:49:56 +02:00
|
|
|
csv_archive.save()
|
|
|
|
CSVArchive().import_csv([csv_archive])
|
2013-12-03 08:54:20 +01:00
|
|
|
if not attch:
|
2015-08-12 01:28:41 +02:00
|
|
|
logger.info('Not attachment CSV: %s' % (msgeid))
|
2013-09-18 08:49:56 +02:00
|
|
|
|
2013-09-12 11:17:22 +02:00
|
|
|
return True
|
2013-12-02 19:49:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CSVProfileParty(ModelSQL):
|
|
|
|
'Profile - Party'
|
|
|
|
__name__ = 'csv.profile-party.party'
|
|
|
|
_table = 'csv_profile_party_rel'
|
|
|
|
profile = fields.Many2One('csv.profile', 'CSV Profile', ondelete='CASCADE',
|
|
|
|
required=True, select=True)
|
|
|
|
party = fields.Many2One('party.party', 'Party',
|
|
|
|
ondelete='CASCADE', required=True, select=True)
|