Zope
  Home arrow Zope arrow Page 3 - 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 - Mutators and Accessors
    ( Page 3 of 4 )

    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.



     
     
    >>> 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 6 Hosted by Hostway
    Stay green...Green IT