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 @@ ...@@ -71,63 +71,69 @@
<value> <string encoding="cdata"><![CDATA[ <value> <string encoding="cdata"><![CDATA[
"""\n """\n
The email ingestion script, called by ZMailIn upon reception of an email.\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 Finds out who sent the letter, finds the sender, discovers properties that may\n
be contained in the mail body (using Document_getPropertyListFromMail),\n be contained in the mail body (using Document_getPropertyListFromMail),\n
then creates and object from every attachment and sends notifications to the\n then creates and object from every attachment and sends notifications to the \n
sender.\n sender.\n
\n \n
If it returns anything, it is bounced back to the sender as an error message. No return\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 value means everything was fine.\n
"""\n """\n
\n \n
#from Products.ERP5Type.Message import Message\n # get sender from email headers\n
# translating doesn\'t make sense since we have no session, so no preferences\n raw_sender_email = theMail[\'headers\'][\'from\']\n
\n sender_email = context.findAddress(raw_sender_email)\n
noSenderInHeaderMsg = "no sender in headers"\n if sender_email is None:\n
noSenderMsg = "you are not in user database"\n print "no sender in headers"\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
return printed\n return printed\n
\n \n
# find sender\n # find sender in ERP5\n
context.log(senderemail)\n found_persons = context.portal_catalog(portal_type=\'Person\', reference=sender_email)\n
r = context.portal_catalog(portal_type=\'Email\', url_string=senderemail)\n if len(found_persons) == 0:\n
if len(r) == 0:\n msg = \'[DMS] You are not in user database\'\n
print noSenderMsg\n context.log(msg)\n
return printed\n return msg\n
if len(r) > 1:\n if len(found_persons) > 1:\n
print manySendersMsg\n msg = \'[DMS] Your email address has multiple entries\'\n
return printed\n context.log(msg)\n
return msg\n
\n \n
senderm = r[0]\n person = found_persons[0]\n
person = senderm.getParent()\n
context.log(script.getId(), \'ok, this address belongs to \' + person.getRelativeUrl())\n
\n \n
# get mail properties out of mail body\n
mailprops = context.Document_getPropertyDictFromMail(theMail[\'body\'])\n mailprops = context.Document_getPropertyDictFromMail(theMail[\'body\'])\n
context.log(mailprops)\n
\n \n
# create objects from attachments\n # try to ingest document for each attachment\n
try:\n try:\n
for fname, data in theMail[\'attachments\'].items():\n for attachment in theMail[\'attachment_list\']:\n
obj = context.portal_contributions.newContent(file_name=fname, \n file_name, content_type, data = attachment\n
data=data, \n kw = dict(file_name = file_name,\n
portal_type=mailprops.get(\'document_type\'), \n data = data,\n
user_login=person.getReference(),\n discover_metadata = 0)\n
**mailprops)\n new_content = context.portal_contributions.newContent(**kw)\n
gr = person.Person_getPrincipalGroup()\n merged_content = new_content.discoverMetadata(file_name = file_name)\n
obj.manage_setLocalRoles(person.getReference(), [\'Owner\',])\n if new_content == merged_content:\n
context.Document_notifyByEmail(address=senderemail, event=\'ingest\', object=obj)\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 except Exception, e:\n
context.log(e)\n # failure during ingestion, inform user to try again later\n
raise\n context.Document_notifyByEmail(sender_email, \n
return\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> ]]></string> </value>
...@@ -152,8 +158,6 @@ return\n ...@@ -152,8 +158,6 @@ return\n
<key> <string>_proxy_roles</string> </key> <key> <string>_proxy_roles</string> </key>
<value> <value>
<tuple> <tuple>
<string>Assignor</string>
<string>Author</string>
<string>Manager</string> <string>Manager</string>
</tuple> </tuple>
</value> </value>
...@@ -185,27 +189,27 @@ return\n ...@@ -185,27 +189,27 @@ return\n
<string>theMail</string> <string>theMail</string>
<string>_print_</string> <string>_print_</string>
<string>_print</string> <string>_print</string>
<string>noSenderInHeaderMsg</string>
<string>noSenderMsg</string>
<string>manySendersMsg</string>
<string>_getitem_</string> <string>_getitem_</string>
<string>fromraw</string> <string>raw_sender_email</string>
<string>_getattr_</string> <string>_getattr_</string>
<string>context</string> <string>context</string>
<string>senderemail</string> <string>sender_email</string>
<string>None</string> <string>None</string>
<string>r</string> <string>found_persons</string>
<string>len</string> <string>len</string>
<string>senderm</string> <string>msg</string>
<string>person</string> <string>person</string>
<string>script</string>
<string>mailprops</string> <string>mailprops</string>
<string>_getiter_</string> <string>_getiter_</string>
<string>fname</string> <string>attachment</string>
<string>file_name</string>
<string>content_type</string>
<string>data</string> <string>data</string>
<string>dict</string>
<string>kw</string>
<string>_apply_</string> <string>_apply_</string>
<string>obj</string> <string>new_content</string>
<string>gr</string> <string>merged_content</string>
<string>Exception</string> <string>Exception</string>
<string>e</string> <string>e</string>
</tuple> </tuple>
......
...@@ -71,59 +71,96 @@ ...@@ -71,59 +71,96 @@
<value> <string encoding="cdata"><![CDATA[ <value> <string encoding="cdata"><![CDATA[
"""\n """\n
Used by email ingestor (Document_ingestEmail) to notify the user\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 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 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 because consistency is checked also upon an attempt to validate).\n
\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 \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
\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 if pref is not None and len(pref) != 0:\n
pref=pref[0]\n pref=pref[0]\n
if pref == \'never\': return\n if pref == \'never\':\n
\n # do not send any email notifications at all \n
ob = kw[\'object\']\n return\n
if ob.getPortalType() == \'Memo\': # we don\'t check constistency for Memo XXX use type groups, or what?\n \n
res = \'\'\n if event in (\'ingestion_success\',):\n
else:\n # successful ingestion socheck if ingested document is consistent\n
res = ob.checkConsistency()\n if doc.getPortalType() == \'Memo\': \n
res = [c for c in res if c[1] == \'DocumentCoordinatesConstraint inconsistency\']\n # we don\'t check constistency for Memo XXX use type groups, or what?\n
\n isObjectConsistent = True\n
# shall we send?\n else:\n
send = (pref == \'always\' or len(res) > 0)\n consistency_result = doc.checkConsistency()\n
if not send: return\n consistency_result = [x for x in consistency_result if x[1] == \'DocumentCoordinatesConstraint inconsistency\']\n
\n isObjectConsistent = len(consistency_result) == 0\n
if len(res) > 0:\n if not isObjectConsistent:\n
errmsg = \'<br/>\'.join(str(c[3]) for c in res)\n error_msg = \'<br/>\'.join(str(c[3]) for x in consistency_result)\n
subjecttpl = \'ingested %(name)s - there was a problem\'\n subject_template = \'[DMS] Failure ingestion %(name)s\'\n
msgtpl = """Your document "%(name)s" was ingested.\n email_template = """Your document "%(name)s" was not successfuly ingested.\n
\n \n
The following problems were detected:\n The following problems were detected:\n
\n \n
%(errmsg)s\n %(error_msg)s\n
\n \n
click here: %(url)s/view to proceed and fix the problems.\n click here: %(url)s/view to proceed and fix the problems.\n
"""\n """\n
else:\n else:\n
errmsg = \'\'\n error_msg = \'\'\n
subjecttpl = \'ingested %(name)s\'\n subject_template = \'[DMS] Successful ingestion for %(name)s\'\n
msgtpl = """Your document "%(name)s" was successfully ingested.\n email_template = """Your document "%(name)s" was successfully ingested.\n
\n
click here: %(url)s/view to proceed with your work.\n
"""\n
\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 \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 \n
mto = kw[\'address\']\n # not always we have ingested document passed (i.e when failure happens)\n
mfro = \'dms@dms.nexedi.com\' # XXX this should be in preferences\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 \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> ]]></string> </value>
...@@ -142,7 +179,7 @@ context.MailHost.send(msg, mto, mfro, subject)\n ...@@ -142,7 +179,7 @@ context.MailHost.send(msg, mto, mfro, subject)\n
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>**kw</string> </value> <value> <string>email_to, event, doc, **kw</string> </value>
</item> </item>
<item> <item>
<key> <string>errors</string> </key> <key> <string>errors</string> </key>
...@@ -162,33 +199,44 @@ context.MailHost.send(msg, mto, mfro, subject)\n ...@@ -162,33 +199,44 @@ context.MailHost.send(msg, mto, mfro, subject)\n
<dictionary> <dictionary>
<item> <item>
<key> <string>co_argcount</string> </key> <key> <string>co_argcount</string> </key>
<value> <int>0</int> </value> <value> <int>3</int> </value>
</item> </item>
<item> <item>
<key> <string>co_varnames</string> </key> <key> <string>co_varnames</string> </key>
<value> <value>
<tuple> <tuple>
<string>email_to</string>
<string>event</string>
<string>doc</string>
<string>kw</string> <string>kw</string>
<string>_getattr_</string> <string>_getattr_</string>
<string>None</string>
<string>original_email</string>
<string>context</string> <string>context</string>
<string>portal</string>
<string>portal_preferences</string>
<string>portal_catalog</string>
<string>pref</string> <string>pref</string>
<string>None</string>
<string>len</string> <string>len</string>
<string>_getitem_</string> <string>_getitem_</string>
<string>ob</string> <string>True</string>
<string>res</string> <string>isObjectConsistent</string>
<string>consistency_result</string>
<string>append</string> <string>append</string>
<string>$append0</string> <string>$append0</string>
<string>_getiter_</string> <string>_getiter_</string>
<string>c</string> <string>x</string>
<string>send</string> <string>error_msg</string>
<string>errmsg</string> <string>subject_template</string>
<string>subjecttpl</string> <string>email_template</string>
<string>msgtpl</string> <string>email_from</string>
<string>dict</string>
<string>email_kw</string>
<string>getattr</string>
<string>subject</string> <string>subject</string>
<string>msg</string> <string>_write_</string>
<string>mto</string> <string>original_email_text</string>
<string>mfro</string> <string>email_text</string>
</tuple> </tuple>
</value> </value>
</item> </item>
......
...@@ -76,6 +76,7 @@ ...@@ -76,6 +76,7 @@
<list> <list>
<string>my_preferred_document_file_name_regular_expression</string> <string>my_preferred_document_file_name_regular_expression</string>
<string>my_preferred_document_reference_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_ingestion_email_notification</string>
<string>my_preferred_document_reference_method_id</string> <string>my_preferred_document_reference_method_id</string>
</list> </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