Coverage for middle_layer/solicit/application_layer/rest_api/views/science_category.py: 100.00%
41 statements
« prev ^ index » next coverage.py v7.10.5, created at 2026-03-09 06:13 +0000
« 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
20from pyramid.httpexceptions import HTTPBadRequest, HTTPPreconditionFailed
21from pyramid.request import Request
22from pyramid.response import Response
23from pyramid.view import view_config
25from common.application_layer.rest_api import make_expected_params_message
26from solicit.domain_layer.entities.solicitation import ScienceCategory
29@view_config(route_name="science_categories_list", renderer="json", permission="science_categories_list")
30def science_categories_list(request: Request) -> Response:
31 """
32 Get a list of all ScienceCategory objects
33 URL: science_categories
35 :param request: GET request
36 :return: Response with JSON-formatted array of ScienceCategory objects
37 """
38 science_categories = request.repo.science_category_repo.list_all()
39 response = []
40 for sc in science_categories:
41 response.append(sc.__json__())
42 return Response(status_code=HTTPStatus.OK, json_body=response)
45@view_config(route_name="science_category_by_id", renderer="json", permission="science_category_by_id")
46def science_category_by_id(request: Request) -> Response:
47 """
48 Get a ScienceCategory by id
49 URL: science_categories/{id}
51 :param request: GET request
52 :return: Response with JSON-formatted ScienceCategory
53 or 400 response (HTTPBadRequest) if the id was not an int
54 or 404 response (HTTPNotFound) if the ScienceCategory was not found
55 """
56 science_category = request.lookup(request.matchdict["id"], ScienceCategory)
57 return Response(status_code=HTTPStatus.OK, json_body=science_category.__json__())
60@view_config(route_name="science_category_upsert", renderer="json", permission="science_category_update")
61def science_category_upsert(request: Request) -> Response:
62 """
63 Create or Update a ScienceCategory
64 URL: science_categories
66 :param request: PUT request with JSON object like:
67 {
68 ["scienceCategoryId", <int>],
69 "name": str,
70 "shortName": str
71 }
72 :return: Response with JSON-formatted updated ScienceCategory
73 or 400 response (HTTPBadRequest) if expected parameters not given or id is not an int
74 or 404 response (HTTPNotFound) if the ScienceCategory cannot be found
75 or 412 response (HTTPPreconditionFailed) if ScienceCategory with given name already exists
76 """
77 expected_params = ["scienceCategoryName", "shortName"]
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 if "scienceCategoryId" in params.keys() and params["scienceCategoryId"] is not None:
84 # this is an update
85 science_category = request.lookup(params["scienceCategoryId"], ScienceCategory)
86 science_category.update_from_json(params)
87 request.repo.science_category_repo.update(science_category)
88 return Response(status_code=HTTPStatus.OK, json_body=science_category.__json__())
89 else:
90 new_science_category = ScienceCategory(params["scienceCategoryName"], params["shortName"])
91 new_science_category.is_active = params["isActive"] if "isActive" in params else True
92 try:
93 new_science_category.science_category_id = request.repo.science_category_repo.add(new_science_category)
94 return Response(status_code=HTTPStatus.CREATED, json_body=new_science_category.__json__())
95 except ValueError as e:
96 raise HTTPPreconditionFailed(body=str(e))
99@view_config(route_name="science_category_delete", renderer="json", permission="science_category_delete")
100def science_category_delete(request: Request) -> Response:
101 """
102 Delete a ScienceCategory
103 URL: science_categories/{id}
105 :param request: DELETE request
106 :return: Response with JSON-formatted deleted ScienceCategory
107 or 400 response (HTTPBadRequest) if the id is not an int
108 or 404 response (HTTPNotFound) if the science_category cannot be found
109 """
110 science_category = request.lookup(request.matchdict["id"], ScienceCategory)
111 request.repo.science_category_repo.delete(science_category)
112 return Response(status_code=HTTPStatus.CREATED, json_body=science_category.__json__())