Skip to content

Commit

Permalink
frontend: unify naming convention for safe methods
Browse files Browse the repository at this point in the history
Originally, safe methods were considered as methods raising
ObjectNotFound for API (mainly with session.one() method). Now
methods which returns None or ignores the error completely
are also considered as "safe".

For the sake of consistency:
- *_noop_safe -> methods that ignores error completely (they basically
  logs)
- *_none_safe -> returns None if object was not found in the database
- *_safe -> raises an exception (ObjectNotFound) if no object found
  • Loading branch information
nikromen committed Nov 13, 2023
1 parent f5e6e17 commit 867d0d5
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 84 deletions.
55 changes: 34 additions & 21 deletions frontend/coprs_frontend/coprs/logic/complex_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_transitive_runtime_dependencies(cls, copr):

for dep in analyzed_copr.runtime_deps:
try:
copr_dep = cls.get_copr_by_repo_safe(dep)
copr_dep = cls.get_copr_by_repo(dep)
except exceptions.ObjectNotFound:
non_existing.add(dep)
continue
Expand Down Expand Up @@ -170,8 +170,8 @@ def fork_copr(cls, copr, user, dstname, dstgroup=None):
return fcopr, created

@staticmethod
def get_group_copr_safe(group_name, copr_name, **kwargs):
group = ComplexLogic.get_group_by_name_safe(group_name)
def get_group_copr(group_name, copr_name, **kwargs):
group = ComplexLogic.get_group_by_name(group_name)
try:
return CoprsLogic.get_by_group_id(
group.id, copr_name, **kwargs).one()
Expand All @@ -181,10 +181,10 @@ def get_group_copr_safe(group_name, copr_name, **kwargs):
.format(group_name, copr_name))

@staticmethod
def get_copr_safe(user_name, copr_name, **kwargs):
def get_copr(user_name, copr_name, **kwargs):
""" Get one project.
This always return personal project. For group projects see get_group_copr_safe().
This always return personal project. For group projects see get_group_copr().
"""
try:
return CoprsLogic.get(user_name, copr_name, **kwargs).filter(Copr.group_id.is_(None)).one()
Expand All @@ -194,13 +194,26 @@ def get_copr_safe(user_name, copr_name, **kwargs):
.format(user_name, copr_name))

@staticmethod
def get_copr_by_owner_safe(owner_name, copr_name, **kwargs):
def get_copr_by_owner(owner_name, copr_name, **kwargs):
if owner_name[0] == "@":
return ComplexLogic.get_group_copr_safe(owner_name[1:], copr_name, **kwargs)
return ComplexLogic.get_copr_safe(owner_name, copr_name, **kwargs)
return ComplexLogic.get_group_copr(owner_name[1:], copr_name, **kwargs)
return ComplexLogic.get_copr(owner_name, copr_name, **kwargs)

@staticmethod
def get_copr_by_repo_safe(repo_url):
def get_copr_by_repo(repo_url):
"""
Safely get copr repo by repo url.
Args:
repo_url: str
Returns:
Copr repo or None in case of invalid url format or different url
scheme than copr.
Raises:
ObjectNotFound if no such Copr (group) repo was found.
"""
copr_repo = helpers.copr_repo_fullname(repo_url)
if not copr_repo:
return None
Expand All @@ -209,18 +222,18 @@ def get_copr_by_repo_safe(repo_url):
except:
# invalid format, e.g. multiple slashes in copr_repo
return None
return ComplexLogic.get_copr_by_owner_safe(owner, copr)
return ComplexLogic.get_copr_by_owner(owner, copr)

@staticmethod
def get_copr_dir_safe(ownername, copr_dirname, **kwargs):
def get_copr_dir(ownername, copr_dirname):
try:
return CoprDirsLogic.get_by_ownername(ownername, copr_dirname).one()
except sqlalchemy.orm.exc.NoResultFound:
raise ObjectNotFound(message="copr dir {}/{} does not exist."
.format(ownername, copr_dirname))

@staticmethod
def get_copr_by_id_safe(copr_id):
def get_copr_by_id(copr_id):
try:
return CoprsLogic.get_by_id(copr_id).one()
except sqlalchemy.orm.exc.NoResultFound:
Expand All @@ -229,7 +242,7 @@ def get_copr_by_id_safe(copr_id):
.format(copr_id))

@staticmethod
def get_build_safe(build_id):
def get_build(build_id):
try:
return BuildsLogic.get_by_id(build_id).one()
except (sqlalchemy.orm.exc.NoResultFound,
Expand All @@ -243,7 +256,7 @@ def get_build_chroot(build_id, chrootname):
Return a `models.BuildChroot` instance based on build ID and name of the chroot.
If there is no such chroot, `ObjectNotFound` execption is raised.
"""
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
try:
return build.chroots_dict_by_name[chrootname]
except KeyError:
Expand All @@ -253,15 +266,15 @@ def get_build_chroot(build_id, chrootname):
raise ObjectNotFound(message=msg)

@staticmethod
def get_package_by_id_safe(package_id):
def get_package_by_id(package_id):
try:
return PackagesLogic.get_by_id(package_id).one()
except sqlalchemy.orm.exc.NoResultFound:
raise ObjectNotFound(
message="Package {} does not exist.".format(package_id))

@staticmethod
def get_package_safe(copr, package_name):
def get_package(copr, package_name):
try:
return PackagesLogic.get(copr.id, package_name).one()
except sqlalchemy.orm.exc.NoResultFound:
Expand All @@ -270,7 +283,7 @@ def get_package_safe(copr, package_name):
.format(package_name, copr))

@staticmethod
def get_group_by_name_safe(group_name):
def get_group_by_name(group_name):
try:
group = UsersLogic.get_group_by_alias(group_name).one()
except sqlalchemy.orm.exc.NoResultFound:
Expand All @@ -279,9 +292,9 @@ def get_group_by_name_safe(group_name):
return group

@staticmethod
def get_copr_chroot_safe(copr, chroot_name):
def get_copr_chroot(copr, chroot_name):
try:
chroot = CoprChrootsLogic.get_by_name_safe(copr, chroot_name)
chroot = CoprChrootsLogic.get_by_name_or_none(copr, chroot_name)
except (ValueError, KeyError, RuntimeError) as e:
raise ObjectNotFound(message=str(e))

Expand Down Expand Up @@ -552,14 +565,14 @@ def get_additional_repo_views(cls, repos_list, chroot_id):
"name": "Additional repo " + helpers.generate_repo_name(repo),
}

# We ask get_copr_by_repo_safe() here only to resolve the
# We ask get_copr_by_repo() here only to resolve the
# module_hotfixes attribute. If the asked project doesn't exist, we
# still adjust the 'repos' variable -- the build will eventually
# fail on repo downloading, but at least the copr maintainer will be
# notified about the misconfiguration. Better than just skip the
# repo.
try:
copr = ComplexLogic.get_copr_by_repo_safe(repo)
copr = ComplexLogic.get_copr_by_repo(repo)
except ObjectNotFound:
copr = None
if copr and copr.module_hotfixes:
Expand Down
12 changes: 6 additions & 6 deletions frontend/coprs_frontend/coprs/logic/coprs_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def get_by_ownername(cls, ownername):
return cls.filter_by_ownername(query, ownername)

@classmethod
def get_by_ownername_coprname(cls, ownername, coprname):
def get_by_ownername_coprname_safe(cls, ownername, coprname):
"""
Return a Copr object owned by OWNERNAME (either '@groupname' or
'username') with a name COPRNAME.
Expand All @@ -206,7 +206,7 @@ def get_by_ownername_and_dirname(cls, ownername, dirname):
# Some of the calling methods (routes) strictly require us not to check
# if the dirname exists.
coprname = CoprDirsLogic.copr_name_from_dirname(dirname)
return cls.get_by_ownername_coprname(ownername, coprname)
return cls.get_by_ownername_coprname_safe(ownername, coprname)

@classmethod
def filter_without_ids(cls, query, ids):
Expand Down Expand Up @@ -622,7 +622,7 @@ def request_permission(cls, copr, user, permission, req_bool):

class CoprDirsLogic(object):
@classmethod
def get_by_copr_safe(cls, copr, dirname):
def get_by_copr_or_none(cls, copr, dirname):
"""
Return _query_ for getting CoprDir by Copr and dirname
"""
Expand All @@ -637,7 +637,7 @@ def get_by_copr(cls, copr, dirname):
Return CoprDir instance per given Copr instance and dirname. Raise
ObjectNotFound if it doesn't exist.
"""
coprdir = cls.get_by_copr_safe(copr, dirname)
coprdir = cls.get_by_copr_or_none(copr, dirname)
if not coprdir:
raise exceptions.ObjectNotFound(
"Dirname '{}' doesn't exist in '{}' copr".format(
Expand Down Expand Up @@ -669,7 +669,7 @@ def get_or_create(cls, copr, dirname, trusted_caller=False):
submitted. We don't create the "main" CoprDirs here (those are created
when a new project is created.
"""
copr_dir = cls.get_by_copr_safe(copr, dirname)
copr_dir = cls.get_by_copr_or_none(copr, dirname)
if copr_dir:
return copr_dir

Expand Down Expand Up @@ -948,7 +948,7 @@ def get_by_name(cls, copr, chroot_name, active_only=True):
return cls.get_by_mock_chroot_id(copr, mc.id)

@classmethod
def get_by_name_safe(cls, copr, chroot_name):
def get_by_name_or_none(cls, copr, chroot_name):
"""
:rtype: models.CoprChroot
"""
Expand Down
2 changes: 1 addition & 1 deletion frontend/coprs_frontend/coprs/views/apiv3_ns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_copr(ownername=None, projectname=None):
request = flask.request
ownername = ownername or request.form.get("ownername") or request.json["ownername"]
projectname = projectname or request.form.get("projectname") or request.json["projectname"]
return ComplexLogic.get_copr_by_owner_safe(ownername, projectname)
return ComplexLogic.get_copr_by_owner(ownername, projectname)


class Paginator(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def to_dict(build_chroot):

def build_config(build_chroot):
config = BuildConfigLogic.generate_build_config(build_chroot.build.copr, build_chroot.name)
copr_chroot = CoprChrootsLogic.get_by_name_safe(build_chroot.build.copr, build_chroot.name)
copr_chroot = CoprChrootsLogic.get_by_name_or_none(build_chroot.build.copr, build_chroot.name)
dict_data = {
"repos": config.get("repos"),
"additional_repos": BuildConfigLogic.generate_additional_repos(copr_chroot),
Expand Down
12 changes: 6 additions & 6 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get(self, build_id):
Get a build
Get details for a single Copr build.
"""
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
result = to_dict(build)

# Workaround - `marshal_with` needs the input `build_id` to be present
Expand Down Expand Up @@ -154,13 +154,13 @@ def get_build_list(ownername, projectname, packagename=None, status=None, **kwar

@apiv3_ns.route("/build/source-chroot/<int:build_id>/", methods=GET)
def get_source_chroot(build_id):
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
return flask.jsonify(to_source_chroot(build))


@apiv3_ns.route("/build/source-build-config/<int:build_id>/", methods=GET)
def get_source_build_config(build_id):
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
return flask.jsonify(to_source_build_config(build))


Expand All @@ -169,14 +169,14 @@ def get_build_built_packages(build_id):
"""
Return built packages (NEVRA dicts) for a given build
"""
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
return flask.jsonify(build.results_dict)


@apiv3_ns.route("/build/cancel/<int:build_id>", methods=PUT)
@api_login_required
def cancel_build(build_id):
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
BuildsLogic.cancel_build(flask.g.user, build)
db.session.commit()
return render_build(build)
Expand Down Expand Up @@ -396,7 +396,7 @@ def process_creating_new_build(copr, form, create_new_build):
@apiv3_ns.route("/build/delete/<int:build_id>", methods=DELETE)
@api_login_required
def delete_build(build_id):
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
build_dict = to_dict(build)
BuildsLogic.delete_build(flask.g.user, build)
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get(self):
"""
args = self.parser.parse_args()
copr = get_copr(args.ownername, args.projectname)
chroot = ComplexLogic.get_copr_chroot_safe(copr, args.chrootname)
chroot = ComplexLogic.get_copr_chroot(copr, args.chrootname)
return to_dict(chroot)


Expand All @@ -103,7 +103,7 @@ def get(self):
"""
args = self.parser.parse_args()
copr = get_copr(args.ownername, args.projectname)
chroot = ComplexLogic.get_copr_chroot_safe(copr, args.chrootname)
chroot = ComplexLogic.get_copr_chroot(copr, args.chrootname)
if not chroot:
raise ObjectNotFound('Chroot not found.')
return to_build_config_dict(chroot)
Expand All @@ -116,7 +116,7 @@ def edit_project_chroot(ownername, projectname, chrootname):
copr = get_copr(ownername, projectname)
data = rename_fields(get_form_compatible_data(preserve=["additional_modules"]))
form = forms.ModifyChrootForm(data, meta={'csrf': False})
chroot = ComplexLogic.get_copr_chroot_safe(copr, chrootname)
chroot = ComplexLogic.get_copr_chroot(copr, chrootname)

if not form.validate_on_submit():
raise InvalidForm(form)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def owner2tuple(ownername):
user = flask.g.user
group = None
if ownername[0] == "@":
group = ComplexLogic.get_group_by_name_safe(ownername[1:])
group = ComplexLogic.get_group_by_name(ownername[1:])
elif ownername != flask.g.user.name:
user = UsersLogic.get(ownername).first()
if not user:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from coprs.logic import actions_logic
from coprs.logic.builds_logic import BuildsLogic
from coprs.logic.complex_logic import ComplexLogic, BuildConfigLogic
from coprs.logic.coprs_logic import MockChrootsLogic
from coprs.logic.coprs_logic import CoprChrootsLogic, MockChrootsLogic
from coprs.exceptions import CoprHttpException, ObjectNotFound
from coprs.helpers import streamed_json

Expand Down Expand Up @@ -81,7 +81,7 @@ def dist_git_upload_completed():
build_id = flask.request.json.get("build_id")

try:
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
except ObjectNotFound:
return flask.jsonify({"updated": False})

Expand Down Expand Up @@ -168,7 +168,7 @@ def get_build_record(task, for_backend=False):
"repo_priority": task.build.copr.repo_priority
})

copr_chroot = ComplexLogic.get_copr_chroot_safe(task.build.copr, task.mock_chroot.name)
copr_chroot = CoprChrootsLogic.get_by_name_or_none(task.build.copr, task.mock_chroot.name)
modules = copr_chroot.module_setup_commands
if modules:
build_record["modules"] = {'toggle': modules}
Expand Down Expand Up @@ -419,7 +419,7 @@ def starting_build():
data = flask.request.json

try:
build = ComplexLogic.get_build_safe(data.get('build_id'))
build = ComplexLogic.get_build(data.get('build_id'))
except ObjectNotFound:
return flask.jsonify({"can_start": False})

Expand All @@ -440,7 +440,7 @@ def reschedule_build_chroot():
chroot = flask.request.json.get("chroot")

try:
build = ComplexLogic.get_build_safe(build_id)
build = ComplexLogic.get_build(build_id)
except ObjectNotFound:
response["result"] = "noop"
response["msg"] = "Build {} wasn't found".format(build_id)
Expand Down
Loading

0 comments on commit 867d0d5

Please sign in to comment.