summaryrefslogtreecommitdiff
path: root/service/pixelated/resources/mails_resource.py
diff options
context:
space:
mode:
authorAlexandre Pretto Nunes <anunes@thoughtworks.com>2015-01-28 14:42:47 -0200
committerPixpoa pairing <pixpoapairing@pixelated-project.org>2015-01-29 14:55:22 -0200
commit4ac714acfd3192a397028f1260c5929e80ef15b5 (patch)
tree119377886ac719318bdbe4a6b4e7533dd3fbd936 /service/pixelated/resources/mails_resource.py
parent1af7c273015246c0cf4130278b2c17fca9de563a (diff)
#249 make the sending process a deferred
Diffstat (limited to 'service/pixelated/resources/mails_resource.py')
-rw-r--r--service/pixelated/resources/mails_resource.py18
1 files changed, 14 insertions, 4 deletions
diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py
index a6eb0fe0..40b29cfc 100644
--- a/service/pixelated/resources/mails_resource.py
+++ b/service/pixelated/resources/mails_resource.py
@@ -1,6 +1,7 @@
import json
from pixelated.adapter.model.mail import InputMail
-from pixelated.resources import respond_json
+from pixelated.resources import respond_json, respond_json_deferred
+from twisted.web import server
from twisted.web.resource import Resource
@@ -102,10 +103,19 @@ class MailsResource(Resource):
draft_id = content_dict.get('ident')
if draft_id:
self._search_engine.remove_from_index(draft_id)
- _mail = self._mail_service.send(draft_id, _mail)
- self._search_engine.index_mail(_mail)
+ sendDeferred = self._mail_service.send(draft_id, _mail)
- return respond_json(_mail.as_dict(), request)
+ def onSuccess(mail):
+ self._search_engine.index_mail(mail)
+ respond_json_deferred(mail.as_dict(), request)
+
+ def onError(error):
+ return respond_json_deferred({'message': _format_exception(error)}, request, status_code=422)
+
+ sendDeferred.addCallback(onSuccess)
+ sendDeferred.addErrback(onError)
+
+ return server.NOT_DONE_YET
except Exception as error:
return respond_json({'message': _format_exception(error)}, request, status_code=422)