Skip to main content

A simple demo application using collective.trajectory

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!
Post a Comment

Popular posts from this blog

Consolidated community site infrastructure on Plone

In Inigo, we believe in helping out local FOSS communities and help them grow. We help out in community events where we can, present FOSS talks, and provide some platforms for local communities to grow. One of such platform is our consolidated community site infrastructure on Plone.

The system/infra and its components was originally developed for the Fedora Malaysia website, while keeping in mind to keep it generic enough so that other communities could use the same components for their own community sites. The infra is already at a usable state, and we can add new sites easily with just a few clicks.

Features in this consolidated infra are:
Document/Content management (Plone built-in)Calendar system (powered by solgema.fullcalendar addon)Conference/BarCamp system (powered by collective.conference addon, which was developed for FUDCon Kuala Lumpur 2012)Blog (powered by Products.Scrawl)Simple yet powerful theming engine (powered by plone.app.theming/ Diazo) - Check out Diazo, you'l…

HOWTO: Mirroring Yum repositories using Yum-Utils

As promised before in one of my previous post, a Howto on how to mirror and manage yum repositories using some of the utilities in yum-utils.

The first step is, well, of course, is to get yum-utils from fedora repository
yum install yum-utils

Reposync
Reposync is a utility for mirroring and synchronizing local copy of a yum/rpmmetadata repository.

This utility is very useful if you wanted to make a yum repository mirror. Before this, I used "wget -R -np -N" but this method is a little bit tedious and it doesnt work with repos that didn't use directory listing. Plus, it also download together additional site stuff that I don't need/want and it doesn't verify checksum of the downloaded packages.

Mirroring a repo using this utility is easy, just execute this command
reposync -r <repoid> -a <arch> -n
and the repo will be mirrored in a folder with the same name of the repoid in the directory you executed the command. Eg: you executed the command in /mnt/storage/mi…

Adding simple popup to Plone frontpage

Here is a little guide for those who want to add a simple popup to the Plone frontpage for some purpose (eg: announcements, advertisements, etc).

Create a basic html file containing the content you want to appear in the popup. Upload it into $PLONE_SITE/portal_skins/custom (as Page Template) and for the sake of this example, name it popup.html

Afterward, create a Javascript file with your Pop-Up loader script. For example , this script:


function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=220,height=400,scrollbars=no');
return false;
};

popup('popup.html', 'My Popup');


Also upload this file into $PLONE_SITE/portal_skins/custom (as Page Template too). For this example, name it as popup.js

Afterward, in $PLONE_SITE/portal_javascripts , add popup.js as a new script into portal_javascripts…