Skip to content

Commit

Permalink
Added function to get marks by period
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeusina committed Aug 16, 2023
1 parent 115f91b commit 69b30ef
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
58 changes: 58 additions & 0 deletions petersbugredu_wrap/types/child.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import datetime
import json
import logging

import requests

from petersbugredu_wrap.types.action_payload import ActionPayload
from petersbugredu_wrap.types.mark_entry import MarkEntry
from petersbugredu_wrap.types.teacher import Teacher
from petersbugredu_wrap.types.identity import Identity
from petersbugredu_wrap.types.education import Education
Expand Down Expand Up @@ -70,3 +72,59 @@ def get_teacher_list(self) -> list[Teacher]:
subjects=subjects
))
return teacher_list

def get_mark_list_by_period(self, date_from: datetime.date, date_to: datetime.date, education_number: int = 0)\
-> list[MarkEntry]:
"""
Function for getting mark list from API
:param date_from:
:param date_to:
:param education_number:
:return:
"""
url = ((endpoints.MARKS_BY_DATE_URL
.replace("{{education_id}}", str(self.educations[education_number].education_id))
.replace("{{date_from}}", date_from.strftime("%d.%m.%Y")))
.replace("{{date_to}}", date_to.strftime("%d.%m.%Y")))
cookie = {"X-JWT-Token": self._token}
self.logger.debug("Data for get marks by period prepaired")
pages = []
response = requests.request("GET", url.replace("{{page}}", "0"), cookies=cookie)
self.logger.debug("Made request 1st page of marks with status code %code%"
.replace("%code%", str(response.status_code)))
pages.append(json.loads(response.text))
total_pages: int = pages[0]["data"]["total_pages"]
if total_pages > 1:
for page_number in range(2, total_pages + 1):
response = requests.request("GET", url.replace("{{page}}", str(page_number)), cookies=cookie)
pages.append(json.loads(response.text))
self.logger.debug("Made request %page% page of marks with status code %code%"
.replace("%code%", str(response.status_code)).replace("%page%", str(page_number)))
marks = []
for page in pages:
for entry in page["data"]["items"]:
id = entry["id"]
education_id = entry["education_id"]
lesson_id = entry.get("lesson_id", 0)
subject_id = entry["subject_id"]
subject_name = entry["subject_name"]
date = datetime.datetime.strptime(entry["date"], "%d.%m.%Y")
estimate_value_code = entry.get("estimate_value_code", "")
estimate_value_name = entry.get("estimate_value_name", "")
estimate_type_code = entry.get("estimate_type_code", "")
estimate_type_name = entry.get("estimate_type_name", "")
estimate_comment = entry.get("estimate_comment", "")
mark_entry = MarkEntry(
id=id,
education_id=education_id,
lesson_id=lesson_id,
subject_id=subject_id,
subject_name=subject_name,
date=date,
estimate_value_code=estimate_value_code,
estimate_value_name=estimate_value_name,
estimate_type_code=estimate_type_code,
estimate_type_name=estimate_type_name,
estimate_comment=estimate_comment)
marks.append(mark_entry)
return marks
13 changes: 10 additions & 3 deletions petersbugredu_wrap/types/mark_entry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import datetime


class MarkEntry:
def __init__(self, id: int, education_id: int, lesson_id: int, subject_id: int, subject_name: str, date: str,
estimate_value_code: str, estimate_value_name: str, estimate_comment: str):
def __init__(self, id: int, education_id: int, lesson_id: int, subject_id: int, subject_name: str, date: datetime.datetime,
estimate_value_code: str, estimate_value_name: str, estimate_type_code: str, estimate_type_name: str,
estimate_comment: str):
"""
Class represents mark entry in API
:param id:
Expand All @@ -11,8 +15,12 @@ def __init__(self, id: int, education_id: int, lesson_id: int, subject_id: int,
:param date:
:param estimate_value_code:
:param estimate_value_name:
:param estimate_type_code:
:param estimate_type_name:
:param estimate_comment:
"""
self.estimate_type_name = estimate_type_name
self.estimate_type_code = estimate_type_code
self.estimate_comment = estimate_comment
self.estimate_value_name = estimate_value_name
self.estimate_value_code = estimate_value_code
Expand All @@ -22,4 +30,3 @@ def __init__(self, id: int, education_id: int, lesson_id: int, subject_id: int,
self.lesson_id = lesson_id
self.id = id
self.education_id = education_id
raise NotImplementedError
1 change: 1 addition & 0 deletions petersbugredu_wrap/utils/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
LOGIN_URL = "https://dnevnik2.petersburgedu.ru/api/user/auth/login"
RELATED_CHILD_LIST_URL = "https://dnevnik2.petersburgedu.ru/api/journal/person/related-child-list"
TEACHER_LIST_URL="https://dnevnik2.petersburgedu.ru/api/journal/teacher/list?p_page={{page}}&p_educations%5B%5D={{education_id}}"
MARKS_BY_DATE_URL="https://dnevnik2.petersburgedu.ru/api/journal/estimate/table?p_educations%5B%5D={{education_id}}p_date_from={{date_from}}&p_date_to={{date_to}}&p_limit=100&p_page={{page}}"

0 comments on commit 69b30ef

Please sign in to comment.