Programming


26
Feb 10

Learning Django

I’ve always liked Django (Python) but never really pushed myself hard enough to learn it in full. But this week is different.

I’ve picked up 5 (five!) books on Django and started learning. I know there are the official docs and the official book, but I really felt like doing it old school and learn stuff like a complete n00b.

After some test reading, I settled for Learning website development with Django. The book is for true beginners and does things in an old fashion. Throughout the book you build a single website (a bookmark app) and iterate several times over the entire app – from the basic way of getting things done – to what is considered best practice.

After this I plan to read through Practical Django Projects. I initially planned to go with this book first but I don’t like how the author wrote the syntax (e.g. actual code is mixed with normal text – this makes it hard to scan through the text).


3
Jan 10

The missing gems of Django in CakePHP

Django is a really powerful web framework and I really miss some of it’s gems back in CakePHP. The thing I really miss is get_object_or_404 which is a shortcut for looking up database results and raising a 404 if none are found. When working with Cake most people typically do the following:

function view($id=null) {
	if (!$id) {
		$this->Session->setFlash(__('Invalid Category.', true));
		$this->redirect(array('action'=>'index'));
	}
	$this->set('category', $this->Category->read(null, $id));
}

So basically, if the read method doesn’t return anything, you get a blank page. Of course, you can check if something was returned or not. But it’s just extra work (and the console should do it for you if you ask me..). So why not make our own get_object_or_404 method?

The easiest way to do this is to create our own method in the AppModel class. Open up app_model.php in your project/app root (or create one if you don’t have it).

<?php
class AppModel extends Model {

    function getDataOr404($id = null) {
        if(!$id) {
            // return a 404 if the ID wasn't supplied
            $this->cakeError('error404');
        }

        $data = $this->read(null, $id);

        if($data) {
           return $data;
        } else {
            // $this->read returns FALSE if no data is found
            $this->cakeError('error404');
        }
    }

}
?>

So how do you use this? Open up one of your controllers and do the following:

function view($id=null) {
     $this->set('category', $this->Category->getDataOr404($id));
}

17
Dec 09

Semantic Versioning

In the world of software management there exists a dread place called “dependency hell.” The bigger your system grows and the more packages you integrate into your software, the more likely you are to find yourself, one day, in this pit of despair.

Read all about semantic versioning and how to apply it to your projects.