Brad's Blog

web dev, django, running, food, or whatever

Published: 2009-04-24

Restricting Access by Group in Django

Python django web

Django's authentication system provides built-in support for Groups. When developing an app, you may want to prevent users in a particular group from accessing part of your app. For example, if you were building a tool to be used by Faculty and Students, it's quite possible that there would be parts of the app you wouldn't want Students to access (like the part that allows a User to change grades!). Luckily, there's a decorator called user_passes_test ...

Published: 2009-04-21

Dynamically Displaying Fields in a ModelForm

Programming django web

The Problem: I want to dynamically include some fields in a ModelForm based on some external criteria. Sometimes I want the fields displayed, sometimes I don't. I'm going to try to explain this scenario through a (albeit contrived) example. I have a Model that looks like the following:class Suff(models.Model): foo = models.CharField(max_length=255) bar = models.BooleanField(default=False, blank=True) def is_foo_bar(self): ''' is this model's foo attribute set to 'bar' ''' return ...

Published: 2009-04-01

How to Set up a Foreign Key Constraint in MySQL

Programming mysql web

The default storage engine in MySQL (MyISAM) does not support Foreign Key constraints. If you want to use Foreign Keys in Mysql, you need to use InnoDB.The following is a simple example that illustrates Foreign Key constraints, we'll create tables to store information about Authors and their Books. The Foreign key will link a book to an Author. Note, that in MySQL we need to use the InnoDB storage engine to support Foreign Key Constraints.First, we need ...

Published: 2009-03-25

Public Keys don't like write access!

Backup Linux

The first part of my Automatic Backups with cron, tar, and SSH details how to set up remote login (sans passwords) using RSA public key.Apparently this doesn't work if your home directory allows groups or others write access. So if used ssh-keygen -t rsa to generate a public key which you added to a remote host (under .ssh/authorized_keys), but ssh is still prompting you for a password, try setting your home directory's permissions to something like ...

Published: 2009-03-24

mod_python checks your blood pressure

Fun Python django

I'm deploying a django project using mod_python... now, usually I will just use my package management tools to install a binary version, but this time I need to build it from source.As I get ready to go through the whole configure/make/make install process, I peruse the output of configure just to make sure everything is ok...checking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether ...

Published: 2009-02-20

Scheduled Tasks (or cron jobs) with Django

Python cron django web

This is my take on setting up cron jobs for the apps in a Django project. It is based on my own convention, and it solves my initial problems where I want to perform some action on all of my Django apps at a periodic interval (currently this is a once-a-day task).In order for this to work, I create a module for all of my INSTALLED_APPS. This module must contain a run method. Other than that, it ...

Published: 2009-01-14

A Custom form for Django's Automatic Admin.

Python django web

A huge selling-point for Django (at least for developers) is its Automatic Admin. However, the ease at which the Admin can be set up, might make one second-guess an attempt to customize what is provided by default. Of course, the default admin site may not be without its drawbacks...Many of the django Apps that I have built, tap into Django's User Authentication System. Simply put, when I build a model, it has a Foreign Key to django's ...

Published: 2009-01-05

Add a Context Processor for your Django app using Sites

Programming Python django

I've recently refactored a significant number of my Django Apps so that they include the "sites" framework. Essentially, this allows me to use the same code (and database) for multiple sites. For Example, if I was was building a CMS (and I am!), I might have a model that defines a "page":from django.db import modelsfrom django.contrib.auth.models import Userfrom django.contrib.sites.models import Siteclass Page(models.Model): title = models.CharField('title', max_length=255) content ...

Published: 2009-01-04

Morphology is Fun!

Fun Image Programming morphology vision

Morphological operations are very common in image processing. The two most basic of these are Erosion and Dilations, and from these, additional morphological operations can be performed. While there is an abundance of literature on the topic of morphology, I often like to view concrete examples of the operations. So, I've published a few images that illustrate the output of 1-to-5 iterations of various morphological operations --erosion, dilation, opening, closing, gradient, top hat, and black hat--with various structuring elements--rectangle ...

Published: 2008-12-17

How to convert HTML to PDF using Python.

PDF Python web

I'm building web-based, data-driven apps using Django. Eventually (or unfortunately), I will need to generate some reports that are printer-friendly. Logically, PDF is the format for such files... so how am I going to convert my xHTML and CSS to a nice-looking PDF document?The Django Book has a whole chapter dedicated to Generating Non-HTML Content. They seem to to be fond of ReportLab ToolKit. The caveat here, though, is that you need to know a bit about the ...