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 43995f7 commit 7ffc918
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import flask
from flask_restx import Namespace, Resource
from coprs.views.misc import api_login_required, payload_multiple_get
from coprs.views.misc import api_login_required, request_multiple_args
from coprs.views.apiv3_ns import apiv3_ns, api, rename_fields_helper
from coprs.views.apiv3_ns.schema.schemas import (
project_chroot_model,
Expand Down Expand Up @@ -82,11 +82,11 @@ def get(self):
Get a project chroot
Get settings for a single project chroot.
"""
ownername, projectname, chrootname = payload_multiple_get(
apiv3_project_chroots_ns.payload, "ownername", "projectname", "chrootname"
ownername, projectname, chrootname = request_multiple_args(
"ownername", "projectname", "chrootname"
)
copr = get_copr(ownername, projectname)
chroot = ComplexLogic.get_copr_chroot_safe(copr, chrootname)
chroot = ComplexLogic.get_copr_chroot(copr, chrootname)
return to_dict(chroot)


Expand All @@ -99,11 +99,11 @@ def get(self):
Get a build config
Generate a build config based on a project chroot settings.
"""
ownername, projectname, chrootname = payload_multiple_get(
apiv3_project_chroots_ns.payload, "ownername", "projectname", "chrootname"
ownername, projectname, chrootname = request_multiple_args(
"ownername", "projectname", "chrootname"
)
copr = get_copr(ownername, projectname)
chroot = ComplexLogic.get_copr_chroot_safe(copr, chrootname)
chroot = ComplexLogic.get_copr_chroot(copr, chrootname)
if not chroot:
raise ObjectNotFound('Chroot not found.')
return to_build_config_dict(chroot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
restx_editable_copr)
from coprs.views.apiv3_ns.json2form import get_form_compatible_data, get_input_dict
from coprs import db, models, forms, db_session_scope
from coprs.views.misc import restx_api_login_required, payload_multiple_get
from coprs.views.misc import restx_api_login_required, request_multiple_args
from coprs.views.apiv3_ns import rename_fields_helper, api
from coprs.views.apiv3_ns.schema.schemas import (
project_model,
Expand Down Expand Up @@ -117,9 +117,7 @@ def get(self):
Get a project
Get details for a single Copr project according to ownername and projectname.
"""
ownername, projectname = payload_multiple_get(
apiv3_projects_ns.payload, "ownername", "projectname"
)
ownername, projectname = request_multiple_args("ownername", "projectname")
copr = get_copr(ownername, projectname)
return to_dict(copr)

Expand Down
12 changes: 8 additions & 4 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/schema/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ def input_model(self):
Returns an input model (input to @ns.expect()) with properly set required
parameters.
"""
for field in self.get_required_attrs:
required_args = self.get_required_attrs
if getattr(self, "__all_required", False):
required_args = fields(self)

for field in required_args:
field.required = True

return api.model(self.__class__.__name__, self.schema())
Expand Down Expand Up @@ -116,9 +120,7 @@ class ProjectChrootGet(InputSchema):
projectname: String
chrootname: String = mock_chroot

@property
def get_required_attrs(self) -> list:
return [self.ownername, self.projectname, self.chrootname]
__all_required: bool = True


@dataclass
Expand Down Expand Up @@ -320,6 +322,8 @@ class ProjectGet(InputSchema):
ownername: String
projectname: String

__all_required: bool = True


# OUTPUT MODELS
project_chroot_model = ProjectChroot.get_cls().model()
Expand Down
25 changes: 25 additions & 0 deletions frontend/coprs_frontend/coprs/views/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ def make_response(content, status=HTTPStatus.OK):
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
Expand All @@ -363,3 +364,27 @@ def payload_multiple_get(payload: dict, *parameters) -> list:
return multiple_get(payload, parameters)
except KeyError as exc:
raise BadRequest(str(exc)) from exc


def request_multiple_args(*query_params, raise_on_missing=True) -> list:
"""
Get multiple values from query parameters.
Args:
raise_on_missing: raises BadRequest if set to True, otherwise ignores
missing query parameters
*query_params: list of args to obtain values from flask.request.args
Returns:
*args values in the same order as they were given.
"""
result = []
flask_args = flask.request.args
empty = "__empty_content"
for arg in query_params:
flask_arg = flask_args.get(arg, empty)
if raise_on_missing and flask_arg == empty:
raise BadRequest(f"Missing arg: {arg}")

result.append(flask_arg)

return result

0 comments on commit 7ffc918

Please sign in to comment.