2018-01-14 12:19:11 +00:00
|
|
|
from django.db import models
|
2018-01-31 09:15:51 +00:00
|
|
|
from django.contrib.auth.models import User, Group
|
|
|
|
from django.db.models.signals import post_save
|
2018-02-28 20:25:44 +00:00
|
|
|
from django.urls import reverse
|
2018-01-31 09:15:51 +00:00
|
|
|
from django.dispatch import receiver
|
2018-01-14 12:19:11 +00:00
|
|
|
|
2018-01-31 09:15:51 +00:00
|
|
|
|
|
|
|
class SchoolProfile(models.Model):
|
|
|
|
"""Ajoute un champ pour distinguer les groupes écoles des autres."""
|
2018-02-28 20:25:44 +00:00
|
|
|
group = models.OneToOneField(
|
|
|
|
Group,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="students"
|
|
|
|
)
|
2018-01-31 09:15:51 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.group.name
|
|
|
|
|
2018-02-28 20:25:44 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse("users:school", kwargs={'pk':self.pk})
|
2018-01-31 09:15:51 +00:00
|
|
|
|
|
|
|
class UserProfile(models.Model):
|
|
|
|
"""Profil d'un utilisateur"""
|
|
|
|
school = models.ForeignKey(SchoolProfile, on_delete=models.SET_NULL, null=True, blank=True)
|
|
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=UserProfile)
|
|
|
|
def update_groups(sender, instance, **kwargs):
|
|
|
|
instance.user.groups.add(instance.school.group)
|