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 2019/02/08 22:52:30 UTC

[allura] 03/03: [#8268] relax category shortname uniqueness to per-trove-type

This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch db/8268
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 1c2acf41f97c110cdeb9cf01ee7b1c40f094c817
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Fri Feb 8 17:50:38 2019 -0500

    [#8268] relax category shortname uniqueness to per-trove-type
---
 Allura/allura/controllers/trovecategories.py | 25 ++++++++++++++++---------
 Allura/allura/lib/widgets/forms.py           |  4 ++--
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/Allura/allura/controllers/trovecategories.py b/Allura/allura/controllers/trovecategories.py
index 2e2c33f..392de1c 100644
--- a/Allura/allura/controllers/trovecategories.py
+++ b/Allura/allura/controllers/trovecategories.py
@@ -14,6 +14,7 @@
 #       KIND, either express or implied.  See the License for the
 #       specific language governing permissions and limitations
 #       under the License.
+import re
 from collections import OrderedDict
 
 from tg import expose, flash, redirect, validate, config
@@ -128,25 +129,31 @@ class TroveCategoryController(BaseController):
             [el.trove_cat_id for el in M.TroveCategory.query.find()]) + 1
         shortname = h.slugify(shortname or name)[1]
 
-        oldcat = M.TroveCategory.query.get(shortname=shortname)
+        if upper:
+            trove_type = upper.fullpath.split(' :: ')[0]
+            fullpath_re = re.compile(r'^{} :: '.format(re.escape(trove_type)))  # e.g. scope within "Topic :: "
+        else:
+            # no parent, so making a top-level.  Don't limit fullpath_re, so enforcing global uniqueness
+            fullpath_re = re.compile(r'')
+        oldcat = M.TroveCategory.query.get(shortname=shortname, fullpath=fullpath_re)
         if oldcat:
-            flash('Category "%s" with shortname "%s" already exists.  Try a different, unique shortname' % (name, shortname), "error")
+            flash('A category with shortname "%s" already exists (%s).  Try a different, unique shortname'
+                  % (shortname, oldcat.fullpath), "error")
+            redir_params = u'?categoryname={}&shortname={}'.format(name, shortname)
         else:
-            category = M.TroveCategory(
+            M.TroveCategory(
                 trove_cat_id=newid,
                 trove_parent_id=upper_id,
                 fullname=name,
                 shortname=shortname,
                 fullpath=path,
                 show_as_skill=show_as_skill)
-            if category:
-                flash('Category "%s" successfully created.' % name)
-            else:
-                flash('An error occured while crearing the category.', "error")
+            flash('Category "%s" successfully created.' % name)
+            redir_params = ''
         if upper:
-            redirect(u'/categories/{}/?categoryname={}&shortname={}'.format(upper.trove_cat_id, name, shortname))
+            redirect(u'/categories/{}/{}'.format(upper.trove_cat_id, redir_params))
         else:
-            redirect(u'/categories/?categoryname={}&shortname={}'.format(name, shortname))
+            redirect(u'/categories/{}'.format(redir_params))
 
     @expose()
     @require_post()
diff --git a/Allura/allura/lib/widgets/forms.py b/Allura/allura/lib/widgets/forms.py
index e3f8232..9c75c51 100644
--- a/Allura/allura/lib/widgets/forms.py
+++ b/Allura/allura/lib/widgets/forms.py
@@ -649,9 +649,9 @@ class AddTroveCategoryForm(ForgeForm):
             attrs={},
             validator=fev.UnicodeString(not_empty=True))
         shortname = ew.TextField(
-            label="Short Name",
+            label="Short name",
             validator=fev.UnicodeString(),
-            attrs={'placeholder': 'optional'})
+            attrs={'placeholder': 'optional; unique identifier'})
 
     def display(self, **kw):
         upper_category = kw.get('uppercategory_id', 0)