site_tps/users/models.py

25 lines
805 B
Python
Raw Normal View History

2018-01-14 12:19:11 +00:00
from django.db import models
from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save
from django.dispatch import receiver
2018-01-14 12:19:11 +00:00
class SchoolProfile(models.Model):
"""Ajoute un champ pour distinguer les groupes écoles des autres."""
is_school = models.BooleanField()
group = models.OneToOneField(Group, on_delete=models.CASCADE)
def __str__(self):
return self.group.name
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)