eds/cms/views.py

408 lines
15 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-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-08-14 07:43:21 +02:00
from django.conf import settings
2017-08-13 22:58:46 +02:00
from reportlab.platypus import Paragraph, Spacer, Table, TableStyle, Preformatted
2017-04-07 09:17:20 +02:00
from reportlab.lib.units import cm
from reportlab.lib import colors
from reportlab.lib.colors import HexColor
2017-08-13 22:58:46 +02:00
from cms.pdf import PeriodPDF
from .models import style_normal, style_title
from .models import (Domaine, Processus, Module, Competence, Document, UploadDoc,
PDFResponse, MyDocTemplate, MyDocTemplateLandscape)
2017-04-07 09:17:20 +02:00
# Create your views here.
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
2017-08-13 22:58:46 +02:00
2017-06-06 07:37:14 +02:00
class Element(object):
2017-08-13 22:58:46 +02:00
def __init__(self, el):
self.txt = el.__str__()
2017-04-07 09:17:20 +02:00
class HomePDFView(TemplateView):
template_name = 'cms/index.html'
2017-08-13 22:58:46 +02:00
def formating(self, el1='', length=40):
el1 = '' if el1 == '' else el1.__str__()
2017-06-06 07:37:14 +02:00
return Preformatted(el1, style_normal, maxLineLength=length)
2017-08-13 22:58:46 +02:00
# def pf40(self, txt):
# return Preformatted(txt, style_normal, maxLineLength=40)
2017-06-06 07:37:14 +02:00
2017-04-07 09:17:20 +02:00
def render_to_response(self, context, **response_kwargs):
2017-08-13 22:58:46 +02:00
response = PDFResponse('PlanFormation.pdf', 'Plan de formation', portrait=False)
2017-04-07 09:17:20 +02:00
d = Domaine.objects.all().order_by('code')
p = Processus.objects.all().order_by('code')
2017-08-13 22:58:46 +02:00
data = [
['Domaines', 'Processus', 'Sem1', 'Sem2', 'Sem3', 'Sem4', 'Sem5', 'Sem6'],
[self.formating(d[0]), self.formating(p[0], 60), 'M01', '', '', '', '', ''],
[self.formating(''), self.formating('', 60), 'M02', '', '', '', '', ''],
[self.formating(''), self.formating(p[1], 60), '', '', '', 'M03', '', ''],
[self.formating(''), self.formating('', 60), '', 'M04', '', '', '', ''],
[self.formating(d[1]), self.formating(p[2], 60), 'M05', '', 'M06', '', '', ''],
[self.formating(''), self.formating(p[3], 60), '', '', '', '', 'M07', 'M09'],
[self.formating(''), self.formating('', 60), '', '', '', '', 'M08', ''],
[self.formating(d[2]), self.formating(p[4], 60), '', '', 'M10', '', 'M12'],
[self.formating(''), self.formating(p[5], 60), '', '', 'M11', '', ''],
[self.formating(d[3]), self.formating(p[6], 60), '', '', 'M13', '', '', 'M14'],
[self.formating(d[4]), self.formating(p[7], 60), 'M15', '', '', '', '', ''],
[self.formating(d[5]), self.formating(p[8], 60), 'M16_1', '', 'M16_2', '', 'M16_3', ''],
[self.formating(d[6]), self.formating(p[9], 60), 'M17_1', '', 'M17_2', '', 'M17_3', ''],
[self.formating(d[7]), self.formating(p[10], 60), 'Macc', '', '', '', '', ''],
]
2017-04-07 09:17:20 +02:00
2017-06-06 07:37:14 +02:00
print(data)
2017-08-13 22:58:46 +02:00
t = Table(data, colWidths=[5.5*cm, 8*cm, 1.5*cm, 1.5*cm, 1.5*cm, 1.5*cm, 1.5*cm, 1.5*cm],
spaceBefore=0.5*cm, spaceAfter=1*cm)
2017-04-07 09:17:20 +02:00
t.setStyle(TableStyle([
2017-08-13 22:58:46 +02:00
('SIZE', (0, 0), (-1, -1), 8),
('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
('ALIGN', (2, 0), (-1, -1), 'CENTER'),
('GRID', (0, 0), (-1, -1), 0.25, colors.black),
# Domaine 1
('SPAN', (0, 1), (0, 4)),
('SPAN', (1, 1), (1, 2)),
('SPAN', (1, 3), (1, 4)),
('BACKGROUND', (0, 1), (1, 4), colors.orange),
('BACKGROUND', (2, 1), (2, 2), colors.orange),
('BACKGROUND', (5, 3), (5, 3), colors.orange),
('BACKGROUND', (3, 4), (3, 4), colors.orange),
# Domaine 2
('SPAN', (0, 5), (0, 7)),
('BACKGROUND', (0, 5), (1, 7), colors.red),
('BACKGROUND', (2, 5), (2, 5), colors.red),
('BACKGROUND', (4, 5), (4, 5), colors.red),
('BACKGROUND', (6, 6), (6, 6), colors.red),
('BACKGROUND', (7, 6), (7, 6), colors.red),
('BACKGROUND', (6, 7), (6, 7), colors.red),
# Domaine 3
('SPAN', (0, 8), (0, 9)),
('SPAN', (1, 6), (1, 7)),
('SPAN', (4, 8), (5, 8)),
('SPAN', (4, 9), (5, 9)),
('BACKGROUND', (0, 8), (1, 9), colors.pink),
('BACKGROUND', (4, 8), (6, 8), colors.pink),
('BACKGROUND', (4, 9), (5, 9), colors.pink),
# Domaine 4
('BACKGROUND', (0, 10), (1, 10), HexColor('#AD7FA8')),
('BACKGROUND', (4, 10), (4, 10), HexColor('#AD7FA8')),
('BACKGROUND', (7, 10), (7, 10), HexColor('#AD7FA8')),
# Domaine 5
('SPAN', (2, 11), (-1, 11)),
('BACKGROUND', (0, 11), (-1, 11), HexColor('#729FCF')),
# Domaine 6
('SPAN', (2, 12), (3, 12)),
('SPAN', (4, 12), (5, 12)),
('SPAN', (6, 12), (7, 12)),
('BACKGROUND', (0, 12), (-1, 12), colors.lightgreen),
# Domaine 7
('SPAN', (2, 13), (3, 13)),
('SPAN', (4, 13), (5, 13)),
('SPAN', (6, 13), (7, 13)),
('BACKGROUND', (0, 13), (-1, 13), colors.white),
# Domaine 8
('SPAN', (2, 14), (-1, 14)),
('BACKGROUND', (0, 14), (-1, 14), colors.lightgrey),
2017-06-06 07:37:14 +02:00
2017-04-07 09:17:20 +02:00
]))
t.hAlign = 0
response.story.append(t)
doc = MyDocTemplateLandscape(response)
doc.build(response.story)
return response
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
2017-08-13 22:58:46 +02:00
def preformatted_left(text):
2017-04-07 09:17:20 +02:00
return Preformatted(text, style_normal, maxLineLength=15)
2017-08-13 22:58:46 +02:00
def preformatted_right(text):
2017-04-07 09:17:20 +02:00
return Preformatted(text, style_normal, maxLineLength=110)
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-04-07 09:17:20 +02:00
class ModulePDF(DetailView):
template_name = 'cms/module_detail.html'
model = Module
2017-04-19 18:49:49 +02:00
def render_to_response(self, context, **response_kwargs):
2017-04-07 09:17:20 +02:00
m = self.get_object()
2017-08-13 22:58:46 +02:00
response = PDFResponse('Module_{0}.pdf'.format(m.code), 'Module de formation')
2017-04-07 09:17:20 +02:00
str_comp = ''
for c in m.competence_set.all():
str_comp += '- {0} ({1})\n'.format(c.nom, c.code)
2017-05-16 20:48:55 +02:00
"""
2017-04-07 09:17:20 +02:00
if self.request.user.is_authenticated:
2017-05-16 20:48:55 +02:00
2017-04-07 09:17:20 +02:00
for sc in c.souscompetence_set.all():
str_comp += ' -- {0}\n'.format(sc.nom)
2017-05-16 20:48:55 +02:00
"""
2017-05-16 20:45:01 +02:00
str_scom = ''
for c in m.competence_set.all():
for sc in c.souscompetence_set.all():
2017-06-16 09:14:46 +02:00
str_scom += '- {0} (voir {1})\n'.format(sc.nom, c.code)
2017-04-07 09:17:20 +02:00
str_res = ''
for c in m.ressource_set.all():
str_res += '- {0}\n'.format(c.nom)
str_obj = ''
for c in m.objectif_set.all():
str_obj += '- {0}\n'.format(c.nom)
lines = m.contenu.split('\n')
str_con = ''
for l in lines:
str_con += '{0}\n'.format(l)
2017-08-13 22:58:46 +02:00
response.story.append(Spacer(0, 1*cm))
2017-04-07 09:17:20 +02:00
response.story.append(Paragraph(m.__str__(), style_title))
2017-08-13 22:58:46 +02:00
data = [
[preformatted_left('Domaine'), preformatted_right(m.processus.domaine.__str__())],
[preformatted_left('Processus'), preformatted_right(m.processus.__str__())],
[preformatted_left('Situation emblématique'), preformatted_right(m.situation)],
[preformatted_left('Compétences visées'), preformatted_right(str_comp)],
[preformatted_left('Plus-value sur le CFC ASE'), preformatted_right(str_scom)],
# [Preformatted_left('Ressources à acquérir'), Preformatted_right(str_res)],
[preformatted_left('Objectifs à atteindre'), preformatted_right(str_obj)],
[preformatted_left('Didactique'), preformatted_right(m.didactique)],
# [Preformatted_left('Contenu'), Preformatted_right(str_con)],
[preformatted_left('Evaluation'), preformatted_right(m.evaluation)],
[preformatted_left('Type'), preformatted_right('{0}, obligatoire'.format(m.type))],
[preformatted_left('Semestre'), preformatted_right('Sem. {0}'.format(m.semestre))],
[preformatted_left('Présentiel'), preformatted_right('{0} heures'.format(m.periode_presentiel))],
[preformatted_left('Travail personnel'), preformatted_right('{0} heures'.format(m.travail_perso))],
[preformatted_left('Responsable'), preformatted_right(m.processus.domaine.responsable.descr_pdf())],
]
t = Table(data, colWidths=[2.5*cm, 10*cm])
2017-08-14 07:43:21 +02:00
t.setStyle(
TableStyle([
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('LEFTPADDING', (0, 0), (-1, -1), 0), ]
)
2017-08-13 22:58:46 +02:00
)
t.hAlign = 0
response.story.append(Spacer(0, 1*cm))
2017-04-07 09:17:20 +02:00
response.story.append(t)
doc = MyDocTemplate(response)
doc.build(response.story)
return response
2017-04-19 18:49:49 +02:00
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-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']
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'
def get_context_data(self, **kwargs):
context = TemplateView.get_context_data(self, **kwargs)
return get_context(context)
2017-08-13 22:58:46 +02:00
class PeriodePDFView(TemplateView):
2017-04-07 09:17:20 +02:00
def render_to_response(self, context, **response_kwargs):
context = get_context(context)
2017-08-14 07:43:21 +02:00
filename = os.path.join(settings.MEDIA_ROOT, 'periode.pdf')
2017-08-13 22:58:46 +02:00
pdf = PeriodPDF(filename)
for semestre_id in range(1, 7):
modules = context['sem{0}'.format(str(semestre_id))]
total = context['tot{0}'.format(str(semestre_id))]
pdf.produce_half_year(semestre_id, modules, total)
pdf.print_total(context['tot'])
pdf.canv.save()
with open(filename, mode='rb') as fh:
response = HttpResponse(fh.read(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="{0}"'.format(filename)
2017-04-07 09:17:20 +02:00
return response
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)
2017-04-19 18:49:49 +02:00
"""
2017-04-07 09:17:20 +02:00
class AddDocument(TemplateView):
template_name = 'cms/upload.html'
def post(self, request):
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
return HttpResponseRedirect('')
def get(self, request):
form = DocumentForm()
return render (request, 'cms/upload.html', {'form': form})
def AddDoc(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
return HttpResponseRedirect('')
else:
form = DocumentForm()
documents = Document.objects.all()
return render (request, 'cms/upload.html', {'documents': documents,'form': form})
def Download(request, file_name):
f = os.path.join(settings.MEDIA_ROOT, file_name)
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename={0}'.format(file_name)
response['Content-Length'] = os.stat(f).st_size
return response
def pdf_view(request):
2017-04-12 08:55:48 +02:00
with open(settings.MEDIA_ROOT + 'media/EDS_Calendrier_2017.pdf', 'r') as pdf:
2017-04-07 09:17:20 +02:00
response = HttpResponse(pdf.read().decode('latin-1') , content_type='application/pdf')
response['Content-Disposition'] = 'inline;filename=some_file.pdf'
return response
pdf.closed
2017-04-19 18:49:49 +02:00
"""
2017-04-12 08:52:38 +02:00