Main

January 25, 2007

Using TextIndexNG3 with Archetypes and automated testing

I'm currently working on the latest version of our Business Directory product for Business Link Kent (it should be going live on 8th Feb, in fact).

Because of the complex searching requirements plain old ZCTextIndex or TextIndex just aren't enough, and we've incorporated the very powerful TextIndexNG3 instead.

Unfortunately the installation documentation and so on doesn't really deal with implementing new indexes using the product. Nor does it show how to programatically convert the pre-existing indexes upon installation of your product; instead it is described as manual steps.

Manual steps do not play nice with either our automated testing strategy or the fact that we blow away and rebuild our development environments 3 or 4 times a day.

In your Install.py (yes, this is a Plone 2.1 site and no, I'm not using Generic Setup for this one) put the following function (I'm using stemming here, so don't copy this blindly if you don't want the effects of stemming):

Then call updateIndexes(out) from your install method and lo! your indexes are converted by your install script, rather than having to do it manually each time.

To implement new TextIndexNG3 indexes from within your Archetypes schema definition is very simple. I'm still debating whether I should be making use of the broader ability of TextIndexNG3 to search across a subset of the fields that it indexes (meaning that I might not need to create multiple TextIndexNG3 indexes), but for right now I'm going the simple route and defining them on the schema as I would for any other type of index.

Once you've installed TextIndexNG3 have a look at Products/TextIndexNG3/src/config.py. In there are all the parameters that you can pass at the point of index creation. To do this in Archetypes it's exactly the same as it would be for any other index; any non-default creation parameters should be comma separated after the index type is specified. For example:

So far the product looks very powerful, and it's definitely sorted all my complex searching requirements (ranges, booleans and so forth) that until now had me completely stumped.

January 08, 2007

Archetypes validation on the basis of the value of more than one field

In the general category of 'you learn something new every day'.... Today I'm back using Plone (it's been a while!). I'm trying to validate a particular field on the basis of the value of another. Specifically I'm trying to validate a postcode depending on the selected country (so use different validators depending on the country the user has chosen).

If you use Archetypes validators it initially appears as if you are passed the entire object as well as the new value (kwargs['instance']), but unfortunately the validator is called 3 times, and the first two calls have the changed value that you're trying to validate, but the unchanged object. What that means is if the user changed the postcode and the country in the same edit the validator would be using the wrong country on the first call, and raise an error.

Some digging around led me to this reference on the plone site which describes how to correctly validate across multiple fields. Instead of registering a validator as usual, define a pre_validation or post_validation method on your object, pull the values that you need from the REQUEST object, perform your validation as required and write any errors to the errors object. It's not quite as elegant as the general validation machinery, but it certainly does the job.

November 08, 2006

Caching Archetypes ImageFields with Squid

This is an issue that has cropped up on a couple of our sites. We generally only use Squid to cache images, javascript and css. Cached Javascript and CSS are not a problem in Plone, because the resource tools change the url of the javascript and css every time a new version is released. That way we can have extremely aggressive caching strategies for them.

Images are another matter however. We certainly don’t want to serve them from Zope if we can avoid it — but caching is a problem when the images are changed.

Enter CMFSquidTool. This patches the catalog reindexing hook to generate a sequence of PURGE commands to Squid, for the item being reindexed. This works great if you are using a single Archetypes object to store your content, but if you have anything like an ImageField it doesn’t work so well. You get a PURGE generated for your object, but not for it’s dependent images — which might be what changed.

So, I’ve pached CMFSquidTool to look for ImageFields in the object it’s purging, and generate additional PURGE commands for all images, and all of their different sized thumbnails. This works well, and solves what is otherwise a difficult issue.

I’d like to produce an adaptor-based solution for this - With an interface for objects and fields that says what purgable urls they have, if any, and adapters for the base Archetype object and it’s various fields. Does anyone know if anything like this exists already?

April 06, 2006

Altering an Archetypes validator after the schema has been defined

To change the validator for a field on an Archetypes schema after the schema has been defined is not as simple as it first looks.

Continue reading "Altering an Archetypes validator after the schema has been defined" »