Skip to content

Commit

Permalink
fix(backend): handle modules ids from scrapers
Browse files Browse the repository at this point in the history
  • Loading branch information
EliotAmn committed Jan 31, 2025
1 parent 192e74b commit 8aee5e8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/api/routes/modules_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def modules_route():
"required_credits": student.required_credits
}

@app.route("/api/modules/<string:mod_code>", methods=["POST"])
@app.route("/api/modules/<int:module_id>", methods=["POST"])
@student_auth_middleware()
def module_update_route(mod_code):
def module_update_route(module_id):
student = request.student

module = ModuleService.get_module_by_code(student.id, mod_code)
module = ModuleService.get_module_by_id(student.id, module_id)
if not module:
return {"message": "Module not found"}, 404
data = request.get_json()
Expand Down
2 changes: 1 addition & 1 deletion app/api/routes/scrapers_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_all_moulis():

return {
"known_tests": moulis_ids,
"known_modules": [m.code_module for m in ModuleService.get_recent_fetched_modules(student.id)],
"known_modules": [m.module_id for m in ModuleService.get_recent_fetched_modules(student.id)],
"asked_slugs": asked_slugs,
"asked_pictures": [] if StudentPictureService.is_picture_exists(student.login) else [student.login],
"fetch_start": start.strftime("%Y-%m-%d"),
Expand Down
4 changes: 4 additions & 0 deletions app/models/Module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Module:
student_id: str

code_module: str
module_id: int
semester_id: int
instance_code: str
scol_year: int
Expand Down Expand Up @@ -32,6 +33,7 @@ def __init__(self, mongo_data=None):
self._id = mongo_data["_id"]
self.student_id = mongo_data.get("student_id", None)
self.code_module = mongo_data.get("code_module", None)
self.module_id = mongo_data.get("module_id", None)
self.instance_code = mongo_data.get("instance_code", None)
self.semester_id = mongo_data.get("semester_id", None)

Expand Down Expand Up @@ -61,6 +63,7 @@ def to_dict(self):
"_id": self._id,
"student_id": self.student_id,
"code_module": self.code_module,
"module_id": self.module_id,
"instance_code": self.instance_code,
"semester_id": self.semester_id,
"scol_year": self.scol_year,
Expand All @@ -84,6 +87,7 @@ def to_api(self):
return {
"code_module": self.code_module,
"semester_id": self.semester_id,
"module_id": self.module_id,
"scol_year": self.scol_year,
"instance_code": self.instance_code,
"title": self.title,
Expand Down
6 changes: 2 additions & 4 deletions app/parsers/module_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

def fill_module_from_intra(intra_json: dict, module: Module,
student_id: int):
for key in ["codemodule", "semester", "scolaryear", "title", "begin", "end", "end_register", "student_credits", "student_grade", "credits", "allow_register", "student_registered", "tb_is_roadblock", "tb_roadblock_submodules", "tb_required_credits", "codeinstance"]:
for key in ["id", "codemodule", "semester", "scolaryear", "title", "begin", "end", "end_register", "student_credits", "student_grade", "credits", "allow_register", "student_registered", "tb_is_roadblock", "tb_roadblock_submodules", "tb_required_credits", "codeinstance"]:
if key not in intra_json:
return False
module.student_id = student_id
module.module_id = intra_json["id"]
module.code_module = intra_json["codemodule"]
module.instance_code = intra_json["codeinstance"]
module.semester_id = intra_json["semester"]
Expand All @@ -22,9 +23,6 @@ def fill_module_from_intra(intra_json: dict, module: Module,
module.student_registered = str(intra_json["student_registered"]) == "1"





module.student_credits = intra_json["student_credits"]
module.student_grade = intra_json["student_grade"]
module.credits = intra_json["credits"]
Expand Down
10 changes: 9 additions & 1 deletion app/services/module_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def get_module_by_code(student_id: str, code_module: str) -> Module | None:
return Module(p)
return None

@staticmethod
def get_module_by_id(student_id: str, module_id: int) -> Module | None:
p = Globals.database["modules"].find_one({"module_id": module_id, "student_id": student_id})
if p:
return Module(p)
return None

@staticmethod
def get_latest_fetchdate(student_id: str) -> str:
p = Globals.database["modules"].find_one({"student_id": student_id},
Expand All @@ -32,7 +39,7 @@ def get_latest_fetchdate(student_id: str) -> str:

@staticmethod
def upload_module(module: Module):
curr = ModuleService.get_module_by_code(module.student_id, module.code_module)
curr = ModuleService.get_module_by_id(module.student_id, module.module_id)
if curr:
module._id = curr._id
module.code_module = curr.code_module
Expand All @@ -55,6 +62,7 @@ def get_modules_json(student_id: str) -> {str: {str: str}}:
modules[mod.code_module] = {
"code": mod.code_module,
"scolaryear": mod.scol_year,
"id": mod.module_id,
"name": mod.title,
}
return modules

0 comments on commit 8aee5e8

Please sign in to comment.