changeset 8718:f8b092f9e6a3

lib: consistently use webutils.url instead of h.url Templates are still the valid use case for accessing through h. Simplify mocking of url() function.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 29 Oct 2020 14:22:49 +0100
parents b551f9c441dc
children 216ed3859869
files kallithea/controllers/admin/repos.py kallithea/controllers/admin/settings.py kallithea/controllers/changelog.py kallithea/controllers/changeset.py kallithea/controllers/compare.py kallithea/controllers/files.py kallithea/controllers/forks.py kallithea/model/db.py kallithea/model/user.py kallithea/tests/fixture.py
diffstat 10 files changed, 26 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/admin/repos.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/controllers/admin/repos.py	Thu Oct 29 14:22:49 2020 +0100
@@ -38,6 +38,7 @@
 
 import kallithea
 from kallithea.lib import helpers as h
+from kallithea.lib import webutils
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired, NotAnonymous
 from kallithea.lib.base import BaseRepoController, jsonify, render
 from kallithea.lib.exceptions import AttachedForksError
@@ -127,7 +128,7 @@
             h.flash(msg, category='error')
             raise HTTPFound(location=url('home'))
 
-        raise HTTPFound(location=h.url('repo_creating_home',
+        raise HTTPFound(location=webutils.url('repo_creating_home',
                               repo_name=form_result['repo_name_full'],
                               task_id=task_id))
 
@@ -182,7 +183,7 @@
                         % (repo.repo_name, repo.clone_uri_hidden), category='success')
             else:
                 repo_url = h.link_to(repo.repo_name,
-                                     h.url('summary_home',
+                                     webutils.url('summary_home',
                                            repo_name=repo.repo_name))
                 fork = repo.fork
                 if fork is not None:
--- a/kallithea/controllers/admin/settings.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/controllers/admin/settings.py	Thu Oct 29 14:22:49 2020 +0100
@@ -36,6 +36,7 @@
 from webob.exc import HTTPFound
 
 from kallithea.lib import helpers as h
+from kallithea.lib import webutils
 from kallithea.lib.auth import HasPermissionAnyDecorator, LoginRequired
 from kallithea.lib.base import BaseController, render
 from kallithea.lib.celerylib import tasks
@@ -147,7 +148,7 @@
                                             user=request.authuser.username,
                                             overwrite_git_hooks=overwrite_git_hooks)
             added_msg = h.HTML(', ').join(
-                h.link_to(safe_str(repo_name), h.url('summary_home', repo_name=repo_name)) for repo_name in added
+                h.link_to(safe_str(repo_name), webutils.url('summary_home', repo_name=repo_name)) for repo_name in added
             ) or '-'
             removed_msg = h.HTML(', ').join(
                 safe_str(repo_name) for repo_name in removed
--- a/kallithea/controllers/changelog.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/controllers/changelog.py	Thu Oct 29 14:22:49 2020 +0100
@@ -34,6 +34,7 @@
 from webob.exc import HTTPBadRequest, HTTPFound, HTTPNotFound
 
 import kallithea.lib.helpers as h
+from kallithea.lib import webutils
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.base import BaseRepoController, render
 from kallithea.lib.graphmod import graph_data
@@ -112,7 +113,7 @@
                         collection = cs.get_file_history(f_path)
                     except RepositoryError as e:
                         h.flash(e, category='warning')
-                        raise HTTPFound(location=h.url('changelog_home', repo_name=repo_name))
+                        raise HTTPFound(location=webutils.url('changelog_home', repo_name=repo_name))
             else:
                 collection = c.db_repo_scm_instance.get_changesets(start=0, end=revision,
                                                         branch_name=branch_name, reverse=True)
--- a/kallithea/controllers/changeset.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/controllers/changeset.py	Thu Oct 29 14:22:49 2020 +0100
@@ -36,7 +36,7 @@
 from webob.exc import HTTPBadRequest, HTTPForbidden, HTTPNotFound
 
 import kallithea.lib.helpers as h
-from kallithea.lib import diffs
+from kallithea.lib import diffs, webutils
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.base import BaseRepoController, jsonify, render
 from kallithea.lib.graphmod import graph_data
@@ -100,7 +100,7 @@
             h.flash(_('Successfully deleted pull request %s') % pull_request_id,
                     category='success')
             return {
-               'location': h.url('my_pullrequests'), # or repo pr list?
+               'location': webutils.url('my_pullrequests'), # or repo pr list?
             }
         raise HTTPForbidden()
 
--- a/kallithea/controllers/compare.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/controllers/compare.py	Thu Oct 29 14:22:49 2020 +0100
@@ -36,6 +36,7 @@
 
 from kallithea.lib import diffs
 from kallithea.lib import helpers as h
+from kallithea.lib import webutils
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.base import BaseRepoController, render
 from kallithea.lib.graphmod import graph_data
@@ -100,7 +101,7 @@
         # is_ajax_preview puts hidden input field with changeset revisions
         c.is_ajax_preview = partial and request.GET.get('is_ajax_preview')
         # swap url for compare_diff page - never partial and never is_ajax_preview
-        c.swap_url = h.url('compare_url',
+        c.swap_url = webutils.url('compare_url',
             repo_name=c.cs_repo.repo_name,
             org_ref_type=other_ref_type, org_ref_name=other_ref_name,
             other_repo=c.a_repo.repo_name,
--- a/kallithea/controllers/files.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/controllers/files.py	Thu Oct 29 14:22:49 2020 +0100
@@ -41,6 +41,7 @@
 import kallithea
 from kallithea.lib import diffs
 from kallithea.lib import helpers as h
+from kallithea.lib import webutils
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.base import BaseRepoController, jsonify, render
 from kallithea.lib.exceptions import NonRelativePathError
@@ -294,7 +295,7 @@
         if revision not in _branches and revision not in _branches.values():
             h.flash(_('You can only delete files with revision '
                       'being a valid branch'), category='warning')
-            raise HTTPFound(location=h.url('files_home',
+            raise HTTPFound(location=webutils.url('files_home',
                                   repo_name=repo_name, revision='tip',
                                   f_path=f_path))
 
@@ -348,7 +349,7 @@
         if revision not in _branches and revision not in _branches.values():
             h.flash(_('You can only edit files with revision '
                       'being a valid branch'), category='warning')
-            raise HTTPFound(location=h.url('files_home',
+            raise HTTPFound(location=webutils.url('files_home',
                                   repo_name=repo_name, revision='tip',
                                   f_path=f_path))
 
--- a/kallithea/controllers/forks.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/controllers/forks.py	Thu Oct 29 14:22:49 2020 +0100
@@ -37,6 +37,7 @@
 
 import kallithea
 import kallithea.lib.helpers as h
+from kallithea.lib import webutils
 from kallithea.lib.auth import HasPermissionAnyDecorator, HasRepoPermissionLevel, HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.base import BaseRepoController, render
 from kallithea.lib.page import Page
@@ -168,6 +169,6 @@
             h.flash(_('An error occurred during repository forking %s') %
                     repo_name, category='error')
 
-        raise HTTPFound(location=h.url('repo_creating_home',
+        raise HTTPFound(location=webutils.url('repo_creating_home',
                               repo_name=form_result['repo_name_full'],
                               task_id=task_id))
--- a/kallithea/model/db.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/model/db.py	Thu Oct 29 14:22:49 2020 +0100
@@ -44,7 +44,7 @@
 from webob.exc import HTTPNotFound
 
 import kallithea
-from kallithea.lib import ext_json, ssh
+from kallithea.lib import ext_json, ssh, webutils
 from kallithea.lib.exceptions import DefaultUserException
 from kallithea.lib.utils2 import asbool, ascii_bytes, aslist, get_changeset_safe, get_clone_url, remove_prefix, safe_bytes, safe_int, safe_str, urlreadable
 from kallithea.lib.vcs import get_backend
@@ -1932,9 +1932,8 @@
 
     def url(self):
         anchor = "comment-%s" % self.comment_id
-        import kallithea.lib.helpers as h
         if self.revision:
-            return h.url('changeset_home', repo_name=self.repo.repo_name, revision=self.revision, anchor=anchor)
+            return webutils.url('changeset_home', repo_name=self.repo.repo_name, revision=self.revision, anchor=anchor)
         elif self.pull_request_id is not None:
             return self.pull_request.url(anchor=anchor)
 
@@ -2150,7 +2149,7 @@
         if canonical:
             return h.canonical_url('pullrequest_show', repo_name=self.other_repo.repo_name,
                                    pull_request_id=self.pull_request_id, **kwargs)
-        return h.url('pullrequest_show', repo_name=self.other_repo.repo_name,
+        return webutils.url('pullrequest_show', repo_name=self.other_repo.repo_name,
                      pull_request_id=self.pull_request_id, **kwargs)
 
 
--- a/kallithea/model/user.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/model/user.py	Thu Oct 29 14:22:49 2020 +0100
@@ -36,6 +36,7 @@
 from tg import config
 from tg.i18n import ugettext as _
 
+from kallithea.lib import webutils
 from kallithea.lib.exceptions import DefaultUserException, UserOwnsReposException
 from kallithea.lib.utils2 import generate_api_key, get_current_authuser
 from kallithea.model import db, meta
@@ -330,7 +331,7 @@
                 # URL must be fully qualified; but since the token is locked to
                 # the current browser session, we must provide a URL with the
                 # current scheme and hostname, rather than the canonical_url.
-                link = h.url('reset_password_confirmation', qualified=True,
+                link = webutils.url('reset_password_confirmation', qualified=True,
                              email=user_email,
                              timestamp=timestamp,
                              token=token)
@@ -354,7 +355,7 @@
         else:
             log.debug("password reset email %s not found", user_email)
 
-        return h.url('reset_password_confirmation',
+        return webutils.url('reset_password_confirmation',
                      email=user_email,
                      timestamp=timestamp)
 
--- a/kallithea/tests/fixture.py	Thu Oct 29 15:12:56 2020 +0100
+++ b/kallithea/tests/fixture.py	Thu Oct 29 14:22:49 2020 +0100
@@ -22,11 +22,9 @@
 import tarfile
 from os.path import dirname
 
-import mock
 from tg import request
 from tg.util.webtest import test_context
 
-from kallithea.lib import helpers
 from kallithea.lib.auth import AuthUser
 from kallithea.lib.db_manage import DbManage
 from kallithea.lib.vcs.backends.base import EmptyChangeset
@@ -332,15 +330,15 @@
     def create_pullrequest(self, testcontroller, repo_name, pr_src_rev, pr_dst_rev, title='title'):
         org_ref = 'branch:stable:%s' % pr_src_rev
         other_ref = 'branch:default:%s' % pr_dst_rev
-        with test_context(testcontroller.app): # needed to be able to mock request user
+        with test_context(testcontroller.app): # needed to be able to mock request user and routes.url
             org_repo = other_repo = db.Repository.get_by_repo_name(repo_name)
             owner_user = db.User.get_by_username(TEST_USER_ADMIN_LOGIN)
             reviewers = [db.User.get_by_username(TEST_USER_REGULAR_LOGIN)]
             request.authuser = AuthUser(dbuser=owner_user)
             # creating a PR sends a message with an absolute URL - without routing that requires mocking
-            with mock.patch.object(helpers, 'url', (lambda arg, qualified=False, **kwargs: ('https://localhost' if qualified else '') + '/fake/' + arg)):
-                cmd = CreatePullRequestAction(org_repo, other_repo, org_ref, other_ref, title, 'No description', owner_user, reviewers)
-                pull_request = cmd.execute()
+            request.environ['routes.url'] = lambda arg, qualified=False, **kwargs: ('https://localhost' if qualified else '') + '/fake/' + arg
+            cmd = CreatePullRequestAction(org_repo, other_repo, org_ref, other_ref, title, 'No description', owner_user, reviewers)
+            pull_request = cmd.execute()
             meta.Session().commit()
         return pull_request.pull_request_id