authens/authens/models.py
2020-05-29 16:28:30 +02:00

44 lines
1.4 KiB
Python

from django.contrib.auth import get_user_model
from django.db import models
from django.utils.translation import gettext_lazy as _
User = get_user_model()
class CASAccount(models.Model):
"""Information about CAS accounts.
A user is given an instance of this model iff she has a CAS account.
Instances of this model should only be created by the `ENSCASBackend` authentication
backend.
"""
user = models.OneToOneField(
User,
verbose_name=_("utilisateurice"),
on_delete=models.CASCADE,
related_name="cas_account",
)
cas_login = models.CharField(
verbose_name=_("login CAS"), max_length=1023, blank=False, unique=True,
)
entrance_year = models.SmallIntegerField(
verbose_name=_("année de création du compte CAS"), blank=False, null=False
)
# This is True if and only if the user is connected via CAS (and not e.g. by
# password). This is used to decide whether to redirect to user to the CAS logout
# page or not when the user disconnects.
connected_to_cas = models.BooleanField(default=False, editable=False)
class Meta:
verbose_name = _("Compte CAS")
verbose_name_plural = _("Comptes CAS")
def __str__(self):
return _("compte CAS %(cas_login) (promo %(entrance_year)s) lié à %(user)s") % {
"cas_login": self.cas_login,
"entrance_year": self.entrance_year,
"user": self.user.username,
}