plone quotator portlet
After setting up this website again, see my post "plone with eggs" I have one more itch. On the old website we used to show a random quotatione from a sql database full with quotes. In dutch we called that thing "citator" the english equivalent is more or less something like "quotator". The next challenge will be to reuse our mysql quotator database in this plone site.
Getting a random quote whenever I (re)opened our webpage was one of the things I liked a lot of our old page and which I could not find a standard product for in Plone. At least not a product that uses the MySQL database which holds our quotations.
Of course it would be much easyer now to just use a small java script from http://www.quotationspage.com/ but that would simply be too easy, wouldn't it? It would not be a nice challenge and it would not give use the opportunity to manage our own quotation collection.
In this first post about my new challenge I will explain the baby steps I have taken so far to start making this Plone Product or Portlet. Somehow i feel it is difficult to find a way to get started developing on Zope/Plone, maybe this post could help someone else later on to get started.
Finding a debugger
Every developer should become friends, or get married with a good debugger. So the first steps i took was finding a debugger for Plone.
I first tried to use the plonectl from my instance. Also I found the product Clouseau. It is equivalent to plonectl, only it is online, as a webpage. Giving the opportunity to do much neat things, like working online together.
Clouseau is a Zope and Plone debugger and introspector. It allows you to interact with Plone and alter it without being encumbered by security restrictions. It provides a Python prompt that is connected to the Zope database and contains all the objects from your Plone site. This is equivalent to a zopectl or PloneShell prompt. (Clouseau help page)
Getting Plone talk with SLQ
After getting some sleep this morning I continued still looking at the Clouseau command line on my test site. And I had no clue where or how to start. Maybe the title of this paragraph would better be getting me to talk to Plone.
One of the challenges is getting the python-mysql moduel installed. Using the buildout egg entry python-mysql did not work, it failed with strange unexpected errors. Installing the package libmysql++-dev was the way to go.
I won't go details about this issue because the description and solution can be found at: http://plone.org/support/forums/general#nabble-td333556
Now things are setup to start experimenting with python and sql, lets try to connect to the database. To try if you can connect, try something similar to the following in the plonectl or in the web interface of Clueseau
>>> import MySQLdb
>>> conn = MySQLdb.connect (host="localhost", user="me", db="test")
>>> cursor = conn.cursor ()
>>> cursor.execute ("SELECT VERSION()")
1L
>>> row = cursor.fetchone ()
>>> print "server version:", row[0]
server version: 5.0.32-Debian_7etch8-log
>>> cursor.close ()
>>> conn.close ()
Yes it worked, Ii could connect to my database.
Now try if it is possible to get one quote out of the quotes table. Lets start plonectl and give it a try, try to get quotation 2 (the first one is dutch)
bin/plonectl debug
...
>>> import MySQLdb
>>> conn = MySQLdb.connect (host="localhost", user="me", db="test")
>>> cursor = conn.cursor ()
>>> cursor.execute ("SELECT citaat FROM citaten where id=2")
1L
>>> row = cursor.fetchone ()
>>> print "Quotation 2: ", row[0]
Quotation 2: A wise man will make more opportunities than he wil make friends.
Parts needed
Following parts were used / needed for this:
- mysql-python
(zmysqlda)- libmysql++-dev (needed for mysql_config)
References
http://www.kitebird.com/articles/pydbapi.html

