You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2013/05/03 23:57:41 UTC

[11/50] git commit: [#5332] Miscellaneous code cleanup

[#5332] Miscellaneous code cleanup

- Rename project name regex to something logical
- Update make_safe_path_portion() to work in strict and relaxed modes

Signed-off-by: Tim Van Steenburgh <tv...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/cec8151f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/cec8151f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/cec8151f

Branch: refs/heads/db/6007
Commit: cec8151f3d2a9a7a9602816e7f9eb6d3b7a70359
Parents: ba32ac7
Author: Tim Van Steenburgh <tv...@gmail.com>
Authored: Fri Apr 26 20:37:36 2013 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Mon Apr 29 14:44:07 2013 +0000

----------------------------------------------------------------------
 .../controllers/basetest_neighborhood_root.py      |    2 +-
 Allura/allura/controllers/basetest_project_root.py |    2 +-
 Allura/allura/controllers/project.py               |    4 +-
 Allura/allura/controllers/rest.py                  |    2 +-
 .../allura/ext/admin/templates/project_tools.html  |    2 +-
 Allura/allura/lib/helpers.py                       |   27 +++++++++---
 Allura/allura/lib/plugin.py                        |    4 +-
 Allura/allura/lib/widgets/forms.py                 |    2 +-
 Allura/allura/model/project.py                     |    2 +-
 Allura/allura/tests/test_helpers.py                |   33 +++++++++++---
 10 files changed, 56 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/controllers/basetest_neighborhood_root.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/basetest_neighborhood_root.py b/Allura/allura/controllers/basetest_neighborhood_root.py
index cf36aed..b4ef16b 100644
--- a/Allura/allura/controllers/basetest_neighborhood_root.py
+++ b/Allura/allura/controllers/basetest_neighborhood_root.py
@@ -88,7 +88,7 @@ class BasetestNeighborhoodRootController(WsgiDispatchController, NeighborhoodCon
     @expose()
     def _lookup(self, pname, *remainder):
         pname = unquote(pname)
-        if not h.re_path_portion.match(pname):
+        if not h.re_project_name.match(pname):
             raise exc.HTTPNotFound, pname
         project = M.Project.query.get(shortname=self.prefix + pname, neighborhood_id=self.neighborhood._id)
         if project is None:

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/controllers/basetest_project_root.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/basetest_project_root.py b/Allura/allura/controllers/basetest_project_root.py
index 359984c..f9ec3d5 100644
--- a/Allura/allura/controllers/basetest_project_root.py
+++ b/Allura/allura/controllers/basetest_project_root.py
@@ -99,7 +99,7 @@ class BasetestProjectRootController(WsgiDispatchController, ProjectController):
 
     @expose()
     def _lookup(self, name, *remainder):
-        if not h.re_path_portion.match(name):
+        if not h.re_project_name.match(name):
             raise exc.HTTPNotFound, name
         subproject = M.Project.query.get(shortname=c.project.shortname + '/' + name,
                                          neighborhood_id=self.p_nbhd._id)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/controllers/project.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/project.py b/Allura/allura/controllers/project.py
index 75e7792..4f02632 100644
--- a/Allura/allura/controllers/project.py
+++ b/Allura/allura/controllers/project.py
@@ -79,7 +79,7 @@ class NeighborhoodController(object):
     @expose()
     def _lookup(self, pname, *remainder):
         pname = unquote(pname)
-        if not h.re_path_portion.match(pname):
+        if not h.re_project_name.match(pname):
             raise exc.HTTPNotFound, pname
         project = M.Project.query.get(shortname=self.prefix + pname, neighborhood_id=self.neighborhood._id)
         if project is None and self.prefix == 'u/':
@@ -185,7 +185,7 @@ class NeighborhoodController(object):
             W.add_project.fields['project_name'].validate(project_name, '')
         except Invalid as e:
             result['name_message'] = str(e)
-        if not h.re_path_portion.match(unix_name) or not (3 <= len(unix_name) <= 15):
+        if not h.re_project_name.match(unix_name) or not (3 <= len(unix_name) <= 15):
             result['unixname_message'] = 'Please use only letters, numbers, and dashes 3-15 characters long.'
         else:
             result['unixname_message'] = plugin.ProjectRegistrationProvider.get().name_taken(unix_name, self.neighborhood)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/controllers/rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/rest.py b/Allura/allura/controllers/rest.py
index 8ae0c70..c976c21 100644
--- a/Allura/allura/controllers/rest.py
+++ b/Allura/allura/controllers/rest.py
@@ -243,7 +243,7 @@ class NeighborhoodRestController(object):
 
     @expose()
     def _lookup(self, name, *remainder):
-        if not h.re_path_portion.match(name):
+        if not h.re_project_name.match(name):
             raise exc.HTTPNotFound, name
         name = self._neighborhood.shortname_prefix + name
         project = M.Project.query.get(shortname=name, neighborhood_id=self._neighborhood._id, deleted=False)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/ext/admin/templates/project_tools.html
----------------------------------------------------------------------
diff --git a/Allura/allura/ext/admin/templates/project_tools.html b/Allura/allura/ext/admin/templates/project_tools.html
index dc7646c..a607cfd 100644
--- a/Allura/allura/ext/admin/templates/project_tools.html
+++ b/Allura/allura/ext/admin/templates/project_tools.html
@@ -30,7 +30,7 @@
   <div class="nested-grid-container">
     {% for tool in installable_tools %}
       <span class="tcenter grid-4 installable_tool">
-        <a class="install_trig" data-tool="{{ tool['name'] }}" data-relaxed-mount-points="{{ tool.app.relaxed_mount_points and 'true' or 'false' }}">
+        <a class="install_trig" data-tool="{{ tool['name'] }}" data-relaxed-mount-points="{{ 'true' if tool.app.relaxed_mount_points else 'false' }}">
           <span class="tool_title">{{ tool['app'].tool_label }}{{' (%s)' % tool.app.status if tool.app.status != 'production' else ''}}</span><br />
           <img src="{{ g.theme.app_icon_url(tool['app'], 32) or 'unk.png' }}" alt="">
         </a>

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index b118efe..691204a 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -52,10 +52,16 @@ from allura.lib.decorators import exceptionless
 from allura.lib import AsciiDammit
 from .security import has_access
 
-re_path_portion_fragment = re.compile(r'[a-z][-a-z0-9]*')
-re_path_portion = re.compile(r'^[a-z][-a-z0-9]{2,62}$')
+
+# validates project, subproject, and user names
+re_project_name = re.compile(r'^[a-z][-a-z0-9]{2,62}$')
+
+# validates tool mount point names
 re_tool_mount_point = re.compile(r'^[a-z][-a-z0-9]{0,62}$')
+re_tool_mount_point_fragment = re.compile(r'[a-z][-a-z0-9]*')
 re_relaxed_tool_mount_point = re.compile(r'^[a-z0-9][-a-z0-9_\.\+]{0,62}$')
+re_relaxed_tool_mount_point_fragment = re.compile(r'[a-z0-9][-a-z0-9_\.\+]*')
+
 re_clean_vardec_key = re.compile(r'''\A
 ( # first part
 \w+# name...
@@ -67,17 +73,24 @@ re_clean_vardec_key = re.compile(r'''\A
 )+
 \Z''', re.VERBOSE)
 
-def make_safe_path_portion(ustr):
-    """Return an ascii representation of `ustr`
+def make_safe_path_portion(ustr, relaxed=True):
+    """Return an ascii representation of ``ustr`` that conforms to mount point
+    naming :attr:`rules <re_tool_mount_point_fragment>`.
+
+    Will return an empty string if no char in ``ustr`` is latin1-encodable.
+
+    :param relaxed: Use relaxed mount point naming rules (allows more
+        characters. See :attr:`re_relaxed_tool_mount_point_fragment`.
+    :returns: The converted string.
 
-    Will return an empty string if no char in `ustr`
-    is latin1-encodable.
     """
+    regex = (re_relaxed_tool_mount_point_fragment if relaxed else
+                re_tool_mount_point_fragment)
     ustr = really_unicode(ustr)
     s = ustr.encode('latin1', 'ignore')
     s = AsciiDammit.asciiDammit(s)
     s = s.lower()
-    s = '-'.join(re_path_portion_fragment.findall(s))
+    s = '-'.join(regex.findall(s))
     s = s.replace('--', '-')
     return s
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 48aa04b..b7722d2 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -464,7 +464,7 @@ class ProjectRegistrationProvider(object):
 
         self.rate_limit(user, neighborhood)
 
-        if not h.re_path_portion.match(shortname.replace('/', '')):
+        if not h.re_project_name.match(shortname.replace('/', '')):
             raise ValueError('Invalid project shortname: %s' % shortname)
 
         p = M.Project.query.get(shortname=shortname, neighborhood_id=neighborhood._id)
@@ -582,7 +582,7 @@ class ProjectRegistrationProvider(object):
 
     def register_subproject(self, project, name, user, install_apps):
         from allura import model as M
-        assert h.re_path_portion.match(name), 'Invalid subproject shortname'
+        assert h.re_project_name.match(name), 'Invalid subproject shortname'
         shortname = project.shortname + '/' + name
         sp = M.Project(
             parent_id=project._id,

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/lib/widgets/forms.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/forms.py b/Allura/allura/lib/widgets/forms.py
index 43c6dd4..9501b70 100644
--- a/Allura/allura/lib/widgets/forms.py
+++ b/Allura/allura/lib/widgets/forms.py
@@ -618,7 +618,7 @@ class RegistrationForm(ForgeForm):
         username = ew.TextField(
             label='Desired Username',
             validator=fev.Regex(
-                h.re_path_portion))
+                h.re_project_name))
         username.validator._messages['invalid'] = (
             'Usernames must include only letters, numbers, and dashes.'
             ' They must also start with a letter and be at least 3 characters'

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/model/project.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/project.py b/Allura/allura/model/project.py
index b3f1131..350f353 100644
--- a/Allura/allura/model/project.py
+++ b/Allura/allura/model/project.py
@@ -606,7 +606,7 @@ class Project(MappedClass, ActivityNode, ActivityObject):
                 return ac
 
     def new_subproject(self, name, install_apps=True, user=None):
-        if not h.re_path_portion.match(name):
+        if not h.re_project_name.match(name):
             raise exceptions.ToolError, 'Mount point "%s" is invalid' % name
         provider = plugin.ProjectRegistrationProvider.get()
         return provider.register_subproject(self, name, user or c.user, install_apps)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/cec8151f/Allura/allura/tests/test_helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_helpers.py b/Allura/allura/tests/test_helpers.py
index 2b8e179..ed40cfc 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -17,6 +17,7 @@
 #       specific language governing permissions and limitations
 #       under the License.
 
+from unittest import TestCase
 from os import path
 from mock import Mock, patch
 
@@ -34,13 +35,31 @@ def setUp(self):
     """Method called by nose before running each test"""
     setup_basic_test()
 
-def test_make_safe_path_portion():
-    s = u'Задачи'
-    new_s = h.make_safe_path_portion(s)
-    assert len(new_s) == 0
-    s = 'åß∂ƒ'
-    new_s = h.make_safe_path_portion(s)
-    assert new_s == 'ab'
+
+class TestMakeSafePathPortion(TestCase):
+    def setUp(self):
+        self.f = h.make_safe_path_portion
+
+    def test_no_latin1_chars(self):
+        s = self.f(u'Задачи')
+        self.assertEqual(s, '')
+
+    def test_some_latin1_chars(self):
+        s = self.f('åß∂ƒ')
+        self.assertEqual(s, 'ab')
+
+    def test_strict_mount_point_names(self):
+        s = self.f('1this+is.illegal', relaxed=False)
+        self.assertEqual(s, 'this-is-illegal')
+        s = self.f('this-1-is-legal', relaxed=False)
+        self.assertEqual(s, 'this-1-is-legal')
+
+    def test_relaxed_mount_point_names(self):
+        s = self.f('1_this+is.legal')
+        self.assertEqual(s, '1_this+is.legal')
+        s = self.f('not*_legal')
+        self.assertEqual(s, 'not-legal')
+
 
 def test_really_unicode():
     here_dir = path.dirname(__file__)