File size: 1,562 Bytes
6e2b36a
639b49a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import Optional, Union
from utils.enums.grade import Grade

from .student import Student

from sqlmodel import SQLModel, Field, Relationship


class Enrollment(SQLModel, table=True):
    """
    Represents an Enrollment, including the student, the course they are enrolled in, and the assigned grade.

    Attributes:
        id: (int): The enrollment ID
        student_id (str): The ID of the student who is enrolled in the course.
        course_id (str): The ID of the course in which the student is enrolled.
        grade (Union[str, Grade]): The grade assigned to the student for the course. Default if NO_GRADE with enum value of None if no grade has been assigned yet.
    """

    id: int = Field(primary_key=True)
    student_id: str = Field(foreign_key="student.id")
    course_id: str = Field(foreign_key="course.id")
    grade: Union[str, Grade] = Field(default=Grade.A_PLUS)

    course: "Course" = Relationship(
        back_populates="enrollments")

    def assign_grade(self, grade: Grade) -> None:
        """
        Assigns a grade to the student for the course.

        Args:
            grade (Grade): The grade to assign to the student.
        """
        self.grade = grade

    def __str__(self) -> str:
        """
        Returns a string representation of the Enrollment.

        Returns:
            str: A description of the enrollment including the student id_number, course_id, and grade.
        """
        return f"Enrollment(student id: {self.student_id}, course: {self.course_id}, grade: {self.grade.value})"