129 lines
4.4 KiB
Python
129 lines
4.4 KiB
Python
# welco - multichannel request processing
|
|
# Copyright (C) 2015 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
|
|
|
|
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 welco.utils import response_for_json
|
|
|
|
from .models import Mail
|
|
from .forms import MailQualificationForm
|
|
|
|
def viewer(request, *args, **kwargs):
|
|
if not 'file' in request.GET:
|
|
return HttpResponseRedirect('?file=')
|
|
body = template.loader.get_template('welco/mail_viewer.html').render(
|
|
RequestContext(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 = csrf_exempt(Feeder.as_view())
|
|
|
|
class Home(object):
|
|
source_key = 'mail'
|
|
display_filter = False
|
|
allow_reject = True
|
|
|
|
def __init__(self, request):
|
|
self.request = request
|
|
|
|
def get_queryset(self):
|
|
return Mail.objects.exclude(status__startswith='done-')
|
|
|
|
def render(self):
|
|
context = RequestContext(self.request)
|
|
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('welco/mail_home.html')
|
|
return tmpl.render(context)
|
|
|
|
|
|
@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.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):
|
|
Mail.objects.filter(id=request.POST['source_pk']).delete()
|
|
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})
|