Commit e8c9e45e authored by Louis's avatar Louis

La création d'exercices fonctionne

parent 41118297
......@@ -2,6 +2,5 @@ from django.conf.urls import include, url
from django.urls import path
urlpatterns = [
path('', include('utiliser.urls')),
path('creer/', include('utiliser.urls')),
path('', include('utiliser.urls', namespace="utiliser")),
]
......@@ -450,7 +450,7 @@ table.tabs td{
background-position: -652px -31px;
color:#fff;
}
p#msgjs, p#errmsg{
p#msgjs, #errmsg{
color:#911101;
text-align: center;
}
......
......@@ -17,7 +17,14 @@
});
</script>
<p id="errmsg">{% if error %}{{ error }}{% endif %}</p> <!-- TODO Utiliser message -->
{% if messages %}
<ul class="messages" id="errmsg">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<div id="tabs">
<ul>
{% for niveau in NIVEAUX %}
......@@ -25,14 +32,15 @@
{% endfor %}
</ul>
<form method="post" action="/creer" id="idForm">
<form method="post" action="{% url 'utiliser:creer' %}" id="idForm">
{% csrf_token %}
{% for niveau, exercices in BAG.sorted_levels %}
<div id="tabs-{{ forloop.counter0 }}">
<table class="tabs">
{% for exo in exercices %}
{% if forloop.counter0|divisibleby:2 %}<tr>{% endif %}
<td>
<select name="{{ forloop.parentloop.counter0 }}-ex{{ forloop.counter0|add:"-1" }}">
<select name="exo-{{ exo.name }}">
<option selected="selected">0</option>
<option>1</option>
<option>2</option>
......
......@@ -2,7 +2,9 @@ from django.conf.urls import url
from . import views
app_name = "utiliser"
urlpatterns = [
url(r'^$', views.choix),
url(r'creer/', views.creer),
url(r'^$', views.choix, name="choix"),
url(r'^creer/', views.creer, name="creer"),
]
from django.shortcuts import render
import os
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib import messages
from django.urls import reverse
from pyromaths.version import VERSION
from pyromaths.ex import ExerciseBag, NIVEAUX
from pyromaths.outils.System import Fiche
from pyromaths.version import VERSION
from . import MAINSITEURL
BAG = ExerciseBag()
def choix(request, error=None):
def choix(request):
return render(request, 'utiliser/utiliser.html', {
'VERSION': VERSION,
'BAG': BAG,
'ERROR': error,
'MAINSITEURL': MAINSITEURL,
'NIVEAUX': NIVEAUX,
}
)
def creer(request):
pattern = re.compile(r"(?P<niveau>\d)-ex(?P<exo>\d+)")
lst = []
cles = request.POST.keys()
cles.sort()
selection = False
LESFICHES = lesfiches()
LESFICHES.sort(key=itemgetter(0), reverse=True)
MESFICHES = [[LESFICHES[i][0][2:], '', LESFICHES[i][2]] for i in range(len(LESFICHES))]
for la_cle in cles:
la_valeur = int(request.POST.get(la_cle))
if la_valeur > 0:
selection = True
niveau = int(re.match(pattern, la_cle).group("niveau"))
exo = MESFICHES[niveau][2][int(re.match(pattern, la_cle).group("exo"))]
for i in range(int(la_valeur)):
lst.append(exo())
if selection:
# niveau = [r"6\ieme", r"5\ieme", r"4\ieme", r"3\ieme", r"2$^{nde}$"][lst[-1][0]]
niveau = exo.level[-1][2:]
#niveau = lst[-1].level()
dt = tempfile.mkdtemp(prefix='pel-%s-%s-' %
(request.META['REMOTE_ADDR'],
time.strftime('%Y%m%d-%H.%M',time.localtime())),
dir="/tmp/")
# Génération de la liste d'exercices
exercices = []
for key in request.POST:
if key.startswith("exo-"):
number = 0
for string in request.POST[key]:
try:
number += int(string)
except ValueError:
pass
for i in range(number):
exercices.append(BAG[key[4:]]())
# Génération du PDF
if exercices:
parametres = {
'creer_pdf': True,
'creer_unpdf': True,
'titre': u"Fiche de révisions",
'enonce': True,
'corrige': True,
'niveau': niveau,
'nom_fichier': u'exercices.tex',
'chemin_fichier': dt,
'fiche_exo': os.path.join(dt, 'exercices.tex'),
'fiche_cor': os.path.join(dt, 'exercices-corrige.tex'),
'datadir': os.path.join(MY_STATIC_ROOT, 'enligne/'),
'configdir': os.path.join(MY_STATIC_ROOT, 'enligne/'),
'modele': 'modele.tex',
'liste_exos': lst,
'les_fiches': LESFICHES,
'openpdf': 0,
'exercices': exercices,
'titre': "Fiche de révisions",
}
creation(parametres)
buffer = cStringIO.StringIO()
f0 = open(os.path.join(dt, 'exercices.pdf'))
buffer.write(f0.read())
f0.close()
pdf = buffer.getvalue()
buffer.close()
response = HttpResponse(content_type = 'application/pdf')
response['Content-Disposition'] = 'inline; filename=exercices.pdf'
response['Content-Length'] = os.path.getsize(os.path.join(dt, 'exercices.pdf'))
response.write(pdf)
with Fiche(parametres) as fiche:
fiche.write_pdf()
for file in ["exercices.tex", "exercices.pdf", "latexmkrc", "exercices-pyromaths.log"]:
try:
os.remove(os.path.join(dt, file))
except:
pass
os.rmdir(dt)
return response
# Envoi du PDF
with open(fiche.pdfname, mode="rb") as pdf:
response = HttpResponse(content_type = 'application/pdf')
response['Content-Disposition'] = 'attachment; filename=exercices.pdf'
response['Content-Length'] = os.path.getsize(fiche.pdfname)
response.write(pdf.read())
return response
else:
return enligne(request, error="Vous devez sélectionner au moins un exercice")
messages.error(request, "Vous devez sélectionner au moins un exercice.")
return redirect("utiliser:choix")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment