welco/welco/contrib/alfortville/views.py

251 lines
8.8 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.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse, HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView, DetailView
from hobo.agent.common.models import Role
from .models import Inbox
from welco.sources.mail.models import Mail
from welco.sources.mail.views import Home as MailHome
from welco.qualif.models import Association
from welco.utils import get_wcs_data, get_wcs_options, response_for_json
from welco.views import HomeMail as HomeScreen
class DgsMailHome(MailHome):
display_filter = True
allow_reject = False
def get_queryset(self):
return Mail.objects.filter(status='done-qualif')
class Dgs(HomeScreen):
source_klass = DgsMailHome
def check_user_ok(self):
return 'DGS' in [x.name for x in self.request.user.groups.all()]
dgs = login_required(Dgs.as_view())
class DgaMailHome(MailHome):
display_filter = True
allow_reject = False
def filter_formdef_condition(self, formdef):
roles = set()
for function_role in formdef.get('functions', {}).values():
if function_role.get('role', {}).get('name', '').startswith('DGA'):
roles.add(function_role.get('role').get('slug'))
return self.user_roles.intersection(roles)
def get_queryset(self):
mellon = self.request.session['mellon_session']
params = {'NameID': mellon['name_id_content']}
self.user_roles = set([x['slug'] for x in get_wcs_data('api/user/', params).get('user_roles')])
formdef_references = get_wcs_options('api/formdefs/', self.filter_formdef_condition)
return Mail.objects.filter(status='done-dgs',
associations__formdef_reference__in=[x[1][0][0] for x in formdef_references])
class Dga(HomeScreen):
source_klass = DgaMailHome
def check_user_ok(self):
user_roles = [x.name for x in self.request.user.groups.all()]
return any([x for x in user_roles if x['name'].startswith('DGA')])
dga = login_required(Dga.as_view())
class Copies(DetailView):
model = Mail
template_name = 'alfortville/copies.html'
def get_context_data(self, **kwargs):
context = super(Copies, self).get_context_data(**kwargs)
checked_dicts = {
Inbox.MANDATORY_AVIS: {},
Inbox.AVIS: {},
Inbox.INFO: {},
}
for inbox in Inbox.objects.filter(source_pk=self.object.id):
checked_dicts[inbox.subtype][inbox.role_slug] = True
context['checked_info'] = checked_dicts[Inbox.INFO]
context['checked_avis'] = checked_dicts[Inbox.AVIS]
context['checked_mandatory_avis'] = checked_dicts[Inbox.MANDATORY_AVIS]
context['roles'] = get_wcs_data('api/roles').get('data')
return context
def post(self, request, *args, **kwargs):
lists = {
Inbox.MANDATORY_AVIS: request.POST.getlist('mandatory_avis'),
Inbox.AVIS: request.POST.getlist('avis'),
Inbox.INFO: request.POST.getlist('info'),
}
for subtype in lists.keys():
Inbox.objects.filter(subtype=subtype).exclude(
role_slug__in=lists[subtype]).delete()
mail_content_type = ContentType.objects.get_for_model(Mail)
for subtype in lists.keys():
for role_slug in lists[subtype]:
Inbox.objects.get_or_create(source_type=mail_content_type,
source_pk=kwargs['pk'], role_slug=role_slug, subtype=subtype)
return HttpResponseRedirect('.')
copies = login_required(Copies.as_view())
def copies_ajax(request, *args, **kwargs):
roles_by_slug = {}
for role in get_wcs_data('api/roles').get('data'):
roles_by_slug[role['slug']] = role
lists = {
Inbox.MANDATORY_AVIS: [],
Inbox.AVIS: [],
Inbox.INFO: []
}
for inbox in Inbox.objects.filter(source_pk=kwargs.get('pk')):
lists[inbox.subtype].append(roles_by_slug[inbox.role_slug]['text'])
response = HttpResponse(content_type='application/json')
json.dump({'info': ', '.join(lists[Inbox.INFO]) or '-',
'avis': ', '.join(lists[Inbox.AVIS]) or '-',
'mandatory_avis': ', '.join(lists[Inbox.MANDATORY_AVIS]) or '-'},
response, indent=2)
return response
class MailTable(TemplateView):
template_name = 'alfortville/mail-table.html'
def get_context_data(self, *args, **kwargs):
context = super(MailTable, self).get_context_data(**kwargs)
params = {}
if self.request.session.get('mellon_session'):
mellon = self.request.session['mellon_session']
params['NameID'] = mellon['name_id_content']
user_roles = [x.uuid for x in Role.objects.filter(user=self.request.user)]
mails = Mail.objects.filter(status='done-dga')
content_type = ContentType.objects.get_for_model(Mail)
context['objects'] = Inbox.objects.filter(
role_slug__in=user_roles,
source_type=content_type,
source_pk__in=[x.id for x in mails],
subtype=self.subtype, done=False)
context['subtype'] = self.subtype
context['button_text'] = self.button_text
context['title'] = self.title
if self.subtype != Inbox.INFO:
context['display_comments_field'] = True
return context
def post(self, request, *args, **kwargs):
Inbox.objects.filter(id__in=request.POST.getlist('object-pk')).update(done=True,
comments=request.POST.get('comments'))
return HttpResponseRedirect('.')
class TableMandatoryAvis(MailTable):
subtype = Inbox.MANDATORY_AVIS
title = _('Mandatory Avis Copies')
button_text = _('Next')
table_mandatory_avis = login_required(TableMandatoryAvis.as_view())
class TableAvis(MailTable):
subtype = Inbox.AVIS
title = _('Avis Copies')
button_text = _('Next')
table_avis = login_required(TableAvis.as_view())
class TableInfo(MailTable):
subtype = Inbox.INFO
title = _('Info Copies')
button_text = _('Mark as Seen')
table_info = login_required(TableInfo.as_view())
class TableWaiting(TemplateView):
template_name = 'alfortville/mail-table-waiting.html'
def get_context_data(self, *args, **kwargs):
context = super(TableWaiting, self).get_context_data(**kwargs)
content_type = ContentType.objects.get_for_model(Mail)
context['objects'] = []
seen = {}
for association in Association.objects.filter(
formdef_reference__isnull=False,
formdata_id__isnull=True,
source_pk__isnull=False,
source_type=content_type.id):
if not association.source.status: # still on first screen
continue
if association.source_pk not in seen:
context['objects'].append(association.source)
seen['association.source_pk'] = True
return context
table_waiting = login_required(TableWaiting.as_view())
@login_required
def count_dgs(request, *args, **kwargs):
count = DgsMailHome(request).get_queryset().count()
return response_for_json(request, {'count': count})
@login_required
def count_dga(request, *args, **kwargs):
count = DgaMailHome(request).get_queryset().count()
return response_for_json(request, {'count': count})
@login_required
def count_info(request, *args, **kwargs):
table = TableInfo()
table.request = request
count = table.get_context_data().get('objects').count()
return response_for_json(request, {'count': count})
@login_required
def count_avis(request, *args, **kwargs):
table = TableAvis()
table.request = request
count = table.get_context_data().get('objects').count()
return response_for_json(request, {'count': count})
@login_required
def count_mandatory_avis(request, *args, **kwargs):
table = TableMandatoryAvis()
table.request = request
count = table.get_context_data().get('objects').count()
return response_for_json(request, {'count': count})