Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nikromen committed Nov 21, 2023
1 parent edb3118 commit 0017b53
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
24 changes: 23 additions & 1 deletion frontend/coprs_frontend/coprs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
23 changes: 15 additions & 8 deletions frontend/coprs_frontend/coprs/views/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 0017b53

Please sign in to comment.