@@ -1248,36 +1248,10 @@ def duplicate(self, keep_category, keep_folder, keep_reply_to, keep_dms_files, k
12481248 duplicated_mail .reply_to = original_mail .reply_to [:]
12491249
12501250 if keep_dms_files :
1251- dms_files = [sub_content for sub_content in original_mail .values () if sub_content .portal_type == self .mainfile_type ]
1252- used_template_uids = set ()
1253- for dms_file in dms_files :
1254- annot = IAnnotations (dms_file ).get ('documentgenerator' , {})
1255-
1256- # Skip if document was not generated (could be scanned)
1257- if not annot :
1258- continue
1259-
1260- # Skip if document was already generated from the same template
1261- if annot .get ('template_uid' ) in used_template_uids :
1262- continue
1263-
1264- # Skip if document is final step of a mailing
1265- document_generation_helper_view = getMultiAdapter ((duplicated_mail , self .request ), name = "document_generation_helper_view" )
1266- requires_mailing = len (document_generation_helper_view .mailing_list ()) > 1
1267- if requires_mailing and not annot .get ('need_mailing' ):
1268- continue
1269-
1270- # Generate a new document from the same template
1271- template_uid = annot .get ('template_uid' )
1272- generation_view = getMultiAdapter ((duplicated_mail , self .request ), name = "persistent-document-generation" )
1273- pod_template = generation_view .get_pod_template (template_uid )
1274- # Skip if it's a mailing loop template
1275- if IMailingLoopTemplate .providedBy (pod_template ):
1276- continue
1277- generation_view .pod_template = pod_template
1278- generation_view .output_format = 'odt'
1279- generation_view .generate_persistent_doc (pod_template , 'odt' )
1280- used_template_uids .add (template_uid )
1251+ # Add an annotation to copy DMS files only after next edit
1252+ annot = IAnnotations (duplicated_mail )
1253+ annot .setdefault ('imio.dms.mail' , PersistentDict ()) # make sure the key exists
1254+ annot ['imio.dms.mail' ]['copy_dms_files_from' ] = original_mail .UID ()
12811255
12821256 if keep_annexes :
12831257 annexes = [sub_content .getId () for sub_content in original_mail .values () if IDmsAppendixFile .providedBy (sub_content )]
@@ -1294,6 +1268,39 @@ def duplicate(self, keep_category, keep_folder, keep_reply_to, keep_dms_files, k
12941268
12951269 return duplicated_mail
12961270
1271+ def copy_dms_files (self , original_mail ):
1272+ """Re-generate DMS files on this mail based on the templates used in the original mail."""
1273+ dms_files = [sub_content for sub_content in original_mail .values () if sub_content .portal_type == self .mainfile_type ]
1274+ used_template_uids = set ()
1275+ for dms_file in dms_files :
1276+ annot = IAnnotations (dms_file ).get ('documentgenerator' , {})
1277+
1278+ # Skip if document was not generated (could be scanned)
1279+ if not annot :
1280+ continue
1281+
1282+ # Skip if document was already generated from the same template
1283+ if annot .get ('template_uid' ) in used_template_uids :
1284+ continue
1285+
1286+ # Skip if document is final step of a mailing
1287+ document_generation_helper_view = getMultiAdapter ((self .context , self .request ), name = "document_generation_helper_view" )
1288+ requires_mailing = len (document_generation_helper_view .mailing_list ()) > 1
1289+ if requires_mailing and not annot .get ('need_mailing' ):
1290+ continue
1291+
1292+ # Generate a new document from the same template
1293+ template_uid = annot .get ('template_uid' )
1294+ generation_view = getMultiAdapter ((self .context , self .request ), name = "persistent-document-generation" )
1295+ pod_template = generation_view .get_pod_template (template_uid )
1296+ # Skip if it's a mailing loop template
1297+ if IMailingLoopTemplate .providedBy (pod_template ):
1298+ continue
1299+ generation_view .pod_template = pod_template
1300+ generation_view .output_format = 'odt'
1301+ generation_view .generate_persistent_doc (pod_template , 'odt' )
1302+ used_template_uids .add (template_uid )
1303+
12971304
12981305class Dummy (object ):
12991306 """dummy class that allows setting attributes"""
0 commit comments