Skip to content

Commit

Permalink
change Report data classs and fetching behaviour
Browse files Browse the repository at this point in the history
Most fields on ReportSubject can be disabled by the supervisor, so
they're Optional. Instead of having a Report.published attribute, the
Period.report property can directly return none, since the report has no
data anyway.
  • Loading branch information
bain3 committed Aug 3, 2023
1 parent 22c9da8 commit f05fc37
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
58 changes: 39 additions & 19 deletions pronotepy/dataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,42 +324,56 @@ class Report(Object):
"""Represents a student report. You shouldn't have to create this class manually.
Attributes:
published (bool): Is the report published ?
subjects (List[ReportSubject]): the subjects that are present in the report
comments (List[str]): the global report comments
"""

class ReportSubject(Subject):
class ReportSubject(Object):
"""
Represents a subject found in a report. You shouldn't have to create this class manually.
Attributes:
id (str): the id of the subject (used internally)
name (str): name of the subject
color (str): the color of the subject
comments (List[str]): the list of the subject's comments
class_average (str): the average of the class
student_average (str): the average of the student
min_average (str): the lowest average of the class
max_average (str): the highest average of the class
coefficient (str): the coefficient of the subject
class_average (Optional[str]): the average of the class
student_average (Optional[str]): the average of the student
min_average (Optional[str]): the lowest average of the class
max_average (Optional[str]): the highest average of the class
coefficient (Optional[str]): the coefficient of the subject
teachers (List[str]): the subject's teachers' names
"""

def __init__(self, parsed_json: dict) -> None:
super().__init__(parsed_json)

self.id: str = self._resolver(str, "N")
self.name: str = self._resolver(str, "L")

self.color: str = self._resolver(str, "couleur")
self.comments: List[str] = self._resolver(
lambda l: [c["L"] for c in l if "L" in c], "ListeAppreciations", "V"
)
self.class_average: str = self._resolver(
Util.grade_parse, "MoyenneClasse", "V"

def grade_or_none(grade: Any) -> Optional[str]:
return Util.grade_parse(grade) if grade else None

self.class_average: Optional[str] = self._resolver(
grade_or_none, "MoyenneClasse", "V", strict=False
)
self.student_average: Optional[str] = self._resolver(
grade_or_none, "MoyenneEleve", "V", strict=False
)
self.student_average: str = self._resolver(
Util.grade_parse, "MoyenneEleve", "V"
self.min_average: Optional[str] = self._resolver(
grade_or_none, "MoyenneInf", "V", strict=False
)
self.max_average: Optional[str] = self._resolver(
grade_or_none, "MoyenneSup", "V", strict=False
)
self.coefficient: Optional[str] = self._resolver(
str, "Coefficient", "V", strict=False
)
self.min_average: str = self._resolver(Util.grade_parse, "MoyenneInf", "V")
self.max_average: str = self._resolver(Util.grade_parse, "MoyenneSup", "V")
self.coefficient: str = self._resolver(str, "Coefficient", "V")
self.teachers: List[str] = self._resolver(
lambda l: [i["L"] for i in l], "ListeProfesseurs", "V", default=[]
)
Expand All @@ -376,7 +390,7 @@ def __init__(self, parsed_json: dict) -> None:
default=[],
)
self.comments: List[str] = self._resolver(
lambda l: [c["L"] for c in l],
lambda l: [c["L"] for c in l if "L" in c],
"ObjetListeAppreciations",
"V",
"ListeAppreciations",
Expand Down Expand Up @@ -480,11 +494,17 @@ def __init__(self, client: ClientBase, json_dict: dict) -> None:
del self._resolver

@property
def report(self) -> Report:
"""Gets a report from a period"""
def report(self) -> Optional[Report]:
"""
Gets a report from a period.
Returns:
Optional[Report]:
When ``None``, then the report is not yet published or is unavailable for any other reason
"""
json_data = {"periode": {"G": 2, "N": self.id, "L": self.name}}
response = self._client.post("PageBulletins", 13, json_data)
return Report(response["donneesSec"]["donnees"])
data = self._client.post("PageBulletins", 13, json_data)["donneesSec"]["donnees"]
return Report(data) if "Message" not in data else None

@property
def grades(self) -> List["Grade"]:
Expand Down
9 changes: 4 additions & 5 deletions pronotepy/test_pronotepy.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ def test_refresh(self) -> None:
client.refresh()
self.assertEqual(client.session_check(), True)

def test_report(self) -> None:
report = client.periods[0].report

self.assertIsInstance(report, pronotepy.Report)


class TestPeriod(unittest.TestCase):
period: pronotepy.Period
Expand Down Expand Up @@ -138,6 +133,10 @@ def test_class_overall_average(self) -> None:
a = self.period.class_overall_average
self.assertTrue(type(a) is str or a is None)

def test_report(self) -> None:
report = self.period.report
self.assertTrue(report is None or isinstance(report, pronotepy.Report))


class TestInformation(unittest.TestCase):
def test_unread(self) -> None:
Expand Down

0 comments on commit f05fc37

Please sign in to comment.