Plone Content Types With Archetypes

Plone is an excellent content management system. You will probably find everything you need in the system itself, or from third party content types. If you don’t, however, it is very easy to create your own content types, as this article will explain.

Introduction

Plone is a fantastic content management system. Out of the box, it contains a number of useful features, and managing content is stunningly easy. A number of third-party utilities also exist that can be used to expand the capabilities of a Plone site.

However, each website has its own unique needs. While there’s a good chance that one of Plone’s many third-party content types will meet your needs, there’s also a chance that you won’t be able to find exactly what you want. In this case, you are forced to create your own content types. This sounds like quite a task, but, thankfully, it really isn’t. Creating content types in Plone can actually be extremely easy, and the tool that makes it all possible is Archetypes.

In this article, we’ll take a look at using Archetypes to create a content type for Plone. As I said, this can be done very easily, and only minimal knowledge of Python is needed to create something simple.

Archetypes Overview

The way Archetypes works is incredibly simple, which enables you as a developer to easily add new content types made for specific purposes. Say you wanted to build a collection of quotations which interest you. Now, you could just shove them into separate files and format them as best you could. However, this would be very hard to maintain, especially if you wanted to make a universal change to things.

This is where Archetypes comes in. The quotation pages can be broken down into basic fields. Here, there would be a field for the speaker of the quotation and a field for the quotation itself. Both would be represented as basic text. To implement these fields in Archetypes, you would create something called a schema defining the two fields. The schema would then be shoved into a Python script with a few more instructions. Next, you’d create a few more files with more information on the content type as well as instructions telling Plone how to install it. Finally, you’d simply install your new content type. Archetypes would generate the look of the page as well as a management interface based on the data in the Python script. That’s all there is to something as simple as this example. As you can tell, Archetypes is an extremely powerful tool that can handle a lot of work for you.

{mospagebreak title=A Quotation Content Type}

We’ll start with the example I described in the previous section, a simple quotation content type. When an object based off the content type is added, text fields for the speaker of the quotation and the quotation will be presented. After they are filled out and the form is submitted, the object will be created. When the object is viewed, it will display the speaker and the quotation.

Create a folder called Quotation in the Products directory. This is where we’ll store our content type’s files. The first file we will create is config.py. This defines some constants that we’ll use throughout our content type:

from Products.CMFCore.CMFCorePermissions import AddPortalContent

ADD_CONTENT_PERMISSION = AddPortalContent
GLOBALS = globals()
PROJECTNAME = “Quotation”

In config.py, we first define the permission required to manipulate our content type. Since our content type really isn’t anything special, we just assign it to the generic AddPortalContent permission. We then define a constant called GLOBALS, which is used during the installation of our content type. Finally, we give our content type (or project/product, rather, since it’s possible to have multiple content types in the same package) a name.

Next, we’ll have to create our content type’s schema, which will define the fields associated with the content type. When we edit quotations, we’ll want a text field for the speaker and a larger text area for the quotation itself. All of this is specified in the schema:

from Products.Archetypes.public import *
from Products.Quotation.config import *

schema = BaseSchema + Schema((
   TextField(‘speaker’, required = True),
   TextField(‘quotation’, required = True, widget = TextAreaWidget)
   ))

class Quotation(BaseContent):
   “A simple quotation content type.”
   schema = schema

registerType(Quotation, PROJECTNAME)

The first thing we do is create our content type’s schema. We create two text fields, one for the speaker and one for the quotation. Both are required to be filled out. The second one is also set to a text area. Next, we create the content type’s class, where we simply copy the schema, and, finally, we register the content type.

Next is __init__.py, where we glue what we’ve done so far together:

from config import *
from Products.Archetypes import process_types, listTypes
from Products.CMFCore import utils

def initialize(context):
   import Quotation
   content_types, constructors, ftis = process_types(listTypes
(PROJECTNAME), PROJECTNAME)
   utils.ContentInit(PROJECTNAME, content_types = content_types,
permission = ADD_CONTENT_PERMISSION,
                     extra_constructors = constructors, fti =
ftis).initialize(context)

Besides importing our configuration file and various methods that we’ll be using, the first thing we do is define a function and import our Quotation.py file. From there, we get some information about our package, the content types associated with it, constructors and factory type information, in that order. We then create a utils.ContentInit with this information, along with the package name and the permission we defined in config.py.

Finally, we have to create Install.py, which is responsible for installing our product. Create a folder called Extensions inside Quotation to place the install script in:

from Products.Archetypes.Extensions.utils import installTypes
from Products.Archetypes.public import listTypes
from Products.Quotation.config import PROJECTNAME, GLOBALS
import StringIO

def install(self):
   out = StringIO.StringIO()
   installTypes(self, out, listTypes(PROJECTNAME), PROJECTNAME)
   out.write(“Installed: ” + PROJECTNAME)
   return out.getvalue()

This simply installs our content type and inserts a short message into the installation log.

Our Quotation content type is now complete. Restart Zope and then install the product in the “Add/Remove Products” section of Plone. You should now be able to add an object based off our content type to a folder.

You can also create the files version.txt and README.txt. The contents of the former file will be added to the product’s name, and the contents of the latter file will be displayed as the product’s description.

{mospagebreak title=Mutators and Accessors}

Archetypes enables you to manipulate the values of fields. When you manipulate the value of a field when it is set, you are using a mutator. When you manipulate the value of a field when it is requested, you are using an accessor. Both mutators and accessors are simple to create and use in content types.

Let’s take another look at our Quotation product above. Let’s say we want to modify the field for the quotation. When the user sets the value, we’ll put quotation marks around everything. To do this, we simply create a method called setQuotation. The method will be automatically called when the value of the field is set. The method goes into our Quotation class:

class Quotation(BaseContent):
   …
   def setQuotation(self, value):
      value = ‘”‘ + value + ‘”‘
      self.getField(‘quotation’).set(self, value)

Restart Zope and install the product again. Add a quotation and look at the result—the value of the quotation field now has quotation marks around it. However, our system has a serious flaw. Click the “edit” tab and notice how the value of the text area has quotation marks around it. Save it and look again. There are now two sets of quotation marks, which is no good. While we could modify our mutator to work around this, the easiest way to fix this is to use an accessor instead.

Accessors, too, are methods, but they don’t have any sort of value argument. Here’s an accessor that does what we want:

class Quotation(BaseContent):
   …

   def getQuotation(self):
      return ‘”‘ + self.getField(‘quotation’).get(self) + ‘”‘

Remember to delete the mutator we defined above in order for our content type to function properly. Restart Zope and reinstall our product. Create a quotation, or edit one, and save it. If quotation marks are left over from our mutator, make sure to delete them. You should now see quotation marks wrapped around your quotation. Notice that they do not appear when editing.

{mospagebreak title=Validators}

A problem with our Quotation product is that people can enter anything they want when creating a new object. For example, I could set the speaker of the quotation to “Benj4min Frankl1n”, which is unrealistic. Because of this, it might be a good idea to restrict what the user can enter as the speaker.

We can restrict what a use can enter by using things called validators. When a user submits data, any validators attached to that field are run. They look at the information submitted and make sure that everything is valid. If it is, the data is accepted. If it’s not valid, then an error is returned.

There is more than one way to create a validator, but, in my opinion, the easiest is to create one out of the RegexValidator class. It accepts a regular expression, and it matches the value of any associated fields with the regular expression. In our example, we want to make sure that there are no numbers in the speaker’s name. Add this code to your __init__.py file:

def initialize(context):
   from Products.validation.validators.RegexValidator import
RegexValidator
   from Products.validation import validation
   validation.register(RegexValidator(‘isValidSpeaker’,
r’AD*Z’, errmsg = ‘ contains irregular characters.’))

   …

This registers our validator under the name of “isValidSpeaker” so that we can attach it to any field we’d like. Note that the registration of our validator must come before everything else in initialize. Modify the schema variable in Quotation.py and assign our validator to the field containing the speaker’s name:

schema = BaseSchema + Schema((
   TextField(‘speaker’, required = True, validators =
['isValidSpeaker']),
   TextField(‘quotation’, required = True, widget =
TextAreaWidget)
   ))

Restart Zope and reinstall the product. Try to create a quotation with numbers in the speaker’s name and examine what happens.

Conclusion

What’s covered in this article is not, of course, all there is to Archetypes. Archetypes contains a lot more features—fields, widgets, built-in validators and many more interesting things. However, from what’s been covered, I think it’s pretty safe to conclude quite a few things about Archetypes.

Archetypes is a utility that allows developers to create Plone products very easily. A schema is created and put into a class. The schema contains fields which content types are built around. A __init__.py file is then created, along with an installation script. Archetypes then does its magic, generating interfaces for modifying and viewing objects. Using this process, a developer can create a simple content type with just a few lines of Python code.

[gp-comments width="770" linklove="off" ]

chat