From a306737700741838cc33184b64d690d21a065685 Mon Sep 17 00:00:00 2001 From: zhengxie Date: Sat, 13 Oct 2018 15:08:35 +0800 Subject: [PATCH] Fix post office connection and handle invitation error --- requirements.txt | 2 +- seahub/api2/endpoints/invitations.py | 20 +++++++++++++++----- seahub/invitations/models.py | 2 +- seahub/utils/mail.py | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/requirements.txt b/requirements.txt index a178fbbf0c..2dd330da82 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ six==1.11.0 Pillow==4.3.0 Django==1.11.15 django-compressor==2.2 -django-post-office==3.0.4 +git+git://github.com/haiwen/django-post_office.git@2312cf240363721f737b5ac8eb86ab8cb255938f#egg=django-post_office django-statici18n==1.7.0 djangorestframework==3.3.3 git+git://github.com/haiwen/django-constance.git@8508ff29141732190faff51d5c2b5474da297732#egg=django-constance[database] diff --git a/seahub/api2/endpoints/invitations.py b/seahub/api2/endpoints/invitations.py index ba917b31a6..cc885907ca 100644 --- a/seahub/api2/endpoints/invitations.py +++ b/seahub/api2/endpoints/invitations.py @@ -6,6 +6,7 @@ from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView +from post_office.models import STATUS from seahub.api2.authentication import TokenAuthentication from seahub.api2.permissions import CanInviteGuest @@ -69,9 +70,12 @@ class InvitationsView(APIView): i = Invitation.objects.add(inviter=request.user.username, accepter=accepter) - i.send_to(email=accepter) - - return Response(i.to_dict(), status=201) + m = i.send_to(email=accepter) + if m.status == STATUS.sent: + return Response(i.to_dict(), status=201) + else: + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, + _('Internal Server Error')) class InvitationsBatchView(APIView): @@ -132,7 +136,13 @@ class InvitationsBatchView(APIView): except User.DoesNotExist: i = Invitation.objects.add(inviter=request.user.username, accepter=accepter) - i.send_to(email=accepter) - result['success'].append(i.to_dict()) + m = i.send_to(email=accepter) + if m.status == STATUS.sent: + result['success'].append(i.to_dict()) + else: + result['failed'].append({ + 'email': accepter, + 'error_msg': _('Internal Server Error'), + }) return Response(result) diff --git a/seahub/invitations/models.py b/seahub/invitations/models.py index 759ad12ebb..8bb34d94ba 100644 --- a/seahub/invitations/models.py +++ b/seahub/invitations/models.py @@ -89,7 +89,7 @@ class Invitation(models.Model): # context).rstrip() subject = _('%(user)s invited you to join %(site_name)s.') % { 'user': self.inviter, 'site_name': get_site_name()} - send_html_email_with_dj_template( + return send_html_email_with_dj_template( email, dj_template='invitations/invitation_email.html', context=context, subject=subject, diff --git a/seahub/utils/mail.py b/seahub/utils/mail.py index e3f8764a0e..a70ad68048 100644 --- a/seahub/utils/mail.py +++ b/seahub/utils/mail.py @@ -42,7 +42,7 @@ def send_html_email_with_dj_template(recipients, subject, dj_template, t = loader.get_template(dj_template) html_message = t.render(context) - mail.send(recipients, sender=sender, template=template, context=context, + return mail.send(recipients, sender=sender, template=template, context=context, subject=subject, message=message, html_message=html_message, headers=headers, priority=priority, backend=backend)