changeset 8720:0c65a8f15e54

lib: move canonical_url & co to webutils This gives less of the unfortunate use of helpers - especially in model.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 29 Oct 2020 14:32:42 +0100
parents 216ed3859869
children 67e5b90801aa
files kallithea/controllers/feed.py kallithea/controllers/journal.py kallithea/lib/helpers.py kallithea/lib/webutils.py kallithea/model/comment.py kallithea/model/db.py kallithea/model/pull_request.py kallithea/model/user.py kallithea/tests/other/test_libs.py
diffstat 9 files changed, 66 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/feed.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/controllers/feed.py	Thu Oct 29 14:32:42 2020 +0100
@@ -36,6 +36,7 @@
 import kallithea
 from kallithea.lib import feeds
 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
 from kallithea.lib.diffs import DiffProcessor
@@ -84,7 +85,7 @@
                                  _('Changeset was too big and was cut off...')]
 
         # rev link
-        _url = h.canonical_url('changeset_home', repo_name=c.db_repo.repo_name,
+        _url = webutils.canonical_url('changeset_home', repo_name=c.db_repo.repo_name,
                    revision=cs.raw_id)
         desc_msg.append('changeset: <a href="%s">%s</a>' % (_url, cs.raw_id[:8]))
 
@@ -105,7 +106,7 @@
         def _get_feed_from_cache(*_cache_keys):  # parameters are not really used - only as caching key
             header = dict(
                 title=_('%s %s feed') % (c.site_name, repo_name),
-                link=h.canonical_url('summary_home', repo_name=repo_name),
+                link=webutils.canonical_url('summary_home', repo_name=repo_name),
                 description=_('Changes on %s repository') % repo_name,
             )
 
@@ -114,7 +115,7 @@
             for cs in reversed(list(c.db_repo_scm_instance[-rss_items_per_page:])):
                 entries.append(dict(
                     title=self._get_title(cs),
-                    link=h.canonical_url('changeset_home', repo_name=repo_name, revision=cs.raw_id),
+                    link=webutils.canonical_url('changeset_home', repo_name=repo_name, revision=cs.raw_id),
                     author_email=cs.author_email,
                     author_name=cs.author_name,
                     description=''.join(self.__get_desc(cs)),
--- a/kallithea/controllers/journal.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/controllers/journal.py	Thu Oct 29 14:32:42 2020 +0100
@@ -38,7 +38,7 @@
 
 import kallithea.lib.helpers as h
 from kallithea.controllers.admin.admin import _journal_filter
-from kallithea.lib import feeds
+from kallithea.lib import feeds, webutils
 from kallithea.lib.auth import LoginRequired
 from kallithea.lib.base import BaseController, render
 from kallithea.lib.page import Page
@@ -125,13 +125,13 @@
                                     entry.repository.repo_name)
             _url = None
             if entry.repository is not None:
-                _url = h.canonical_url('changelog_home',
+                _url = webutils.canonical_url('changelog_home',
                            repo_name=entry.repository.repo_name)
 
             entries.append(dict(
                 title=title,
                 pubdate=entry.action_date,
-                link=_url or h.canonical_url(''),
+                link=_url or webutils.canonical_url(''),
                 author_email=user.email,
                 author_name=user.full_name_or_username,
                 description=action_extra(),
@@ -141,22 +141,22 @@
 
     def _atom_feed(self, repos, public=True):
         if public:
-            link = h.canonical_url('public_journal_atom')
+            link = webutils.canonical_url('public_journal_atom')
             desc = '%s %s %s' % (c.site_name, _('Public Journal'),
                                   'atom feed')
         else:
-            link = h.canonical_url('journal_atom')
+            link = webutils.canonical_url('journal_atom')
             desc = '%s %s %s' % (c.site_name, _('Journal'), 'atom feed')
 
         return self._feed(repos, feeds.AtomFeed, link, desc)
 
     def _rss_feed(self, repos, public=True):
         if public:
-            link = h.canonical_url('public_journal_atom')
+            link = webutils.canonical_url('public_journal_atom')
             desc = '%s %s %s' % (c.site_name, _('Public Journal'),
                                   'rss feed')
         else:
-            link = h.canonical_url('journal_atom')
+            link = webutils.canonical_url('journal_atom')
             desc = '%s %s %s' % (c.site_name, _('Journal'), 'rss feed')
 
         return self._feed(repos, feeds.RssFeed, link, desc)
--- a/kallithea/lib/helpers.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/lib/helpers.py	Thu Oct 29 14:32:42 2020 +0100
@@ -55,7 +55,7 @@
 # SCM FILTERS available via h.
 #==============================================================================
 from kallithea.lib.vcs.utils import author_email, author_name
-from kallithea.lib.webutils import url
+from kallithea.lib.webutils import canonical_url, url
 from kallithea.model import db
 from kallithea.model.changeset_status import ChangesetStatusModel
 
@@ -78,33 +78,12 @@
 assert age
 assert time_to_datetime
 assert EmptyChangeset
+assert canonical_url
 
 
 log = logging.getLogger(__name__)
 
 
-def canonical_url(*args, **kargs):
-    '''Like url(x, qualified=True), but returns url that not only is qualified
-    but also canonical, as configured in canonical_url'''
-    try:
-        parts = kallithea.CONFIG.get('canonical_url', '').split('://', 1)
-        kargs['host'] = parts[1]
-        kargs['protocol'] = parts[0]
-    except IndexError:
-        kargs['qualified'] = True
-    return url(*args, **kargs)
-
-
-def canonical_hostname():
-    '''Return canonical hostname of system'''
-    try:
-        parts = kallithea.CONFIG.get('canonical_url', '').split('://', 1)
-        return parts[1].split('/', 1)[0]
-    except IndexError:
-        parts = url('home', qualified=True).split('://', 1)
-        return parts[1].split('/', 1)[0]
-
-
 def html_escape(s):
     """Return string with all html escaped.
     This is also safe for javascript in html but not necessarily correct.
--- a/kallithea/lib/webutils.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/lib/webutils.py	Thu Oct 29 14:32:42 2020 +0100
@@ -15,13 +15,19 @@
 kallithea.lib.webutils
 ~~~~~~~~~~~~~~~~~~~~
 
-Helper functions that rely on the current WSGI request, exposed in the TG2
+Helper functions that may rely on the current WSGI request, exposed in the TG2
 thread-local "global" variables. It should have few dependencies so it can be
 imported anywhere - just like the global variables can be used everywhere.
 """
 
 from tg import request
 
+import kallithea
+
+
+#
+# General Kallithea URL handling
+#
 
 class UrlGenerator(object):
     """Emulate pylons.url in providing a wrapper around routes.url
@@ -47,3 +53,25 @@
 
 
 url = UrlGenerator()
+
+
+def canonical_url(*args, **kargs):
+    '''Like url(x, qualified=True), but returns url that not only is qualified
+    but also canonical, as configured in canonical_url'''
+    try:
+        parts = kallithea.CONFIG.get('canonical_url', '').split('://', 1)
+        kargs['host'] = parts[1]
+        kargs['protocol'] = parts[0]
+    except IndexError:
+        kargs['qualified'] = True
+    return url(*args, **kargs)
+
+
+def canonical_hostname():
+    '''Return canonical hostname of system'''
+    try:
+        parts = kallithea.CONFIG.get('canonical_url', '').split('://', 1)
+        return parts[1].split('/', 1)[0]
+    except IndexError:
+        parts = url('home', qualified=True).split('://', 1)
+        return parts[1].split('/', 1)[0]
--- a/kallithea/model/comment.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/model/comment.py	Thu Oct 29 14:32:42 2020 +0100
@@ -31,6 +31,7 @@
 from tg.i18n import ugettext as _
 
 from kallithea.lib import helpers as h
+from kallithea.lib import webutils
 from kallithea.lib.utils import extract_mentioned_users
 from kallithea.model import db, meta
 from kallithea.model.notification import NotificationModel
@@ -72,11 +73,11 @@
             cs = repo.scm_instance.get_changeset(revision)
             desc = cs.short_id
 
-            threading = ['%s-rev-%s@%s' % (repo.repo_name, revision, h.canonical_hostname())]
+            threading = ['%s-rev-%s@%s' % (repo.repo_name, revision, webutils.canonical_hostname())]
             if line_no: # TODO: url to file _and_ line number
                 threading.append('%s-rev-%s-line-%s@%s' % (repo.repo_name, revision, line_no,
-                                                           h.canonical_hostname()))
-            comment_url = h.canonical_url('changeset_home',
+                                                           webutils.canonical_hostname()))
+            comment_url = webutils.canonical_url('changeset_home',
                 repo_name=repo.repo_name,
                 revision=revision,
                 anchor='comment-%s' % comment.comment_id)
@@ -97,9 +98,9 @@
             email_kwargs = {
                 'status_change': status_change,
                 'cs_comment_user': author.full_name_and_username,
-                'cs_target_repo': h.canonical_url('summary_home', repo_name=repo.repo_name),
+                'cs_target_repo': webutils.canonical_url('summary_home', repo_name=repo.repo_name),
                 'cs_comment_url': comment_url,
-                'cs_url': h.canonical_url('changeset_home', repo_name=repo.repo_name, revision=revision),
+                'cs_url': webutils.canonical_url('changeset_home', repo_name=repo.repo_name, revision=revision),
                 'raw_id': revision,
                 'message': cs.message,
                 'message_short': h.shorter(cs.message, 50, firstline=True),
@@ -119,11 +120,11 @@
             _other_ref_type, other_ref_name, _other_rev = comment.pull_request.other_ref.split(':')
             threading = ['%s-pr-%s@%s' % (pull_request.other_repo.repo_name,
                                           pull_request.pull_request_id,
-                                          h.canonical_hostname())]
+                                          webutils.canonical_hostname())]
             if line_no: # TODO: url to file _and_ line number
                 threading.append('%s-pr-%s-line-%s@%s' % (pull_request.other_repo.repo_name,
                                                           pull_request.pull_request_id, line_no,
-                                                          h.canonical_hostname()))
+                                                          webutils.canonical_hostname()))
             comment_url = pull_request.url(canonical=True,
                 anchor='comment-%s' % comment.comment_id)
             subj = h.link_to(
@@ -147,10 +148,10 @@
                 'pr_comment_url': comment_url,
                 'pr_url': pull_request.url(canonical=True),
                 'pr_comment_user': author.full_name_and_username,
-                'pr_target_repo': h.canonical_url('summary_home',
+                'pr_target_repo': webutils.canonical_url('summary_home',
                                    repo_name=pull_request.other_repo.repo_name),
                 'pr_target_branch': other_ref_name,
-                'pr_source_repo': h.canonical_url('summary_home',
+                'pr_source_repo': webutils.canonical_url('summary_home',
                                    repo_name=pull_request.org_repo.repo_name),
                 'pr_source_branch': org_ref_name,
                 'pr_owner': pull_request.owner,
--- a/kallithea/model/db.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/model/db.py	Thu Oct 29 14:32:42 2020 +0100
@@ -1185,8 +1185,7 @@
         else:
             clone_uri_tmpl = clone_uri_tmpl.replace('_{repoid}', '{repo}')
 
-        import kallithea.lib.helpers as h
-        prefix_url = h.canonical_url('home')
+        prefix_url = webutils.canonical_url('home')
 
         return get_clone_url(clone_uri_tmpl=clone_uri_tmpl,
                              prefix_url=prefix_url,
@@ -2139,7 +2138,6 @@
 
     def url(self, **kwargs):
         canonical = kwargs.pop('canonical', None)
-        import kallithea.lib.helpers as h
         b = self.org_ref_parts[1]
         if b != self.other_ref_parts[1]:
             s = '/_/' + b
@@ -2147,7 +2145,7 @@
             s = '/_/' + self.title
         kwargs['extra'] = urlreadable(s)
         if canonical:
-            return h.canonical_url('pullrequest_show', repo_name=self.other_repo.repo_name,
+            return webutils.canonical_url('pullrequest_show', repo_name=self.other_repo.repo_name,
                                    pull_request_id=self.pull_request_id, **kwargs)
         return webutils.url('pullrequest_show', repo_name=self.other_repo.repo_name,
                      pull_request_id=self.pull_request_id, **kwargs)
@@ -2237,8 +2235,7 @@
         if alias_url:
             return alias_url.replace('{gistid}', self.gist_access_id)
 
-        import kallithea.lib.helpers as h
-        return h.canonical_url('gist', gist_id=self.gist_access_id)
+        return webutils.canonical_url('gist', gist_id=self.gist_access_id)
 
     def get_api_data(self):
         """
--- a/kallithea/model/pull_request.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/model/pull_request.py	Thu Oct 29 14:32:42 2020 +0100
@@ -34,6 +34,7 @@
 
 from kallithea.lib import auth
 from kallithea.lib import helpers as h
+from kallithea.lib import webutils
 from kallithea.lib.hooks import log_create_pullrequest
 from kallithea.lib.utils import extract_mentioned_users
 from kallithea.lib.utils2 import ascii_bytes
@@ -80,7 +81,7 @@
         pr_url = pr.url(canonical=True)
         threading = ['%s-pr-%s@%s' % (pr.other_repo.repo_name,
                                       pr.pull_request_id,
-                                      h.canonical_hostname())]
+                                      webutils.canonical_hostname())]
         subject = h.link_to(
             _('%(user)s wants you to review pull request %(pr_nice_id)s: %(pr_title)s') %
                 {'user': user.username,
@@ -96,16 +97,16 @@
             'pr_title': pr.title,
             'pr_title_short': h.shorter(pr.title, 50),
             'pr_user_created': user.full_name_and_username,
-            'pr_repo_url': h.canonical_url('summary_home', repo_name=pr.other_repo.repo_name),
+            'pr_repo_url': webutils.canonical_url('summary_home', repo_name=pr.other_repo.repo_name),
             'pr_url': pr_url,
             'pr_revisions': revision_data,
             'repo_name': pr.other_repo.repo_name,
             'org_repo_name': pr.org_repo.repo_name,
             'pr_nice_id': pr.nice_id(),
-            'pr_target_repo': h.canonical_url('summary_home',
+            'pr_target_repo': webutils.canonical_url('summary_home',
                                repo_name=pr.other_repo.repo_name),
             'pr_target_branch': other_ref_name,
-            'pr_source_repo': h.canonical_url('summary_home',
+            'pr_source_repo': webutils.canonical_url('summary_home',
                                repo_name=pr.org_repo.repo_name),
             'pr_source_branch': org_ref_name,
             'pr_owner': pr.owner,
@@ -342,7 +343,7 @@
         lost = old_revisions.difference(revisions)
 
         infos = ['This is a new iteration of %s "%s".' %
-                 (h.canonical_url('pullrequest_show', repo_name=old_pull_request.other_repo.repo_name,
+                 (webutils.canonical_url('pullrequest_show', repo_name=old_pull_request.other_repo.repo_name,
                       pull_request_id=old_pull_request.pull_request_id),
                   old_pull_request.title)]
 
@@ -362,7 +363,7 @@
 
             if self.create_action.other_ref == old_pull_request.other_ref:
                 infos.append(_("Ancestor didn't change - diff since previous iteration:"))
-                infos.append(h.canonical_url('compare_url',
+                infos.append(webutils.canonical_url('compare_url',
                                  repo_name=org_repo.repo_name, # other_repo is always same as repo_name
                                  org_ref_type='rev', org_ref_name=org_rev[:12], # use old org_rev as base
                                  other_ref_type='rev', other_ref_name=new_org_rev[:12],
--- a/kallithea/model/user.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/model/user.py	Thu Oct 29 14:32:42 2020 +0100
@@ -166,7 +166,6 @@
             raise
 
     def create_registration(self, form_data):
-        import kallithea.lib.helpers as h
         from kallithea.model.notification import NotificationModel
 
         form_data['admin'] = False
@@ -183,7 +182,7 @@
             '- Full Name: {user.full_name}\n'
             '- Email: {user.email}\n'
             ).format(user=new_user)
-        edit_url = h.canonical_url('edit_user', id=new_user.user_id)
+        edit_url = webutils.canonical_url('edit_user', id=new_user.user_id)
         email_kwargs = {
             'registered_user_url': edit_url,
             'new_username': new_user.username,
--- a/kallithea/tests/other/test_libs.py	Thu Oct 29 14:48:03 2020 +0100
+++ b/kallithea/tests/other/test_libs.py	Thu Oct 29 14:32:42 2020 +0100
@@ -31,6 +31,7 @@
 import mock
 from tg.util.webtest import test_context
 
+from kallithea.lib import webutils
 from kallithea.lib.utils2 import AttributeDict, safe_bytes
 from kallithea.model import db
 from kallithea.tests import base
@@ -561,7 +562,6 @@
         import routes
         from tg import request
 
-        from kallithea.lib.helpers import canonical_url
         m = routes.Mapper()
         m.connect('about', '/about-page')
         url = routes.URLGenerator(m, {'HTTP_HOST': 'http_host.example.org'})
@@ -573,7 +573,7 @@
         with test_context(self.app):
             request.environ['routes.url'] = url
             with mock.patch('kallithea.CONFIG', config_mock):
-                assert canonical_url(test) == expected
+                assert webutils.canonical_url(test) == expected
 
     @base.parametrize('canonical,expected', [
         ('http://www.example.org', 'www.example.org'),
@@ -584,8 +584,6 @@
         import routes
         from tg import request
 
-        from kallithea.lib.helpers import canonical_hostname
-
         # setup url(), used by canonical_hostname
         m = routes.Mapper()
         url = routes.URLGenerator(m, {'HTTP_HOST': 'http_host.example.org'})
@@ -597,4 +595,4 @@
         with test_context(self.app):
             request.environ['routes.url'] = url
             with mock.patch('kallithea.CONFIG', config_mock):
-                assert canonical_hostname() == expected
+                assert webutils.canonical_hostname() == expected