diff --git a/rhodecode/controllers/admin/repos.py b/rhodecode/controllers/admin/repos.py --- a/rhodecode/controllers/admin/repos.py +++ b/rhodecode/controllers/admin/repos.py @@ -40,7 +40,7 @@ from rhodecode.lib import helpers as h from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \ HasPermissionAnyDecorator from rhodecode.lib.base import BaseController, render -from rhodecode.lib.utils import invalidate_cache, action_logger +from rhodecode.lib.utils import invalidate_cache, action_logger, repo_name_slug from rhodecode.model.db import User from rhodecode.model.forms import RepoForm from rhodecode.model.scm import ScmModel @@ -118,7 +118,7 @@ class ReposController(BaseController): def new(self, format='html'): """GET /repos/new: Form to create a new item""" new_repo = request.GET.get('repo', '') - c.new_repo = h.repo_name_slug(new_repo) + c.new_repo = repo_name_slug(new_repo) return render('admin/repos/repo_add.html') diff --git a/rhodecode/controllers/admin/settings.py b/rhodecode/controllers/admin/settings.py --- a/rhodecode/controllers/admin/settings.py +++ b/rhodecode/controllers/admin/settings.py @@ -29,7 +29,7 @@ import logging import traceback import formencode -from operator import itemgetter +from sqlalchemy import func from formencode import htmlfill from pylons import request, session, tmpl_context as c, url, app_globals as g, \ config @@ -50,8 +50,6 @@ from rhodecode.model.scm import ScmModel from rhodecode.model.settings import SettingsModel from rhodecode.model.user import UserModel -from sqlalchemy import func - log = logging.getLogger(__name__) @@ -324,7 +322,7 @@ class SettingsController(BaseController) def create_repository(self): """GET /_admin/create_repository: Form to create a new item""" new_repo = request.GET.get('repo', '') - c.new_repo = h.repo_name_slug(new_repo) + c.new_repo = repo_name_slug(new_repo) return render('admin/repos/repo_add_create_repository.html') diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -8,9 +8,11 @@ import hashlib import StringIO from pygments.formatters import HtmlFormatter from pygments import highlight as code_highlight -from pylons import url, app_globals as g +from pylons import url from pylons.i18n.translation import _, ungettext from vcs.utils.annotate import annotate_highlight +from rhodecode.lib.utils import repo_name_slug + from webhelpers.html import literal, HTML, escape from webhelpers.html.tools import * from webhelpers.html.builder import make_tag @@ -73,23 +75,6 @@ class _GetError(object): get_error = _GetError() -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) - class _ToolTip(object): def __call__(self, tooltip_title, trim_at=50): @@ -357,21 +342,6 @@ def pygmentize_annotation(filenode, **kw return uri return literal(annotate_highlight(filenode, url_func, **kwargs)) -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 - def get_changeset_safe(repo, rev): from vcs.backends.base import BaseRepository from vcs.exceptions import RepositoryError diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -29,15 +29,17 @@ import os import logging import datetime import traceback +import paste +import beaker + +from paste.script.command import Command, BadCommand from UserDict import DictMixin from mercurial import ui, config, hg from mercurial.error import RepoError -import paste -import beaker -from paste.script.command import Command, BadCommand +from webhelpers.text import collapse, remove_formatting, strip_tags from vcs.backends.base import BaseChangeset from vcs.utils.lazy import LazyProperty @@ -51,6 +53,38 @@ from rhodecode.model.user import UserMod log = logging.getLogger(__name__) +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 + def get_repo_slug(request): return request.environ['pylons.routes_dict'].get('repo_name') diff --git a/rhodecode/model/forms.py b/rhodecode/model/forms.py --- a/rhodecode/model/forms.py +++ b/rhodecode/model/forms.py @@ -29,8 +29,9 @@ from formencode.validators import Unicod Email, Bool, StringBoolean, Set from pylons.i18n.translation import _ +from webhelpers.pylonslib.secure_form import authentication_token -import rhodecode.lib.helpers as h +from rhodecode.lib.utils import repo_name_slug from rhodecode.lib.auth import authenticate, get_crypt_password from rhodecode.lib.exceptions import LdapImportError from rhodecode.model import meta @@ -40,8 +41,6 @@ from rhodecode.model.users_group import from rhodecode.model.db import User, UsersGroup from rhodecode import BACKENDS -from webhelpers.pylonslib.secure_form import authentication_token - log = logging.getLogger(__name__) #this is needed to translate the messages using _() in validators @@ -214,7 +213,7 @@ def ValidRepoName(edit, old_data): class _ValidRepoName(formencode.validators.FancyValidator): def to_python(self, value, state): - slug = h.repo_name_slug(value) + slug = repo_name_slug(value) if slug in ['_admin']: raise formencode.Invalid(_('This repository name is disallowed'), value, state) diff --git a/rhodecode/model/scm.py b/rhodecode/model/scm.py --- a/rhodecode/model/scm.py +++ b/rhodecode/model/scm.py @@ -29,30 +29,29 @@ import time import traceback import logging +from mercurial import ui + +from sqlalchemy.orm import joinedload +from sqlalchemy.orm.session import make_transient +from sqlalchemy.exc import DatabaseError + +from beaker.cache import cache_region, region_invalidate + from vcs import get_backend from vcs.utils.helpers import get_scm from vcs.exceptions import RepositoryError, VCSError from vcs.utils.lazy import LazyProperty -from mercurial import ui - -from beaker.cache import cache_region, region_invalidate - from rhodecode import BACKENDS from rhodecode.lib import helpers as h from rhodecode.lib.auth import HasRepoPermissionAny from rhodecode.lib.utils import get_repos as get_filesystem_repos, make_ui, action_logger from rhodecode.model import BaseModel from rhodecode.model.user import UserModel - from rhodecode.model.db import Repository, RhodeCodeUi, CacheInvalidation, \ UserFollowing, UserLog from rhodecode.model.caching_query import FromCache -from sqlalchemy.orm import joinedload -from sqlalchemy.orm.session import make_transient -from sqlalchemy.exc import DatabaseError - log = logging.getLogger(__name__)