2015-07-03 11:04:16 +02:00
|
|
|
# welco - multichannel request processing
|
2018-05-19 16:38:57 +02:00
|
|
|
# Copyright (C) 2018 Entr'ouvert
|
2015-07-03 11:04:16 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-10-11 14:38:41 +02:00
|
|
|
import json
|
2018-05-19 16:38:57 +02:00
|
|
|
import logging
|
2015-10-11 14:38:41 +02:00
|
|
|
|
2015-07-03 11:48:16 +02:00
|
|
|
from django import template
|
2015-10-11 14:38:41 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2015-07-08 21:36:05 +02:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2015-09-21 09:58:32 +02:00
|
|
|
from django.contrib import messages
|
2020-04-02 21:10:49 +02:00
|
|
|
from django.urls import reverse
|
2015-07-03 11:48:16 +02:00
|
|
|
from django.template import RequestContext
|
2015-07-03 13:11:24 +02:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2015-09-21 09:58:32 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2015-08-31 15:45:37 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2015-09-21 09:58:32 +02:00
|
|
|
from django.views.generic import TemplateView
|
2018-05-19 16:38:57 +02:00
|
|
|
from django.db.transaction import atomic
|
2015-07-03 11:04:16 +02:00
|
|
|
|
2018-11-07 13:49:31 +01:00
|
|
|
from rest_framework import authentication, serializers, permissions, status
|
|
|
|
from rest_framework.generics import GenericAPIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2015-11-01 20:41:17 +01:00
|
|
|
from welco.utils import response_for_json
|
|
|
|
|
2015-07-03 11:48:16 +02:00
|
|
|
from .models import Mail
|
2015-08-31 15:45:37 +02:00
|
|
|
from .forms import MailQualificationForm
|
2018-11-07 13:49:31 +01:00
|
|
|
from .utils import get_maarch, MaarchError
|
2018-05-19 16:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2015-07-03 11:48:16 +02:00
|
|
|
|
2021-01-11 20:10:12 +01:00
|
|
|
|
2015-07-03 11:04:16 +02:00
|
|
|
def viewer(request, *args, **kwargs):
|
2015-07-03 13:11:24 +02:00
|
|
|
if not 'file' in request.GET:
|
|
|
|
return HttpResponseRedirect('?file=')
|
|
|
|
body = template.loader.get_template('welco/mail_viewer.html').render(request=request)
|
|
|
|
return HttpResponse(body)
|
2015-07-03 11:48:16 +02:00
|
|
|
|
|
|
|
|
2015-09-21 09:58:32 +02:00
|
|
|
class Feeder(TemplateView):
|
|
|
|
template_name = 'welco/mail_feeder.html'
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
for upload in request.FILES.getlist('mail'):
|
|
|
|
mail = Mail(content=upload)
|
|
|
|
mail.save()
|
|
|
|
messages.info(request, _('%d files uploaded successfully.') % len(request.FILES.getlist('mail')))
|
|
|
|
return HttpResponseRedirect(reverse('mail-feeder'))
|
|
|
|
|
2021-01-11 20:10:12 +01:00
|
|
|
|
2016-09-21 08:11:10 +02:00
|
|
|
feeder = login_required(csrf_exempt(Feeder.as_view()))
|
2015-09-21 09:58:32 +02:00
|
|
|
|
2021-01-11 20:10:12 +01:00
|
|
|
|
2015-07-03 11:48:16 +02:00
|
|
|
class Home(object):
|
2015-09-24 15:15:10 +02:00
|
|
|
source_key = 'mail'
|
2016-04-12 13:33:10 +02:00
|
|
|
display_filter = True
|
2015-10-28 16:04:48 +01:00
|
|
|
allow_reject = True
|
2015-09-24 15:15:10 +02:00
|
|
|
|
2016-03-22 13:25:48 +01:00
|
|
|
def __init__(self, request, **kwargs):
|
2015-07-03 11:48:16 +02:00
|
|
|
self.request = request
|
2016-03-22 13:25:48 +01:00
|
|
|
self.kwargs = kwargs
|
2015-07-03 11:48:16 +02:00
|
|
|
|
2015-10-09 14:59:29 +02:00
|
|
|
def get_queryset(self):
|
|
|
|
return Mail.objects.exclude(status__startswith='done-')
|
|
|
|
|
2016-03-22 13:25:48 +01:00
|
|
|
def get_template(self):
|
|
|
|
return 'welco/mail_home.html'
|
|
|
|
|
2015-07-03 11:48:16 +02:00
|
|
|
def render(self):
|
2017-12-28 09:52:27 +01:00
|
|
|
context = {}
|
2015-10-09 14:59:29 +02:00
|
|
|
context['mails'] = self.get_queryset().order_by('creation_timestamp')
|
2015-07-08 21:36:05 +02:00
|
|
|
context['source_type'] = ContentType.objects.get_for_model(Mail)
|
2015-10-09 14:01:59 +02:00
|
|
|
context['form'] = MailQualificationForm()
|
|
|
|
context['source_form_url'] = Mail.get_qualification_form_submit_url()
|
2015-10-28 16:04:48 +01:00
|
|
|
if self.allow_reject:
|
|
|
|
context['reject_url'] = reverse('mail-reject')
|
2015-10-11 19:18:08 +02:00
|
|
|
context['display_filter'] = self.display_filter
|
2016-03-22 13:25:48 +01:00
|
|
|
tmpl = template.loader.get_template(self.get_template())
|
2017-12-28 09:52:27 +01:00
|
|
|
return tmpl.render(context, request=self.request)
|
2015-08-31 15:45:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def qualification_save(request, *args, **kwargs):
|
|
|
|
mail = Mail.objects.get(id=request.POST['source_pk'])
|
|
|
|
form = MailQualificationForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
mail.post_date = form.cleaned_data['post_date']
|
2015-10-10 18:21:37 +02:00
|
|
|
mail.registered_mail_number = form.cleaned_data['registered_mail_number']
|
2016-10-12 07:32:37 +02:00
|
|
|
mail.reference = form.cleaned_data['reference']
|
2016-06-14 15:34:04 +02:00
|
|
|
mail.subject = form.cleaned_data['subject']
|
2015-08-31 15:45:37 +02:00
|
|
|
mail.save()
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse('qualif-zone')
|
|
|
|
+ '?source_type=%s&source_pk=%s' % (request.POST['source_type'], request.POST['source_pk'])
|
|
|
|
)
|
2015-10-11 14:38:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditNote(TemplateView):
|
|
|
|
template_name = 'welco/mail_edit_note.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(EditNote, self).get_context_data(**kwargs)
|
|
|
|
context['mail'] = Mail.objects.get(id=self.request.GET['mail'])
|
|
|
|
return context
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
mail = Mail.objects.get(id=self.request.GET['mail'])
|
|
|
|
mail.note = self.request.POST['note']
|
|
|
|
mail.save()
|
|
|
|
return HttpResponse(json.dumps({'result': 'ok'}))
|
|
|
|
|
2021-01-11 20:10:12 +01:00
|
|
|
|
2015-10-11 14:38:41 +02:00
|
|
|
edit_note = login_required(csrf_exempt(EditNote.as_view()))
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def note(request, *args, **kwargs):
|
|
|
|
mail = Mail.objects.get(id=kwargs['pk'])
|
2016-02-26 12:34:26 +01:00
|
|
|
if not mail.note:
|
|
|
|
mail.note = '+'
|
|
|
|
return HttpResponse(mail.html_note())
|
2015-10-28 16:04:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@csrf_exempt
|
|
|
|
def reject(request, *args, **kwargs):
|
2018-05-19 16:38:57 +02:00
|
|
|
maarch = get_maarch()
|
|
|
|
mail = Mail.objects.filter(id=request.POST['source_pk']).first()
|
|
|
|
if mail:
|
|
|
|
try:
|
|
|
|
with atomic():
|
|
|
|
if maarch and mail.external_id and mail.external_id.startswith('maarch-'):
|
|
|
|
mail_pk = mail.external_id.split('-', 1)[1]
|
|
|
|
maarch.set_grc_refused_status(mail_pk)
|
|
|
|
mail.delete()
|
|
|
|
except Exception:
|
|
|
|
logger.exception('rejection request to maarch failed')
|
|
|
|
messages.error(request, _('Rejection request to Maarch failed'))
|
2015-10-28 16:04:48 +01:00
|
|
|
return HttpResponse()
|
2015-11-01 20:41:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def mail_count(request, *args, **kwargs):
|
|
|
|
count = Mail.objects.exclude(status__startswith='done-').count()
|
|
|
|
return response_for_json(request, {'count': count})
|
2018-11-07 13:49:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
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})
|
|
|
|
|
2021-01-11 20:10:12 +01:00
|
|
|
|
2018-11-07 13:49:31 +01:00
|
|
|
mail_response = MailResponseAPIView.as_view()
|