Commit 5a25f0f8 authored by Ivan Tyagov's avatar Ivan Tyagov

Notify user for succesful or not document email ingestion per email.

Add to DMS preferences preferred email address.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@14516 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 680e4da3
......@@ -71,63 +71,69 @@
<value> <string encoding="cdata"><![CDATA[
"""\n
The email ingestion script, called by ZMailIn upon reception of an email.\n
Finds out who sent the letter, finds the sender, discovers properties that may\n
be contained in the mail body (using Document_getPropertyListFromMail),\n
then creates and object from every attachment and sends notifications to the\n
sender.\n
The email ingestion script, called by ZMailIn upon reception of an email.\n
Finds out who sent the letter, finds the sender, discovers properties that may\n
be contained in the mail body (using Document_getPropertyListFromMail),\n
then creates and object from every attachment and sends notifications to the \n
sender.\n
\n
If it returns anything, it is bounced back to the sender as an error message. No return\n
value means everything was fine.\n
If it returns anything, it is bounced back to the sender as an error message. No return\n
value means everything was fine.\n
"""\n
\n
#from Products.ERP5Type.Message import Message\n
# translating doesn\'t make sense since we have no session, so no preferences\n
\n
noSenderInHeaderMsg = "no sender in headers"\n
noSenderMsg = "you are not in user database"\n
manySendersMsg = "very serious error: your address has multiple entries"\n
\n
# get sender from headers\n
fromraw = theMail[\'headers\'][\'from\']\n
\n
senderemail = context.findAddress(fromraw)\n
if senderemail is None:\n
print noSenderInHeaderMsg\n
# get sender from email headers\n
raw_sender_email = theMail[\'headers\'][\'from\']\n
sender_email = context.findAddress(raw_sender_email)\n
if sender_email is None:\n
print "no sender in headers"\n
return printed\n
\n
# find sender\n
context.log(senderemail)\n
r = context.portal_catalog(portal_type=\'Email\', url_string=senderemail)\n
if len(r) == 0:\n
print noSenderMsg\n
return printed\n
if len(r) > 1:\n
print manySendersMsg\n
return printed\n
# find sender in ERP5\n
found_persons = context.portal_catalog(portal_type=\'Person\', reference=sender_email)\n
if len(found_persons) == 0:\n
msg = \'[DMS] You are not in user database\'\n
context.log(msg)\n
return msg\n
if len(found_persons) > 1:\n
msg = \'[DMS] Your email address has multiple entries\'\n
context.log(msg)\n
return msg\n
\n
senderm = r[0]\n
person = senderm.getParent()\n
context.log(script.getId(), \'ok, this address belongs to \' + person.getRelativeUrl())\n
person = found_persons[0]\n
\n
# get mail properties out of mail body\n
mailprops = context.Document_getPropertyDictFromMail(theMail[\'body\'])\n
context.log(mailprops)\n
\n
# create objects from attachments\n
# try to ingest document for each attachment\n
try:\n
for fname, data in theMail[\'attachments\'].items():\n
obj = context.portal_contributions.newContent(file_name=fname, \n
data=data, \n
portal_type=mailprops.get(\'document_type\'), \n
user_login=person.getReference(),\n
**mailprops)\n
gr = person.Person_getPrincipalGroup()\n
obj.manage_setLocalRoles(person.getReference(), [\'Owner\',])\n
context.Document_notifyByEmail(address=senderemail, event=\'ingest\', object=obj)\n
for attachment in theMail[\'attachment_list\']:\n
file_name, content_type, data = attachment\n
kw = dict(file_name = file_name,\n
data = data,\n
discover_metadata = 0)\n
new_content = context.portal_contributions.newContent(**kw)\n
merged_content = new_content.discoverMetadata(file_name = file_name)\n
if new_content == merged_content:\n
# we have a new document in the system, so set local role owner to email sender\n
new_content.manage_setLocalRoles(person.getReference(), [\'Owner\',])\n
\n
new_content.immediateReindexObject()\n
merged_content.immediateReindexObject()\n
# notify sender for successful email ingestion\n
context.Document_notifyByEmail(sender_email, \'ingestion_success\', merged_content)\n
context.log(\'[DMS] Finished email ingestion for %s <%s> (%s)\' \n
%(person.getTitle(), sender_email, file_name))\n
return \'OK\'\n
except Exception, e:\n
context.log(e)\n
raise\n
return\n
# failure during ingestion, inform user to try again later\n
context.Document_notifyByEmail(sender_email, \n
\'ingestion_failure\', \n
context, \n
original_email = theMail)\n
context.log(\'[DMS] Failure (%s) email ingestion for %s <%s> (%s)\' \n
%(e, person.getTitle(), sender_email, file_name))\n
raise Exception, e\n
return \'ERROR\'\n
]]></string> </value>
......@@ -152,8 +158,6 @@ return\n
<key> <string>_proxy_roles</string> </key>
<value>
<tuple>
<string>Assignor</string>
<string>Author</string>
<string>Manager</string>
</tuple>
</value>
......@@ -185,27 +189,27 @@ return\n
<string>theMail</string>
<string>_print_</string>
<string>_print</string>
<string>noSenderInHeaderMsg</string>
<string>noSenderMsg</string>
<string>manySendersMsg</string>
<string>_getitem_</string>
<string>fromraw</string>
<string>raw_sender_email</string>
<string>_getattr_</string>
<string>context</string>
<string>senderemail</string>
<string>sender_email</string>
<string>None</string>
<string>r</string>
<string>found_persons</string>
<string>len</string>
<string>senderm</string>
<string>msg</string>
<string>person</string>
<string>script</string>
<string>mailprops</string>
<string>_getiter_</string>
<string>fname</string>
<string>attachment</string>
<string>file_name</string>
<string>content_type</string>
<string>data</string>
<string>dict</string>
<string>kw</string>
<string>_apply_</string>
<string>obj</string>
<string>gr</string>
<string>new_content</string>
<string>merged_content</string>
<string>Exception</string>
<string>e</string>
</tuple>
......
......@@ -71,59 +71,96 @@
<value> <string encoding="cdata"><![CDATA[
"""\n
Used by email ingestor (Document_ingestEmail) to notify the user\n
about ingestion of a document (can also be used by other ingestion channels).\n
Checks consistency and lets the user know if everything is ok (this is a bit of a duplication\n
because consistency is checked also upon an attempt to validate).\n
Used by email ingestor (Document_ingestEmail) to notify the user\n
about ingestion of a document (can also be used by other ingestion channels).\n
Checks consistency and lets the user know if everything is ok (this is a bit of a duplication\n
because consistency is checked also upon an attempt to validate).\n
\n
Preferences decide if the notification is sent always, only if something is wrong or never.\n
Preferences decide if the notification is sent always, only if something is wrong or never.\n
\n
Notifcation contains url of the document, so that one can click and do something.\n
Notifcation contains url of the document, so that one can click and do something.\n
"""\n
\n
pref = context.portal_preferences.getPreferredDocumentIngestionEmailNotification()\n
# check if we send or not email at all\n
original_email = kw.get(\'original_email\', None)\n
portal = context.getPortalObject()\n
portal_preferences = portal.portal_preferences\n
portal_catalog = portal.portal_catalog\n
pref = portal_preferences.getPreferredDocumentIngestionEmailNotification()\n
if pref is not None and len(pref) != 0:\n
pref=pref[0]\n
if pref == \'never\': return\n
\n
ob = kw[\'object\']\n
if ob.getPortalType() == \'Memo\': # we don\'t check constistency for Memo XXX use type groups, or what?\n
res = \'\'\n
else:\n
res = ob.checkConsistency()\n
res = [c for c in res if c[1] == \'DocumentCoordinatesConstraint inconsistency\']\n
\n
# shall we send?\n
send = (pref == \'always\' or len(res) > 0)\n
if not send: return\n
\n
if len(res) > 0:\n
errmsg = \'<br/>\'.join(str(c[3]) for c in res)\n
subjecttpl = \'ingested %(name)s - there was a problem\'\n
msgtpl = """Your document "%(name)s" was ingested.\n
if pref == \'never\':\n
# do not send any email notifications at all \n
return\n
\n
if event in (\'ingestion_success\',):\n
# successful ingestion socheck if ingested document is consistent\n
if doc.getPortalType() == \'Memo\': \n
# we don\'t check constistency for Memo XXX use type groups, or what?\n
isObjectConsistent = True\n
else:\n
consistency_result = doc.checkConsistency()\n
consistency_result = [x for x in consistency_result if x[1] == \'DocumentCoordinatesConstraint inconsistency\']\n
isObjectConsistent = len(consistency_result) == 0\n
if not isObjectConsistent:\n
error_msg = \'<br/>\'.join(str(c[3]) for x in consistency_result)\n
subject_template = \'[DMS] Failure ingestion %(name)s\'\n
email_template = """Your document "%(name)s" was not successfuly ingested.\n
\n
The following problems were detected:\n
\n
%(errmsg)s\n
%(error_msg)s\n
\n
click here: %(url)s/view to proceed and fix the problems.\n
"""\n
else:\n
errmsg = \'\'\n
subjecttpl = \'ingested %(name)s\'\n
msgtpl = """Your document "%(name)s" was successfully ingested.\n
\n
click here: %(url)s/view to proceed with your work.\n
"""\n
else:\n
error_msg = \'\'\n
subject_template = \'[DMS] Successful ingestion for %(name)s\'\n
email_template = """Your document "%(name)s" was successfully ingested.\n
\n
subject = subjecttpl % {\'name\': ob.getSourceReference()}\n
click here: %(url)s/view to proceed with your work."""\n
elif event in (\'ingestion_failure\',):\n
# there was an error during ingestion process, inform user to try again\n
error_msg = \'Failure during ingestion.\'\n
subject_template = \'[DMS] Unsuccessful ingestion\'\n
email_template = """Your document was not successfuly ingested.\n
\n
The following problems were detected:\n
\n
msg = msgtpl % {\'url\': ob.absolute_url(), \'name\': ob.getSourceReference(), \'errmsg\': errmsg}\n
%(error_msg)s\n
\n
Please try again after some time. \n
We applogize for inconvinience."""\n
\n
\n
# compose email\n
email_from = portal_preferences.getPreferredDocumentEmailIngestionAddress()\n
email_kw = dict(error_msg = error_msg)\n
\n
mto = kw[\'address\']\n
mfro = \'dms@dms.nexedi.com\' # XXX this should be in preferences\n
# not always we have ingested document passed (i.e when failure happens)\n
if getattr(doc, \'getSourceReference\', None) is not None:\n
subject = subject_template % {\'name\': doc.getSourceReference()}\n
email_kw[\'name\'] = doc.getSourceReference()\n
email_kw[\'url\'] = doc.absolute_url()\n
else:\n
subject = subject_template\n
\n
context.MailHost.send(msg, mto, mfro, subject)\n
# get original email text\n
original_email_text = None\n
if original_email is not None:\n
original_email_text = \'Subject:%s\\n\\nBody:\\n%s\' %(original_email[\'headers\'][\'subject\'], \n
original_email[\'body\'])\n
email_text = email_template % email_kw\n
# add part of original email if failure happens\n
if event in (\'ingestion_failure\',) and original_email_text is not None:\n
email_text = \'%s\\n\\n%s\\nOriginal email text (see below):\\n\\n%s\' %(email_text, \n
\'=\'*80, \n
original_email_text)\n
# send email\n
context.MailHost.send(email_text, \n
email_to, \n
email_from, \n
subject)\n
]]></string> </value>
......@@ -142,7 +179,7 @@ context.MailHost.send(msg, mto, mfro, subject)\n
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>**kw</string> </value>
<value> <string>email_to, event, doc, **kw</string> </value>
</item>
<item>
<key> <string>errors</string> </key>
......@@ -162,33 +199,44 @@ context.MailHost.send(msg, mto, mfro, subject)\n
<dictionary>
<item>
<key> <string>co_argcount</string> </key>
<value> <int>0</int> </value>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>co_varnames</string> </key>
<value>
<tuple>
<string>email_to</string>
<string>event</string>
<string>doc</string>
<string>kw</string>
<string>_getattr_</string>
<string>None</string>
<string>original_email</string>
<string>context</string>
<string>portal</string>
<string>portal_preferences</string>
<string>portal_catalog</string>
<string>pref</string>
<string>None</string>
<string>len</string>
<string>_getitem_</string>
<string>ob</string>
<string>res</string>
<string>True</string>
<string>isObjectConsistent</string>
<string>consistency_result</string>
<string>append</string>
<string>$append0</string>
<string>_getiter_</string>
<string>c</string>
<string>send</string>
<string>errmsg</string>
<string>subjecttpl</string>
<string>msgtpl</string>
<string>x</string>
<string>error_msg</string>
<string>subject_template</string>
<string>email_template</string>
<string>email_from</string>
<string>dict</string>
<string>email_kw</string>
<string>getattr</string>
<string>subject</string>
<string>msg</string>
<string>mto</string>
<string>mfro</string>
<string>_write_</string>
<string>original_email_text</string>
<string>email_text</string>
</tuple>
</value>
</item>
......
......@@ -76,6 +76,7 @@
<list>
<string>my_preferred_document_file_name_regular_expression</string>
<string>my_preferred_document_reference_regular_expression</string>
<string>my_preferred_document_email_ingestion_address</string>
<string>my_preferred_document_ingestion_email_notification</string>
<string>my_preferred_document_reference_method_id</string>
</list>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment