old blog,

A simple demo application using collective.trajectory

Izhar Firdaus Izhar Firdaus Follow Support Oct 04, 2011 · 3 mins read
A simple demo application using collective.trajectory
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.
Last week I wrote about a Plone library called collective.trajectory which allows you to do url routing on Plone content type. Back then it was only able to route the context, but I have yet to try develop something on it yet.

A few days ago on geekcamp.my, I gave a talk on it, so naturally, I'll need to test to develop something on it first before doing the talk. Using trajectory, I hacked together a simple app, which fetch a profile page from Facebook, to demonstrate the library capabilities. Here's the howto.

Goals

Create a content type which can be added on any part of site, where if user load a subpath of the content, it will load a profile info from graph.facebook.com, and serve it as a context in Plone.

Creating an archetype project

First you'll need ZopeSkel in your buildout. Add this section into buildout.cfg:

[paster]
recipe = zc.recipe.egg
eggs =
ZopeSkel
PasteScript

Execute ./bin/buildout install paster afterwards. Once thats done, you should have paster command in your bin directory.

Create an archetype product using ZopeSkel.

./bin/paster create -t archetype 
Selected and implied templates:
ZopeSkel#basic_namespace A basic Python project with a namespace package
ZopeSkel#plone A project for Plone products
ZopeSkel#archetype A Plone project that uses Archetypes content types
Enter project name: example.trajectory
.....

That should create a directory called example.trajectory in the current directory. Now, lets create a simple archetype content.

cd example.trajectory
../bin/paster addcontent contenttype
Enter contenttype_name (Content type name ) ['Example Type']: FacebookProfiles
Enter contenttype_description (Content type description ) ['Description of the Example Type']: An
application which fetch facebook profile from graph.facebook.com
Enter folderish (True/False: Content type is Folderish ) [False]:

Notice that Folderish is False. We doesn't need the content to be folderish here.

Hooking the archetype product to Plone

After creating the archetype product above, we'll need to hook it in Plone.

In your buildout.cfg, add these:

[buildout]
eggs =
...
collective.trajectory
example.trajectory
...

zcml =
...
collective.trajectory
example.trajectory
...

develop =
...
./example.trajectory
...

Run ./bin/buildout and at this point, you should have the base we need for this demo app. If you start the Plone server you should see the product installable, and you can add the content after installing the product. Now for the fun stuff.

Registering trajectory traverser to contenttype

Edit example.trajectory/example/trajectory/configure.zcml , and add this:

<adapter factory="collective.trajectory.components.Traverser"
for="example.trajectory.content.facebookprofiles.FacebookProfiles
zope.publisher.interfaces.IRequest"/>

Registering routes and factories

Edit example.trajectory/example/trajectory/content/facebookprofiles.py, and you can register the routes directly there.

...
import traject
from collective.trajectory import getApp, Model
from zope.globalrequest import getRequest
import json, urllib
...
class FacebookProfiles(base.ATCTContent):
...

class Profile(Model):
def __init__(self, id, data):
self.id = id
self.data = data

def factory(profile_id):
data = urllib.urlopen('http://graph.facebook.com/%s' % profile_id).read()
data = json.loads(data)
return Profile(profile_id, data)

def arguments(obj):
return {
'profile_id': obj.id
}

traject.register(FacebookProfiles, 'profile/:profile_id', factory)
traject.register_inverse(FacebookProfiles, Profile,
'profile/:profile_id', arguments)

Done, now when you access http://site/path/to/facebookprofilesapp/profile/somefacebookid, you should get a Profile object.

Now we need to register a simple view for the profile

Registering a default view

Create example.trajectory/example/trajectory/browser/facebookprofiles.py and fill it with this:

from Products.Five import BrowserView

class ProfileView(BrowserView):
pass

Edit example.trajectory/example/trajectory/browser/configure.zcml and add this:

<browser:page
name="index"
for="example.trajectory.content.facebookprofiles.Profile"
template="profileview.pt"
permission="zope2.View"
class=".facebookprofiles.ProfileView"/>

Create example.trajectory/example/trajectory/browser/profileview.pt and fill it with this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
metal:use-macro="context/main_template/macros/master">
<head>
</head>
<body>
<div metal:fill-slot="content">
<h1 tal:content="context/data/username"/>
<dl>
<dt>Fullname</dt>
<dd tal:content="context/data/name"></dd>
<dt>Facebook Page</dt>
<dd><a href="#" tal:attributes="href context/data/link" tal:content="here/data/link"></a></dd>
</dl>
</div>
</body>
</html>

Thats it, if you add the FacebookProfiles content into your plone site, take for example http://site/myfolder/fbprofiles , when you load http://site/myfolder/fbprofiles/profile/yourfacebookid you should get a page with user 'yourfacebookid' details

I've uploaded the example.trajectory demo into https://svn.plone.org/svn/collective/example.trajectory/trunk/ so that you can try it and refer to it.

Happy hacking!
Written by Izhar Firdaus Follow Support
I'm a system architect, data engineer and developer advocate with passion in Free / Open Source software, entrepreneurship, community building, education and martial art. I take enjoyment in bridging and bringing together different FOSS technologies to help businesses and organizations utilize IT infrastructure to aid and optimize their business and organizational process.

Email server with Postfix, Dovecot, and LDAP

I think I'll skip the introduction as this is mainly a note for myselfThe LDAP SetupI'm not going to cover how to setup LDAP. The set...

In old blog, Oct 03, 2011

« Previous Post

Opps ..

Opps , to Fedora Planet, sorry that some old posts appeared in frontpage .. I changed my feed to aggregate the PlanetFedora tag inste...

In old blog, Oct 07, 2011

Next Post »