Zope
  Home arrow Zope arrow Page 2 - Plone Content Types With Archetypes
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ZOPE

Plone Content Types With Archetypes
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2006-02-28


    Table of Contents:
  • Plone Content Types With Archetypes
  • A Quotation Content Type
  • Mutators and Accessors
  • Validators

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Plone Content Types With Archetypes - A Quotation Content Type
    ( Page 2 of 4 )

    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.



     
     
    >>> More Zope Articles          >>> More By Peyton McCullough
     

       

    ZOPE ARTICLES

    - Creating Zope Products
    - Plone Content Types With Archetypes
    - Flat User Management in Zope
    - Creating Basic Zope Applications
    - Getting started with Zope for Linux and Sola...
    - ZPT Basics (part 4)
    - ZPT Basics (part 3)
    - ZPT Basics (part 2)
    - ZPT Basics (part 1)
    - Exception Handling In DTML
    - DTML Basics (part 4)
    - DTML Basics (part 3)
    - DTML Basics (part 2)
    - DTML Basics (part 1)
    - Using Zope With Apache





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT