2016-07-15 00:02:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-12-22 12:28:03 +01:00
|
|
|
import base64
|
|
|
|
import json
|
2016-12-23 16:08:41 +01:00
|
|
|
from datetime import datetime
|
2016-12-22 12:28:03 +01:00
|
|
|
from captcha.fields import ReCaptchaField
|
|
|
|
from custommail.shortcuts import render_custom_mail
|
2016-12-22 02:00:10 +01:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
2016-12-22 12:28:03 +01:00
|
|
|
from django.core import mail
|
2013-10-06 11:20:59 +02:00
|
|
|
from django.forms import ModelForm
|
|
|
|
from django import forms
|
|
|
|
from django.forms.models import inlineformset_factory, BaseInlineFormSet
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.views.generic import ListView
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.db.models import Min
|
|
|
|
|
2016-06-10 23:59:41 +02:00
|
|
|
from gestioncof.models import CofProfile
|
2016-07-16 18:48:43 +02:00
|
|
|
from gestioncof.petits_cours_models import PetitCoursDemande, \
|
|
|
|
PetitCoursAttribution, PetitCoursAttributionCounter, PetitCoursAbility, \
|
|
|
|
PetitCoursSubject
|
2013-10-06 11:20:59 +02:00
|
|
|
from gestioncof.decorators import buro_required
|
|
|
|
from gestioncof.shared import lock_table, unlock_tables
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
class DemandeListView(ListView):
|
|
|
|
model = PetitCoursDemande
|
|
|
|
template_name = "petits_cours_demandes_list.html"
|
|
|
|
paginate_by = 20
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2016-07-09 21:19:37 +02:00
|
|
|
return PetitCoursDemande.objects.order_by('traitee', '-id').all()
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
@method_decorator(buro_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(DemandeListView, self).dispatch(*args, **kwargs)
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
@buro_required
|
|
|
|
def details(request, demande_id):
|
2016-07-09 21:19:37 +02:00
|
|
|
demande = get_object_or_404(PetitCoursDemande, id=demande_id)
|
|
|
|
attributions = PetitCoursAttribution.objects.filter(demande=demande).all()
|
2013-10-06 11:20:59 +02:00
|
|
|
return render(request, "details_demande_petit_cours.html",
|
|
|
|
{"demande": demande,
|
|
|
|
"attributions": attributions})
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
def _get_attrib_counter(user, matiere):
|
2016-07-09 23:59:25 +02:00
|
|
|
counter, created = PetitCoursAttributionCounter \
|
|
|
|
.objects.get_or_create(user=user, matiere=matiere)
|
2013-10-06 11:20:59 +02:00
|
|
|
if created:
|
2016-07-09 23:59:25 +02:00
|
|
|
mincount = PetitCoursAttributionCounter.objects \
|
|
|
|
.filter(matiere=matiere).exclude(user=user).all() \
|
|
|
|
.aggregate(Min('count'))
|
2013-10-06 11:20:59 +02:00
|
|
|
counter.count = mincount['count__min']
|
|
|
|
counter.save()
|
|
|
|
return counter
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
|
|
|
def _get_demande_candidates(demande, redo=False):
|
2014-08-19 12:54:22 +02:00
|
|
|
for matiere in demande.matieres.all():
|
2016-07-09 22:31:56 +02:00
|
|
|
candidates = PetitCoursAbility.objects.filter(matiere=matiere,
|
|
|
|
niveau=demande.niveau)
|
2016-07-09 21:19:37 +02:00
|
|
|
candidates = candidates.filter(user__profile__is_cof=True,
|
|
|
|
user__profile__petits_cours_accept=True)
|
2014-08-19 12:54:22 +02:00
|
|
|
if demande.agrege_requis:
|
2016-07-09 21:19:37 +02:00
|
|
|
candidates = candidates.filter(agrege=True)
|
2014-08-19 12:54:22 +02:00
|
|
|
if redo:
|
2016-07-09 23:59:25 +02:00
|
|
|
attributions = PetitCoursAttribution.objects \
|
|
|
|
.filter(demande=demande, matiere=matiere).all()
|
2014-08-19 12:54:22 +02:00
|
|
|
for attrib in attributions:
|
2016-07-09 21:19:37 +02:00
|
|
|
candidates = candidates.exclude(user=attrib.user)
|
2014-08-19 12:54:22 +02:00
|
|
|
candidates = candidates.order_by('?').select_related().all()
|
|
|
|
yield (matiere, candidates)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
@buro_required
|
2016-07-09 21:19:37 +02:00
|
|
|
def traitement(request, demande_id, redo=False):
|
|
|
|
demande = get_object_or_404(PetitCoursDemande, id=demande_id)
|
2013-10-06 11:20:59 +02:00
|
|
|
if demande.niveau == "other":
|
2014-08-19 12:54:22 +02:00
|
|
|
return _traitement_other(request, demande, redo)
|
2013-10-06 11:20:59 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
return _traitement_post(request, demande)
|
|
|
|
proposals = {}
|
|
|
|
proposed_for = {}
|
|
|
|
unsatisfied = []
|
|
|
|
attribdata = {}
|
2014-08-19 12:54:22 +02:00
|
|
|
for matiere, candidates in _get_demande_candidates(demande, redo):
|
|
|
|
if candidates:
|
|
|
|
tuples = []
|
|
|
|
for candidate in candidates:
|
|
|
|
user = candidate.user
|
|
|
|
tuples.append((candidate, _get_attrib_counter(user, matiere)))
|
2016-07-09 21:19:37 +02:00
|
|
|
tuples = sorted(tuples, key=lambda c: c[1].count)
|
2013-10-06 11:20:59 +02:00
|
|
|
candidates, _ = zip(*tuples)
|
|
|
|
candidates = candidates[0:min(3, len(candidates))]
|
2014-08-19 12:54:22 +02:00
|
|
|
attribdata[matiere.id] = []
|
2013-10-06 11:20:59 +02:00
|
|
|
proposals[matiere] = []
|
|
|
|
for candidate in candidates:
|
2014-08-19 12:54:22 +02:00
|
|
|
user = candidate.user
|
2013-10-06 11:20:59 +02:00
|
|
|
proposals[matiere].append(user)
|
|
|
|
attribdata[matiere.id].append(user.id)
|
|
|
|
if user not in proposed_for:
|
|
|
|
proposed_for[user] = [matiere]
|
|
|
|
else:
|
|
|
|
proposed_for[user].append(matiere)
|
|
|
|
else:
|
|
|
|
unsatisfied.append(matiere)
|
2014-08-19 12:54:22 +02:00
|
|
|
return _finalize_traitement(request, demande, proposals,
|
|
|
|
proposed_for, unsatisfied, attribdata, redo)
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
@buro_required
|
|
|
|
def retraitement(request, demande_id):
|
2016-07-09 21:19:37 +02:00
|
|
|
return traitement(request, demande_id, redo=True)
|
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
def _finalize_traitement(request, demande, proposals, proposed_for,
|
2016-07-09 21:19:37 +02:00
|
|
|
unsatisfied, attribdata, redo=False, errors=None):
|
2013-10-06 11:20:59 +02:00
|
|
|
proposals = proposals.items()
|
|
|
|
proposed_for = proposed_for.items()
|
2016-09-22 14:01:01 +02:00
|
|
|
attribdata = list(attribdata.items())
|
2013-10-06 11:20:59 +02:00
|
|
|
proposed_mails = _generate_eleve_email(demande, proposed_for)
|
2016-12-22 12:28:03 +01:00
|
|
|
mainmail = render_custom_mail("petits-cours-mail-demandeur", {
|
2016-11-08 07:34:19 +01:00
|
|
|
"proposals": proposals,
|
|
|
|
"unsatisfied": unsatisfied,
|
|
|
|
"extra":
|
|
|
|
'<textarea name="extra" '
|
|
|
|
'style="width:99%; height: 90px;">'
|
|
|
|
'</textarea>'
|
|
|
|
})
|
2013-10-06 11:20:59 +02:00
|
|
|
return render(request, "traitement_demande_petit_cours.html",
|
|
|
|
{"demande": demande,
|
|
|
|
"unsatisfied": unsatisfied,
|
|
|
|
"proposals": proposals,
|
|
|
|
"proposed_for": proposed_for,
|
|
|
|
"proposed_mails": proposed_mails,
|
|
|
|
"mainmail": mainmail,
|
2016-07-09 22:31:56 +02:00
|
|
|
"attribdata":
|
2016-11-13 12:22:48 +01:00
|
|
|
base64.b64encode(json.dumps(attribdata)
|
2016-12-22 12:28:03 +01:00
|
|
|
.encode('utf_8')),
|
2013-10-06 11:20:59 +02:00
|
|
|
"redo": redo,
|
2014-08-19 12:54:22 +02:00
|
|
|
"errors": errors,
|
2016-07-09 22:31:56 +02:00
|
|
|
})
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
def _generate_eleve_email(demande, proposed_for):
|
2016-12-22 02:00:10 +01:00
|
|
|
return [
|
|
|
|
(
|
|
|
|
user,
|
2016-12-22 12:28:03 +01:00
|
|
|
render_custom_mail('petit-cours-mail-eleve', {
|
2016-12-22 02:00:10 +01:00
|
|
|
"demande": demande,
|
|
|
|
"matieres": matieres
|
2016-12-23 10:25:28 +01:00
|
|
|
})
|
2016-12-22 02:00:10 +01:00
|
|
|
)
|
|
|
|
for user, matieres in proposed_for
|
|
|
|
]
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
def _traitement_other_preparing(request, demande):
|
|
|
|
redo = "redo" in request.POST
|
|
|
|
unsatisfied = []
|
|
|
|
proposals = {}
|
|
|
|
proposed_for = {}
|
|
|
|
attribdata = {}
|
|
|
|
errors = []
|
|
|
|
for matiere, candidates in _get_demande_candidates(demande, redo):
|
|
|
|
if candidates:
|
|
|
|
candidates = dict([(candidate.user.id, candidate.user)
|
|
|
|
for candidate in candidates])
|
|
|
|
attribdata[matiere.id] = []
|
|
|
|
proposals[matiere] = []
|
|
|
|
for choice_id in range(min(3, len(candidates))):
|
2016-07-09 22:31:56 +02:00
|
|
|
choice = int(
|
|
|
|
request.POST["proposal-%d-%d" % (matiere.id, choice_id)])
|
2014-08-19 12:54:22 +02:00
|
|
|
if choice == -1:
|
|
|
|
continue
|
|
|
|
if choice not in candidates:
|
2016-07-15 00:02:56 +02:00
|
|
|
errors.append("Choix invalide pour la proposition %d"
|
2016-07-09 22:31:56 +02:00
|
|
|
"en %s" % (choice_id + 1, matiere))
|
2014-08-19 12:54:22 +02:00
|
|
|
continue
|
|
|
|
user = candidates[choice]
|
|
|
|
if user in proposals[matiere]:
|
2016-07-15 00:02:56 +02:00
|
|
|
errors.append("La proposition %d en %s est un doublon"
|
2016-07-10 00:26:02 +02:00
|
|
|
% (choice_id + 1, matiere))
|
2014-08-19 12:54:22 +02:00
|
|
|
continue
|
|
|
|
proposals[matiere].append(user)
|
|
|
|
attribdata[matiere.id].append(user.id)
|
|
|
|
if user not in proposed_for:
|
|
|
|
proposed_for[user] = [matiere]
|
|
|
|
else:
|
|
|
|
proposed_for[user].append(matiere)
|
|
|
|
if not proposals[matiere]:
|
2016-05-26 22:44:10 +02:00
|
|
|
errors.append("Aucune proposition pour %s" % (matiere,))
|
2014-08-19 12:54:22 +02:00
|
|
|
elif len(proposals[matiere]) < 3:
|
2016-05-26 22:44:10 +02:00
|
|
|
errors.append("Seulement %d proposition%s pour %s"
|
2016-07-10 00:26:02 +02:00
|
|
|
% (len(proposals[matiere]),
|
|
|
|
"s" if len(proposals[matiere]) > 1 else "",
|
|
|
|
matiere))
|
2014-08-19 12:54:22 +02:00
|
|
|
else:
|
|
|
|
unsatisfied.append(matiere)
|
2016-07-09 22:31:56 +02:00
|
|
|
return _finalize_traitement(request, demande, proposals, proposed_for,
|
|
|
|
unsatisfied, attribdata, errors=errors)
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
def _traitement_other(request, demande, redo):
|
2013-10-06 11:20:59 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
if "preparing" in request.POST:
|
|
|
|
return _traitement_other_preparing(request, demande)
|
|
|
|
else:
|
|
|
|
return _traitement_post(request, demande)
|
|
|
|
proposals = {}
|
|
|
|
proposed_for = {}
|
|
|
|
unsatisfied = []
|
|
|
|
attribdata = {}
|
2014-08-19 12:54:22 +02:00
|
|
|
for matiere, candidates in _get_demande_candidates(demande, redo):
|
2013-10-06 11:20:59 +02:00
|
|
|
if candidates:
|
|
|
|
tuples = []
|
|
|
|
for candidate in candidates:
|
|
|
|
user = candidate.user
|
|
|
|
tuples.append((candidate, _get_attrib_counter(user, matiere)))
|
2016-07-09 21:19:37 +02:00
|
|
|
tuples = sorted(tuples, key=lambda c: c[1].count)
|
2013-10-06 11:20:59 +02:00
|
|
|
candidates, _ = zip(*tuples)
|
|
|
|
attribdata[matiere.id] = []
|
|
|
|
proposals[matiere] = []
|
|
|
|
for candidate in candidates:
|
|
|
|
user = candidate.user
|
|
|
|
proposals[matiere].append(user)
|
|
|
|
attribdata[matiere.id].append(user.id)
|
|
|
|
if user not in proposed_for:
|
|
|
|
proposed_for[user] = [matiere]
|
|
|
|
else:
|
|
|
|
proposed_for[user].append(matiere)
|
|
|
|
else:
|
|
|
|
unsatisfied.append(matiere)
|
|
|
|
proposals = proposals.items()
|
|
|
|
proposed_for = proposed_for.items()
|
|
|
|
return render(request, "traitement_demande_petit_cours_autre_niveau.html",
|
|
|
|
{"demande": demande,
|
|
|
|
"unsatisfied": unsatisfied,
|
|
|
|
"proposals": proposals,
|
|
|
|
"proposed_for": proposed_for,
|
2016-07-09 22:31:56 +02:00
|
|
|
})
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
def _traitement_post(request, demande):
|
|
|
|
proposals = {}
|
|
|
|
proposed_for = {}
|
|
|
|
unsatisfied = []
|
|
|
|
extra = request.POST["extra"].strip()
|
|
|
|
redo = "redo" in request.POST
|
|
|
|
attribdata = request.POST["attribdata"]
|
2016-11-13 12:22:48 +01:00
|
|
|
attribdata = dict(json.loads(base64.b64decode(attribdata)))
|
2013-10-06 11:20:59 +02:00
|
|
|
for matiere in demande.matieres.all():
|
|
|
|
if matiere.id not in attribdata:
|
|
|
|
unsatisfied.append(matiere)
|
|
|
|
else:
|
|
|
|
proposals[matiere] = []
|
|
|
|
for user_id in attribdata[matiere.id]:
|
2016-07-09 21:19:37 +02:00
|
|
|
user = User.objects.get(pk=user_id)
|
2013-10-06 11:20:59 +02:00
|
|
|
proposals[matiere].append(user)
|
|
|
|
if user not in proposed_for:
|
|
|
|
proposed_for[user] = [matiere]
|
|
|
|
else:
|
|
|
|
proposed_for[user].append(matiere)
|
|
|
|
proposals_list = proposals.items()
|
|
|
|
proposed_for = proposed_for.items()
|
|
|
|
proposed_mails = _generate_eleve_email(demande, proposed_for)
|
2016-12-22 12:28:03 +01:00
|
|
|
mainmail = render_custom_mail("petits-cours-mail-demandeur", {
|
2016-11-08 07:34:19 +01:00
|
|
|
"proposals": proposals_list,
|
|
|
|
"unsatisfied": unsatisfied,
|
|
|
|
"extra": extra,
|
|
|
|
})
|
2016-09-27 17:57:53 +02:00
|
|
|
frommail = settings.MAIL_DATA['petits_cours']['FROM']
|
|
|
|
bccaddress = settings.MAIL_DATA['petits_cours']['BCC']
|
|
|
|
replyto = settings.MAIL_DATA['petits_cours']['REPLYTO']
|
2013-10-06 11:20:59 +02:00
|
|
|
mails_to_send = []
|
|
|
|
for (user, msg) in proposed_mails:
|
2016-12-22 12:28:03 +01:00
|
|
|
msg = mail.EmailMessage("Petits cours ENS par le COF", msg,
|
|
|
|
frommail, [user.email],
|
|
|
|
[bccaddress], headers={'Reply-To': replyto})
|
2013-10-06 11:20:59 +02:00
|
|
|
mails_to_send.append(msg)
|
2016-12-22 12:28:03 +01:00
|
|
|
mails_to_send.append(mail.EmailMessage("Cours particuliers ENS", mainmail,
|
|
|
|
frommail, [demande.email],
|
|
|
|
[bccaddress],
|
|
|
|
headers={'Reply-To': replyto}))
|
2016-07-09 21:19:37 +02:00
|
|
|
connection = mail.get_connection(fail_silently=True)
|
2013-10-06 11:20:59 +02:00
|
|
|
connection.send_messages(mails_to_send)
|
|
|
|
lock_table(PetitCoursAttributionCounter, PetitCoursAttribution, User)
|
|
|
|
for matiere in proposals:
|
|
|
|
for rank, user in enumerate(proposals[matiere]):
|
2016-07-09 22:31:56 +02:00
|
|
|
counter = PetitCoursAttributionCounter.objects.get(user=user,
|
|
|
|
matiere=matiere)
|
2013-10-06 11:20:59 +02:00
|
|
|
counter.count += 1
|
|
|
|
counter.save()
|
2016-07-09 21:19:37 +02:00
|
|
|
attrib = PetitCoursAttribution(user=user, matiere=matiere,
|
|
|
|
demande=demande, rank=rank + 1)
|
2013-10-06 11:20:59 +02:00
|
|
|
attrib.save()
|
|
|
|
unlock_tables()
|
|
|
|
demande.traitee = True
|
|
|
|
demande.traitee_par = request.user
|
|
|
|
demande.processed = datetime.now()
|
|
|
|
demande.save()
|
|
|
|
return render(request, "traitement_demande_petit_cours_success.html",
|
|
|
|
{"demande": demande,
|
|
|
|
"redo": redo,
|
2016-07-09 22:31:56 +02:00
|
|
|
})
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
class BaseMatieresFormSet(BaseInlineFormSet):
|
|
|
|
def clean(self):
|
|
|
|
super(BaseMatieresFormSet, self).clean()
|
|
|
|
if any(self.errors):
|
2016-07-09 22:31:56 +02:00
|
|
|
# Don't bother validating the formset unless each form is
|
|
|
|
# valid on its own
|
2013-10-06 11:20:59 +02:00
|
|
|
return
|
|
|
|
matieres = []
|
|
|
|
for i in range(0, self.total_form_count()):
|
|
|
|
form = self.forms[i]
|
|
|
|
if not form.cleaned_data:
|
|
|
|
continue
|
|
|
|
matiere = form.cleaned_data['matiere']
|
|
|
|
niveau = form.cleaned_data['niveau']
|
|
|
|
delete = form.cleaned_data['DELETE']
|
|
|
|
if not delete and (matiere, niveau) in matieres:
|
2016-07-09 22:31:56 +02:00
|
|
|
raise forms.ValidationError(
|
|
|
|
"Vous ne pouvez pas vous inscrire deux fois pour la "
|
|
|
|
"même matiere avec le même niveau.")
|
2013-10-06 11:20:59 +02:00
|
|
|
matieres.append((matiere, niveau))
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
@login_required
|
|
|
|
def inscription(request):
|
2016-07-09 21:19:37 +02:00
|
|
|
profile, created = CofProfile.objects.get_or_create(user=request.user)
|
2013-10-06 11:20:59 +02:00
|
|
|
if not profile.is_cof:
|
|
|
|
return redirect("cof-denied")
|
|
|
|
MatieresFormSet = inlineformset_factory(User, PetitCoursAbility,
|
2016-07-09 22:31:56 +02:00
|
|
|
fields=("matiere", "niveau",
|
|
|
|
"agrege",),
|
2016-07-09 21:19:37 +02:00
|
|
|
formset=BaseMatieresFormSet)
|
2013-10-06 11:20:59 +02:00
|
|
|
success = False
|
|
|
|
if request.method == "POST":
|
2016-07-09 21:19:37 +02:00
|
|
|
formset = MatieresFormSet(request.POST, instance=request.user)
|
2013-10-06 11:20:59 +02:00
|
|
|
if formset.is_valid():
|
|
|
|
formset.save()
|
|
|
|
profile.petits_cours_accept = "receive_proposals" in request.POST
|
|
|
|
profile.petits_cours_remarques = request.POST["remarques"]
|
|
|
|
profile.save()
|
2016-07-09 22:31:56 +02:00
|
|
|
lock_table(PetitCoursAttributionCounter, PetitCoursAbility, User,
|
|
|
|
PetitCoursSubject)
|
2016-07-09 23:59:25 +02:00
|
|
|
abilities = PetitCoursAbility.objects \
|
|
|
|
.filter(user=request.user).all()
|
2013-10-06 11:20:59 +02:00
|
|
|
for ability in abilities:
|
2016-07-16 20:54:52 +02:00
|
|
|
_get_attrib_counter(ability.user, ability.matiere)
|
2013-10-06 11:20:59 +02:00
|
|
|
unlock_tables()
|
|
|
|
success = True
|
2016-07-09 21:19:37 +02:00
|
|
|
formset = MatieresFormSet(instance=request.user)
|
2013-10-06 11:20:59 +02:00
|
|
|
else:
|
2016-07-09 21:19:37 +02:00
|
|
|
formset = MatieresFormSet(instance=request.user)
|
2016-07-09 22:31:56 +02:00
|
|
|
return render(request, "inscription-petit-cours.html",
|
|
|
|
{"formset": formset, "success": success,
|
|
|
|
"receive_proposals": profile.petits_cours_accept,
|
|
|
|
"remarques": profile.petits_cours_remarques})
|
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
class DemandeForm(ModelForm):
|
2016-07-09 21:19:37 +02:00
|
|
|
captcha = ReCaptchaField(attrs={'theme': 'clean', 'lang': 'fr'})
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DemandeForm, self).__init__(*args, **kwargs)
|
|
|
|
self.fields['matieres'].help_text = ''
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PetitCoursDemande
|
2016-07-09 22:31:56 +02:00
|
|
|
fields = ('name', 'email', 'phone', 'quand', 'freq', 'lieu',
|
|
|
|
'matieres', 'agrege_requis', 'niveau', 'remarques')
|
2013-10-06 11:20:59 +02:00
|
|
|
widgets = {'matieres': forms.CheckboxSelectMultiple}
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
@csrf_exempt
|
|
|
|
def demande(request):
|
|
|
|
success = False
|
|
|
|
if request.method == "POST":
|
|
|
|
form = DemandeForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
success = True
|
|
|
|
else:
|
|
|
|
form = DemandeForm()
|
2016-07-09 22:31:56 +02:00
|
|
|
return render(request, "demande-petit-cours.html", {"form": form,
|
|
|
|
"success": success})
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
@csrf_exempt
|
|
|
|
def demande_raw(request):
|
|
|
|
success = False
|
|
|
|
if request.method == "POST":
|
|
|
|
form = DemandeForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
success = True
|
|
|
|
else:
|
|
|
|
form = DemandeForm()
|
2016-07-09 22:31:56 +02:00
|
|
|
return render(request, "demande-petit-cours-raw.html",
|
|
|
|
{"form": form, "success": success})
|