trytond-csv_import_getmail/csv_import.py

81 lines
3 KiB
Python
Raw Normal View History

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
2013-11-18 17:13:22 +01:00
from trytond.model import ModelSQL, ModelView, fields
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
__metaclass__ = PoolMeta
2013-11-18 17:13:22 +01:00
class CSVProfile(ModelSQL, ModelView):
__name__ = 'csv.profile'
parties = fields.Many2Many('csv.profile-party.party',
'profile', 'party', 'Parties')
2013-09-12 11:17:22 +02:00
@classmethod
def getmail(self, messages, attachments=None):
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:
logging.getLogger('Getmail CSV Import').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:
logging.getLogger('Getmail CSV Import').info(
'Not from address email. Continue')
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:
2013-12-02 19:49:43 +01:00
logging.getLogger('Getmail CSV Import').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:
2013-12-02 19:49:43 +01:00
logging.getLogger('Getmail CSV Import').info(
'Not profile from party %s' % party.name)
2013-09-12 11:17:22 +02:00
continue
csv_profile = csv_profiles[0]
logging.getLogger('CSV Import Get Mail').info(
2015-01-12 18:52:22 +01:00
'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:
if attachment[0][-3:].upper() == 'CSV':
2013-12-03 08:54:20 +01:00
attch = True
2013-12-02 19:49:43 +01:00
logging.getLogger('CSV Import Get Mail').info(
2015-01-12 22:46:05 +01:00
'Process import CSV: %s' % (msgeid))
csv_archive = CSVArchive()
csv_archive.profile = csv_profile
csv_archive.data = attachment[1]
csv_archive.archive_name = (
csv_archive.on_change_profile()['archive_name'])
csv_archive.save()
CSVArchive().import_csv([csv_archive])
2013-12-03 08:54:20 +01:00
if not attch:
logging.getLogger('CSV Import Get Mail').info(
2015-01-12 22:46:05 +01:00
'Not attachment CSV: %s' % (msgeid))
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)