2018-10-06 12:35:49 +02:00
|
|
|
from django.contrib.auth.models import User
|
2013-10-06 11:20:59 +02:00
|
|
|
from django.db import models
|
2017-02-05 17:07:58 +01:00
|
|
|
from django.db.models import Min
|
2018-11-25 17:05:55 +01:00
|
|
|
from django.utils.functional import cached_property
|
2013-10-06 11:20:59 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2019-06-26 18:57:16 +02:00
|
|
|
from shared.utils import choices_length
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
LEVELS_CHOICES = (
|
2018-10-06 12:35:49 +02:00
|
|
|
("college", _("Collège")),
|
|
|
|
("lycee", _("Lycée")),
|
|
|
|
("prepa1styear", _("Prépa 1ère année / L1")),
|
|
|
|
("prepa2ndyear", _("Prépa 2ème année / L2")),
|
|
|
|
("licence3", _("Licence 3")),
|
2018-12-10 20:54:39 +01:00
|
|
|
("master1", _("Master (1ère ou 2ème année)")),
|
2018-10-06 12:35:49 +02:00
|
|
|
("other", _("Autre (préciser dans les commentaires)")),
|
2013-10-06 11:20:59 +02:00
|
|
|
)
|
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
class PetitCoursSubject(models.Model):
|
2016-05-26 22:44:10 +02:00
|
|
|
name = models.CharField(_("Matière"), max_length=30)
|
2018-10-06 12:35:49 +02:00
|
|
|
users = models.ManyToManyField(
|
|
|
|
User, related_name="petits_cours_matieres", through="PetitCoursAbility"
|
|
|
|
)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
class Meta:
|
2018-11-25 00:37:22 +01:00
|
|
|
app_label = "gestioncof"
|
2013-10-06 11:20:59 +02:00
|
|
|
verbose_name = "Matière de petits cours"
|
|
|
|
verbose_name_plural = "Matières des petits cours"
|
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
def __str__(self):
|
2013-10-06 11:20:59 +02:00
|
|
|
return self.name
|
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
class PetitCoursAbility(models.Model):
|
2017-11-19 18:41:39 +01:00
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
|
|
matiere = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
PetitCoursSubject, on_delete=models.CASCADE, verbose_name=_("Matière")
|
|
|
|
)
|
|
|
|
niveau = models.CharField(
|
|
|
|
_("Niveau"), choices=LEVELS_CHOICES, max_length=choices_length(LEVELS_CHOICES)
|
2017-11-19 18:41:39 +01:00
|
|
|
)
|
2016-07-15 00:02:56 +02:00
|
|
|
agrege = models.BooleanField(_("Agrégé"), default=False)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
class Meta:
|
2018-11-25 00:37:22 +01:00
|
|
|
app_label = "gestioncof"
|
2013-10-06 11:20:59 +02:00
|
|
|
verbose_name = "Compétence petits cours"
|
|
|
|
verbose_name_plural = "Compétences des petits cours"
|
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
def __str__(self):
|
2017-02-05 13:49:01 +01:00
|
|
|
return "{:s} - {!s} - {:s}".format(
|
|
|
|
self.user.username, self.matiere, self.niveau
|
|
|
|
)
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2018-11-25 17:05:55 +01:00
|
|
|
@cached_property
|
|
|
|
def counter(self) -> int:
|
|
|
|
"""Le compteur d'attribution associé au professeur pour cette matière."""
|
|
|
|
|
|
|
|
return PetitCoursAttributionCounter.get_uptodate(self.user, self.matiere).count
|
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
class PetitCoursDemande(models.Model):
|
2016-07-15 00:02:56 +02:00
|
|
|
name = models.CharField(_("Nom/prénom"), max_length=200)
|
|
|
|
email = models.CharField(_("Adresse email"), max_length=300)
|
2018-10-06 12:35:49 +02:00
|
|
|
phone = models.CharField(_("Téléphone (facultatif)"), max_length=20, blank=True)
|
2016-06-17 13:26:55 +02:00
|
|
|
quand = models.CharField(
|
2016-07-15 00:02:56 +02:00
|
|
|
_("Quand ?"),
|
2018-10-06 12:35:49 +02:00
|
|
|
help_text=_(
|
|
|
|
"Indiquez ici la période désirée pour les petits"
|
|
|
|
" cours (vacances scolaires, semaine, week-end)."
|
|
|
|
),
|
|
|
|
max_length=300,
|
|
|
|
blank=True,
|
|
|
|
)
|
2016-06-17 13:26:55 +02:00
|
|
|
freq = models.CharField(
|
2016-07-15 00:02:56 +02:00
|
|
|
_("Fréquence"),
|
2018-10-06 12:35:49 +02:00
|
|
|
help_text=_(
|
|
|
|
"Indiquez ici la fréquence envisagée "
|
|
|
|
"(hebdomadaire, 2 fois par semaine, ...)"
|
|
|
|
),
|
|
|
|
max_length=300,
|
|
|
|
blank=True,
|
|
|
|
)
|
2016-06-17 13:26:55 +02:00
|
|
|
lieu = models.CharField(
|
2016-07-15 00:02:56 +02:00
|
|
|
_("Lieu (si préférence)"),
|
|
|
|
help_text=_("Si vous avez avez une préférence sur le lieu."),
|
2018-10-06 12:35:49 +02:00
|
|
|
max_length=300,
|
|
|
|
blank=True,
|
|
|
|
)
|
2016-06-17 13:26:55 +02:00
|
|
|
|
|
|
|
matieres = models.ManyToManyField(
|
2018-10-06 12:35:49 +02:00
|
|
|
PetitCoursSubject, verbose_name=_("Matières"), related_name="demandes"
|
|
|
|
)
|
2016-07-15 00:02:56 +02:00
|
|
|
agrege_requis = models.BooleanField(_("Agrégé requis"), default=False)
|
2018-10-06 12:35:49 +02:00
|
|
|
niveau = models.CharField(
|
|
|
|
_("Niveau"),
|
|
|
|
default="",
|
|
|
|
choices=LEVELS_CHOICES,
|
|
|
|
max_length=choices_length(LEVELS_CHOICES),
|
|
|
|
)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-07-15 00:02:56 +02:00
|
|
|
remarques = models.TextField(_("Remarques et précisions"), blank=True)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2016-07-15 00:02:56 +02:00
|
|
|
traitee = models.BooleanField(_("Traitée"), default=False)
|
2017-11-19 18:41:39 +01:00
|
|
|
traitee_par = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
User, on_delete=models.CASCADE, blank=True, null=True
|
2017-11-19 18:41:39 +01:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
processed = models.DateTimeField(_("Date de traitement"), blank=True, null=True)
|
2016-07-15 00:02:56 +02:00
|
|
|
created = models.DateTimeField(_("Date de création"), auto_now_add=True)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2017-02-05 17:07:58 +01:00
|
|
|
def get_candidates(self, redo=False):
|
|
|
|
"""
|
|
|
|
Donne la liste des profs disponibles pour chaque matière de la demande.
|
|
|
|
- On ne donne que les agrégés si c'est demandé
|
|
|
|
- Si ``redo`` vaut ``True``, cela signifie qu'on retraite la demande et
|
|
|
|
il ne faut pas proposer à nouveau des noms qui ont déjà été proposés
|
|
|
|
"""
|
|
|
|
for matiere in self.matieres.all():
|
|
|
|
candidates = PetitCoursAbility.objects.filter(
|
|
|
|
matiere=matiere,
|
|
|
|
niveau=self.niveau,
|
|
|
|
user__profile__is_cof=True,
|
2018-10-06 12:35:49 +02:00
|
|
|
user__profile__petits_cours_accept=True,
|
2017-02-05 17:07:58 +01:00
|
|
|
)
|
|
|
|
if self.agrege_requis:
|
|
|
|
candidates = candidates.filter(agrege=True)
|
|
|
|
if redo:
|
|
|
|
attrs = self.petitcoursattribution_set.filter(matiere=matiere)
|
2018-10-06 12:35:49 +02:00
|
|
|
already_proposed = [attr.user for attr in attrs]
|
2017-02-05 17:07:58 +01:00
|
|
|
candidates = candidates.exclude(user__in=already_proposed)
|
2018-10-06 12:35:49 +02:00
|
|
|
candidates = candidates.order_by("?").select_related().all()
|
2017-02-05 17:07:58 +01:00
|
|
|
yield (matiere, candidates)
|
|
|
|
|
2018-11-25 17:05:55 +01:00
|
|
|
def get_proposals(self, *, max_candidates: int = None, redo: bool = False):
|
|
|
|
"""Calcule une proposition de profs pour la demande.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
max_candidates (optionnel; défaut: `None`): Le nombre maximum de
|
|
|
|
candidats à proposer par demande. Si `None` ou non spécifié,
|
|
|
|
il n'y a pas de limite.
|
|
|
|
|
|
|
|
redo (optionel; défaut: `False`): Détermine si on re-calcule les
|
|
|
|
propositions pour la demande (les professeurs à qui on a déjà
|
|
|
|
proposé cette demande sont exclus).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
proposals: Le dictionnaire qui associe à chaque matière la liste
|
|
|
|
des professeurs proposés. Les matières pour lesquelles aucun
|
|
|
|
professeur n'est disponible ne sont pas présentes dans
|
|
|
|
`proposals`.
|
|
|
|
unsatisfied: La liste des matières pour lesquelles aucun
|
|
|
|
professeur n'est disponible.
|
|
|
|
"""
|
|
|
|
|
|
|
|
proposals = {}
|
|
|
|
unsatisfied = []
|
|
|
|
for matiere, candidates in self.get_candidates(redo=redo):
|
|
|
|
if not candidates:
|
|
|
|
unsatisfied.append(matiere)
|
|
|
|
else:
|
|
|
|
proposals[matiere] = matiere_proposals = []
|
|
|
|
|
|
|
|
candidates = sorted(candidates, key=lambda c: c.counter)
|
|
|
|
candidates = candidates[:max_candidates]
|
|
|
|
for candidate in candidates[:max_candidates]:
|
|
|
|
matiere_proposals.append(candidate.user)
|
|
|
|
|
|
|
|
return proposals, unsatisfied
|
|
|
|
|
2019-01-03 16:42:46 +01:00
|
|
|
def get_absolute_url(self):
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
return reverse("petits-cours-demande-details", kwargs={"pk": str(self.id)})
|
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
class Meta:
|
2018-11-25 00:37:22 +01:00
|
|
|
app_label = "gestioncof"
|
2013-10-06 11:20:59 +02:00
|
|
|
verbose_name = "Demande de petits cours"
|
|
|
|
verbose_name_plural = "Demandes de petits cours"
|
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
def __str__(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return "Demande {:d} du {:s}".format(self.id, self.created.strftime("%d %b %Y"))
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
class PetitCoursAttribution(models.Model):
|
2017-11-19 18:41:39 +01:00
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
|
|
demande = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
PetitCoursDemande, on_delete=models.CASCADE, verbose_name=_("Demande")
|
2017-11-19 18:41:39 +01:00
|
|
|
)
|
|
|
|
matiere = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
PetitCoursSubject, on_delete=models.CASCADE, verbose_name=_("Matière")
|
2017-11-19 18:41:39 +01:00
|
|
|
)
|
2016-07-15 00:02:56 +02:00
|
|
|
date = models.DateTimeField(_("Date d'attribution"), auto_now_add=True)
|
2013-10-06 11:20:59 +02:00
|
|
|
rank = models.IntegerField("Rang dans l'email")
|
2018-10-06 12:35:49 +02:00
|
|
|
selected = models.BooleanField(_("Sélectionné par le demandeur"), default=False)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
class Meta:
|
2018-11-25 00:37:22 +01:00
|
|
|
app_label = "gestioncof"
|
2013-10-06 11:20:59 +02:00
|
|
|
verbose_name = "Attribution de petits cours"
|
|
|
|
verbose_name_plural = "Attributions de petits cours"
|
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
def __str__(self):
|
2017-02-05 13:49:01 +01:00
|
|
|
return "Attribution de la demande {:d} à {:s} pour {!s}".format(
|
|
|
|
self.demande.id, self.user.username, self.matiere
|
|
|
|
)
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
|
|
|
|
class PetitCoursAttributionCounter(models.Model):
|
2017-11-19 18:41:39 +01:00
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
|
|
matiere = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
PetitCoursSubject, on_delete=models.CASCADE, verbose_name=_("Matiere")
|
2017-11-19 18:41:39 +01:00
|
|
|
)
|
2016-06-17 13:26:55 +02:00
|
|
|
count = models.IntegerField("Nombre d'envois", default=0)
|
2013-10-06 11:20:59 +02:00
|
|
|
|
2017-02-05 17:07:58 +01:00
|
|
|
@classmethod
|
|
|
|
def get_uptodate(cls, user, matiere):
|
|
|
|
"""
|
|
|
|
Donne le compteur de l'utilisateur pour cette matière. Si le compteur
|
|
|
|
n'existe pas encore, il est initialisé avec le minimum des valeurs des
|
|
|
|
compteurs de tout le monde.
|
|
|
|
"""
|
2018-10-06 12:35:49 +02:00
|
|
|
counter, created = cls.objects.get_or_create(user=user, matiere=matiere)
|
2017-02-05 17:07:58 +01:00
|
|
|
if created:
|
|
|
|
mincount = (
|
2018-10-06 12:35:49 +02:00
|
|
|
cls.objects.filter(matiere=matiere)
|
|
|
|
.exclude(user=user)
|
|
|
|
.aggregate(Min("count"))["count__min"]
|
2017-02-05 17:07:58 +01:00
|
|
|
)
|
2017-10-25 22:01:58 +02:00
|
|
|
counter.count = mincount or 0
|
2017-02-05 17:07:58 +01:00
|
|
|
counter.save()
|
|
|
|
return counter
|
|
|
|
|
2013-10-06 11:20:59 +02:00
|
|
|
class Meta:
|
2018-11-25 00:37:22 +01:00
|
|
|
app_label = "gestioncof"
|
2013-10-06 11:20:59 +02:00
|
|
|
verbose_name = "Compteur d'attribution de petits cours"
|
|
|
|
verbose_name_plural = "Compteurs d'attributions de petits cours"
|
|
|
|
|
2016-07-15 00:02:56 +02:00
|
|
|
def __str__(self):
|
2017-02-05 13:49:01 +01:00
|
|
|
return "{:d} demandes envoyées à {:s} pour {!s}".format(
|
|
|
|
self.count, self.user.username, self.matiere
|
|
|
|
)
|