Coverage for  / usr / local / lib / python3.14 / site-packages / twinpad_backend / routes / configurator.py: 100%

51 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-11 15:40 +0000

1from fastapi import APIRouter, HTTPException, Response, UploadFile, Depends 

2 

3from twinpad_backend.auth import get_current_active_user 

4from twinpad_backend.responses import ListResponse 

5from twinpad_backend.queries import ConfigurationQuery 

6from twinpad_backend.models import Configuration, is_valid_excel_file 

7 

8router = APIRouter() 

9 

10 

11# region Configurations 

12 

13 

14@router.get("/configs", dependencies=[Depends(get_current_active_user)]) 

15async def get_configs(query: ConfigurationQuery = Depends()) -> ListResponse[Configuration]: 

16 return Configuration.response_from_query(query) 

17 

18 

19@router.get("/configs/names", dependencies=[Depends(get_current_active_user)]) 

20async def get_config_names() -> list[dict[str, str]]: 

21 return Configuration.get_all_names_devices() 

22 

23 

24@router.post("/configs", dependencies=[Depends(get_current_active_user)]) 

25async def create_config( 

26 config_name: str, excel_file: UploadFile, petri_json: UploadFile | str = "", pid_json: UploadFile | str = "" 

27): 

28 if not is_valid_excel_file(excel_file.file): 

29 raise HTTPException(415, "File is not a valid Excel file.") 

30 if petri_json != "" and petri_json.content_type != "application/json": 

31 raise HTTPException(415, "Petri network is not a correct JSON file.") 

32 if pid_json != "" and pid_json.content_type != "application/json": 

33 raise HTTPException(415, "PID is not a correct JSON file.") 

34 

35 pid_file = pid_json.file if pid_json != "" else None 

36 petri_file = petri_json.file if petri_json != "" else None 

37 

38 try: 

39 config = Configuration.get_from_excel(config_name, excel_file.file, pid_file, petri_file) 

40 except Exception as exception: 

41 raise HTTPException(400, f"{type(exception).__name__}: {exception}") from exception 

42 

43 return config 

44 

45 

46@router.get("/configs/{config_id}", response_model=Configuration, dependencies=[Depends(get_current_active_user)]) 

47async def get_config(config_id: str, exclude_sensitive_info: bool = True): 

48 config = Configuration.get_from_config_id(config_id, exclude_sensitive_info) 

49 if config is None: 

50 raise HTTPException(404, "Configuration not found.") 

51 

52 return config 

53 

54 

55@router.patch("/configs/{config_id}", response_model=str, dependencies=[Depends(get_current_active_user)]) 

56async def update_config( 

57 config_id: str, 

58 config_name: str = None, 

59 petri_json: UploadFile | str = "", 

60 pid_json: UploadFile | str = "", 

61 save_as_new: bool = True, 

62): 

63 config = Configuration.get_from_config_id(config_id, exclude_sensitive_info=False) 

64 

65 if config is None: 

66 raise HTTPException(404, "Configuration not found.") 

67 

68 if petri_json != "" and petri_json.content_type != "application/json": 

69 raise HTTPException(415, "Petri network is not a correct JSON file.") 

70 if pid_json != "" and pid_json.content_type != "application/json": 

71 raise HTTPException(415, "PID is not a correct JSON file.") 

72 

73 pid_file = pid_json.file if pid_json != "" else None 

74 petri_file = petri_json.file if petri_json != "" else None 

75 

76 return config.update(config_name, petri_file, pid_file, save_as_new) 

77 

78 

79@router.get("/configs/{config_id}/json", response_model=Configuration, dependencies=[Depends(get_current_active_user)]) 

80async def get_config_json(config_id: str): 

81 configuration = Configuration.get_from_config_id(config_id, False) 

82 if configuration is None: 

83 raise HTTPException(404, "Configuration not found.") 

84 

85 return Response( 

86 content=configuration.to_json(), 

87 media_type="application/json", 

88 headers={"Content-Disposition": f'attachment; filename="{configuration.config_name}.json"'}, 

89 ) 

90 

91 

92# endregion