diff --git a/kallithea/lib/utils.py b/kallithea/lib/utils.py --- a/kallithea/lib/utils.py +++ b/kallithea/lib/utils.py @@ -34,7 +34,6 @@ import beaker from tg import request, response from tg.i18n import ugettext as _ -from webhelpers.text import collapse, remove_formatting, strip_tags from beaker.cache import _cache_decorate from kallithea.lib.vcs.utils.hgcompat import ui, config @@ -54,42 +53,6 @@ log = logging.getLogger(__name__) REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}_.*') -def recursive_replace(str_, replace=' '): - """ - Recursive replace of given sign to just one instance - - :param str_: given string - :param replace: char to find and replace multiple instances - - Examples:: - >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-') - 'Mighty-Mighty-Bo-sstones' - """ - - if str_.find(replace * 2) == -1: - return str_ - else: - str_ = str_.replace(replace * 2, replace) - return recursive_replace(str_, replace) - - -def repo_name_slug(value): - """ - Return slug of name of repository - This function is called on each creation/modification - of repository to prevent bad names in repo - """ - - slug = remove_formatting(value) - slug = strip_tags(slug) - - for c in """`?=[]\;'"<>,/~!@#$%^&*()+{}|: """: - slug = slug.replace(c, '-') - slug = recursive_replace(slug, '-') - slug = collapse(slug, '-') - return slug - - #============================================================================== # PERM DECORATOR HELPERS FOR EXTRACTING NAMES FOR PERM CHECKS #============================================================================== diff --git a/kallithea/lib/utils2.py b/kallithea/lib/utils2.py --- a/kallithea/lib/utils2.py +++ b/kallithea/lib/utils2.py @@ -15,7 +15,9 @@ kallithea.lib.utils2 ~~~~~~~~~~~~~~~~~~~~ -Some simple helper functions +Some simple helper functions. +Note: all these functions should be independent of Kallithea classes, i.e. +models, controllers, etc. to prevent import cycles. This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: @@ -37,6 +39,7 @@ import binascii import webob import urlobject +from webhelpers.text import collapse, remove_formatting, strip_tags from tg.i18n import ugettext as _, ungettext from kallithea.lib.vcs.utils.lazy import LazyProperty @@ -646,3 +649,39 @@ class Optional(object): def urlreadable(s, _cleanstringsub=re.compile('[^-a-zA-Z0-9./]+').sub): return _cleanstringsub('_', safe_str(s)).rstrip('_') + + +def recursive_replace(str_, replace=' '): + """ + Recursive replace of given sign to just one instance + + :param str_: given string + :param replace: char to find and replace multiple instances + + Examples:: + >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-') + 'Mighty-Mighty-Bo-sstones' + """ + + if str_.find(replace * 2) == -1: + return str_ + else: + str_ = str_.replace(replace * 2, replace) + return recursive_replace(str_, replace) + + +def repo_name_slug(value): + """ + Return slug of name of repository + This function is called on each creation/modification + of repository to prevent bad names in repo + """ + + slug = remove_formatting(value) + slug = strip_tags(slug) + + for c in """`?=[]\;'"<>,/~!@#$%^&*()+{}|: """: + slug = slug.replace(c, '-') + slug = recursive_replace(slug, '-') + slug = collapse(slug, '-') + return slug diff --git a/kallithea/model/repo.py b/kallithea/model/repo.py --- a/kallithea/model/repo.py +++ b/kallithea/model/repo.py @@ -33,7 +33,7 @@ import traceback from datetime import datetime from sqlalchemy.orm import subqueryload -import kallithea.lib.utils +import kallithea.lib.utils2 from kallithea.lib.utils import make_ui, is_valid_repo_uri from kallithea.lib.vcs.backends import get_backend from kallithea.lib.utils2 import LazyProperty, safe_str, safe_unicode, \ @@ -316,7 +316,7 @@ class RepoModel(object): if 'repo_name' in kwargs: repo_name = kwargs['repo_name'] - if kallithea.lib.utils.repo_name_slug(repo_name) != repo_name: + if kallithea.lib.utils2.repo_name_slug(repo_name) != repo_name: raise Exception('invalid repo name %s' % repo_name) cur_repo.repo_name = cur_repo.get_new_name(repo_name) @@ -367,7 +367,7 @@ class RepoModel(object): # with name and path of group repo_name_full = repo_name repo_name = repo_name.split(self.URL_SEPARATOR)[-1] - if kallithea.lib.utils.repo_name_slug(repo_name) != repo_name: + if kallithea.lib.utils2.repo_name_slug(repo_name) != repo_name: raise Exception('invalid repo name %s' % repo_name) new_repo = Repository() diff --git a/kallithea/model/repo_group.py b/kallithea/model/repo_group.py --- a/kallithea/model/repo_group.py +++ b/kallithea/model/repo_group.py @@ -32,7 +32,7 @@ import traceback import shutil import datetime -import kallithea.lib.utils +import kallithea.lib.utils2 from kallithea.lib.utils2 import LazyProperty from kallithea.model.db import RepoGroup, Session, Ui, UserRepoGroupToPerm, \ @@ -136,7 +136,7 @@ class RepoGroupModel(object): def create(self, group_name, group_description, owner, parent=None, just_db=False, copy_permissions=False): try: - if kallithea.lib.utils.repo_name_slug(group_name) != group_name: + if kallithea.lib.utils2.repo_name_slug(group_name) != group_name: raise Exception('invalid repo group name %s' % group_name) owner = User.guess_instance(owner) @@ -295,7 +295,7 @@ class RepoGroupModel(object): repo_group.parent_group = RepoGroup.get(kwargs['parent_group_id']) if 'group_name' in kwargs: group_name = kwargs['group_name'] - if kallithea.lib.utils.repo_name_slug(group_name) != group_name: + if kallithea.lib.utils2.repo_name_slug(group_name) != group_name: raise Exception('invalid repo group name %s' % group_name) repo_group.group_name = repo_group.get_new_name(group_name) new_path = repo_group.full_path diff --git a/kallithea/model/validators.py b/kallithea/model/validators.py --- a/kallithea/model/validators.py +++ b/kallithea/model/validators.py @@ -31,8 +31,8 @@ from formencode.validators import ( ) from kallithea.lib.compat import OrderedSet from kallithea.lib import ipaddr -from kallithea.lib.utils import repo_name_slug, is_valid_repo_uri -from kallithea.lib.utils2 import str2bool, aslist +from kallithea.lib.utils import is_valid_repo_uri +from kallithea.lib.utils2 import str2bool, aslist, repo_name_slug from kallithea.model.db import RepoGroup, Repository, UserGroup, User from kallithea.lib.exceptions import LdapImportError from kallithea.config.routing import ADMIN_PREFIX