Automatic organisation of your news folder
by
antonh
—
last modified
07-Sep-06 11:48 AM
I posted a recipe on "ZopeLabs":http://www.zopelabs.com/ a while back which allowed for the creation of a 'Post News' action which would automatically create a News Item in the current folder and take you straight to the metadata_edit_form.
But now I've improved it to group these created news items by monthly folders...
Have a look at the URL for this news item (the first created in this way). The script checks to see whether there is a news folder. If not it creates it. Then it checks to see whether there is a folder for the current month. If not, it creates it. Finally we create the News Item in the appropriate folder, and redirect to its metadata_edit_form.
post_article.py:
#create a news item
news_folder = 'news'
article_id = str(DateTime().strftime('%d%H%M'))
current_month = str(DateTime().strftime('%b%Y'))
portal_root = context.portal_url.getPortalObject()
# make sure we have the news folder object
if hasattr(portal_root, news_folder):
news_object = getattr(portal_root, news_folder)
else:
news_object = portal_root.invokeFactory('Folder',news_folder)
#now get the folder for the current month
if hasattr(news_object, current_month):
current_news = getattr(news_object, current_month)
else:
news_object.invokeFactory('Folder',current_month)
if hasattr(news_object, current_month):
current_news = getattr(news_object, current_month)
else:
print "Unable to create folder %s in %s folder!" % (current_month, news_folder)
current_news.invokeFactory('News Item', article_id)
o = getattr(current_news, article_id)
destination = o.absolute_url() + '/metadata_edit_form'
context.REQUEST.RESPONSE.redirect(destination)
return printed
Note:
The solution is not scalable. The main problem is the absolute URL is ${portal_url}/news/MonYYYY/DDHHMM. So you can only create one uniquely identifiable news item per minute. If you were running a large site, this wouldn't be enough. Even adding in seconds/milliseconds might not be enough. The solution (as implemented in Plone) is to append a random number to the end. The before creating the item, you check to see whether an item with that URL already exists. If not, you regenerate the number and try again (repeat ad nauseum). Its a very ethernet approach :)
Where I got the ideas...
ZClip! like function
Move object in DCWorkflow