Coverage for yaptide/routes/estimator_routes.py: 91%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-04 00:31 +0000

1from flask import request 

2from flask_restful import Resource 

3from marshmallow import Schema, fields 

4 

5from yaptide.persistence.db_methods import (fetch_estimators_by_sim_id, fetch_pages_metadata_by_est_id, 

6 fetch_simulation_id_by_job_id) 

7from yaptide.persistence.models import (UserModel) 

8from yaptide.routes.utils.decorators import requires_auth 

9from yaptide.routes.utils.response_templates import yaptide_response 

10from yaptide.routes.utils.utils import check_if_job_is_owned_and_exist 

11 

12 

13class EstimatorResource(Resource): 

14 """Class responsible for retreving estimator names""" 

15 

16 class APIParametersSchema(Schema): 

17 """Class specifies API parameters""" 

18 

19 job_id = fields.String() 

20 

21 @staticmethod 

22 @requires_auth() 

23 def get(user: UserModel): 

24 """Method returning estimators metadata for specific simulation""" 

25 schema = EstimatorResource.APIParametersSchema() 

26 errors: dict[str, list[str]] = schema.validate(request.args) 

27 if errors: 

28 return yaptide_response(message="Wrong parameters", code=400, content=errors) 

29 param_dict: dict = schema.load(request.args) 

30 

31 job_id = param_dict['job_id'] 

32 

33 is_owned, error_message, res_code = check_if_job_is_owned_and_exist(job_id=job_id, user=user) 

34 if not is_owned: 

35 return yaptide_response(message=error_message, code=res_code) 

36 

37 simulation_id = fetch_simulation_id_by_job_id(job_id=job_id) 

38 if not simulation_id: 

39 return yaptide_response(message="Simulation does not exist", code=404) 

40 

41 estimators = fetch_estimators_by_sim_id(sim_id=simulation_id) 

42 results = [] 

43 

44 for estimator in estimators: 

45 pages_metadata = fetch_pages_metadata_by_est_id(est_id=estimator.id) 

46 estimator_dict = { 

47 "name": 

48 estimator.name, 

49 "pages_metadata": [{ 

50 "page_number": page[0], 

51 "page_name": page[1], 

52 "page_dimension": page[2] 

53 } for page in pages_metadata] 

54 } 

55 results.append(estimator_dict) 

56 

57 if len(results) == 0: 

58 return yaptide_response(message="Pages metadata not found", code=404) 

59 

60 return yaptide_response(message="Estimators metadata", code=200, content={"estimators_metadata": results})