Home arrow Zope arrow Page 3 - Creating Basic Zope Applications

Topics - Zope

While some people immediately understand the usefulness of Zope, others don't understand how to apply their skills. In this article, Peyton McCullough explains how to apply the skills to create a simple forum.

  1. Creating Basic Zope Applications
  2. Categories
  3. Topics
  4. Replies
By: Peyton McCullough
Rating: starstarstarstarstar / 19
January 25, 2005

print this article




Let's now create a list of topics in a given category. As you saw in the previous script, we need to create a file named cat. Go ahead and create a Zope Page Template named cat. Insert the following text into it:


<span metal:use-macro='container/base/macros/base'>

   <h1 metal:fill-slot='title' tal:content='here/title'>Title</h1>

   <span metal:fill-slot='content'>

      <span tal:condition='here/objectValues'>

         <span tal:replace='structure context/getTopics'></span>


      <span tal:condition='not:here/objectValues'>

         Sorry, this category contains no topics.<br />


      <br />

      <b>New Topic</b><br />

      <form method='POST' action='newTopic'>

         <label>Subject:</label><br />

         <input type='text' name='subject'><br />

         <label>Author:</label><br />

         <input type='text' name='author'><br />

         <label>Message:</label><br />

         <textarea name='message' rows='10' cols='40'></textarea><br />

         <input type='submit' value='Add Topic'>





Again, we use the macro to make our page's layout match the layouts of the other files. Feel free to edit the macro and experiment with different styles.


We use a conditional expression to determine whether the category contains any topics. If it does, we call the file getTopics in the current context. If the folder contains no objects, we display a message which explains that there are no topics available. Finally, we create a form that allows us to add new topics. The form sends its data to a file called newTopic.


Create a Script ( Python ) object with the name of getTopics. This script will create a list of topics and format them. Insert the following script into it:


for topic in context.objectValues():

   if topic.meta_type == 'Folder':

      print '<a href=\'%s/topic\'>%s</a>' % ( topic.getId(), topic.getProperty ( 'title' ) )

      print '<br />By %s<br />' % ( topic.getProperty ( 'author' ) )

return printed


This script is very similar to the last script. Notice how we retrieve an author property, however.


We'll now make the file newTopic. Create a Zope Page Template named newTopic and insert the following code:


<span metal:use-macro='container/base/macros/base'>

   <h1 metal:fill-slot='title' tal:content='context/title'>Title</h1>

   <span metal:fill-slot='content'>

      <span tal:replace='structure context/addTopic'></span>




We don't do anything amazing, as you can see. We only call the file addTopic in the current context. Create a Script ( Python ) named addTopic. It will do the real work. Insert the following text:


id = str ( len ( context.objectIds() ) + 1 )

context.manage_addProduct [ 'OFSP' ].manage_addFolder ( id, title = context.REQUEST.subject )

replyFolder = getattr ( context, id )

replyFolder.manage_addProperty ( 'author', context.REQUEST.author, 'string' )

replyFolder.manage_addProduct [ 'OFSP' ].manage_addFile ( '1', file = context.REQUEST.message )

replyObject = getattr ( replyFolder, '1' )

replyObject.manage_addProperty ( 'author', context.REQUEST.author, 'string' )

return 'Your topic has been added.<br />'


Although the code may look complicated, its function is pretty simple. First, it counts the number of topics currently in the category. For simplicity's sake, the topic folder Ids will just be numbers. The first topic will be 1, the second 2, the third 3 and so on.


We then create a folder for the topic with the appropriate title. Next, we add an author property, and, finally, we create the first reply and add its author property.


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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

Developer Shed Affiliates


Dev Shed Tutorial Topics: