summaryrefslogtreecommitdiffstats
path: root/welco/sources/mail/views.py
blob: d16201955128ef29668569feea479ba006118132 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# welco - multichannel request processing
# Copyright (C) 2018  Entr'ouvert
#
# 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/>.

import json
import logging

from django import template
from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
from django.db.transaction import atomic

from welco.utils import response_for_json

from .models import Mail
from .forms import MailQualificationForm
from .utils import get_maarch


logger = logging.getLogger(__name__)

def viewer(request, *args, **kwargs):
    if not 'file' in request.GET:
        return HttpResponseRedirect('?file=')
    body = template.loader.get_template('welco/mail_viewer.html').render(
            request=request)
    return HttpResponse(body)


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'))

feeder = login_required(csrf_exempt(Feeder.as_view()))

class Home(object):
    source_key = 'mail'
    display_filter = True
    allow_reject = True

    def __init__(self, request, **kwargs):
        self.request = request
        self.kwargs = kwargs

    def get_queryset(self):
        return Mail.objects.exclude(status__startswith='done-')

    def get_template(self):
        return 'welco/mail_home.html'

    def render(self):
        context = {}
        context['mails'] = self.get_queryset().order_by('creation_timestamp')
        context['source_type'] = ContentType.objects.get_for_model(Mail)
        context['form'] = MailQualificationForm()
        context['source_form_url'] = Mail.get_qualification_form_submit_url()
        if self.allow_reject:
            context['reject_url'] = reverse('mail-reject')
        context['display_filter'] = self.display_filter
        tmpl = template.loader.get_template(self.get_template())
        return tmpl.render(context, request=self.request)


@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']
        mail.registered_mail_number = form.cleaned_data['registered_mail_number']
        mail.reference = form.cleaned_data['reference']
        mail.subject = form.cleaned_data['subject']
        mail.save()
    return HttpResponseRedirect(reverse('qualif-zone') +
            '?source_type=%s&source_pk=%s' % (request.POST['source_type'],
                request.POST['source_pk']))


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'}))

edit_note = login_required(csrf_exempt(EditNote.as_view()))


@login_required
def note(request, *args, **kwargs):
    mail = Mail.objects.get(id=kwargs['pk'])
    if not mail.note:
        mail.note = '+'
    return HttpResponse(mail.html_note())


@login_required
@csrf_exempt
def reject(request, *args, **kwargs):
    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'))
    return HttpResponse()


@login_required
def mail_count(request, *args, **kwargs):
    count = Mail.objects.exclude(status__startswith='done-').count()
    return response_for_json(request, {'count': count})