welco/welco/views.py

115 lines
4.1 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.contrib.auth import logout as auth_logout
from django.contrib.auth import views as auth_views
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import resolve_url
from django import template
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
try:
from mellon.utils import get_idps
except ImportError:
get_idps = lambda: []
from sources.mail.views import Home as MailHome
from .qualif.models import Association, FormdefReference
from .kb.views import HomeZone as KbHomeZone
from .contacts.views import HomeZone as ContactsHomeZone
from .forms import QualificationForm
def login(request, *args, **kwargs):
if any(get_idps()):
if not 'next' in request.GET:
return HttpResponseRedirect(resolve_url('mellon_login'))
return HttpResponseRedirect(resolve_url('mellon_login') + '?next='
+ urllib.quote(request.GET.get('next')))
return auth_views.login(request, *args, **kwargs)
def logout(request, next_page=None):
if any(get_idps()):
return HttpResponseRedirect(resolve_url('mellon_logout'))
auth_logout(request)
if next_page is not None:
next_page = resolve_url(next_page)
else:
next_page = '/'
return HttpResponseRedirect(next_page)
class Qualification(TemplateView):
template_name = 'welco/qualification.html'
def get_context_data(self, **kwargs):
context = super(Qualification, self).get_context_data(**kwargs)
context['form'] = QualificationForm()
try:
context['association'] = Association.objects.get(
source_type=ContentType.objects.get(id=self.request.GET['source_type']),
source_pk=self.request.GET['source_pk'])
except Association.DoesNotExist:
pass
return context
def post(self, request, *args, **kwargs):
association, created = Association.objects.get_or_create(
source_type=ContentType.objects.get(id=request.POST['source_type']),
source_pk=request.POST['source_pk'])
if created:
association.save()
formdef_ref, created = FormdefReference.objects.get_or_create(
reference=request.POST['formdef_reference'])
if created:
formdef_ref.save()
association.formdefs.add(formdef_ref)
request.GET = request.POST
return self.get(request)
qualification = csrf_exempt(Qualification.as_view())
class Home(TemplateView):
template_name = 'welco/home.html'
def get_context_data(self, **kwargs):
context = super(Home, self).get_context_data(**kwargs)
context['source'] = MailHome(self.request)
context['kb'] = KbHomeZone(self.request)
context['contacts'] = ContactsHomeZone(self.request)
return context
home = Home.as_view()
@csrf_exempt
def qualification_done(request):
association = Association.objects.get(
source_type=ContentType.objects.get(id=request.POST['source_type']),
source_pk=request.POST['source_pk'])
association.triaged = True
association.save()
response = HttpResponse(content_type='application/json')
json.dump({'result': 'ok'}, response, indent=2)
return response