Converting Plone to Django
Tue, 22 Apr 08, 16:04:55
filed in
Django
I've got quite a few Plone sites floating around. And quite a lot of those sites I end up maintaining and I don't really want to, I'd really like it if someone else could. Further those sites aren't really suited to Plone. They are more external brochure ware sites. Mostly static with a small number of editors and no looking at the Plone interface at all. For those sites, Plone is a fine choice, but it's really not its main strength.
So after talking about how much I like Django I thought it was time to start actually doing something. I took one of our Plone sites and said: can I convert this to Django in a weekend? And the answer is simply no. I was hampered by sucking at coding and the family wanting attention. After a few nights of messing around I thought it was time to kick something out and see if I can get on to the next few things in my rather long queue.
First off, converting Plone to Django is a hopeless task, Plone is so big and has so many features that this is a very, very, very big project. All I was initially curious in was my site, could I convert over some content types, pull the data over, set up the URL's and put the views up. This would not mean doing things like security, i18n, users, properties, topics, gosh anything useful at all. So with that caveat, here goes.
Django needs some models, rather than hard code them, how about a little conversion script that goes and reads your Archetypes schema and outputs a Django model. I picked a few fields I really wanted and pulled out some key fields into a metadata (such as effective date), tell it the models you want and you'd get:
class Document(models.Model):
uid = models.CharField(max_length=255, help_text="Should not [snip]")
title = models.CharField(max_length=255, blank=False)
description = models.TextField(help_text="A short summary [snip]", blank=True)
text = models.TextField(blank=False)
...
Because things like reference fields or lists are unclear, it just skips over those. All possible, but a little too much for the moment.
So you'll get a project and app which I called plango with a bunch of models. Next we want to run through the database and pull out the object and write them into Django, that's a matter importing Django inside a zopectl run script. The magic code to pull the fields out of the content types so I can get them from Zope is:
obj._meta.fields
...that gives you the Django fields. One thing that is important is that the URL's which are great in Plone are maintained. For that there's a urlpaths model that keeps track of the url for that object. It uses Django signals and we rip off and grab the normalize function from Plone to make a nice URL like about/my-company.
Once that's done, we've got our objects in and accessible through the Django admin interface. Wee. The changing is pretty easy, all the fields are there in the Django interface, once you click on edit. Oh sigh if now we could just get Kupu in, which is still the best editor out there.
Next there was a piece of middleware which grabs the url such as about/test and grabs the relevant page in urlpaths. There's a bit of magic that grabs the object and then looks up a default view for that object. The view is a page template, yay, for example in document.pt:
<h1 tal:content="object/title" class="documentFirstHeading">
Title or id
</h1>
Using the templates was easy, I was able to just drop main_template in, and then spend an ages ripping everything out that I wasn't going to build out tonight. And the end result is extremely unremarkable screenshot to our right.
Phew. Well it was an interesting exercise, it's certainly something that needs a great deal of work to continue before its useable, although might be an interesting tool on top of things like entransit. Certainly I feel that if I to start upgrading those Plone sites, I will be working on this tool instead.
Disclaimer: this won't work, if it does I'll fall over with shock. But here's some code:
http://svn.clearwind.ca/public/django/plone-django
Owl pictures
Mon, 14 Apr 08, 14:44:00
filed in
England
Spent the weekend in Ravenglass and Danae got these pictures.
Django templating
Wed, 09 Apr 08, 16:49:14
filed in
Django
This comment from the Django documentation was pointed out to me tonight and it winds me up a bit:
Why use a text-based template instead of an XML-based one (like Zope’s TAL)? We wanted Django’s template language to be usable for more than just XML/HTML templates. At World Online, we use it for e-mails, JavaScript and CSV. You can use the template language for any text-based format.
Oh, and one more thing: Making humans edit XML is sadistic!
Django templates
There's a few things in this comment that annoy me.
Most websites are written as HTML. Almost every site these days is XHTML. And XHTML is "A Reformulation of HTML 4 in XML 1.0". At this point yes I'm aware, XHTML isn't quite XML as this article points out, but it's as near as damn it. Everyone writes XHMTL these days. In fact look at the heading HTML of the Django admin interface:
<!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"
lang="en-us" xml:lang="en-us" >
So how can you write an interface in XHTML and claim that forcing people to write in XML is sadistic? The difference is so small to be neglible..
Next, you use it for "e-mails, JavaScript and CSV". Excellent, I put together sites using XHTML, Ajax (XML), RSS (XML), e-mail, Javascript, CSV's and PDF's. I've almost never, in the many sites I've done, started formatting Javascript, CSV's or PDF's using a templating language - the only exception I can think of is DTML which CMF and Plone (and I didn't write it).
In fact in Plone e-mails are formed using Page Templates, here's a snippet:
From: "<span tal:replace="portal/email_from_name" />"
<span tal:replace="structure lt"/><span tal:replace="portal/email_from_address" />
<span tal:replace="structure gt"/>
mail_password_template line breaks added for readability
I would accept that is a little complicated and personally I've always found using python string formatting (the % operator) more than sufficient. So we don't need Django templating for that either. But let's face it, in most sites I have done, 95% of the templating demands are for HTML. I bet yours are too. So focus on what you need to do the most and build good tools that allow you to do it fast and simply with great flexibility.
Throwing out a great tool for an edge case, which it allegedly can't meet, is not a good answer. There's better reasons for not using Page Templates and I bet in any feature comparison, Page Templates would beat Django templates. Just say "I don't like Page Templates, it's ok".
Google Web Apps, authentication
Tue, 08 Apr 08, 15:19:52
filed in
Python
One nice thing about Google Web Apps, is that you can automatically log in with a Google account. That is if you live in the US. Otherwise it doesn't work. Well that saves me from working on it tonight. Look forward to seeing that bug fixed.
Update: appears to be fixed, that was fast. Hopefully they won't take down my app when I write it though.
Google Web Apps, first look
Tue, 08 Apr 08, 01:56:55
filed in
Python
With some fanfare Google Web Apps have been launched. This is great because its using Python and Django, how much more perfect can it get. This morning I've been having a play and I'm just waiting for my account so I can upload something.
Would be nice to take my (as yet unreleased) Plone to Django conversion script, plonk the result on Google Web Apps and then link up a nice little Adobe Air app. Anyway two things that irked me (of course I'm going to complain): 1) the example Python all has two spaces as the indent and 2) it uses Django templating.
Fortunately it says:
You can bundle a framework of your choosing with your application code by copying its code into your application directory
And looking at later comments, it's not going to block a module like simpletemplates, which I use for templating in Django. So. lets alter helloworld.py a bit (original):
from simpletemplate.google import DjangoSimpleTAL
And change the MainPage class to read (only changed last two lines):
class MainPage(webapp.RequestHandler):
def get(self):
greetings = Greeting.all().order('-date')
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'greetings': greetings,
'url': url,
'url_linktext': url_linktext,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
template = DjangoSimpleTAL(path)
self.response.out.write(template.render(template_values)
And finally lets change index.html to read:
<html>
<body>
<tal:loop repeat="greeting greetings">
<tal:block condition="greeting/author|none">
<b tal:condition="python:greeting.author.nickname">Author</b> wrote:
</tal:block>
<tal:block condition="not:greeting/author|none">
An anonymous person wrote:
</tal:block>
<blockquote tal:content="greeting/content">Comment</blockquote>
</tal:loop>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<a tal:attributes="href url" tal:content="url_linktext">Login</a>
</body>
</html>
And things seem to work just dandy. For those who do use simpletemplate, the google module isn't in SVN yet, and needs a bit of work, but just a quick refactoring of the simpletemplate code to be friendly with the new imports that the Google app server uses.
Update: by the time my blog post was done, my account was activated. Unfortunately I have to work today though.
English ISP's suck
Wed, 02 Apr 08, 13:40:53
filed in
England
Well actually, TalkTalk that my mother uses is great, if you don't mind the 3 month waiting list and the 18 month contract. Virgin, which I'm with because we needed something in a hurry when we came to the UK, is now running at 0.04 Mb/sec for the last few days. Not the "Up to 8 Mb sec" promised.
Time to start complaining to Ofcom or anyone who can do anything about it (no-one until the government wakes up I suppose). The result is that wonderful initiatives like iPlayer are rendered useless (it's unwatchable here). Only 8 months till back in western civilization.
|
About
Andy McKay works at Clearwind Consulting and can emailed at andy@clearwind.ca. If you are web developer, you need to try Arecibo.
Blogs
Months
-
All
-
February, 10
-
January, 10
-
December, 09
-
November, 09
-
October, 09
-
September, 09
-
August, 09
-
July, 09
-
June, 09
-
May, 09
-
April, 09
-
March, 09
-
February, 09
-
January, 09
-
December, 08
-
November, 08
-
October, 08
-
September, 08
-
August, 08
-
July, 08
-
June, 08
-
May, 08
-
April, 08
-
March, 08
-
February, 08
-
January, 08
-
December, 07
-
November, 07
-
October, 07
-
September, 07
-
August, 07
-
July, 07
-
June, 07
-
May, 07
-
April, 07
-
March, 07
-
February, 07
-
January, 07
-
December, 06
-
November, 06
-
October, 06
-
September, 06
-
August, 06
-
July, 06
-
June, 06
-
May, 06
Categories
|