Coverage for middle_layer/solicit/application_layer/rest_api/views/capability.py: 93.55%

31 statements  

« prev     ^ index     » next       coverage.py v7.10.5, created at 2026-03-09 06:13 +0000

1# Copyright 2024 Associated Universities, Inc. 

2# 

3# This file is part of Telescope Time Allocation Tools (TTAT). 

4# 

5# TTAT is free software: you can redistribute it and/or modify 

6# it under the terms of the GNU General Public License as published by 

7# the Free Software Foundation, either version 3 of the License, or 

8# any later version. 

9# 

10# TTAT is distributed in the hope that it will be useful, 

11# but WITHOUT ANY WARRANTY; without even the implied warranty of 

12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

13# GNU General Public License for more details. 

14# 

15# You should have received a copy of the GNU General Public License 

16# along with TTAT. If not, see <https://www.gnu.org/licenses/>. 

17# 

18from http import HTTPStatus 

19 

20from pyramid.httpexceptions import HTTPBadRequest, HTTPNotFound, HTTPPreconditionFailed 

21from pyramid.request import Request 

22from pyramid.response import Response 

23from pyramid.view import view_config 

24 

25from common.application_layer.rest_api import make_expected_params_message 

26from solicit.domain_layer.entities.capability import Capability 

27 

28 

29@view_config(route_name="capabilities_list", renderer="json", permission="capabilities_list") 

30def capabilities_list(request: Request) -> Response: 

31 """ 

32 List all Capabilities 

33 URL: capabilities 

34 

35 :param request: GET request 

36 :return: Response with JSON-formatted array of Capabilities 

37 """ 

38 capabilities = request.repo.capability_repo.list_all() 

39 response = [] 

40 for capability in capabilities: 

41 response.append(capability.__json__()) 

42 return Response(status_code=HTTPStatus.OK, json_body=response) 

43 

44 

45@view_config(route_name="capability_by_id", renderer="json", permission="capability_by_id") 

46def capability_by_id(request: Request) -> Response: 

47 """ 

48 View a Capability by id 

49 URL: capabilities/{id} 

50 

51 :param request: GET request 

52 :return: Response with JSON-formatted capability 

53 or 400 response (HTTPBadRequest) if id was not an int 

54 or 404 response (HTTPNotFound) if no Capability with ID id was found 

55 """ 

56 capability: Capability = request.lookup(request.matchdict["capability_id"], Capability) 

57 return Response(status_code=HTTPStatus.OK, json_body=capability.__json__()) 

58 

59 

60@view_config(route_name="capability_update", renderer="json", permission="capability_update") 

61def capability_update(request: Request) -> Response: 

62 """ 

63 Update a Capability 

64 URL: capabilities 

65 

66 :param request: PUT request with JSON object like: 

67 { 

68 "capabilityId", <int, 

69 "description": <str>, 

70 "isActive": <bool>, 

71 } 

72 :return: Response with JSON-formatted new or updated Capability 

73 or 400 response (HTTPBadRequest) if expected parameters not given, or capabilityId is not an int 

74 or 404 response (HTTPNotFound) if the Capability cannot be found 

75 or 412 response (HTTPPreconditionFailed) if the Capability can't be updated 

76 """ 

77 expected_params = ["capabilityId", "description", "isActive"] 

78 params = request.json_body 

79 if not all([expected in params for expected in expected_params]): 

80 # JSON params do not contain all expected params 

81 raise HTTPBadRequest(body=make_expected_params_message(expected_params, params.keys())) 

82 else: 

83 capability: Capability = request.lookup(params["capabilityId"], Capability) 

84 capability.update_from_json(params) 

85 try: 

86 request.repo.capability_repo.update(capability) 

87 except ValueError as e: 

88 raise HTTPPreconditionFailed(body=str(e)) 

89 

90 return Response(status_code=HTTPStatus.OK, json_body=capability.__json__())