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
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-11 15:40 +0000
1from fastapi import APIRouter, HTTPException, Response, UploadFile, Depends
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
8router = APIRouter()
11# region Configurations
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)
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()
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.")
35 pid_file = pid_json.file if pid_json != "" else None
36 petri_file = petri_json.file if petri_json != "" else None
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
43 return config
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.")
52 return config
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)
65 if config is None:
66 raise HTTPException(404, "Configuration not found.")
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.")
73 pid_file = pid_json.file if pid_json != "" else None
74 petri_file = petri_json.file if petri_json != "" else None
76 return config.update(config_name, petri_file, pid_file, save_as_new)
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.")
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 )
92# endregion