msgbartop
by Brian Neal
msgbarbottom

14 Jan 09 Update on Comments and CSS Woes

I’ve made good progress on the comments system, and I’m glad I took the time to roll my own. I have a nice AJAX style commenting system integrated with the markItUp! editor. I’m allowing comments to be marked up with Markdown, and I’m very happy with it. Having the ability for users to easily post links and images will be a big improvement from the existing site’s commenting systems.  I need to cut the private messages application over to markItUp instead of TinyMCE. I think TinyMCE is a bit overkill for anything except news stories. I also added the ability for users to “flag” comments for spam or abuse.

The markItUp editor is very nice, and I was even able to add a drop-down menu for my smiley system. Now one can click on a smiley from the editor and get it added to the comment! The markItUp editor has a slick preview system, which can use AJAX to post what the user has typed so far and get an HTML response. The only drawback is I can’t easily clear or hide the preview pane once the comment has been posted by AJAX. So you are left with a blank editor ready for the next comment, but the preview pane is still showing whatever you previewed last. This is minor, I think, and I’ve already made the author aware of this issue. He told me he will look into it for the next release. I highly recommend markItUp.

And finally I took a cue from Malcom Tredinnick’s blog (the Django source is available for study – Thanks Malcom!). I added a field to the comments table to store the rendered HTML for each comment. This field is updated on each comment object’s save. This will save processing time to convert the Markdown markup to HTML. Since comments are viewed many, many times compared to how often they are edited, this seemed like a sensible optimization at the expense of some disk space.

I need to add comments to the other parts of the system I have developed already (news, polls, etc.). However I’m being distracted at the moment by a problem I am having with CSS. This has been bugging me for months now. I don’t know why it happens, but I’m seeing <div> tags in a left floating <div> affect <div> tags in my main content area. Large gaps get created. It seems that I may be trying too hard to duplicate the PHP-Nuke blocks, and nesting lots of divs seems to be very fragile. I think I might have to investigate a different CSS layout and redo my base template. I’m googling various CSS layout tutorials in my spare time. I don’t want to become an expert in this, but I need something simple that looks decent. I wish I had a web designer to help me out with this part.

Tags: , , ,

01 Jan 09 Comments

Happy New Year!

I spent a fair amount of time googling and studying blogs of other people on what they had done to get the Django comments application to work smoother for registered users. I also looked at the django-threadedcomments application a bit. It seems very good, I just don’t like threaded comments. Then I started looking at the source code to the Django comments application and seeing that there wasn’t a lot of magic going on. In other words, even I could sort of understand what was happening. So I started to write my own comments system, tailoring it to my requirements. At first I thought this would be a ton of work, especially the template tags. However Django really makes this kind of stuff easy, and I am really learning a lot about how Django works by undertaking this. So any trepidation I felt at first is gone. I am having a lot of fun and learning a lot. I am keeping my template tags the same as the contributed comments app, so in case Django 1.1 has a much nicer system I can easily cut over to it. Learning about the Content Types framework and more about template tags has made this exercise very worthwhile for me.

I intend on supporting both a traditional comment post and an AJAX style post. Once I get some more stuff working I will investigate using Markdown and markItUp! editor I mentioned in my last post.

Tags: ,

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: , ,