77import re
88
99from odoo import api , models
10+ from odoo .exceptions import UserError
1011
1112_logger = logging .getLogger (__name__ )
1213
@@ -16,42 +17,69 @@ class EdiExchangeRecord(models.Model):
1617
1718 @api .model
1819 def message_new (self , msg_dict , custom_values = None ):
20+ backend_id = custom_values .get ("backend_id" ) if custom_values else None
21+ if backend_id :
22+ backend = self .env ["edi.backend" ].browse (backend_id )
23+ content = False
24+ filename = False
25+ if (
26+ not custom_values .get ("type_id" )
27+ and not backend .full_mail_exchange_type_id
28+ ):
29+ types = self .env ["edi.exchange.type" ].search (
30+ backend ._mail_exchange_type_pending_input_domain ()
31+ )
32+ for exchange_type in types :
33+ for attachment in msg_dict .get ("attachments" , []):
34+ if re .match (
35+ exchange_type .exchange_filename_pattern ,
36+ attachment .fname ,
37+ re .IGNORECASE ,
38+ ):
39+ content = self ._process_email_attachment (attachment )
40+ filename = attachment .fname
41+ custom_values ["type_id" ] = exchange_type .id
42+ break
43+ if content :
44+ custom_values ["type_id" ] = exchange_type .id
45+ break
46+ else :
47+ if backend .full_mail_exchange_type_id :
48+ custom_values ["type_id" ] = backend .full_mail_exchange_type_id .id
49+ new_message_dict = msg_dict .copy ()
50+ attachments = new_message_dict .pop ("attachments" , [])
51+ new_message_dict ["attachments" ] = []
52+ for attachment in attachments :
53+ new_message_dict ["attachments" ].append (
54+ {
55+ "info" : attachment .info ,
56+ "data" : self ._process_email_attachment (attachment ),
57+ "fname" : attachment .fname ,
58+ }
59+ )
60+ content = json .dumps (new_message_dict )
61+ filename = "email_message.json"
62+ if not custom_values or "type_id" not in custom_values :
63+ _logger .warning (
64+ "No exchange type found for incoming email with subject '%s'" ,
65+ msg_dict .get ("subject" ),
66+ )
67+ raise UserError (
68+ self .env ._ (
69+ "No exchange type found for incoming email with subject '%s'" ,
70+ msg_dict .get ("subject" ),
71+ )
72+ )
1973 record = super ().message_new (
2074 msg_dict ,
2175 custom_values = custom_values ,
2276 )
23- if record .type_id .mail_as_attachment :
24- new_message_dict = msg_dict .copy ()
25- attachments = new_message_dict .pop ("attachments" , [])
26- new_message_dict ["attachments" ] = []
27- for attachment in attachments :
28- new_message_dict ["attachments" ].append (
29- {
30- "info" : attachment .info ,
31- "data" : self ._process_email_attachment (attachment ),
32- "fname" : attachment .fname ,
33- }
34- )
35- record ._set_file_content (json .dumps (new_message_dict ))
36- record .edi_exchange_state = "input_received"
37- else :
38- content = False
39- filename = False
40- for attachment in msg_dict .get ("attachments" , []):
41- if re .match (
42- record .type_id .exchange_filename_pattern or ".*" ,
43- attachment .fname ,
44- re .IGNORECASE ,
45- ):
46- content = self ._process_email_attachment (attachment )
47- filename = attachment .fname
48- break
49- if content :
50- record ._set_file_content (content )
51- record .exchange_filename = filename
52- record .edi_exchange_state = "input_received"
77+ record ._set_file_content (content )
78+ record .exchange_filename = filename
79+ record .edi_exchange_state = "input_received"
5380 return record
5481
82+ @api .model
5583 def _process_email_attachment (self , attachment ):
5684 """Process email attachment to be stored as file content."""
5785 data = attachment [1 ]
0 commit comments