Zope
  Home arrow Zope arrow Page 4 - Plone Content Types With Archetypes
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Plone Content Types With Archetypes - Validators
    (Page 4 of 4 )

    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'\A\D*\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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello,Archetypes is an extremely simple tool, as this article illustrates. Using...
       · Should it be in Quotation.py?
       · Yes, it should be. Thanks for pointing that missing bit of information out to...
       · Thanks for a very simple into to archetypes (the ones on plone.org semmed too...
       · i created a folder named Quotation and did as the text said but i am not able to c...
       · The accessor and mutator described on page 3 of this tutorial need to be declared in...
     

       

    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-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway