msgbartop
by Brian Neal
msgbarbottom

30 Dec 08 Photo of the Day Progress

I’ve been working on the photo of the day application, and I have it all working except for the user comments. Things went pretty well, but I am reminded now and then that I am not an expert by any means with Python. Unlike the PHP version, I am not doing any crazy table locking at midnight to prevent race conditions. I created a custom management command that I intend for a cron job to run at midnight. This greatly simplifies things. The normal view logic simply reads the database to get the current photo. Only the management command needs to write to the current table. I am still maintaining a sequence of photos, this time in a Django CommaSeparatedIntegerField. The only tricky part is when I add a photo through the admin interface, I need to insert this new photo for the next day in the sequence. And likewise if I delete a photo, I need to remove it from the sequence. I accomplished this by overriding the Photo model’s save() and delete() methods. Inside those methods, I make calls to a custom model manager I wrote on the Sequence table. Django’s model managers are very cool, and allow you to easily add handy functionality.

There isn’t a lot of documentation on creating your own custom management commands in Django. However this isn’t really a problem, as the docs direct you to the source code. The source code is very well documented, and I was able to figure out what I needed to do by reading the comments in the code and examining the source for some standard management commands.

When I upload photos I now automatically generate a thumbnail. I know there are many Django plugins that already do this for you, but the code is so simple (< 10 lines?) with the PIL library there is no need to entangle my code with a 3rd party application for something this simple. I have one quirk. I used the strftime formatting in the upload_to parameter to my ImageFields to help minimize filename clashes. Whenever I save() a photo of the day, I update the thumbnail because I don’t know if the main image has changed or not. It might have changed, or maybe I am just updating the “photo of the day count” statistic on it. This causes the thumbnail to move to a different directory. Maybe there is a way to make it save in-place, I’m not sure yet. I’ll look into that a bit. In any event I don’t think that is a big deal, it will still minimize name clashes and it is all being managed correctly (stray files are not being left laying around).

Now I have to decide what to do about user comments. I’ve avoided this until now. I need to add comments to the polls and news items also. I will look at the contributed Django comments application a bit. At first glance, I like that it is generic and can be used on any model, and thus cuts down on duplicated code. On the other hand, I might want to use AJAX to update comments, and I’m not sure if that fits. The contributed comments application also seems to be geared for allowing anyone to comment, and thus contains a lot of spam and consistency checking that I won’t need (only registered users can comment on my site). There was a blog post about this I read somewhere. I need to dig that up.

I did some googling, and started researching my options for a Javascript editor for comments. My site users aren’t very technical, so having a GUI editor is probably a must. So far, on SG101 2.0, I have been using TinyMCE for submitting news stories and even in the private messages. But this may be overkill for comments (and even private messages). My users are used to BBCode since the current forum is using phpBB. However I’m not a huge fan of BBCode. Markdown looks interesting and seems to be widely used on the Django related blogs. There is a Javascript editor for Markdown called the WMD editor, but development on it seems to have stopped, and it still isn’t fully open sourced. However, the stackoverflow.com site is using it, so it must be decent. I also came across the markItUp! Universal Markup Editor which looks very interesting to me. I am thinking about using it in conjuction with Markdown. I’ll have to retrain my users a bit, but the GUI editor should make that easier. Having a nice editor with a preview function will be a step up from the existing site. And of course I will use my smiley application that I developed earlier with the shoutbox here.

So next I’m off to do some reading, thinking, and experimenting with comments.

Tags: , ,

Comments are closed.