changeset 8722:5dfb757197c9

lib: move shorter to utils2 - less use of helpers in model
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 29 Oct 2020 15:51:01 +0100
parents 67e5b90801aa
children 247de7d8efb6
files kallithea/controllers/feed.py kallithea/lib/helpers.py kallithea/lib/utils2.py kallithea/model/comment.py kallithea/model/pull_request.py
diffstat 5 files changed, 21 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/feed.py	Sun Nov 01 04:59:46 2020 +0100
+++ b/kallithea/controllers/feed.py	Thu Oct 29 15:51:01 2020 +0100
@@ -40,7 +40,7 @@
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.base import BaseRepoController
 from kallithea.lib.diffs import DiffProcessor
-from kallithea.lib.utils2 import asbool, safe_int, safe_str
+from kallithea.lib.utils2 import asbool, safe_int, safe_str, shorter
 
 
 log = logging.getLogger(__name__)
@@ -54,7 +54,7 @@
         super(FeedController, self)._before(*args, **kwargs)
 
     def _get_title(self, cs):
-        return h.shorter(cs.message, 160)
+        return shorter(cs.message, 160)
 
     def __get_desc(self, cs):
         desc_msg = [(_('%s committed on %s')
--- a/kallithea/lib/helpers.py	Sun Nov 01 04:59:46 2020 +0100
+++ b/kallithea/lib/helpers.py	Thu Oct 29 15:51:01 2020 +0100
@@ -38,7 +38,7 @@
 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, safe_bytes, safe_int, safe_str, time_to_datetime
+from kallithea.lib.utils2 import MENTIONS_REGEX, AttributeDict, age, asbool, credentials_filter, 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
 #==============================================================================
@@ -80,6 +80,7 @@
 assert HasRepoPermissionLevel
 # from utils2
 assert age
+assert shorter
 assert time_to_datetime
 # from vcs
 assert EmptyChangeset
@@ -133,17 +134,6 @@
     return js(escape(val))
 
 
-def shorter(s, size=20, firstline=False, postfix='...'):
-    """Truncate s to size, including the postfix string if truncating.
-    If firstline, truncate at newline.
-    """
-    if firstline:
-        s = s.split('\n', 1)[0].rstrip()
-    if len(s) > size:
-        return s[:size - len(postfix)] + postfix
-    return s
-
-
 def FID(raw_id, path):
     """
     Creates a unique ID for filenode based on it's hash of path and revision
--- a/kallithea/lib/utils2.py	Sun Nov 01 04:59:46 2020 +0100
+++ b/kallithea/lib/utils2.py	Thu Oct 29 15:51:01 2020 +0100
@@ -153,6 +153,17 @@
     return s
 
 
+def shorter(s, size=20, firstline=False, postfix='...'):
+    """Truncate s to size, including the postfix string if truncating.
+    If firstline, truncate at newline.
+    """
+    if firstline:
+        s = s.split('\n', 1)[0].rstrip()
+    if len(s) > size:
+        return s[:size - len(postfix)] + postfix
+    return s
+
+
 def age(prevdate, show_short_version=False, now=None):
     """
     turns a datetime into an age string.
--- a/kallithea/model/comment.py	Sun Nov 01 04:59:46 2020 +0100
+++ b/kallithea/model/comment.py	Thu Oct 29 15:51:01 2020 +0100
@@ -30,9 +30,9 @@
 
 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.lib.utils2 import shorter
 from kallithea.model import db, meta
 from kallithea.model.notification import NotificationModel
 
@@ -103,7 +103,7 @@
                 '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),
+                'message_short': shorter(cs.message, 50, firstline=True),
                 'cs_author': cs_author,
                 'cs_author_username': cs_author.username,
                 'repo_name': repo.repo_name,
@@ -141,7 +141,7 @@
             # set some variables for email notification
             email_kwargs = {
                 'pr_title': pull_request.title,
-                'pr_title_short': h.shorter(pull_request.title, 50),
+                'pr_title_short': shorter(pull_request.title, 50),
                 'pr_nice_id': pull_request.nice_id(),
                 'status_change': status_change,
                 'closing_pr': closing_pr,
--- a/kallithea/model/pull_request.py	Sun Nov 01 04:59:46 2020 +0100
+++ b/kallithea/model/pull_request.py	Thu Oct 29 15:51:01 2020 +0100
@@ -37,7 +37,7 @@
 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
+from kallithea.lib.utils2 import ascii_bytes, shorter
 from kallithea.model import db, meta
 from kallithea.model.notification import NotificationModel
 
@@ -95,7 +95,7 @@
                          for x in map(pr.org_repo.get_changeset, pr.revisions)]
         email_kwargs = {
             'pr_title': pr.title,
-            'pr_title_short': h.shorter(pr.title, 50),
+            'pr_title_short': shorter(pr.title, 50),
             'pr_user_created': user.full_name_and_username,
             'pr_repo_url': webutils.canonical_url('summary_home', repo_name=pr.other_repo.repo_name),
             'pr_url': pr_url,
@@ -359,7 +359,7 @@
             for r in reversed(revisions):
                 if r in new_revisions:
                     rev_desc = org_repo.get_changeset(r).message.split('\n')[0]
-                    infos.append('  %s %s' % (r[:12], h.shorter(rev_desc, 80)))
+                    infos.append('  %s %s' % (r[:12], shorter(rev_desc, 80)))
 
             if self.create_action.other_ref == old_pull_request.other_ref:
                 infos.append(_("Ancestor didn't change - diff since previous iteration:"))