2017-02-05 13:49:01 +01:00
|
|
|
import json
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
from custommail.shortcuts import render_custom_mail
|
2013-10-06 11:20:59 +02:00
|
|
|
from django.conf import settings
|
2017-01-30 13:50:24 +01:00
|
|
|
from django.contrib import messages
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core import mail
|
2017-05-23 06:56:29 +02:00
|
|
|
from django.db import transaction
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
2017-11-19 18:41:39 +01:00
|
|
|
from django.utils import timezone
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.generic import DetailView, ListView
|
2019-01-05 18:14:29 +01:00
|
|
|
|
|
|
|
from gestioncof.decorators import buro_required
|
|
|
|
from gestioncof.models import CofProfile
|
2018-11-25 00:37:22 +01:00
|
|
|
from petitscours.forms import DemandeForm, MatieresFormSet
|
|
|
|
from petitscours.models import (
|
2018-10-06 12:35:49 +02:00
|
|
|
PetitCoursAbility,
|
|
|
|
PetitCoursAttribution,
|
|
|
|
PetitCoursAttributionCounter,
|
|
|
|
PetitCoursDemande,
|
2017-02-05 13:49:01 +01: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 DemandeListView(ListView):
|
2018-10-06 12:35:49 +02:00
|
|
|
queryset = PetitCoursDemande.objects.prefetch_related("matieres").order_by(
|
|
|
|
"traitee", "-id"
|
2017-04-09 17:51:40 +02:00
|
|
|
)
|
2018-11-25 00:37:22 +01:00
|
|
|
template_name = "petitscours/demande_list.html"
|
2013-10-06 11:20:59 +02:00
|
|
|
paginate_by = 20
|
|
|
|
|
|
|
|
|
2017-02-05 17:35:41 +01:00
|
|
|
class DemandeDetailView(DetailView):
|
|
|
|
model = PetitCoursDemande
|
2018-10-06 12:35:49 +02:00
|
|
|
queryset = PetitCoursDemande.objects.prefetch_related(
|
|
|
|
"petitcoursattribution_set", "matieres"
|
2017-04-09 17:57:11 +02:00
|
|
|
)
|
2018-11-25 00:37:22 +01:00
|
|
|
template_name = "petitscours/demande_detail.html"
|
2017-02-11 02:51:43 +01:00
|
|
|
context_object_name = "demande"
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2017-02-05 17:35:41 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
2018-01-16 16:22:52 +01:00
|
|
|
context = super().get_context_data(**kwargs)
|
2017-02-11 02:51:43 +01:00
|
|
|
obj = self.object
|
2018-10-06 12:35:49 +02:00
|
|
|
context["attributions"] = obj.petitcoursattribution_set.all()
|
2017-02-05 17:35:41 +01:00
|
|
|
return context
|
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)
|
2018-11-25 17:05:55 +01:00
|
|
|
proposals, unsatisfied = demande.get_proposals(redo=redo, max_candidates=3)
|
|
|
|
return _finalize_traitement(request, demande, proposals, unsatisfied, redo)
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
def _finalize_traitement(
|
2018-11-25 17:05:55 +01:00
|
|
|
request, demande, proposals, unsatisfied, redo=False, errors=None
|
2018-10-06 12:35:49 +02:00
|
|
|
):
|
2018-11-25 17:05:55 +01:00
|
|
|
attribdata = [
|
|
|
|
(matiere.id, [user.id for user in users])
|
|
|
|
for matiere, users in proposals.items()
|
|
|
|
]
|
|
|
|
proposed_for = {}
|
|
|
|
for matiere, users in proposals.items():
|
|
|
|
for user in users:
|
|
|
|
proposed_for.setdefault(user, []).append(matiere)
|
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
proposed_mails = _generate_eleve_email(demande, proposed_for)
|
2018-10-06 12:35:49 +02:00
|
|
|
mainmail = render_custom_mail(
|
|
|
|
"petits-cours-mail-demandeur",
|
|
|
|
{
|
2018-11-25 17:05:55 +01:00
|
|
|
"proposals": proposals.items(),
|
2018-10-06 12:35:49 +02:00
|
|
|
"unsatisfied": unsatisfied,
|
|
|
|
"extra": '<textarea name="extra" '
|
2016-11-08 07:34:19 +01:00
|
|
|
'style="width:99%; height: 90px;">'
|
2018-10-06 12:35:49 +02:00
|
|
|
"</textarea>",
|
|
|
|
},
|
|
|
|
)
|
2017-03-15 12:59:41 +01:00
|
|
|
if errors is not None:
|
|
|
|
for error in errors:
|
|
|
|
messages.error(request, error)
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
2018-11-25 00:37:22 +01:00
|
|
|
"petitscours/traitement_demande.html",
|
2018-10-06 12:35:49 +02:00
|
|
|
{
|
|
|
|
"demande": demande,
|
|
|
|
"unsatisfied": unsatisfied,
|
2018-11-25 17:05:55 +01:00
|
|
|
"proposals": proposals.items(),
|
|
|
|
"proposed_for": proposed_for.items(),
|
2018-10-06 12:35:49 +02:00
|
|
|
"proposed_mails": proposed_mails,
|
|
|
|
"mainmail": mainmail,
|
|
|
|
"attribdata": json.dumps(attribdata),
|
|
|
|
"redo": redo,
|
|
|
|
},
|
|
|
|
)
|
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,
|
2018-10-06 12:35:49 +02:00
|
|
|
render_custom_mail(
|
|
|
|
"petit-cours-mail-eleve", {"demande": demande, "matieres": matieres}
|
|
|
|
),
|
2016-12-22 02:00:10 +01:00
|
|
|
)
|
2018-11-25 17:05:55 +01:00
|
|
|
for user, matieres in proposed_for.items()
|
2016-12-22 02:00:10 +01:00
|
|
|
]
|
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 = {}
|
|
|
|
errors = []
|
2017-02-05 17:07:58 +01:00
|
|
|
for matiere, candidates in demande.get_candidates(redo):
|
2014-08-19 12:54:22 +02:00
|
|
|
if candidates:
|
2018-10-06 12:35:49 +02:00
|
|
|
candidates = dict(
|
|
|
|
[(candidate.user.id, candidate.user) for candidate in candidates]
|
|
|
|
)
|
2014-08-19 12:54:22 +02:00
|
|
|
proposals[matiere] = []
|
|
|
|
for choice_id in range(min(3, len(candidates))):
|
2016-07-09 22:31:56 +02:00
|
|
|
choice = int(
|
2018-10-06 12:35:49 +02:00
|
|
|
request.POST["proposal-{:d}-{:d}".format(matiere.id, choice_id)]
|
2017-02-05 13:49:01 +01:00
|
|
|
)
|
2014-08-19 12:54:22 +02:00
|
|
|
if choice == -1:
|
|
|
|
continue
|
|
|
|
if choice not in candidates:
|
2018-10-06 12:35:49 +02:00
|
|
|
errors.append(
|
|
|
|
"Choix invalide pour la proposition {:d}"
|
|
|
|
"en {!s}".format(choice_id + 1, matiere)
|
|
|
|
)
|
2014-08-19 12:54:22 +02:00
|
|
|
continue
|
|
|
|
user = candidates[choice]
|
|
|
|
if user in proposals[matiere]:
|
2018-10-06 12:35:49 +02:00
|
|
|
errors.append(
|
|
|
|
"La proposition {:d} en {!s} est un doublon".format(
|
|
|
|
choice_id + 1, matiere
|
|
|
|
)
|
|
|
|
)
|
2014-08-19 12:54:22 +02:00
|
|
|
continue
|
|
|
|
proposals[matiere].append(user)
|
|
|
|
if not proposals[matiere]:
|
2017-02-05 13:49:01 +01:00
|
|
|
errors.append("Aucune proposition pour {!s}".format(matiere))
|
2014-08-19 12:54:22 +02:00
|
|
|
elif len(proposals[matiere]) < 3:
|
2018-10-06 12:35:49 +02:00
|
|
|
errors.append(
|
|
|
|
"Seulement {:d} proposition{:s} pour {!s}".format(
|
|
|
|
len(proposals[matiere]),
|
|
|
|
"s" if len(proposals[matiere]) > 1 else "",
|
|
|
|
matiere,
|
|
|
|
)
|
|
|
|
)
|
2014-08-19 12:54:22 +02:00
|
|
|
else:
|
|
|
|
unsatisfied.append(matiere)
|
2018-11-25 17:05:55 +01:00
|
|
|
return _finalize_traitement(request, demande, proposals, unsatisfied, 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)
|
2018-11-25 17:05:55 +01:00
|
|
|
proposals, unsatisfied = demande.get_proposals(redo=redo)
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
2018-11-25 00:37:22 +01:00
|
|
|
"petitscours/traitement_demande_autre_niveau.html",
|
2018-10-06 12:35:49 +02:00
|
|
|
{
|
|
|
|
"demande": demande,
|
|
|
|
"unsatisfied": unsatisfied,
|
2018-11-25 17:05:55 +01:00
|
|
|
"proposals": proposals.items(),
|
2018-10-06 12:35:49 +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-12-23 09:37:54 +01:00
|
|
|
attribdata = dict(json.loads(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)
|
|
|
|
proposed_mails = _generate_eleve_email(demande, proposed_for)
|
2017-03-15 19:35:42 +01:00
|
|
|
mainmail_object, mainmail_body = render_custom_mail(
|
|
|
|
"petits-cours-mail-demandeur",
|
2018-11-25 17:05:55 +01:00
|
|
|
{"proposals": proposals.items(), "unsatisfied": unsatisfied, "extra": extra},
|
2017-03-15 19:35:42 +01:00
|
|
|
)
|
2018-10-06 12:35:49 +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 = []
|
2017-03-15 19:17:08 +01:00
|
|
|
for (user, (mail_object, body)) in proposed_mails:
|
2018-10-06 12:35:49 +02:00
|
|
|
msg = mail.EmailMessage(
|
|
|
|
mail_object,
|
|
|
|
body,
|
|
|
|
frommail,
|
|
|
|
[user.email],
|
|
|
|
[bccaddress],
|
|
|
|
headers={"Reply-To": replyto},
|
|
|
|
)
|
2013-10-06 11:20:59 +02:00
|
|
|
mails_to_send.append(msg)
|
2018-10-06 12:35:49 +02:00
|
|
|
mails_to_send.append(
|
|
|
|
mail.EmailMessage(
|
|
|
|
mainmail_object,
|
|
|
|
mainmail_body,
|
|
|
|
frommail,
|
|
|
|
[demande.email],
|
|
|
|
[bccaddress],
|
|
|
|
headers={"Reply-To": replyto},
|
|
|
|
)
|
|
|
|
)
|
2017-03-15 19:21:26 +01:00
|
|
|
connection = mail.get_connection(fail_silently=False)
|
2013-10-06 11:20:59 +02:00
|
|
|
connection.send_messages(mails_to_send)
|
2017-05-23 06:56:29 +02:00
|
|
|
with transaction.atomic():
|
2018-11-25 17:05:55 +01:00
|
|
|
for matiere, users in proposals.items():
|
|
|
|
for rank, user in enumerate(users):
|
2018-10-27 18:48:09 +02:00
|
|
|
# TODO(AD): Prefer PetitCoursAttributionCounter.get_uptodate()
|
2017-05-23 06:56:29 +02:00
|
|
|
counter = PetitCoursAttributionCounter.objects.get(
|
|
|
|
user=user, matiere=matiere
|
|
|
|
)
|
|
|
|
counter.count += 1
|
|
|
|
counter.save()
|
2018-10-06 12:35:49 +02:00
|
|
|
attrib = PetitCoursAttribution(
|
|
|
|
user=user, matiere=matiere, demande=demande, rank=rank + 1
|
|
|
|
)
|
2017-05-23 06:56:29 +02:00
|
|
|
attrib.save()
|
2013-10-06 11:20:59 +02:00
|
|
|
demande.traitee = True
|
|
|
|
demande.traitee_par = request.user
|
2017-11-19 18:41:39 +01:00
|
|
|
demande.processed = timezone.now()
|
2013-10-06 11:20:59 +02:00
|
|
|
demande.save()
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
2018-11-25 00:37:22 +01:00
|
|
|
"petitscours/traitement_demande_success.html",
|
2018-10-06 12:35:49 +02:00
|
|
|
{"demande": demande, "redo": redo},
|
|
|
|
)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
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")
|
|
|
|
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()
|
2017-05-23 06:56:29 +02:00
|
|
|
with transaction.atomic():
|
2018-10-06 12:35:49 +02:00
|
|
|
abilities = PetitCoursAbility.objects.filter(user=request.user).all()
|
2017-05-23 06:56:29 +02:00
|
|
|
for ability in abilities:
|
|
|
|
PetitCoursAttributionCounter.get_uptodate(
|
2018-10-06 12:35:49 +02:00
|
|
|
ability.user, ability.matiere
|
2017-05-23 06:56:29 +02:00
|
|
|
)
|
2013-10-06 11:20:59 +02:00
|
|
|
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)
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
2018-11-25 00:37:22 +01:00
|
|
|
"petitscours/inscription.html",
|
2018-10-06 12:35:49 +02:00
|
|
|
{
|
|
|
|
"formset": formset,
|
|
|
|
"success": success,
|
|
|
|
"receive_proposals": profile.petits_cours_accept,
|
|
|
|
"remarques": profile.petits_cours_remarques,
|
|
|
|
},
|
|
|
|
)
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
@csrf_exempt
|
2018-11-25 17:05:55 +01:00
|
|
|
def demande(request, *, raw: bool = False):
|
2013-10-06 11:20:59 +02:00
|
|
|
success = False
|
|
|
|
if request.method == "POST":
|
|
|
|
form = DemandeForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
success = True
|
|
|
|
else:
|
|
|
|
form = DemandeForm()
|
2018-11-25 17:05:55 +01:00
|
|
|
template_name = "petitscours/demande.html"
|
|
|
|
if raw:
|
|
|
|
template_name = "petitscours/demande_raw.html"
|
|
|
|
return render(request, template_name, {"form": form, "success": success})
|