Académique Documents
Professionnel Documents
Culture Documents
net/fairsource/courses/material/apiPlone_en
Introduction
This document describes how Plone's objects can be manipulated by using Plone/Zope's API.
Very often, we'd like to manipulate Plone's objects by using scripts (Python). We are going to
learn how to use Plone/Zope's API in order to be able to:
We call content objects to those objects that can be added through Plone's interface in order
to keep the portal's content: documents, images, links, folders, news and files.
The content objects can be manipulated by means of scripts written in Python. In other
words, content objects can be created, modified and deleted; its state can be modified, and
the information each object holds can be accessed.
First of all, let's create a content object through Plone's interface. Then, we're going to access
it from a script according to this steps:
Add a document (Page) with id my_document. This step is sightly different if we're
working under Plone 2.1.1 because the page's adding form only requests for the page's
title, in this case we write My Document (and the id gets automatically generated).
From ZMI, navigate trough your Plone Site's folder and go to portal_skins/custom.
There, add a script (Script (Python)) with the following content:
In the above example, we can see the use of getToolByName and getattr functions, and
getPortalObject, Title and CookedBody methods. Let's dig deeper:
This is done by modifying the script shown in example 1: we add a new sentence before the
print block:
document.edit(text_format="html",
text="<div><b>This is a new text...</b></div>")
Example 3: how to copy and paste content objects from a Python script.
1 of 5 7/13/10 1:39 PM
Plone API — ifPeople http://www.ifpeople.net/fairsource/courses/material/apiPlone_en
Create a folder with id my_folder. Remember that this process is a little different if
working with Plone 2.1.1 (you only have to provie the new folder's name).
Below, a script tha copies the recently created document my_document into the new
folder 'my_folder':
Example 5: how to create a document, a folder and an event from a Python script.
Here, a few explanations about the methods used in the above example:
2 of 5 7/13/10 1:39 PM
Plone API — ifPeople http://www.ifpeople.net/fairsource/courses/material/apiPlone_en
parameters that are passed to the object's constructor method. One of the most
common is title.
refreshCatalog(self, clear=0) This methods reindexes all objects found inside self.
Example 6: how to change the state of a private document from a Python script.
Plone provides a tool (Catalog), that can be queried about content objects inside a given
Portal.
In this section we're going to learn the searchResults method. First, the use of it is going to
be explained, and secondly, an example of its common usage is going to be shown.
The following script lists all documents in the current Portal ordered decreasingly by id. Please
note that portal_type restrictes the type of the content objects, and sort_on and sort_order
specify the requerid ordering.
results = context.portal_catalog.searchResults(sort_on="id",
portal_type="Document",
sort_order="reverse")
print [i.getObject().id for i in results]
return printed
Under Plone 2.1.1, if we'd want to order results by title, we should make the searchResults
call setting sort_on parameter to sortable_title.
Please also note that the method getObject is called for each result obtained from the
catalog. This is because the objects resulting of a catalog search are not content objects
themself. As you migth guessed, getObject method returns the real content object.
3 of 5 7/13/10 1:39 PM
Plone API — ifPeople http://www.ifpeople.net/fairsource/courses/material/apiPlone_en
Example 8: how to list all private objects that include texto word.
Each content object defines what part of its information is searchable. The parameter
SearchableText searches for that information in every object. The parameter review_state
filters objects according its state:
results = context.portal_catalog.searchResults(SearchableText="texto",
review_state="private")
print [i.getObject().Title() for i in results]
return printed
Example 9: how to list all objects with creation date after November, 30th, 2005 and with
creation user admin.
This scripts shows how a record object can be passed to searchResults method. The Date
parameter receives a record object to query for those objects created after a given date. The
Creator parameter filters the objects that weren't created by admin user:
results = context.portal_catalog.searchResults
(Date={"query": DateTime("2005/11/30"), "range": "min"},
Creator="admin")
print [i.getObject().Title() for i in results]
return printed
In this section we describe how to use the Plone's API to create users, groups, and how to
assign users to groups.
id = "user"
fullname = "Emanuel Sartor"
password = "changeme"
email = "emanuel@menttes.com"
roles = ("Manager",)
status=""
props = {"username": id,
"fullname": fullname,
"password": password,
"email": email}
# add a new member to the Portal
try:
context.portal_registration.addMember(id, password, roles,domains="",
properties=props)
status+="The user "+fullname+" was successfully added.\n"
except:
status+="The user "+fullname+" was not added.\n"
print status
return printed
More on RegistrationTool-class.html
groupname = "Group0"
status=""
# add a new group
try:
context.portal_groups.addGroup(groupname,)
status += "The group was successfully added.\n"
except:
status += "Manager group couldn't be added\n"
print status
return printed
More on GroupsTool-class.html
4 of 5 7/13/10 1:39 PM
Plone API — ifPeople http://www.ifpeople.net/fairsource/courses/material/apiPlone_en
id = "usuario"
groupname = "Group0"
status=""
# assign the user "user" to the group "groupname"
try:
group = context.portal_groups.getGroupById(groupname)
group.addMember(id)
status += "The user "+id+" was successfully added to "+groupname+".\n"
except:
status += "The user "+id+" was not added to group "+groupname+".\n"
print status
return printed
Exercices
Exercice 1:
Codify an script that receives two string arguments doc1 and doc2, and assume that this two
parameters correspond to two document identifiers in the current folder. Make the script
modify the content of doc2 so it'll hold the concatenation of doc1 and 'doc2's content.
Exercice 2:
Help:
5 of 5 7/13/10 1:39 PM