Getmail move to CSV Profile

This commit is contained in:
resteve 2013-12-02 19:49:43 +01:00
parent a668d53ca8
commit fb6fa04ca7
5 changed files with 50 additions and 54 deletions

View file

@ -2,12 +2,11 @@
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from trytond.pool import Pool
from .csv_profile import *
from .csv_import import *
def register():
Pool.register(
CSVProfile,
CSVProfileParty,
CSVImport,
module='csv_import_getmail', type_='model')

View file

@ -6,7 +6,7 @@ from trytond.pool import Pool, PoolMeta
from trytond.model import ModelSQL, ModelView, fields
import logging
__all__ = ['CSVProfile', 'CSVProfileParty', 'CSVImport']
__all__ = ['CSVProfile', 'CSVProfileParty']
__metaclass__ = PoolMeta
@ -15,6 +15,52 @@ class CSVProfile(ModelSQL, ModelView):
parties = fields.Many2Many('csv.profile-party.party',
'profile', 'party', 'Parties')
@classmethod
def getmail(cls, messages, attachments=None):
pool = Pool()
GetMail = pool.get('getmail.server')
CSVArchive = pool.get('csv.archive')
CSVProfile = pool.get('csv.profile')
for (_, message) in messages:
if not message.attachments:
logging.getLogger('Getmail CSV Import').info(
'Not attachments. Continue')
break
sender = GetMail.get_email(message.sender)
party, _ = GetMail.get_party_from_email(sender)
if not party:
logging.getLogger('Getmail CSV Import').info(
'Not party from email %s' % sender)
continue
csv_profiles = CSVProfile.search([('parties', 'in', [party.id])])
if not csv_profiles:
logging.getLogger('Getmail CSV Import').info(
'Not profile from party %s' % party.name)
continue
csv_profile = csv_profiles[0]
logging.getLogger('CSV Import Get Mail').info(
'Process email: %s' % (message.messageid))
for attachment in message.attachments:
if attachment[0][-3:].upper() == 'CSV':
logging.getLogger('CSV Import Get Mail').info(
'Process import CSV: %s' % (message.messageid))
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])
else:
logging.getLogger('CSV Import Get Mail').info(
'Not attachment CSV: %s' % (message.messageid))
return True
class CSVProfileParty(ModelSQL):
'Profile - Party'
@ -24,52 +70,3 @@ class CSVProfileParty(ModelSQL):
required=True, select=True)
party = fields.Many2One('party.party', 'Party',
ondelete='CASCADE', required=True, select=True)
class CSVImport:
__name__ = 'csv.import'
@classmethod
def getmail(cls, messages, attachments=None):
pool = Pool()
GetMail = pool.get('getmail.server')
CSVArchive = pool.get('csv.archive')
CSVProfile = pool.get('csv.profile')
for (_, message) in messages:
if not attachments:
break
sender = GetMail.get_email(message.sender)
party, _ = GetMail.get_party_from_email(sender)
if not party:
continue
csv_profiles = CSVProfile.search([('parties', 'in', [party.id])])
if not csv_profiles:
continue
csv_profile = csv_profiles[0]
logging.getLogger('CSV Import Get Mail').info(
'Process email: %s' % (message.messageid))
for attachment in message.attachments:
if attachment[0][-3:].upper() == 'CSV':
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()
comment = (message.date + '\n' + message.title + '\n' +
message.sender + '\n\n' + message.body)
vals = {
'create_date': datetime.now(),
'record': None,
'status': 'done',
'comment': comment,
'archive': csv_archive,
}
cls.create([vals])
CSVArchive().import_csv([csv_archive])
return True

View file

@ -6,7 +6,7 @@ Importa registros desde ficheros CSV enviados por correo electrónico.
* Configure una cuenta de correo electrónico de entrada IMAP a través del menú
Getmail Server tal y como se indica para el módulo Recepción de
correo electrónico, y relacionarlo con el modelo "Importaciones CSV".
correo electrónico, y relacionarlo con el modelo "Perfil CSV".
* Cree una acción planificada a accediendo al menú Administración/Acciones planificadas
y asigne al campo "Modelo" el modelo "getmail.server" y al campo "Función" el valor

View file

@ -4,4 +4,4 @@ depends:
csv_import
getmail
xml:
csv_profile.xml
csv_import.xml