changeset 8724:7e7489e1672d

lib: move link_to_ref from helpers to utils2 Less reason to import the messy helpers.
author Mads Kiilerich <mads@kiilerich.com>
date Fri, 30 Oct 2020 00:50:38 +0100
parents 247de7d8efb6
children 6a9e5841cc51
files kallithea/lib/helpers.py kallithea/lib/utils2.py kallithea/model/pull_request.py
diffstat 3 files changed, 33 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/helpers.py	Fri Oct 30 14:56:50 2020 +0100
+++ b/kallithea/lib/helpers.py	Fri Oct 30 00:50:38 2020 +0100
@@ -38,8 +38,8 @@
 from kallithea.lib.diffs import BIN_FILENODE, CHMOD_FILENODE, DEL_FILENODE, MOD_FILENODE, NEW_FILENODE, RENAMED_FILENODE
 from kallithea.lib.markup_renderer import url_re
 from kallithea.lib.pygmentsutils import get_custom_lexer
-from kallithea.lib.utils2 import (MENTIONS_REGEX, AttributeDict, age, asbool, credentials_filter, fmt_date, safe_bytes, safe_int, safe_str, shorter,
-                                  time_to_datetime)
+from kallithea.lib.utils2 import (MENTIONS_REGEX, AttributeDict, age, asbool, credentials_filter, fmt_date, link_to_ref, safe_bytes, safe_int, safe_str,
+                                  shorter, time_to_datetime)
 from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset
 from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError
 #==============================================================================
@@ -82,6 +82,7 @@
 # from utils2
 assert age
 assert fmt_date
+assert link_to_ref
 assert shorter
 assert time_to_datetime
 # from vcs
@@ -1128,30 +1129,6 @@
     return literal('<div class="formatted-fixed">%s</div>' % s)
 
 
-def short_ref(ref_type, ref_name):
-    if ref_type == 'rev':
-        return short_id(ref_name)
-    return ref_name
-
-
-def link_to_ref(repo_name, ref_type, ref_name, rev=None):
-    """
-    Return full markup for a href to changeset_home for a changeset.
-    If ref_type is branch it will link to changelog.
-    ref_name is shortened if ref_type is 'rev'.
-    if rev is specified show it too, explicitly linking to that revision.
-    """
-    txt = short_ref(ref_type, ref_name)
-    if ref_type == 'branch':
-        u = url('changelog_home', repo_name=repo_name, branch=ref_name)
-    else:
-        u = url('changeset_home', repo_name=repo_name, revision=ref_name)
-    l = link_to(repo_name + '#' + txt, u)
-    if rev and ref_type != 'rev':
-        l = literal('%s (%s)' % (l, link_to(short_id(rev), url('changeset_home', repo_name=repo_name, revision=rev))))
-    return l
-
-
 def changeset_status(repo, revision):
     return ChangesetStatusModel().get_status(repo, revision)
 
--- a/kallithea/lib/utils2.py	Fri Oct 30 14:56:50 2020 +0100
+++ b/kallithea/lib/utils2.py	Fri Oct 30 00:50:38 2020 +0100
@@ -45,6 +45,7 @@
 from webhelpers2.text import collapse, remove_formatting, strip_tags
 
 import kallithea
+from kallithea.lib import webutils
 from kallithea.lib.vcs.backends.base import BaseRepository, EmptyChangeset
 from kallithea.lib.vcs.exceptions import RepositoryError
 from kallithea.lib.vcs.utils import ascii_bytes, ascii_str, safe_bytes, safe_str  # re-export
@@ -345,6 +346,31 @@
     return str(url_obj)
 
 
+def short_ref_name(ref_type, ref_name):
+    """Return short description of PR ref - revs will be truncated"""
+    if ref_type == 'rev':
+        return ref_name[:12]
+    return ref_name
+
+
+def link_to_ref(repo_name, ref_type, ref_name, rev=None):
+    """
+    Return full markup for a PR ref to changeset_home for a changeset.
+    If ref_type is 'branch', it will link to changelog.
+    ref_name is shortened if ref_type is 'rev'.
+    if rev is specified, show it too, explicitly linking to that revision.
+    """
+    txt = short_ref_name(ref_type, ref_name)
+    if ref_type == 'branch':
+        u = webutils.url('changelog_home', repo_name=repo_name, branch=ref_name)
+    else:
+        u = webutils.url('changeset_home', repo_name=repo_name, revision=ref_name)
+    l = webutils.link_to(repo_name + '#' + txt, u)
+    if rev and ref_type != 'rev':
+        l = webutils.literal('%s (%s)' % (l, webutils.link_to(rev[:12], webutils.url('changeset_home', repo_name=repo_name, revision=rev))))
+    return l
+
+
 def get_changeset_safe(repo, rev):
     """
     Safe version of get_changeset if this changeset doesn't exists for a
--- a/kallithea/model/pull_request.py	Fri Oct 30 14:56:50 2020 +0100
+++ b/kallithea/model/pull_request.py	Fri Oct 30 00:50:38 2020 +0100
@@ -32,12 +32,10 @@
 from tg import request
 from tg.i18n import ugettext as _
 
-from kallithea.lib import auth
-from kallithea.lib import helpers as h
-from kallithea.lib import webutils
+from kallithea.lib import auth, webutils
 from kallithea.lib.hooks import log_create_pullrequest
 from kallithea.lib.utils import extract_mentioned_users
-from kallithea.lib.utils2 import ascii_bytes, shorter
+from kallithea.lib.utils2 import ascii_bytes, short_ref_name, shorter
 from kallithea.model import db, meta
 from kallithea.model.notification import NotificationModel
 
@@ -199,7 +197,7 @@
         (org_ref_type,
          org_ref_name,
          org_rev) = org_ref.split(':')
-        org_display = h.short_ref(org_ref_type, org_ref_name)
+        org_display = short_ref_name(org_ref_type, org_ref_name)
         if org_ref_type == 'rev':
             cs = org_repo.scm_instance.get_changeset(org_rev)
             org_ref = 'branch:%s:%s' % (cs.branch, cs.raw_id)
@@ -211,7 +209,7 @@
             cs = other_repo.scm_instance.get_changeset(other_rev)
             other_ref_name = cs.raw_id[:12]
             other_ref = '%s:%s:%s' % (other_ref_type, other_ref_name, cs.raw_id)
-        other_display = h.short_ref(other_ref_type, other_ref_name)
+        other_display = short_ref_name(other_ref_type, other_ref_name)
 
         cs_ranges, _cs_ranges_not, ancestor_revs = \
             org_repo.scm_instance.get_diff_changesets(other_rev, org_repo.scm_instance, org_rev) # org and other "swapped"