Skip to content

Commit

Permalink
add properties
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Dec 8, 2023
1 parent 4a05df4 commit a310da8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions codeforlife/user/models/klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class Class(AbstractModel):
],
)

teacher: "_teacher.Teacher" = models.ForeignKey(
teacher: "_teacher.Teacher" = models.ForeignKey( # type: ignore[assignment]
"user.Teacher",
related_name="classes",
on_delete=models.CASCADE,
)

school: "_school.School" = models.ForeignKey(
school: "_school.School" = models.ForeignKey( # type: ignore[assignment]
"user.School",
related_name="classes",
on_delete=models.CASCADE,
Expand Down
13 changes: 11 additions & 2 deletions codeforlife/user/models/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ def bulk_create_users(
validators=[MinLengthValidator(64)],
)

# TODO: add direct reference to teacher

class Meta(TypedModelMeta):
verbose_name = _("student")
verbose_name_plural = _("students")
Expand All @@ -168,3 +166,14 @@ class Meta(TypedModelMeta):
name="student__auto_gen_password",
),
]

@property
def teacher(self):
"""The student's teacher (if they have one).
Returns:
The student's class-teacher.
"""

if self.klass:
return self.klass.teacher
15 changes: 13 additions & 2 deletions codeforlife/user/models/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ...models import AbstractModel
from . import klass as _class
from . import school as _school
from . import student as _student
from . import user as _user


Expand Down Expand Up @@ -61,8 +62,18 @@ def create_user(self, teacher: t.Dict[str, t.Any], **fields):
help_text=_("Designates if the teacher has admin privileges."),
)

# TODO: add direct reference to students

class Meta(TypedModelMeta):
verbose_name = _("teacher")
verbose_name_plural = _("teachers")

@property
def students(self):
"""All students in this teacher's classes.
Returns:
A queryset
"""

return _student.Student.objects.filter(
klass_id__in=list(self.classes.values_list("id", flat=True)),
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ env = ["DJANGO_SETTINGS_MODULE=manage"]

[tool.mypy]
plugins = ["mypy_django_plugin.main", "mypy_drf_plugin.main"]
check_untyped_defs = true

[tool.django-stubs]
django_settings_module = "manage"
Expand Down

0 comments on commit a310da8

Please sign in to comment.