Skip to content

Commit

Permalink
frontend: delete compatibility code between flask and flask-restx
Browse files Browse the repository at this point in the history
  • Loading branch information
nikromen committed Apr 14, 2024
1 parent 25db60c commit 64bef8c
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 170 deletions.
68 changes: 20 additions & 48 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,39 +95,6 @@ def query_params_wrapper(*args, **kwargs):
return query_params_decorator


def _shared_pagination_wrapper(**kwargs):
form = PaginationForm(flask.request.args)
if not form.validate():
raise CoprHttpException(form.errors)
kwargs.update(form.data)
return kwargs


def pagination():
def pagination_decorator(f):
@wraps(f)
def pagination_wrapper(*args, **kwargs):
kwargs = _shared_pagination_wrapper(**kwargs)
return f(*args, **kwargs)
return pagination_wrapper
return pagination_decorator


def _shared_file_upload_wrapper():
data = json.loads(flask.request.files["json"].read()) or {}
flask.request.form = ImmutableMultiDict(list(data.items()))

def file_upload():
def file_upload_decorator(f):
@wraps(f)
def file_upload_wrapper(*args, **kwargs):
if "json" in flask.request.files:
_shared_file_upload_wrapper()
return f(*args, **kwargs)
return file_upload_wrapper
return file_upload_decorator


class PaginationForm(wtforms.Form):
limit = wtforms.IntegerField("Limit", validators=[wtforms.validators.Optional()])
offset = wtforms.IntegerField("Offset", validators=[wtforms.validators.Optional()])
Expand Down Expand Up @@ -262,14 +229,6 @@ def _check_if_user_can_edit_copr(ownername, projectname):
return copr


def editable_copr(f):
@wraps(f)
def wrapper(ownername, projectname):
copr = _check_if_user_can_edit_copr(ownername, projectname)
return f(copr)
return wrapper


def set_defaults(formdata, form_class):
"""
Take a `formdata` which can be `flask.request.form` or an output from
Expand Down Expand Up @@ -481,42 +440,55 @@ def call_deprecated_endpoint_method(endpoint_method):
return call_deprecated_endpoint_method


def restx_editable_copr(endpoint_method):
def editable_copr(endpoint_method):
"""
Raises an exception if user don't have permissions for editing Copr repo.
Order matters! If flask.g.user is None then this will fail! If used with
@api_login_required it has to be called after it:
@api_login_required
@restx_editable_copr
@editable_copr
...
"""
@wraps(endpoint_method)
def editable_copr_getter(self, ownername, projectname):
copr = _check_if_user_can_edit_copr(ownername, projectname)
copr = get_copr(ownername, projectname)
if not flask.g.user.can_edit(copr):
raise AccessRestricted(
"User '{0}' can not see permissions for project '{1}' " \
"(missing admin rights)".format(
flask.g.user.name,
'/'.join([ownername, projectname])
)
)

return endpoint_method(self, copr)
return editable_copr_getter


def restx_pagination(endpoint_method):
def pagination(endpoint_method):
"""
Validates pagination arguments and converts pagination parameters from query to
kwargs.
"""
@wraps(endpoint_method)
def create_pagination(self, *args, **kwargs):
kwargs = _shared_pagination_wrapper(**kwargs)
form = PaginationForm(flask.request.args)
if not form.validate():
raise CoprHttpException(form.errors)
kwargs.update(form.data)
return endpoint_method(self, *args, **kwargs)
return create_pagination


def restx_file_upload(endpoint_method):
def file_upload(endpoint_method):
"""
Allow uploading a file to a form via endpoint by using this function as an endpoint decorator.
"""
@wraps(endpoint_method)
def inner(self, *args, **kwargs):
if "json" in flask.request.files:
_shared_file_upload_wrapper()
data = json.loads(flask.request.files["json"].read()) or {}
flask.request.form = ImmutableMultiDict(list(data.items()))
return endpoint_method(self, *args, **kwargs)
return inner
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
build_chroot_config_model,
nevra_packages_model,
)
from coprs.views.apiv3_ns import restx_pagination
from coprs.views.apiv3_ns import pagination
from . import Paginator


Expand Down Expand Up @@ -83,7 +83,7 @@ def get(self, build_id, chrootname):
doc={"deprecated": True, "description": "Use query parameters instead"},
)
class BuildChrootList(Resource):
@restx_pagination
@pagination
@query_to_parameters
@apiv3_bchroots_ns.doc(params=build_id_params | pagination_params)
@apiv3_bchroots_ns.marshal_list_with(pagination_build_chroot_model)
Expand Down
36 changes: 18 additions & 18 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from copr_common.enums import StatusEnum
from coprs import db, forms, models
from coprs.exceptions import (BadRequest, AccessRestricted)
from coprs.views.misc import restx_api_login_required
from coprs.views.misc import api_login_required
from coprs.views.apiv3_ns import api, rename_fields_helper, deprecated_route_method_type
from coprs.views.apiv3_ns.schema.schemas import build_model, pagination_build_model, source_chroot_model, \
source_build_config_model, list_build_params, create_build_url_input_model, create_build_upload_input_model, \
Expand All @@ -29,8 +29,8 @@
SubqueryPaginator,
json2form,
query_to_parameters,
restx_pagination,
restx_file_upload,
pagination,
file_upload,
)
from .json2form import get_form_compatible_data

Expand Down Expand Up @@ -141,7 +141,7 @@ def get(self, build_id):

@apiv3_builds_ns.route("/list")
class ListBuild(Resource):
@restx_pagination
@pagination
@query_to_parameters
@apiv3_builds_ns.doc(params=list_build_params)
@apiv3_builds_ns.marshal_with(pagination_build_model)
Expand Down Expand Up @@ -237,7 +237,7 @@ def _common(build_id):
db.session.commit()
return to_dict(build)

@restx_api_login_required
@api_login_required
@apiv3_builds_ns.doc(params=get_build_docs)
@apiv3_builds_ns.marshal_with(build_model)
def put(self, build_id):
Expand All @@ -247,7 +247,7 @@ def put(self, build_id):
"""
return self._common(build_id)

@restx_api_login_required
@api_login_required
@apiv3_builds_ns.doc(params=get_build_docs)
@apiv3_builds_ns.marshal_with(build_model)
@deprecated_route_method_type(apiv3_builds_ns, "POST", "PUT")
Expand All @@ -261,7 +261,7 @@ def post(self, build_id):

@apiv3_builds_ns.route("/create/url")
class CreateFromUrl(Resource):
@restx_api_login_required
@api_login_required
@apiv3_builds_ns.expect(create_build_url_input_model)
@apiv3_builds_ns.marshal_with(pagination_build_model)
def post(self):
Expand All @@ -288,8 +288,8 @@ def create_new_build(options):

@apiv3_builds_ns.route("/create/upload")
class CreateFromUpload(Resource):
@restx_file_upload
@restx_api_login_required
@file_upload
@api_login_required
@apiv3_builds_ns.expect(create_build_upload_input_model)
@apiv3_builds_ns.marshal_with(build_model)
def post(self):
Expand All @@ -315,7 +315,7 @@ def create_new_build(options):

@apiv3_builds_ns.route("/create/scm")
class CreateFromScm(Resource):
@restx_api_login_required
@api_login_required
@apiv3_builds_ns.expect(create_build_scm_input_model)
@apiv3_builds_ns.marshal_with(build_model)
def post(self):
Expand Down Expand Up @@ -346,7 +346,7 @@ def create_new_build(options):

@apiv3_builds_ns.route("/create/distgit")
class CreateFromDistGit(Resource):
@restx_api_login_required
@api_login_required
@apiv3_builds_ns.expect(create_build_distgit_input_model)
@apiv3_builds_ns.marshal_with(build_model)
def post(self):
Expand Down Expand Up @@ -375,7 +375,7 @@ def create_new_build(options):

@apiv3_builds_ns.route("/create/pypi")
class CreateFromPyPi(Resource):
@restx_api_login_required
@api_login_required
@apiv3_builds_ns.expect(create_build_pypi_input_model)
@apiv3_builds_ns.marshal_with(build_model)
def post(self):
Expand Down Expand Up @@ -409,7 +409,7 @@ def create_new_build(options):

@apiv3_builds_ns.route("/create/rubygems")
class CreateFromRubyGems(Resource):
@restx_api_login_required
@api_login_required
@apiv3_builds_ns.expect(create_build_rubygems_input_model)
@apiv3_builds_ns.marshal_with(build_model)
def post(self):
Expand All @@ -435,7 +435,7 @@ def create_new_build(options):

@apiv3_builds_ns.route("/create/custom")
class CreateCustom(Resource):
@restx_api_login_required
@api_login_required
@apiv3_builds_ns.expect(create_build_custom_input_model)
@apiv3_builds_ns.marshal_with(build_model)
def post(self):
Expand Down Expand Up @@ -465,7 +465,7 @@ def create_new_build(options):

@apiv3_builds_ns.route("/delete/<int:build_id>")
class DeleteBuild(Resource):
@restx_api_login_required
@api_login_required
@apiv3_builds_ns.doc(params=get_build_docs)
@apiv3_builds_ns.marshal_with(build_model)
def delete(self, build_id):
Expand All @@ -490,7 +490,7 @@ def _common():
db.session.commit()
return {"builds": build_ids}

@restx_api_login_required
@api_login_required
@apiv3_builds_ns.expect(delete_builds_input_model)
@apiv3_builds_ns.marshal_with(list_build_model)
@deprecated_route_method_type(apiv3_builds_ns, "POST", "DELETE")
Expand All @@ -501,7 +501,7 @@ def post(self):
"""
return self._common()

@restx_api_login_required
@api_login_required
@apiv3_builds_ns.expect(delete_builds_input_model)
@apiv3_builds_ns.marshal_with(list_build_model)
def delete(self):
Expand All @@ -516,7 +516,7 @@ def delete(self):
# this endoint is not meant to be used by the end user
@apiv3_builds_ns.hide
class CheckBeforeBuild(Resource):
@restx_api_login_required
@api_login_required
@apiv3_builds_ns.doc(
responses={200: {"message": "It should be safe to submit a build like this"}}
)
Expand Down
4 changes: 2 additions & 2 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from coprs import app, oid, db
from coprs.views.apiv3_ns import api
from coprs.exceptions import AccessRestricted
from coprs.views.misc import restx_api_login_required
from coprs.views.misc import api_login_required
from coprs.auth import UserAuth


Expand Down Expand Up @@ -64,7 +64,7 @@ def krb_straighten_username(krb_remote_user):

@apiv3_general_ns.route("/auth-check")
class AuthCheck(Resource):
@restx_api_login_required
@api_login_required
def get(self):
"""
Check if the user is authenticated
Expand Down
8 changes: 4 additions & 4 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from wtforms import ValidationError

from coprs import forms, db_session_scope
from coprs.views.apiv3_ns import api, get_copr, restx_file_upload
from coprs.views.apiv3_ns import api, get_copr, file_upload
from coprs.views.apiv3_ns.schema.schemas import (module_build_model, fullname_params,
module_add_input_model)
from coprs.views.misc import restx_api_login_required
from coprs.views.misc import api_login_required
from coprs.exceptions import DuplicateException, BadRequest, InvalidForm
from coprs.logic.modules_logic import ModuleProvider, ModuleBuildFacade

Expand All @@ -30,8 +30,8 @@ def to_dict(module):

@apiv3_module_ns.route("/build/<ownername>/<projectname>")
class Module(Resource):
@restx_api_login_required
@restx_file_upload
@api_login_required
@file_upload
@apiv3_module_ns.doc(params=fullname_params)
@apiv3_module_ns.expect(module_add_input_model)
@apiv3_module_ns.marshal_with(module_build_model)
Expand Down
Loading

0 comments on commit 64bef8c

Please sign in to comment.