diff --git a/frontend/coprs_frontend/coprs/helpers.py b/frontend/coprs_frontend/coprs/helpers.py index 8ed0e1a98..92002f9df 100644 --- a/frontend/coprs_frontend/coprs/helpers.py +++ b/frontend/coprs_frontend/coprs/helpers.py @@ -454,7 +454,7 @@ def to_dict(self, options=None): for related, values in options.items(): if hasattr(self, related): - result[related] = getattr(self, related).to_dict(values) + result[related] = getattr(self, related).as_dict(values) return result @property @@ -933,3 +933,25 @@ def generate_repo_id_and_name_ext(dependent, url, dep_idx): generate_repo_name(url), ) return repo_id, name + + +def multiple_get(dictionary: dict, *keys) -> list: + """ + Get multiple values from dictionary. + + Args: + dictionary: Any dictionary + *keys: list of keys to obtain from dictionary + + Returns: + *keys values in the same order as keys were given. + """ + empty = "__empty_content" + result = [] + for key in keys: + content = dictionary.get(key, empty) + if content == empty: + raise KeyError(f"Key missing: {key}") + + result.append(content) + return result diff --git a/frontend/coprs_frontend/coprs/views/misc.py b/frontend/coprs_frontend/coprs/views/misc.py index b5c4f2746..1264fea7e 100644 --- a/frontend/coprs_frontend/coprs/views/misc.py +++ b/frontend/coprs_frontend/coprs/views/misc.py @@ -20,6 +20,7 @@ from coprs.measure import checkpoint_start from coprs.auth import FedoraAccounts, UserAuth, OpenIDConnect from coprs.oidc import oidc_enabled +from coprs.helpers import multiple_get from coprs import oidc @app.before_request @@ -349,12 +350,18 @@ def make_response(content, status=HTTPStatus.OK): return response -def get_from_payload(payload: dict, *args): - empty = "empty_content" - result = [] - for arg in args: - content = payload.get(arg, empty) - if content == empty: - raise BadRequest(f"Missing argument: {arg}") +def payload_multiple_get(payload: dict, *parameters) -> list: + """ + Get multiple values from dictionary. + + Args: + payload: Any dictionary obtain from API request + *parameters: list of parameters to obtain values from request - result.append(content) + Returns: + *parameters values in the same order as they were given. + """ + try: + return multiple_get(payload, parameters) + except KeyError as exc: + raise BadRequest(str(exc)) from exc