Someplace nice

Plone.API

I recently attended Plone Conference 2012, in Arnhem, Holland. As usual the conference was great, with some excellent talks. One of the main talks that caught my attention, was the talk on plone.api. 

I had heard bits and pieces about an api for plone a couple of months prior, but had yet to look into it further. My initial reaction was, “why didn’t we think of this before?”. There are parts of plone and zope, that are quite inconsistent. Naming conventions change around quite a bit and imports can be from obscure parts of different eggs, so an api to simplify the main jobs that we do 80% of the time is a great idea.

I was lucky enough to be able to stay for the sprints, so I got involved in plone.api along with some other plone people. It’s still in the beta phase so there were a few functions that hadn’t been implemented yet, but by the end of the sprints, we’d filled in most of the gaps.

The great thing about this api, is it makes regular tasks like content creation, user manipulation and creating and manipulating groups really easy. Here is an example of how you would get the portal with plone.api:

from plone import api
portal = api.portal.get()

Which is so much easier to remember than either using the porta_url tool or the plone_portal_state multiadapter. Creating content is just as easy:

from plone import api
# First get the portal
portal = api.portal.get()

# Now we can create a document
doc= api.content.create(
type='Document',
title='My Content',
container=portal)

One last example, getting a user:

from plone import api
user = api.user.get(username='adam')

plone.api is just about in beta phase now, but it’s definitely worth checking out. You can read more here: read the docs

Plone “events”

Recently I was looking for a list of all events available in plone. By ‘events’ what I actually mean is:

Zope provides an events system. Various components (e.g the standard add and edit forms) notify any number of event subscribers (also known as event handlers) of a particular event.

So these are ‘events’ you can subscribe to and trigger custom code when they are fired. I wasn’t able to find a complete list so I’ve decided to compile one myself:

From zope.lifecycleevent.interfaces:

  • IObjectCreatedEvent
  • IObjectCopiedEvent
  • IObjectModifiedEvent

From Products.Archetypes.interfaces:

  • IObjectInitializedEvent
  • IWebDAVObjectInitializedEvent
  • IObjectEditedEvent
  • IWebDAVObjectEditedEvent
  • IEditBegunEvent
  • IEditCancelledEvent

From zope.app.container.interfaces:

  • IObjectMovedEvent
  • IObjectAddedEvent
  • IObjectRemovedEvent
  • IContainerModifiedEvent

From Products.CMFCore.interfaces:

  • IWorkflowActionEvent
  • IActionWillBeInvokedEvent
  • IActionRaisedExceptionEvent
  • IActionSucceededEvent

From Products.DCWorkflow.interfaces:

  • ITransitionEvent
  • IBeforeTransitionEvent
  • IAfterTransitionEvent

For more information visit the following links:

http://plone.org/products/dexterity/documentation/manual/five.grok/core-components/events

http://plone.org/documentation/kb/five-zope3-walkthrough/events

Plone limiting catalog search results

After some confusion today regarding the ‘sort_limit’ index in plone’s catalog, I discovered the following:

The sort_limit is only a hint for the search algorhitms and can potentially return a few more items, so it’s preferable to use both sort_limit and slicing simultaneously

Discovered this on the following page and it seems like a good resource for any question regarding catalog indexing in plone: querying-the-catalog

mr.developer svn certification issue

Mr.Developer is a good little tool to use in conjunction with buildout, but there is one problem I ran into:

red@byers:/home/zope/plone4Test/4.0$ ./bin/buildout 
mr.developer: Updating 'Products.PasswordResetTool' with subversion.
mr.developer: Updating 'Products.ATContentTypes' with subversion.
mr.developer: Updating 'Products.CMFEditions' with subversion.
mr.developer: Updating 'Products.Archetypes' with subversion.
mr.developer: Checking out 'kss.core' with subversion.
mr.developer: svn: OPTIONS of 'https://codespeak.net/svn/kukit/kss.core/trunk': Server certificate verification failed: issuer is not trusted (https://codespeak.net)
mr.developer: Updating 'Products.PlonePAS' with subversion.
mr.developer: Updating 'Products.TinyMCE' with subversion.
mr.developer: Updating 'plone.app.blob' with subversion.
mr.developer: Updating 'Plone' with subversion.
mr.developer: There have been errors, see messages above.

As of the writing of this post, this is an open issue in mr.developer. A quick and dirty work around is to just check out the code manually from svn, accept the certificate permanently then re-run buildout.

The Python getattr function

This one confused me for quite a while. After a bit of reading, I understood what the function did. It allows one to fetch an attribute from an object, using a string object or variable instead of an identifier. It basically goes like this:

class Blank: 
  def __init__(self):
    self.test_attr = 1


>>>t = Blank()

>>>t.test_attr
1

>>>t.test_attr_2
Traceback...
AttributeError: Blank instance has no attribute 'test_attr_2'

>>>getattr(t, 'test_attr')
1

>>>getattr(t, 'test_attr_2')
Traceback...
AttributeError: Blank instance has no attribute 'test_attr_2'

This all seemed to make sense, but what I couldn’t work out was why you would bother to use the getattr function when it appeared to give exactly the same results with just a little more typing. The answer, of course, is that you might not know the name of the attribute until runtime, so this can be accessed with a variable assigned when the code is run.

>>>getattr(object, variable_assigned_at_runtime)

Converting a month name from a string to an int

I was recently writing a python script where a function was passed a month name as a string and I wanted to get out the month number as an int based on that string.

I found quite a few posts about going the other way, but this is how I managed to do it:

import calendar

list(calendar.month_name).index(‘<insert month name here>’)

Phorm hits back

Phorm have hit back against all the bad press they’ve been getting with a new site:

http://www.stopphoulplay.com/

They are trying very very hard to make it seem as if all the bad press they are getting is completely unfounded and that certain people are out to bring their company down. Well, let’s imagine for a second that what they say is true and the Phorm system is completely annoymous, there are no records kept and they are not going to inject any code; even in this case, people aren’t going to be singing Phorm’s praises and saying how great they are because, truth be told, people hate online advertising. That’s the bottom line. People don’t want another way they can be advertised at. As Bill Hicks once said:

” By the way if anyone here is in advertising or marketing… kill yourself.”

Phorm

Phorm have come up quite a lot in the main stream media recently. It’s something I read up on a little while ago. In short they’re a company that specialise in targeted advertising based on deep packet inspection. It is not clear how Phorm will then deliver these targeted advertisments but as they will have servers at the ISP level, it is possible for the to redirect or even inject HTML or javaScript into the pages that are being delivered.

There are a few things you can do to stop Phorm:

  1. Use an ISP that doesn’t have any deal with Phorm. The three who are confirmed to at the moment are Virgin media, BT and talktalk.
  2. Email website-exclusion at webwise.com and have your own site excluded from their system.
  3. Spread the word. Join the facebook group, blog about it and generally let people know what is going on with their ISP.

simple cd commands that I didn’t know

Typing cd on its own will switch you back to your /home directory (i.e. the equivalent of cd ~). Typing cd - will switch you to the last directory you were browsing before you switched to the current one.

Archive