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')
def make_shortname(name, max_len): # lowercase, drop periods and underscores shortname = slugify(name)[1].replace('_', '-') # must start with a letter if not shortname[0].isalpha(): shortname = 'a-' + shortname # truncate length, avoid trailing dash shortname = shortname[:max_len].rstrip('-') # too short if len(shortname) < 3: shortname += '-z' return shortname
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: path = name show_as_skill = True elif upper is None: flash('Invalid upper category.', "error") redirect('/categories') return else: path = upper.fullpath + " :: " + name show_as_skill = upper.show_as_skill newid = max( [el.trove_cat_id for el in M.TroveCategory.query.find()]) + 1 shortname = h.slugify(shortname or name)[1] 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('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: M.TroveCategory( trove_cat_id=newid, trove_parent_id=upper_id, fullname=name, shortname=shortname, fullpath=path, show_as_skill=show_as_skill) flash('Category "%s" successfully created.' % name) redir_params = '' if upper: redirect(u'/categories/{}/{}'.format(upper.trove_cat_id, redir_params)) else: redirect(u'/categories/{}'.format(redir_params))
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: path = name show_as_skill = True elif upper is None: flash('Invalid upper category.', "error") redirect('/categories') return else: path = upper.fullpath + " :: " + name show_as_skill = upper.show_as_skill newid = max( [el.trove_cat_id for el in M.TroveCategory.query.find()]) + 1 shortname = h.slugify(shortname or name)[1] 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('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: M.TroveCategory( trove_cat_id=newid, trove_parent_id=upper_id, fullname=name, shortname=shortname, fullpath=path, show_as_skill=show_as_skill) flash('Category "%s" successfully created.' % name) redir_params = '' if upper: redirect(u'/categories/{}/{}'.format(upper.trove_cat_id, redir_params)) else: redirect(u'/categories/{}'.format(redir_params))
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: path = name show_as_skill = True elif upper is None: flash('Invalid upper category.', "error") redirect('/categories') return else: path = upper.fullpath + " :: " + name show_as_skill = upper.show_as_skill newid = max( [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 oldcat: flash('Category "%s" with shortname "%s" already exists. Try a different, unique shortname' % (name, shortname), "error") else: category = 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") if upper: redirect(u'/categories/{}/?categoryname={}&shortname={}'.format(upper.trove_cat_id, name, shortname)) else: redirect(u'/categories/?categoryname={}&shortname={}'.format(name, shortname))