Skip to content

Commit 5a77836

Browse files
committed
Moved copy dms files after duplication to execute after next edit
1 parent 16203e0 commit 5a77836

3 files changed

Lines changed: 48 additions & 31 deletions

File tree

imio/dms/mail/subscribers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,16 @@ def dmsoutgoingmail_added(mail, event):
567567
zope.event.notify(ObjectModifiedEvent(mail, Attributes(ISigningBehavior, "ISigningBehavior.signers")))
568568

569569

570+
def dmsoutgoingmail_modified(mail, event):
571+
annot = IAnnotations(mail).get('imio.dms.mail', {})
572+
copy_dms_files_from = annot.get('copy_dms_files_from')
573+
if copy_dms_files_from:
574+
del annot['copy_dms_files_from']
575+
original_mail = uuidToObject(copy_dms_files_from, unrestricted=True)
576+
odm_utils = getMultiAdapter((mail, mail.REQUEST), name="odm-utils")
577+
odm_utils.copy_dms_files(original_mail)
578+
579+
570580
def dv_handle_file_creation(obj, event):
571581
"""Intermediate function to avoid converting some files in documentviewer"""
572582
if obj.portal_type in DV_AVOIDED_TYPES:

imio/dms/mail/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def test_OdmUtilsMethods_duplicate(self):
939939
keep_annexes=True,
940940
link_to_duplicated=True,
941941
)
942-
self.assertEqual(duplicated_mail.keys(), ['012999900000004', 'a001'])
942+
self.assertEqual(duplicated_mail.keys(), ['a001', '012999900000004'])
943943
dms_file = duplicated_mail['012999900000004']
944944
annot = IAnnotations(dms_file)['documentgenerator']
945945
self.assertEqual(annot['template_uid'], pod_template.UID())

imio/dms/mail/utils.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

12981305
class Dummy(object):
12991306
"""dummy class that allows setting attributes"""

0 commit comments

Comments
 (0)