Creating Basic Zope Applications

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.

In this tutorial, we will be exploring the basics of creating Web applications in Zope. We will use Zope Page Templates and a bit of Python to create a primitive forum system. Yes, primitive. Note that what we create will not be the best in its category, and this is certainly not the only way to create a forum. With that stated, however, you will still end up with a fully functional application.

 

A basic understanding of Zope and Zope Page Templates will be useful in this article. Knowledge of acquisition might also help because we will use it to create our forum.

 

Let’s get started.

 

The Plan

 

Let’s try to keep our forum as simple as possible. When the user first accesses the forum, he or she will see a list of categories. The categories will be drawn from the folders contained in our forum’s main directory. So, for example, if I created a folder with an ID of test and a title of Category One, a category named “Category One” would appear on the index page.

 

When the user clicks on a category, he or she will be taken to a list of topics in the category. The topics, as with the categories, will be drawn from folders within the category’s folder. If no topics are present in the category, a message will state this fact. At the bottom of the topic index, there will be a form for adding new topics with fields for the subject, author and message of the topic.

 

Clicking on a topic will bring the user to a list of replies to the topic. Replies will be drawn from files within the topic’s folder. The contents of the file will be the reply’s message, and a property named author would be added to the file. A form for adding new replies will be at the bottom of the topic’s page, with fields for the author and messages of the reply.

 

For simplicity’s sake, users will not have to register in order to post. There will be no user database system present in our forum, nor will there be any bells and whistles, such as moderation or administration features. However, feel free to expand the forum system for extra practice.

 

Here’s a short example that shows our forum’s structure:

 

main folder

   category one

      topic one

      topic two

   category two

   category three

      topic one

      topic two

      topic three

 

{mospagebreak title=Categories}

 

All right, let’s get to work. Create a folder called “forum” with the title “My Forum.” This will be our forum’s main folder. All of our forum’s files should be created in our main folder. Create the folders 1, 2 and 3 with the titles Category One, Category Two and Category Three, respectively. These are our forum’s categories.

 

Next, we need to create an index page with a list of categories. Before we do that, however, let’s create a macro to define a basic layout. If you’re not too familiar with Zope Page Templates, a macro provides a common look and feel. Say you wanted every page in your site to have a header and footer, yet you need to update the style of the header and footer frequently. You could simplify this process by using a macro. Create a Zope Page Template named base and insert the following text:

 

<html metal:define-macro=’base’>

   <head>

      <title tal:content=’container/title’>Page Title</title>

   </head>

   <body>

      <h1 metal:define-slot=’title’>Title</h1>

      <span metal:define-slot=’content’>Content</span>

      <br />

      Copyright &copy; 2004 Your Website Name Here

   </body>

</html>

 

Our macro will allow us to replace “Title” and “Content” with the appropriate text.

 

Now we’ll create the list of categories. Since the separation of presentation and logic is encouraged in Zope, we will use both Zope Page Templates and Python to accomplish this. Create a Zope Page Template object named index_html and insert the following text:

 

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

   <h1 metal:fill-slot=’title’ tal:content=’container/title’>Title</h1>

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

      <span tal:replace=’structure container/getCategories’></span>

   </span>

</span>

 

Our macro, base, governs index_html‘s look.

 

As you can see, index_html calls the file getCategories. This will be a Python script that retrieves the list of categories and formats it. Create a Script ( Python ) Object named getCategories with the following text:

 

for category in container.objectValues():

   if category.meta_type == ‘Folder':

      print ‘<a href=’%s/cat’>%s</a><br />’ % ( category.getId(), category.getProperty ( ‘title’ ) )

return printed

 

If you are not familiar with Python, this script retrieves the objects in its folder. If the object is a folder, it treats it as a category and outputs a link to the file cat with the category’s folder as cat‘s context.

 

{mospagebreak title=Topics}

 

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>

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

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

      </span>

      <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’>

      </form>

   </span>

</span>

 

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>

   </span>

</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.

 

{mospagebreak title=Replies}

 

We’re now on the final part of our forum system: replies. As I said eariler, each reply will have its own file within the topic’s folder. Create a Zope Page Template object named topic in our forum’s main folder and insert this into it:

 

<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/getReplies’></span>

      <b>New Reply</b><br />

      <form method=’POST’ action=’newReply’>

         <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 Reply’>

      </form>

   </span>

</span>

 

This calls the file getReplies in the current context and then outputs a forum to add replies. The form sends its data to the file newReply. This is very similar to the cat file.

 

The next step is to create a Script ( Python ) object by the name of getReplies. Like the getTopics script, this handles the dirty work. It gathers the replies and formats them, quoting any HTML code and handling linebreaks.

 

from Products.PythonScripts.standard import html_quote, newline_to_br

for reply in context.objectValues():

   print ‘<b>%s</b><br />’ % ( reply.getProperty ( ‘author’ ) )

   print ‘%s<br /><br />’ % ( newline_to_br ( html_quote ( reply ) ) )

return printed

 

Finally, we need to create the files required to add new replies. Create a Zope Page Template object named newReply.

 

<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/addReply’></span>

   </span>

</span>

 

The only thing the page does worth noting is that it calls the file addReply in the current context. Create a Script ( Python ) object named addReply. Fill it with the following code:

 

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

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

replyObject = getattr ( context, id )

replyObject.manage_addProperty ( ‘author’, context.REQUEST.author, ‘string’ )

return ‘Your reply has been added.<br />’

 

The script creates a new file in the topic’s folder, fills it with the reply and adds an author property, giving it the appropriate value.

 

Conclusion

 

Your forum is now fully functional! Go ahead and test it out. Notice how easy it was to create the forum. This illustrates how easy Zope makes the creation of Web applications.

 

Feel free to expand the forum for extra practice. Now that you understand the basics of creating applications in Zope, you should find it quite easy. Try adding a user database or moderation and administration functions.

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

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort