@@ -1222,36 +1222,10 @@ def duplicate(self, keep_category, keep_folder, keep_reply_to, keep_dms_files, k
12221222 duplicated_mail .reply_to = original_mail .reply_to [:]
12231223
12241224 if keep_dms_files :
1225- dms_files = [sub_content for sub_content in original_mail .values () if sub_content .portal_type == self .mainfile_type ]
1226- used_template_uids = set ()
1227- for dms_file in dms_files :
1228- annot = IAnnotations (dms_file ).get ('documentgenerator' , {})
1229-
1230- # Skip if document was not generated (could be scanned)
1231- if not annot :
1232- continue
1233-
1234- # Skip if document was already generated from the same template
1235- if annot .get ('template_uid' ) in used_template_uids :
1236- continue
1237-
1238- # Skip if document is final step of a mailing
1239- document_generation_helper_view = getMultiAdapter ((duplicated_mail , self .request ), name = "document_generation_helper_view" )
1240- requires_mailing = len (document_generation_helper_view .mailing_list ()) > 1
1241- if requires_mailing and not annot .get ('need_mailing' ):
1242- continue
1243-
1244- # Generate a new document from the same template
1245- template_uid = annot .get ('template_uid' )
1246- generation_view = getMultiAdapter ((duplicated_mail , self .request ), name = "persistent-document-generation" )
1247- pod_template = generation_view .get_pod_template (template_uid )
1248- # Skip if it's a mailing loop template
1249- if IMailingLoopTemplate .providedBy (pod_template ):
1250- continue
1251- generation_view .pod_template = pod_template
1252- generation_view .output_format = 'odt'
1253- generation_view .generate_persistent_doc (pod_template , 'odt' )
1254- used_template_uids .add (template_uid )
1225+ # Add an annotation to copy DMS files only after next edit
1226+ annot = IAnnotations (duplicated_mail )
1227+ annot .setdefault ('imio.dms.mail' , PersistentDict ()) # make sure the key exists
1228+ annot ['imio.dms.mail' ]['copy_dms_files_from' ] = original_mail .UID ()
12551229
12561230 if keep_annexes :
12571231 annexes = [sub_content .getId () for sub_content in original_mail .values () if IDmsAppendixFile .providedBy (sub_content )]
@@ -1268,6 +1242,39 @@ def duplicate(self, keep_category, keep_folder, keep_reply_to, keep_dms_files, k
12681242
12691243 return duplicated_mail
12701244
1245+ def copy_dms_files (self , original_mail ):
1246+ """Re-generate DMS files on this mail based on the templates used in the original mail."""
1247+ dms_files = [sub_content for sub_content in original_mail .values () if sub_content .portal_type == self .mainfile_type ]
1248+ used_template_uids = set ()
1249+ for dms_file in dms_files :
1250+ annot = IAnnotations (dms_file ).get ('documentgenerator' , {})
1251+
1252+ # Skip if document was not generated (could be scanned)
1253+ if not annot :
1254+ continue
1255+
1256+ # Skip if document was already generated from the same template
1257+ if annot .get ('template_uid' ) in used_template_uids :
1258+ continue
1259+
1260+ # Skip if document is final step of a mailing
1261+ document_generation_helper_view = getMultiAdapter ((self .context , self .request ), name = "document_generation_helper_view" )
1262+ requires_mailing = len (document_generation_helper_view .mailing_list ()) > 1
1263+ if requires_mailing and not annot .get ('need_mailing' ):
1264+ continue
1265+
1266+ # Generate a new document from the same template
1267+ template_uid = annot .get ('template_uid' )
1268+ generation_view = getMultiAdapter ((self .context , self .request ), name = "persistent-document-generation" )
1269+ pod_template = generation_view .get_pod_template (template_uid )
1270+ # Skip if it's a mailing loop template
1271+ if IMailingLoopTemplate .providedBy (pod_template ):
1272+ continue
1273+ generation_view .pod_template = pod_template
1274+ generation_view .output_format = 'odt'
1275+ generation_view .generate_persistent_doc (pod_template , 'odt' )
1276+ used_template_uids .add (template_uid )
1277+
12711278
12721279class Dummy (object ):
12731280 """dummy class that allows setting attributes"""
0 commit comments