Skip to content

Commit

Permalink
fix: model managers
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Feb 8, 2024
1 parent 53e2bee commit ac9d027
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
# Generated by Django 3.2.20 on 2024-02-05 10:04
# Generated by Django 3.2.23 on 2024-02-08 14:57

import django.contrib.auth.models
import codeforlife.user.models.user
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('common', '0045_otp'),
('common', '0048_unique_school_names'),
('user', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='IndependentUser',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('user.user',),
managers=[
('objects', codeforlife.user.models.user.IndependentUserManager()),
],
),
migrations.CreateModel(
name='NonSchoolTeacher',
fields=[
Expand All @@ -34,7 +48,7 @@ class Migration(migrations.Migration):
},
bases=('user.user',),
managers=[
('objects', django.contrib.auth.models.UserManager()),
('objects', codeforlife.user.models.user.NonSchoolTeacherUserManager()),
],
),
migrations.CreateModel(
Expand All @@ -59,7 +73,7 @@ class Migration(migrations.Migration):
},
bases=('user.user',),
managers=[
('objects', django.contrib.auth.models.UserManager()),
('objects', codeforlife.user.models.user.SchoolTeacherUserManager()),
],
),
migrations.CreateModel(
Expand All @@ -73,7 +87,7 @@ class Migration(migrations.Migration):
},
bases=('user.user',),
managers=[
('objects', django.contrib.auth.models.UserManager()),
('objects', codeforlife.user.models.user.StudentUserManager()),
],
),
migrations.CreateModel(
Expand All @@ -87,7 +101,7 @@ class Migration(migrations.Migration):
},
bases=('user.user',),
managers=[
('objects', django.contrib.auth.models.UserManager()),
('objects', codeforlife.user.models.user.TeacherUserManager()),
],
),
]
1 change: 1 addition & 0 deletions codeforlife/user/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .student import Student
from .teacher import NonSchoolTeacher, SchoolTeacher, Teacher
from .user import ( # TODO: remove UserProfile
IndependentUser,
NonSchoolTeacherUser,
SchoolTeacherUser,
StudentUser,
Expand Down
16 changes: 15 additions & 1 deletion codeforlife/user/models/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Created on 05/02/2024 at 09:49:56(+00:00).
"""

from common.models import Teacher
from common.models import Teacher, TeacherModelManager
from django_stubs_ext.db.models import TypedModelMeta

from .school import School
Expand All @@ -17,6 +17,13 @@ class SchoolTeacher(Teacher):
class Meta(TypedModelMeta):
proxy = True

# pylint: disable-next=missing-class-docstring
class Manager(TeacherModelManager):
def get_queryset(self):
return super().get_queryset().filter(school__isnull=False)

objects: Manager = Manager()


class NonSchoolTeacher(Teacher):
"""A teacher that is not in a school."""
Expand All @@ -25,3 +32,10 @@ class NonSchoolTeacher(Teacher):

class Meta(TypedModelMeta):
proxy = True

# pylint: disable-next=missing-class-docstring
class Manager(TeacherModelManager):
def get_queryset(self):
return super().get_queryset().filter(school__isnull=True)

objects: Manager = Manager()
81 changes: 75 additions & 6 deletions codeforlife/user/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# pylint: disable-next=imported-auth-user
from django.contrib.auth.models import User as _User
from django.contrib.auth.models import UserManager
from django.db.models.query import QuerySet
from django.utils.translation import gettext_lazy as _
from django_stubs_ext.db.models import TypedModelMeta
Expand Down Expand Up @@ -74,6 +75,17 @@ def aimmo_badges(self):
return self.userprofile.aimmo_badges


# pylint: disable-next=missing-class-docstring
class TeacherUserManager(UserManager):
# pylint: disable-next=missing-function-docstring
def get_queryset(self):
return (
super()
.get_queryset()
.filter(teacher__isnull=False, student__isnull=True)
)


class TeacherUser(User):
"""A user that is a teacher."""

Expand All @@ -83,6 +95,15 @@ class TeacherUser(User):
class Meta(TypedModelMeta):
proxy = True

objects: TeacherUserManager = TeacherUserManager() # type: ignore[misc]


# pylint: disable-next=missing-class-docstring
class SchoolTeacherUserManager(TeacherUserManager):
# pylint: disable-next=missing-function-docstring
def get_queryset(self):
return super().get_queryset().filter(teacher__school__isnull=False)


class SchoolTeacherUser(User):
"""A user that is a teacher in a school."""
Expand All @@ -93,6 +114,15 @@ class SchoolTeacherUser(User):
class Meta(TypedModelMeta):
proxy = True

objects: SchoolTeacherUserManager = SchoolTeacherUserManager() # type: ignore[misc]


# pylint: disable-next=missing-class-docstring
class NonSchoolTeacherUserManager(TeacherUserManager):
# pylint: disable-next=missing-function-docstring
def get_queryset(self):
return super().get_queryset().filter(teacher__school__isnull=True)


class NonSchoolTeacherUser(User):
"""A user that is a teacher not in a school."""
Expand All @@ -103,6 +133,24 @@ class NonSchoolTeacherUser(User):
class Meta(TypedModelMeta):
proxy = True

objects: NonSchoolTeacherUserManager = NonSchoolTeacherUserManager() # type: ignore[misc]


# pylint: disable-next=missing-class-docstring
class StudentUserManager(UserManager):
# pylint: disable-next=missing-function-docstring
def get_queryset(self):
return (
super()
.get_queryset()
.filter(
teacher__isnull=True,
student__isnull=False,
# TODO: remove in new model
student__class_field__isnull=False,
)
)


class StudentUser(User):
"""A user that is a student."""
Expand All @@ -113,11 +161,32 @@ class StudentUser(User):
class Meta(TypedModelMeta):
proxy = True

objects: StudentUserManager = StudentUserManager() # type: ignore[misc]


# pylint: disable-next=missing-class-docstring
class IndependentUserManager(UserManager):
# pylint: disable-next=missing-function-docstring
def get_queryset(self):
# TODO: student__isnull=True in new model
return (
super()
.get_queryset()
.filter(
teacher__isnull=True,
student__isnull=False,
student__class_field__isnull=True,
)
)


class IndependentUser(User):
"""A user that is an independent learner."""

# TODO: uncomment this when using new models.
# class IndependentUser(User):
# teacher: None
# student: None
teacher: None
student: Student # TODO: set to None in new model

class Meta(TypedModelMeta):
proxy = True

# class Meta(TypedModelMeta):
# proxy = True
objects: IndependentUserManager = IndependentUserManager() # type: ignore[misc]

0 comments on commit ac9d027

Please sign in to comment.