Wednesday, September 8, 2010

Adding a list of items to a content type without CompoundField

With all the Plone 4 goodness that surrounds us, you'd almost forget that there still are Plone 3 sites which need attention and extra features.

Our client wanted "be able to add a list of images to item X", for display in a slideshow on the item's view. I first thought of the CompoundField add-on for Archetypes content, which I'd used before. But because of technical difficulties with the current Archetypes release, I went looking for another way.

I found it in Plone's Collections feature: We add a reference to a Collection (code below), and let editors decide what images they want to show there. Advantages:
* It's easier to change a lot of images
* It allows for re-use of images
* It allows for dynamic content (if you add a criterion for a certain tag, for example, all new content with that tag will be "on item X" as well)
* Last but not least: It's a well-known, well supported Plone feature.

The last point is important: This approach fits the philosophy of letting standard Plone functionality take care of things, avoiding custom content as much as possible, or keeping them as minimal as possible when you do use them.

There might also be a disadvantage: it's less intuitive. But everybody should know about Collections, they're central to Plone and they can help you (and your customer) in many ways.


custom_content_type_schema = Schema ((
(...)
ReferenceField(
name='slideshowImagesCollection',
widget=ReferenceBrowserWidget(
label="Extra images for slideshow",
description="Point to a Collection that contains Images",
startup_directory="/",
),
relationship="imagesForSlideshow",
allowed_types=('Topic',),
(...)

Thursday, September 2, 2010

Creating an egg? Clean first!

I ran into an error on a production site which turned out to be due to an egg containing extra files. How did these old files get included in the egg? I'd been running python setup.py bdist_egg to create the egg, and then i uploaded them to a www server. (Not a "real" egg server.)

Before creating a new egg, the build/ directory is not cleaned by default. Therefore, files that were present in an earlier build but have now been removed from the package will still be included in the egg.

Running python setup.py clean remedies this.