core: run ruff format

This commit is contained in:
RhodeCode Admin 2025-01-13 17:47:37 +01:00
parent 7130effb58
commit 166db21812
821 changed files with 82905 additions and 84121 deletions

View file

@ -26,9 +26,7 @@ from rhodecode import events
from rhodecode.apps._base import RepoAppView
from rhodecode.lib import helpers as h
from rhodecode.lib import audit_logger
from rhodecode.lib.auth import (
LoginRequired, HasRepoPermissionAnyDecorator, CSRFRequired,
HasRepoPermissionAny)
from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator, CSRFRequired, HasRepoPermissionAny
from rhodecode.lib.exceptions import AttachedForksError, AttachedPullRequestsError, AttachedArtifactsError
from rhodecode.lib.utils2 import safe_int
from rhodecode.lib.vcs import RepositoryError
@ -41,7 +39,6 @@ log = logging.getLogger(__name__)
class RepoSettingsAdvancedView(RepoAppView):
def load_default_context(self):
c = self._get_local_tmpl_context()
return c
@ -54,35 +51,38 @@ class RepoSettingsAdvancedView(RepoAppView):
return user_permissions
@LoginRequired()
@HasRepoPermissionAnyDecorator('repository.admin')
@HasRepoPermissionAnyDecorator("repository.admin")
def edit_advanced(self):
_ = self.request.translate
c = self.load_default_context()
c.active = 'advanced'
c.active = "advanced"
c.default_user_id = User.get_default_user_id()
c.in_public_journal = UserFollowing.query() \
.filter(UserFollowing.user_id == c.default_user_id) \
.filter(UserFollowing.follows_repository == self.db_repo).scalar()
c.in_public_journal = (
UserFollowing.query()
.filter(UserFollowing.user_id == c.default_user_id)
.filter(UserFollowing.follows_repository == self.db_repo)
.scalar()
)
c.ver_info_dict = self.rhodecode_vcs_repo.get_hooks_info()
c.hooks_outdated = False
try:
if Version(c.ver_info_dict['pre_version']) < Version(c.rhodecode_version):
if Version(c.ver_info_dict["pre_version"]) < Version(c.rhodecode_version):
c.hooks_outdated = True
except Exception:
pass
# update commit cache if GET flag is present
if self.request.GET.get('update_commit_cache'):
if self.request.GET.get("update_commit_cache"):
self.db_repo.update_commit_cache()
h.flash(_('updated commit cache'), category='success')
h.flash(_("updated commit cache"), category="success")
return self._get_template_context(c)
@LoginRequired()
@HasRepoPermissionAnyDecorator('repository.admin')
@HasRepoPermissionAnyDecorator("repository.admin")
@CSRFRequired()
def edit_advanced_archive(self):
"""
@ -98,31 +98,26 @@ class RepoSettingsAdvancedView(RepoAppView):
repo = audit_logger.RepoWrap(repo_id=None, repo_name=self.db_repo.repo_name)
audit_logger.store_web(
'repo.archive', action_data={'old_data': old_data},
user=self._rhodecode_user, repo=repo)
"repo.archive", action_data={"old_data": old_data}, user=self._rhodecode_user, repo=repo
)
ScmModel().mark_for_invalidation(self.db_repo_name, delete=True)
h.flash(
_('Archived repository `%s`') % self.db_repo_name,
category='success')
h.flash(_("Archived repository `%s`") % self.db_repo_name, category="success")
Session().commit()
except Exception:
log.exception("Exception during archiving of repository")
h.flash(_('An error occurred during archiving of `%s`')
% self.db_repo_name, category='error')
h.flash(_("An error occurred during archiving of `%s`") % self.db_repo_name, category="error")
# redirect to advanced for more deletion options
raise HTTPFound(
h.route_path('edit_repo_advanced', repo_name=self.db_repo_name,
_anchor='advanced-archive'))
raise HTTPFound(h.route_path("edit_repo_advanced", repo_name=self.db_repo_name, _anchor="advanced-archive"))
# flush permissions for all users defined in permissions
affected_user_ids = self._get_users_with_permissions().keys()
PermissionModel().trigger_permission_flush(affected_user_ids)
raise HTTPFound(h.route_path('home'))
raise HTTPFound(h.route_path("home"))
@LoginRequired()
@HasRepoPermissionAnyDecorator('repository.admin')
@HasRepoPermissionAnyDecorator("repository.admin")
@CSRFRequired()
def edit_advanced_delete(self):
"""
@ -130,80 +125,79 @@ class RepoSettingsAdvancedView(RepoAppView):
because of attached forks or other errors.
"""
_ = self.request.translate
handle_forks = self.request.POST.get('forks', None)
if handle_forks == 'detach_forks':
handle_forks = 'detach'
elif handle_forks == 'delete_forks':
handle_forks = 'delete'
handle_forks = self.request.POST.get("forks", None)
if handle_forks == "detach_forks":
handle_forks = "detach"
elif handle_forks == "delete_forks":
handle_forks = "delete"
repo_advanced_url = h.route_path(
'edit_repo_advanced', repo_name=self.db_repo_name,
_anchor='advanced-delete')
repo_advanced_url = h.route_path("edit_repo_advanced", repo_name=self.db_repo_name, _anchor="advanced-delete")
try:
old_data = self.db_repo.get_api_data()
RepoModel().delete(self.db_repo, forks=handle_forks)
_forks = self.db_repo.forks.count()
if _forks and handle_forks:
if handle_forks == 'detach_forks':
h.flash(_('Detached %s forks') % _forks, category='success')
elif handle_forks == 'delete_forks':
h.flash(_('Deleted %s forks') % _forks, category='success')
if handle_forks == "detach_forks":
h.flash(_("Detached %s forks") % _forks, category="success")
elif handle_forks == "delete_forks":
h.flash(_("Deleted %s forks") % _forks, category="success")
repo = audit_logger.RepoWrap(repo_id=None, repo_name=self.db_repo.repo_name)
audit_logger.store_web(
'repo.delete', action_data={'old_data': old_data},
user=self._rhodecode_user, repo=repo)
"repo.delete", action_data={"old_data": old_data}, user=self._rhodecode_user, repo=repo
)
ScmModel().mark_for_invalidation(self.db_repo_name, delete=True)
h.flash(
_('Deleted repository `%s`') % self.db_repo_name,
category='success')
h.flash(_("Deleted repository `%s`") % self.db_repo_name, category="success")
Session().commit()
except AttachedForksError:
delete_anchor = h.link_to(_('detach or delete'), repo_advanced_url)
h.flash(_('Cannot delete `{repo}` it still contains attached forks. '
'Try using {delete_or_detach} option.')
.format(repo=self.db_repo_name, delete_or_detach=delete_anchor),
category='warning')
delete_anchor = h.link_to(_("detach or delete"), repo_advanced_url)
h.flash(
_(
"Cannot delete `{repo}` it still contains attached forks. Try using {delete_or_detach} option."
).format(repo=self.db_repo_name, delete_or_detach=delete_anchor),
category="warning",
)
# redirect to advanced for forks handle action ?
raise HTTPFound(repo_advanced_url)
except AttachedPullRequestsError:
attached_prs = len(self.db_repo.pull_requests_source +
self.db_repo.pull_requests_target)
attached_prs = len(self.db_repo.pull_requests_source + self.db_repo.pull_requests_target)
h.flash(
_('Cannot delete `{repo}` it still contains {num} attached pull requests. '
'Consider archiving the repository instead.').format(
repo=self.db_repo_name, num=attached_prs), category='warning')
_(
"Cannot delete `{repo}` it still contains {num} attached pull requests. "
"Consider archiving the repository instead."
).format(repo=self.db_repo_name, num=attached_prs),
category="warning",
)
# redirect to advanced for forks handle action ?
raise HTTPFound(repo_advanced_url)
except AttachedArtifactsError:
attached_artifacts = len(self.db_repo.artifacts)
h.flash(
_('Cannot delete `{repo}` it still contains {num} attached artifacts. '
'Consider archiving the repository instead.').format(
repo=self.db_repo_name, num=attached_artifacts), category='warning')
_(
"Cannot delete `{repo}` it still contains {num} attached artifacts. "
"Consider archiving the repository instead."
).format(repo=self.db_repo_name, num=attached_artifacts),
category="warning",
)
# redirect to advanced for forks handle action ?
raise HTTPFound(repo_advanced_url)
except Exception:
log.exception("Exception during deletion of repository")
h.flash(_('An error occurred during deletion of `%s`')
% self.db_repo_name, category='error')
h.flash(_("An error occurred during deletion of `%s`") % self.db_repo_name, category="error")
# redirect to advanced for more deletion options
raise HTTPFound(
h.route_path('edit_repo_advanced', repo_name=self.db_repo_name,
_anchor='advanced-delete'))
raise HTTPFound(h.route_path("edit_repo_advanced", repo_name=self.db_repo_name, _anchor="advanced-delete"))
raise HTTPFound(h.route_path('home'))
raise HTTPFound(h.route_path("home"))
@LoginRequired()
@HasRepoPermissionAnyDecorator('repository.admin')
@HasRepoPermissionAnyDecorator("repository.admin")
@CSRFRequired()
def edit_advanced_journal(self):
"""
@ -215,19 +209,15 @@ class RepoSettingsAdvancedView(RepoAppView):
try:
user_id = User.get_default_user_id()
ScmModel().toggle_following_repo(self.db_repo.repo_id, user_id)
h.flash(_('Updated repository visibility in public journal'),
category='success')
h.flash(_("Updated repository visibility in public journal"), category="success")
Session().commit()
except Exception:
h.flash(_('An error occurred during setting this '
'repository in public journal'),
category='error')
h.flash(_("An error occurred during setting this repository in public journal"), category="error")
raise HTTPFound(
h.route_path('edit_repo_advanced', repo_name=self.db_repo_name))
raise HTTPFound(h.route_path("edit_repo_advanced", repo_name=self.db_repo_name))
@LoginRequired()
@HasRepoPermissionAnyDecorator('repository.admin')
@HasRepoPermissionAnyDecorator("repository.admin")
@CSRFRequired()
def edit_advanced_fork(self):
"""
@ -235,14 +225,13 @@ class RepoSettingsAdvancedView(RepoAppView):
"""
_ = self.request.translate
new_fork_id = safe_int(self.request.POST.get('id_fork_of'))
new_fork_id = safe_int(self.request.POST.get("id_fork_of"))
# valid repo, re-check permissions
if new_fork_id:
repo = Repository.get(new_fork_id)
# ensure we have at least read access to the repo we mark
perm_check = HasRepoPermissionAny(
'repository.read', 'repository.write', 'repository.admin')
perm_check = HasRepoPermissionAny("repository.read", "repository.write", "repository.admin")
if repo and perm_check(repo_name=repo.repo_name):
new_fork_id = repo.repo_id
@ -250,52 +239,45 @@ class RepoSettingsAdvancedView(RepoAppView):
new_fork_id = None
try:
repo = ScmModel().mark_as_fork(
self.db_repo_name, new_fork_id, self._rhodecode_user.user_id)
fork = repo.fork.repo_name if repo.fork else _('Nothing')
repo = ScmModel().mark_as_fork(self.db_repo_name, new_fork_id, self._rhodecode_user.user_id)
fork = repo.fork.repo_name if repo.fork else _("Nothing")
Session().commit()
h.flash(
_('Marked repo %s as fork of %s') % (self.db_repo_name, fork),
category='success')
h.flash(_("Marked repo %s as fork of %s") % (self.db_repo_name, fork), category="success")
except RepositoryError as e:
log.exception("Repository Error occurred")
h.flash(str(e), category='error')
h.flash(str(e), category="error")
except Exception:
log.exception("Exception while editing fork")
h.flash(_('An error occurred during this operation'),
category='error')
h.flash(_("An error occurred during this operation"), category="error")
raise HTTPFound(
h.route_path('edit_repo_advanced', repo_name=self.db_repo_name))
raise HTTPFound(h.route_path("edit_repo_advanced", repo_name=self.db_repo_name))
@LoginRequired()
@HasRepoPermissionAnyDecorator('repository.admin')
@HasRepoPermissionAnyDecorator("repository.admin")
@CSRFRequired()
def edit_advanced_toggle_locking(self):
"""
Toggle locking of repository
"""
_ = self.request.translate
set_lock = self.request.POST.get('set_lock')
set_unlock = self.request.POST.get('set_unlock')
set_lock = self.request.POST.get("set_lock")
set_unlock = self.request.POST.get("set_unlock")
try:
if set_lock:
Repository.lock(self.db_repo, self._rhodecode_user.user_id,
lock_reason=Repository.LOCK_WEB)
h.flash(_('Locked repository'), category='success')
Repository.lock(self.db_repo, self._rhodecode_user.user_id, lock_reason=Repository.LOCK_WEB)
h.flash(_("Locked repository"), category="success")
elif set_unlock:
Repository.unlock(self.db_repo)
h.flash(_('Unlocked repository'), category='success')
h.flash(_("Unlocked repository"), category="success")
except Exception as e:
log.exception("Exception during unlocking")
h.flash(_('An error occurred during unlocking'), category='error')
h.flash(_("An error occurred during unlocking"), category="error")
raise HTTPFound(
h.route_path('edit_repo_advanced', repo_name=self.db_repo_name))
raise HTTPFound(h.route_path("edit_repo_advanced", repo_name=self.db_repo_name))
@LoginRequired()
@HasRepoPermissionAnyDecorator('repository.admin')
@HasRepoPermissionAnyDecorator("repository.admin")
def edit_advanced_install_hooks(self):
"""
Install Hooks for repository
@ -303,7 +285,5 @@ class RepoSettingsAdvancedView(RepoAppView):
_ = self.request.translate
self.load_default_context()
self.rhodecode_vcs_repo.install_hooks(force=True)
h.flash(_('installed updated hooks into this repository'),
category='success')
raise HTTPFound(
h.route_path('edit_repo_advanced', repo_name=self.db_repo_name))
h.flash(_("installed updated hooks into this repository"), category="success")
raise HTTPFound(h.route_path("edit_repo_advanced", repo_name=self.db_repo_name))