recopytex/backend/model/tribe.py

56 lines
1.4 KiB
Python
Raw Permalink Normal View History

from __future__ import annotations
from dataclasses import dataclass, field
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from backend.model.assessment import Assessment
from backend.model.student import Student
class TribeError(Exception):
pass
@dataclass
class Tribe:
name: str
level: str
students: list[Student] = field(default_factory=list)
assessments: list[Assessment] = field(default_factory=list)
def register_assessment(self, assessment: Assessment) -> None:
self.assessments.append(assessment)
@property
def students_id(self) -> list[str]:
return [s.id for s in self.students]
def register_student(self, student: Student) -> None:
"""Register a student
If the student is already registered, it is modified.
"""
try:
old_student = next(filter(lambda s: s.id == student.id, self.students))
except StopIteration:
pass
else:
self.students.remove(old_student)
self.students.append(student)
2022-12-20 09:25:12 +00:00
def __eq__(self, other: object) -> bool:
if isinstance(other, Tribe):
return self.name == other.name
2022-12-20 09:25:12 +00:00
return False
2022-12-21 05:53:12 +00:00
2022-12-30 06:35:05 +00:00
def __hash__(self) -> int:
return hash(self.name)
2022-12-21 05:53:12 +00:00
def to_dict(self) -> dict:
return {"name": self.name, "level": self.level}
2022-12-27 07:13:34 +00:00
def to_tuple(self) -> tuple:
return (self.name, self.level)