29 lines
1021 B
Python
29 lines
1021 B
Python
from django.db import models
|
|
from django.db.models import Q
|
|
from django.contrib.auth.models import AbstractUser
|
|
from django.contrib.auth import authenticate
|
|
|
|
# Create your models here.
|
|
class SchoolID(models.Model):
|
|
school_index = models.CharField(max_length=8, db_index=True, unique=True)
|
|
|
|
def __str__(self):
|
|
return self.school_index
|
|
|
|
class User(AbstractUser):
|
|
class Roles(models.TextChoices):
|
|
COMMON = 'common', 'Common'
|
|
PROFESSOR = 'prof', 'Professor'
|
|
|
|
school_index = models.ForeignKey(SchoolID, on_delete=models.PROTECT)
|
|
role = models.CharField(max_length=20, choices=Roles, default=Roles.COMMON)
|
|
|
|
@staticmethod
|
|
def authenticate(request, username_or_schoolid, password):
|
|
user = User.objects.filter(Q(username=username_or_schoolid) | Q(school_index__school_index=username_or_schoolid)).first()
|
|
|
|
if not user:
|
|
return False
|
|
|
|
return authenticate(request, username=user.username, password=password)
|