Skip to content

Commit 2cec978

Browse files
committed
Moved copy dms files after duplication to execute after next edit
1 parent 83eabbb commit 2cec978

File tree

4 files changed

+55
-31
lines changed

4 files changed

+55
-31
lines changed

imio/dms/mail/subscribers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
# from zope.component.interfaces import ComponentLookupError
7373
from zope.annotation import IAnnotations
7474
from zope.component import getAdapter
75+
from zope.component import getMultiAdapter
7576
from zope.component import getSiteManager
7677
from zope.component import getUtility
7778
from zope.component import queryUtility
@@ -384,6 +385,16 @@ def dmsoutgoingmail_transition(mail, event):
384385
mail.portal_catalog.reindexObject(mail, idxs=("in_out_date",), update_metadata=0)
385386

386387

388+
def dmsoutgoingmail_modified(mail, event):
389+
annot = IAnnotations(mail).get('imio.dms.mail', {})
390+
copy_dms_files_from = annot.get('copy_dms_files_from')
391+
if copy_dms_files_from:
392+
del annot['copy_dms_files_from']
393+
original_mail = uuidToObject(copy_dms_files_from, unrestricted=True)
394+
odm_utils = getMultiAdapter((mail, mail.REQUEST), name="odm-utils")
395+
odm_utils.copy_dms_files(original_mail)
396+
397+
387398
def dv_handle_file_creation(obj, event):
388399
"""Intermediate function to avoid converting some files in documentviewer"""
389400
if obj.portal_type in DV_AVOIDED_TYPES:

imio/dms/mail/subscribers.zcml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@
9898
handler=".subscribers.dmsoutgoingmail_transition"
9999
/>
100100

101+
<subscriber
102+
for="imio.dms.mail.dmsmail.IImioDmsOutgoingMail
103+
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
104+
handler=".subscribers.dmsoutgoingmail_modified"
105+
/>
106+
101107
<!-- Various -->
102108
<subscriber
103109
for="plone.dexterity.interfaces.IDexterityContent

imio/dms/mail/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ def test_OdmUtilsMethods_duplicate(self):
931931
keep_annexes=True,
932932
link_to_duplicated=True,
933933
)
934-
self.assertEqual(duplicated_mail.keys(), ['012999900000004', 'a001'])
934+
self.assertEqual(duplicated_mail.keys(), ['a001', '012999900000004'])
935935
dms_file = duplicated_mail['012999900000004']
936936
annot = IAnnotations(dms_file)['documentgenerator']
937937
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
@@ -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

12721279
class Dummy(object):
12731280
"""dummy class that allows setting attributes"""

0 commit comments

Comments
 (0)