How to force Plone to restrict the creation of short names with spaces, underscores, capital case, etc.
Imagine a scenario where someone tries to give out a URL link to your Plone site. If they don’t remember or copy the link EXACTLY, it will not work. For example, http://www.mysite.com/Mike and http://www.mysite.com/mike are two different links as far as Plone is concerned. To the end user, it’s the same word.
By default, Plone 2.1.x does not allow the editing of short names. It simply creates it for you based on the title of your content object. Let’s say you want to create a new folder and you decide to give it the title of, “The Wonderful Life and Times of Mike Takahashi”. Plone will translate the short name to “the-wonderful-life-and-times-of-mike-takahashi”. Your URL would look something like this: http://www.mysite.com/wonderful-life-and-times-of-mike-takahashi. Also, you can see how this URL can get insane if you were to add a page with an even longer title within this folder.
So let’s say you choose to enable users to edit short names. This can and most likely will open up a whole can of worms. User will do all sorts of things like add spaces, underscores, and capital case to short names even if you tell them not to hundreds of times. And even when Plone gives the user a nice reminder under the Short Name field: “Should not contain spaces, underscores or mixed case. Short Name is part of the item's web address.”
So how can you force the user to make short names with better accessibility?
The key is to customize a Python script called “check_id” which is located in portal_skins/plone_scripts. Before an object is created, its short name (id) is passed to this script and it goes through a series of checks. By adding your own additional checks and return text, you can also get the nice UI feedback with the orange highlighted box if the short name fails one of your checks.
The checks can be very granular, so feel free to add whatever you need. In my site, I have added three checks to “check_id” for better accessibility:
1. Make sure the short name is always in all lower case
if not id.islower():
return 'For accessibility reasons \'%s\' must be in all lower case.' % id
2. Make sure the short name does not contain any spaces
if id.find(" ") > -1:
return '\'%s\' cannot contain any spaces.' % id
3. Make sure the short name does not contain any underscores
if id.find("_") > -1:
# copy_of_ is the only exception for use of underscores since
# this appears to be built into Zope's function manage_pasteObject()
if not id.find("copy_of_") > -1:
return 'For accessibility reasons \'%s\' cannot contain any underscores.' % id
