Repository: allura
Updated Branches:
  refs/heads/master daefc6a73 -> a7be6461e


[#8182] Improvements to Category/Trove admin


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

Branch: refs/heads/master
Commit: a7be6461e41bcc22ee2a61ad22c91ab99d5155a4
Parents: daefc6a
Author: Kenton Taylor <ktay...@slashdotmedia.com>
Authored: Thu Feb 1 14:49:50 2018 +0000
Committer: Dave Brondsema <dbronds...@slashdotmedia.com>
Committed: Fri Feb 2 22:28:39 2018 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/trovecategories.py      | 14 +++++++-------
 .../allura/ext/admin/templates/project_trove.html |  2 +-
 Allura/allura/lib/helpers.py                      | 18 +++++++++++++++++-
 Allura/allura/lib/widgets/forms.py                |  8 ++++++++
 Allura/allura/model/project.py                    |  3 ++-
 Allura/allura/templates/trovecategories.html      |  5 +++--
 Allura/allura/tests/test_helpers.py               | 12 ++++++++++++
 7 files changed, 50 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/a7be6461/Allura/allura/controllers/trovecategories.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/trovecategories.py 
b/Allura/allura/controllers/trovecategories.py
index c05d2ed..2e2c33f 100644
--- a/Allura/allura/controllers/trovecategories.py
+++ b/Allura/allura/controllers/trovecategories.py
@@ -78,7 +78,8 @@ class TroveCategoryController(BaseController):
         return dict(
             categories=l,
             selected_cat=selected_cat,
-            hierarchy=hierarchy)
+            hierarchy=hierarchy,
+            kw=kw)
 
     def generate_category(self, category):
         if not category:
@@ -109,6 +110,7 @@ class TroveCategoryController(BaseController):
     def create(self, **kw):
         name = kw.get('categoryname')
         upper_id = int(kw.get('uppercategory_id', 0))
+        shortname = kw.get('shortname', None)
 
         upper = M.TroveCategory.query.get(trove_cat_id=upper_id)
         if upper_id == 0:
@@ -124,13 +126,11 @@ class TroveCategoryController(BaseController):
 
         newid = max(
             [el.trove_cat_id for el in M.TroveCategory.query.find()]) + 1
-        shortname = name.replace(" ", "_").lower()
-        shortname = ''.join([(c if (c in digits or c in lowercase) else "_")
-                             for c in shortname])
+        shortname = h.slugify(shortname or name)[1]
 
         oldcat = M.TroveCategory.query.get(shortname=shortname)
         if oldcat:
-            flash('Category "%s" already exists.' % name, "error")
+            flash('Category "%s" with shortname "%s" already exists.  Try a 
different, unique shortname' % (name, shortname), "error")
         else:
             category = M.TroveCategory(
                 trove_cat_id=newid,
@@ -144,9 +144,9 @@ class TroveCategoryController(BaseController):
             else:
                 flash('An error occured while crearing the category.', "error")
         if upper:
-            redirect('/categories/%s' % upper.trove_cat_id)
+            
redirect(u'/categories/{}/?categoryname={}&shortname={}'.format(upper.trove_cat_id,
 name, shortname))
         else:
-            redirect('/categories')
+            redirect(u'/categories/?categoryname={}&shortname={}'.format(name, 
shortname))
 
     @expose()
     @require_post()

http://git-wip-us.apache.org/repos/asf/allura/blob/a7be6461/Allura/allura/ext/admin/templates/project_trove.html
----------------------------------------------------------------------
diff --git a/Allura/allura/ext/admin/templates/project_trove.html 
b/Allura/allura/ext/admin/templates/project_trove.html
index 81173cf..ea1a3bf 100644
--- a/Allura/allura/ext/admin/templates/project_trove.html
+++ b/Allura/allura/ext/admin/templates/project_trove.html
@@ -94,7 +94,7 @@
       </form>
     </div>
   </div>
-  {% for base in base_troves %}
+  {% for base in base_troves if hasattr(c.project, 
"trove_{}".format(base.shortname))%}
     {{show_trove_base_cat(base)}}
   {% endfor %}
 {% endblock %}

http://git-wip-us.apache.org/repos/asf/allura/blob/a7be6461/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index de9273a..572b6e6 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -24,6 +24,7 @@ import difflib
 import urllib
 import urllib2
 import re
+import unicodedata
 import json
 import logging
 import string
@@ -1293,4 +1294,19 @@ def base64uri(content_or_image, image_format='PNG', 
mimetype='image/png'):
     else:
         content = content_or_image
     data = base64.b64encode(content)
-    return 'data:{};base64,{}'.format(mimetype, data)
\ No newline at end of file
+    return 'data:{};base64,{}'.format(mimetype, data)
+
+
+def slugify(name, allow_periods=False):
+    """
+    Returns a tuple with slug and lowered slug based on name
+    """
+    dash_collapse_pattern = r'[^.\w]+' if allow_periods else r'[^\w]+'
+    slug = re.sub(r'(^-)|(-$)', '',  # leading - or trailing - gets removed
+                  unicode(
+                      re.sub(dash_collapse_pattern, '-',  # replace non ". 
alphanum_" sequences into single -
+                             re.sub(r"'", '',  # remove any apostrophes
+                                    unicodedata.normalize('NFKD', name)
+                                    .encode('ascii', 'ignore')))
+                  ))
+    return slug, slug.lower()

http://git-wip-us.apache.org/repos/asf/allura/blob/a7be6461/Allura/allura/lib/widgets/forms.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/forms.py 
b/Allura/allura/lib/widgets/forms.py
index 98d7efe..d92471c 100644
--- a/Allura/allura/lib/widgets/forms.py
+++ b/Allura/allura/lib/widgets/forms.py
@@ -602,6 +602,9 @@ class RemoveTroveCategoryForm(ForgeForm):
                     ew.LinkField(
                         text=cat.fullname,
                         href="/categories/%s" % cat.trove_cat_id),
+                    ew.HTMLField(
+                        text=cat.shortname,
+                        attrs={'disabled':True, 'value':cat.shortname}),
                     ew.SubmitButton(
                         show_errors=False,
                         attrs={'value': 'Remove'})],
@@ -629,7 +632,12 @@ class AddTroveCategoryForm(ForgeForm):
             show_errors=False)
         categoryname = ew.TextField(
             label="Category name",
+            attrs={},
             validator=fev.UnicodeString(not_empty=True))
+        shortname = ew.TextField(
+            label="Short Name",
+            validator=fev.UnicodeString(),
+            attrs={'placeholder': 'optional'})
 
     def display(self, **kw):
         upper_category = kw.get('uppercategory_id', 0)

http://git-wip-us.apache.org/repos/asf/allura/blob/a7be6461/Allura/allura/model/project.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/project.py b/Allura/allura/model/project.py
index 99d4d78..7eac462 100644
--- a/Allura/allura/model/project.py
+++ b/Allura/allura/model/project.py
@@ -296,7 +296,8 @@ class Project(SearchIndexable, MappedClass, ActivityNode, 
ActivityObject):
         return result
 
     def troves_by_type(self, trove_type):
-        troves = getattr(self, 'trove_%s' % trove_type)
+        trove_key = 'trove_%s' % trove_type
+        troves = getattr(self, trove_key) if hasattr(self, trove_key) else None
         if troves:
             return TroveCategory.query.find({'_id': {'$in': troves}}).all()
         else:

http://git-wip-us.apache.org/repos/asf/allura/blob/a7be6461/Allura/allura/templates/trovecategories.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/trovecategories.html 
b/Allura/allura/templates/trovecategories.html
index 81b6c1c..ff7959c 100644
--- a/Allura/allura/templates/trovecategories.html
+++ b/Allura/allura/templates/trovecategories.html
@@ -48,6 +48,7 @@
         <thead>
           <tr>
             <th>Name</th>
+            <th>Short Name</th>
             <th>Actions</th>
           </tr>
         </thead>
@@ -67,9 +68,9 @@
   <div class="grid-20">
     <h2>Create a new item in this category</h2>
     {% if selected_cat %}
-      
{{g.theme.add_trove_category.display(uppercategory_id=selected_cat.trove_cat_id)}}
+      
{{g.theme.add_trove_category.display(uppercategory_id=selected_cat.trove_cat_id,
 value=kw)}}
     {% else %}
-      {{g.theme.add_trove_category.display(uppercategory_id=0)}}
+      {{g.theme.add_trove_category.display(uppercategory_id=0, value=kw)}}
     {% endif %}
     <div class="grid-20" style="margin-bottom:10px;">
       Are you done creating new categories? <a 
href="/auth/user_info/skills/{{selected_cat.trove_cat_id}}">Click here</a> to 
configure your skills!

http://git-wip-us.apache.org/repos/asf/allura/blob/a7be6461/Allura/allura/tests/test_helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_helpers.py 
b/Allura/allura/tests/test_helpers.py
index fbc9813..e7ace1c 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -601,3 +601,15 @@ def test_base64uri_img():
 def test_base64uri_text():
     b64txt = h.base64uri('blah blah blah 123 456 foo bar baz', 
mimetype='text/plain')
     assert b64txt.startswith('data:text/plain;base64,'), b64txt
+
+
+def test_slugify():
+    assert_equals(h.slugify(u'Foo Bar Bat')[0], 'Foo-Bar-Bat')
+    assert_equals(h.slugify(u'Foo_Bar')[0], 'Foo_Bar')
+    assert_equals(h.slugify(u'Foo   ')[0], 'Foo')
+    assert_equals(h.slugify(u'    Foo   ')[0], 'Foo')
+    assert_equals(h.slugify(u'"    Foo   ')[0], 'Foo')
+    assert_equals(h.slugify(u'Fôö')[0], 'Foo')
+    assert_equals(h.slugify(u'Foo.Bar')[0], 'Foo-Bar')
+    assert_equals(h.slugify(u'Foo.Bar', True)[0], 'Foo.Bar')
+

Reply via email to