Nam Ngo's blog

Musings of a Software Developer.

Django Dynamic Unsubscribe Forms

The normal usecase of Django forms is to have predefined fields in forms like this:

1
2
3
4
5
6
from django import forms

class EmailPreferenceForm(forms.Form):
    daily_digest = forms.BooleanField(required=False)
    weekly_news = forms.BooleanField(required=False)
    monthly_summary = forms.BooleanField(required=False)

What if we wanted to add more fields to the form like weekly_digest and so on? Well, of course you would have to add a new field to the form and maybe handle extra work in your views. Let’s make this more dynamic.

Loading Fixtures in Django Tests

When writing tests in Django apps, I have an urge to reuse code in setUp() across all tests (most of my apps use the same setUp). So I come up with a GlobalSetup class that other tests should inherit from (it’s located at the project level). However, this approach creates a problem with loading fixtures. According to Django docs:

First Post From Octopress

Hey there! This is my new blog powered by Octropress and hosted on Github. Octopress was exactly what I was looking for in a blog engine (Yes, code snippets should be built out of the box in a blog). I’ve had Blogger previously but ever since I switched it to the new theme, all my github gists were not able to display. So I abandoned it. I might migrate the posts from Blogger here, however I’ll mainly focus on writing about Python and Django in this one. That’s about it, stay tuned for the next post. :wq

Fabric Deployment

Having played with Capistrano before, I suddenly had an urge to find its Pythonic alternative. After few google searches, I stumbled upon Fabric. It’s basically a “python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks”. Spent few minutes trying to write functions to deploy Django applications to my Linux server and I have to say, Fabric’s API is really easy to use, very customizable (it’s as easy as wrapping up your shell commands in a python function) and more importantly it has Django integration which I’m gonna need the most. Having said all these, here’s the video demo I recorded while messing around with Fabric, enjoy:

P/S: django-nose, nosegrowl2 and tdaemon were used in order to show notification of testing results (just FYI)

Fatal Error: Call to Undefined Function _make_cat_compat()

If you have the following error after uploading your WordPress, and re-uploading category-template.php didn’t work, you could try pasting this function at the of the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

function _make_cat_compat( &$category ) {
    if ( is_object( $category ) ) {
        $category->cat_ID = &$category->term_id;
        $category->category_count = &$category->count;
        $category->category_description = &$category->description;
        $category->cat_name = &$category->name;
        $category->category_nicename = &$category->slug;
        $category->category_parent = &$category->parent;
    }
    elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
        $category['cat_ID'] = &$category['term_id'];
        $category['category_count'] = &$category['count'];
        $category['category_description'] = &$category['description'];
        $category['cat_name'] = &$category['name'];
        $category['category_nicename'] = &$category['slug'];
        $category['category_parent'] = &$category['parent'];
    }
}

?>

Aliases for Google Storage

I’ve just registered an account on Google Storage last month and I totally love it as. I particularly like gsutil since I’m most of my time on Terminal. Here are two aliases I use with gsutil in my .bash_profile. Of course, remember to replace your bucket name

1
2
3
4
5
6
7
function gsup(){
    gsutil cp "$@" gs://bucketname.com
}

function gsdown(){
    gsutil cp gs://bucketname.com/"$@" ~/Desktop
}

Using PHP to Parse Git History

Here is how you can store git command output in a good manner after which you will be able to easily retrieve necessary information. This snippet belongs to the project I’m currently working on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
// Author: Ngo Minh Nam

$dir = "/path/to/your/repo/";
$output = array();
chdir($dir);
exec("git log",$output);
$history = array();
foreach($output as $line){
    if(strpos($line, 'commit')===0){
        if(!empty($commit)){
            array_push($history, $commit);
            unset($commit);
        }
        $commit['hash']   = substr($line, strlen('commit'));
    }
    else if(strpos($line, 'Author')===0){
        $commit['author'] = substr($line, strlen('Author:'));
    }
    else if(strpos($line, 'Date')===0){
        $commit['date']   = substr($line, strlen('Date:'));
    }
    else{
        $commit['message']  .= $line;
    }
}

print_r($history);

?>

GAE XMPPHandler

Finally got time to continue developing Push Contacts. I’ve gotten a nerve-wrecking problem while developing with C2DM applications. When the app supposed to get the push message, it didn’t respond to it. Looking at the logs I encountered an error stack related to GTalk (with TalkProvider tag). Decided to google the problem and the solution was to sign out of GTalk on the phone.

Fix Screwed Up Keyboard Layout on Ubuntu 9.04

Got Gnome desktop installed on TonidoPlug but when trying to vnc to it I realised the keyboard layout is messed up. This is how you fix it:

  1. Kill existing vnc session: vncserver -kill :1 (1 here is the pid)
  2. Edit with: nano /root/.vnc/xstartup
  3. Add this line: export XKL_XMODMAP_DISABLE=1
  4. Restart vncserver

TonidoPlug

Yes, TonidoPlug is a new toy that I’ve gotten last week. It is a basically a plug computer that can act likea home server. The hardware specs are the same as SheevaPlug with 1.2GHz ARM and 512MB RAM and 512MB Flash. The best thing about it is the energy consumption ranging from 5-10 Watts.