January 28, 2020
Hot Topics:

Modeling Content Types with Schemata

  • March 21, 2007
  • By Sean Kelly
  • Send Email »
  • More Articles »

This second schema, classifierSchema, has three fields in it:

  1. The first field, runningTime, is an optional integer field that tells how long the movie on the DVD lasts. It's optional because the required flag is set to False. (You also could just leave out the required flag because False is the default.)
  2. The second field, rating, tells the rating assigned by the Motion Picture Association of America. (You can substitute whatever other rating scheme you prefer.) This is a string-based field that's also optional. It's also indexed for quick lookup by setting the index attribute. The vocabulary attribute lists the valid values users may enter for this field, in this case, the MPAA rating codes. You also listed the widget for this field as a SelectionWidget. This kind of widget displays either radio buttons or a drop-down menu (depending on how many values are in the vocabulary), enabling the user to select one and only one value.
  3. The last field, genre, is a required field. Like rating, it's indexed and it uses a controlled vocabulary for values. Unlike rating, it also includes the multiValued flag, set to True, meaning that movies may belong to more than one genre. In addition, the widget for this field is the MultiSelectionWidget that displays either checkboxes or a multi-select list. You added the format attribute to force the widget to use checkboxes (mostly because I don't like multi-select lists).

And now you're done defining the fields of a DVD! Wait, what about the title of the movie? Well, why define your own title field when you can reuse an existing schema that already provides it? Add these lines to DVD.py:

from Products.Archetypes.public import BaseSchema
dvdSchema = BaseSchema.copy() + idSchema + classifierSchema

The schema called BaseSchema comes with Archetypes; it provides several fields, such as title, description, subject keywords, last modification date, and so forth—all of which could be useful for your DVD content type. You then just "add 'em up": the dvdSchema is a copy of the BaseSchema plus the idSchema plus the classifierSchema.

You're now the proud owner of a complete schema. But, although Archetypes letsyou define schemata, Plone, its underlying application server, and its object database deal with objects, not schemata. And, before Plone can make objects, you need to provide a class definition for those objects. Luckily, Archetypes helps out again by providing base classes from which you can inherit all the necessary behavior. Add these lines to DVD.py:

from Products.Archetypes.public import BaseContent
class DVD(BaseContent):
   schema = dvdSchema
   archetype_name = portal_type = meta_type = 'DVD'
   typeDescription = "A DVD object captures a DVD's ID number,
                      title, genre, and other attributes."
   content_icon = 'dvd.gif'

This block of code defines a class called DVD, whose schema is the dvdSchema. It also tells Archetypes and other parts of Plone and Zope the name to use ("DVD", in thise case), plus some descriptive "help text" explaining what the DVD class does. Lastly, it tells what icon to use on-screen to represent DVD objects, in this case dvd.gif. Where do you get the DVD icon dvd.gif? Help yourself to this one: . Just save it as a file to the DVDCollection subdirectory under the skins subdirectory (all user-interface elements go under skins).

The coding's complete! You should now have the following files and directories:

Activating and Trying the Product

You're entering the home stretch now. All that remains is to install the product into Plone and give it a try. You see, merely having the DVDCollection directory in the Products directory isn't quite enough. You've got to tell Plone to make that product active. You do so through the web with a browser.

Start up your Plone instance using the same technique as in the last article. Log in with your administrator account and click on the "site setup" link at the top. Then, click on "Add/Remove Products". You should see "DVDCollection" appear in the list of installable products. Check the box next to it, and click the Install button to make it active:

The DVD content type is now ready to use. By default, objects of the DVD type may be added anywhere, although you can control that with other features of Archetypes. For now, try out creating a few DVDs in a convenient place. Where's a convenient place? Your account's home folder is always nearby; just click "my folder" in the blue personal bar to jump there.

Once there, click the green "add item" menu and choose "dvd" from the list:

You'll have a form you can fill in to create a DVD object. Fill in the fields and press Save.

Congratulations! You now have a new DVD object in your Plone site. You can search for this DVD by using Plone's LiveSearch or advanced search, see it in folder listings, and update its items. Archetypes applies validation to the data you enter (for example, by displaying an error if you enter a non-integer for the DVD number field) and also generates the edit form and view web pages for DVDs. Archetypes and Plone take care of saving this object to the Zope application server's object database, and cataloging its fields in the Plone catalog for quick searches and other uses. You can cut and paste DVD objects between folders and have DVDs appear in Plone's "smart folders." Plone also applies security and workflow to the content type.

Admittedly, this isn't much of a web application: all you can do is create, retrieve, update, and delete DVD objects. But, building content types serves as the foundation for web applications—and you'll use this DVD content type in a future article. See you next month!

About the Author

Sean Kelly is a software consultant to the media, medical, and aerospace industries. He spends much of his time modeling the real world in terms of content types, their workflows, and their interactions with each other. Apparently, he's not been keeping up with his medication. He lives in a "house" object with his "wife" and "daughter" objects in an undisclosed object store.

Page 2 of 2

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date