summaryrefslogtreecommitdiffstats
path: root/welco/sources/mail/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'welco/sources/mail/views.py')
-rw-r--r--welco/sources/mail/views.py46
1 files changed, 45 insertions, 1 deletions
diff --git a/welco/sources/mail/views.py b/welco/sources/mail/views.py
index d162019..8a7b1fb 100644
--- a/welco/sources/mail/views.py
+++ b/welco/sources/mail/views.py
@@ -29,11 +29,15 @@ from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
from django.db.transaction import atomic
+from rest_framework import authentication, serializers, permissions, status
+from rest_framework.generics import GenericAPIView
+from rest_framework.response import Response
+
from welco.utils import response_for_json
from .models import Mail
from .forms import MailQualificationForm
-from .utils import get_maarch
+from .utils import get_maarch, MaarchError
logger = logging.getLogger(__name__)
@@ -149,3 +153,43 @@ def reject(request, *args, **kwargs):
def mail_count(request, *args, **kwargs):
count = Mail.objects.exclude(status__startswith='done-').count()
return response_for_json(request, {'count': count})
+
+
+class NotificationSerializer(serializers.Serializer):
+ mail_id = serializers.CharField()
+ content = serializers.CharField()
+
+
+class MailResponseAPIView(GenericAPIView):
+ permission_classes = (permissions.IsAuthenticated,)
+ serializer_class = NotificationSerializer
+
+ def post(self, request, *args, **kwargs):
+ serializer = self.get_serializer(data=request.data)
+ if not serializer.is_valid():
+ response = {'err': 1, 'err_desc': serializer.errors}
+ return Response(response, status.HTTP_400_BAD_REQUEST)
+ mail_id = serializer.validated_data['mail_id']
+ content = serializer.validated_data['content']
+ # for now, we only support maarch
+ if not mail_id.startswith('maarch-'):
+ response = {'err': 1, 'err_desc': 'only maarch is supported'}
+ return Response(response)
+ mail = Mail.objects.filter(external_id=mail_id).first()
+ if not mail:
+ response = {'err': 1, 'err_desc': 'unknown mail_id'}
+ return Response(response, status.HTTP_404_NOT_FOUND)
+ return self.maarch_response(mail, content)
+
+ def maarch_response(self, mail, content):
+ maarch = get_maarch()
+ if not maarch:
+ return Response({'err': 1, 'err_desc': 'maarch is unconfigured'})
+ mail_pk = int(mail.external_id.split('-', 1)[1])
+ try:
+ maarch.set_grc_response_status(mail_pk, content)
+ except MaarchError as e:
+ return Response({'err': 1, 'err_desc': str(e)})
+ return Response({'err': 0})
+
+mail_response = MailResponseAPIView.as_view()