eds/cms/views.py

192 lines
6.1 KiB
Python
Raw Normal View History

2017-08-13 22:58:46 +02:00
"""
2017-04-07 09:17:20 +02:00
Created on 4 déc. 2012
@author: alzo
2017-08-13 22:58:46 +02:00
"""
2017-08-14 07:43:21 +02:00
import os
2017-10-30 14:31:02 +01:00
import tempfile
2017-08-14 07:43:21 +02:00
2017-04-07 09:17:20 +02:00
from django.views.generic import ListView, TemplateView, DetailView
from django.db.models import F, Sum
2017-08-13 22:58:46 +02:00
from django.http import HttpResponse
2017-10-30 14:31:02 +01:00
from reportlab.pdfgen import canvas
2017-04-07 09:17:20 +02:00
2017-10-30 14:31:02 +01:00
from cms.pdf import PeriodeFormationPdf, ModulePdf, PlanFormationPdf
from cms.models import (Domaine, Processus, Module, Competence, Document, UploadDoc,)
2017-04-07 09:17:20 +02:00
2017-08-13 22:58:46 +02:00
2017-04-07 09:17:20 +02:00
class HomeView(TemplateView):
template_name = 'cms/index.html'
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
for d in Domaine.objects.all().order_by('code'):
context[d.code] = d
for c in Processus.objects.all().order_by('code'):
context[c.code] = c
for m in Module.objects.all().order_by('code'):
context[m.code] = m
return context
class DomaineDetailView(DetailView):
template_name = 'cms/domaine_detail.html'
model = Domaine
class DomaineListView(ListView):
template_name = 'cms/domaine_list.html'
model = Domaine
class ProcessusDetailView(DetailView):
template_name = 'cms/processus_detail.html'
model = Processus
class ProcessusListView(ListView):
template_name = 'cms/processus_list.html'
model = Processus
class ModuleDetailView(DetailView):
template_name = 'cms/module_detail.html'
model = Module
class ModuleListView(ListView):
template_name = 'cms/module_list.html'
model = Module
class EvaluationView(ListView):
template_name = 'cms/evaluation.html'
model = Processus
2017-08-13 22:58:46 +02:00
2017-04-07 09:17:20 +02:00
class DocumentListView(ListView):
template_name = 'cms/document_list.html'
2017-04-19 18:49:49 +02:00
model = Document
2017-04-07 09:17:20 +02:00
2017-04-25 21:07:48 +02:00
def get_queryset(self, **kwargs):
query = Document.objects.filter(published=True)
return query
2017-08-13 22:58:46 +02:00
2017-04-19 18:49:49 +02:00
def get_context_data(self, **kwargs):
context = super(DocumentListView, self).get_context_data(**kwargs)
context['upload'] = UploadDoc.objects.filter(published=True)
return context
2017-04-07 09:17:20 +02:00
class DocumentDetailView(DetailView):
2017-08-13 22:58:46 +02:00
template_name = 'cms/document_detail.html'
2017-04-07 09:17:20 +02:00
model = Document
2017-04-19 18:49:49 +02:00
class UploadDetailView(DetailView):
2017-04-25 21:07:48 +02:00
"""
Affiche les documents uploadés à la suite des doc.
DocumentsInline
"""
2017-04-19 18:49:49 +02:00
template_name = 'cms/upload_detail.html'
model = UploadDoc
def get_context_data(self, **kwargs):
context = super(UploadDetailView, self).get_context_data(**kwargs)
context['fichier'] = self.get_object().docfile.url
return context
2017-10-30 14:31:02 +01:00
def print_module_pdf(request, pk):
filename = 'module.pdf'
path = os.path.join(tempfile.gettempdir(), filename)
pdf = ModulePdf(path)
module = Module.objects.get(pk=pk)
pdf.produce(module)
with open(path, mode='rb') as fh:
response = HttpResponse(fh.read(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="EDS_module_{0}.pdf"'.format(module.code)
return response
def print_plan_formation(request):
filename = 'plan_formation.pdf'
path = os.path.join(tempfile.gettempdir(), filename)
pdf = PlanFormationPdf(path)
domain = Domaine.objects.all().order_by('code')
process = Processus.objects.all().order_by('code')
pdf.produce(domain, process)
with open(path, mode='rb') as fh:
response = HttpResponse(fh.read(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="EDS_plan_formation.pdf"'
return response
def print_periode_formation(request):
filename = 'periode_formation.pdf'
path = os.path.join(tempfile.gettempdir(), filename)
2017-10-30 22:03:31 +01:00
2017-10-30 14:31:02 +01:00
pdf = PeriodeFormationPdf(path)
context = {}
context = get_context(context)
for semestre_id in range(1, 7):
modules = context['sem{0}'.format(str(semestre_id))]
total = context['tot{0}'.format(str(semestre_id))]
2017-10-30 22:03:31 +01:00
pdf.produce_half_year(semestre_id, modules, total)
2017-11-01 06:33:55 +01:00
#pdf.print_total(context['tot'])
2017-10-30 14:31:02 +01:00
2017-10-30 22:03:31 +01:00
with open(path, mode='rb') as fh:
2017-10-30 14:31:02 +01:00
response = HttpResponse(fh.read(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="{0}"'.format(filename)
return response
2017-04-07 09:17:20 +02:00
def get_context(context):
2017-04-19 18:49:49 +02:00
"""
Calcul du nombre de périodes de formation
2017-10-30 14:31:02 +01:00
"""
2017-08-13 22:58:46 +02:00
liste = Module.objects.exclude(periode_presentiel=0)
# context['tot'] = liste.aggregate(Sum(F('periode_presentiel')))
context['sem1'] = liste.exclude(sem1=0)
context['tot1'] = liste.aggregate(Sum(F('sem1')))['sem1__sum']
context['sem2'] = liste.exclude(sem2=0)
context['tot2'] = liste.aggregate(Sum(F('sem2')))['sem2__sum']
context['sem3'] = liste.exclude(sem3=0)
context['tot3'] = liste.aggregate(Sum(F('sem3')))['sem3__sum']
context['sem4'] = liste.exclude(sem4=0)
context['tot4'] = liste.aggregate(Sum(F('sem4')))['sem4__sum']
context['sem5'] = liste.exclude(sem5=0)
context['tot5'] = liste.aggregate(Sum(F('sem5')))['sem5__sum']
context['sem6'] = liste.exclude(sem6=0)
context['tot6'] = liste.aggregate(Sum(F('sem6')))['sem6__sum']
2017-10-30 14:31:02 +01:00
context['tot'] = context['tot1'] + context['tot2'] + context['tot3'] + context['tot4'] \
+ context['tot5'] + context['tot6']
2017-04-07 09:17:20 +02:00
return context
class PeriodeView(TemplateView):
template_name = 'cms/periodes.html'
2017-10-30 14:31:02 +01:00
2017-04-07 09:17:20 +02:00
def get_context_data(self, **kwargs):
context = TemplateView.get_context_data(self, **kwargs)
2017-10-30 14:31:02 +01:00
return get_context(context)
2017-04-07 09:17:20 +02:00
2017-08-13 22:58:46 +02:00
2017-05-04 17:16:04 +02:00
class CompetenceListView(ListView):
model = Competence
template_name = 'cms/competence_list.html'
class TravailPersoListView(ListView):
model = Module
template_name = 'cms/travail_perso.html'
def get_context_data(self, **kwargs):
context = ListView.get_context_data(self, **kwargs)
context['total_perso'] = Module.objects.aggregate((Sum('travail_perso')))['travail_perso__sum']
context['total_presentiel'] = Module.objects.aggregate((Sum('periode_presentiel')))['periode_presentiel__sum']
context['total_pratique'] = Module.objects.aggregate((Sum('pratique_prof')))['pratique_prof__sum']
return get_context(context)