Brad's Blog

web dev, django, running, food, or whatever

Sticky Groups 2009-11-24

I often deploy web projects in a directory that's not owned by the user under which my webserver runs. Therefor, I often have to change permissions so the webserver can read from or write to certain files. So, for this example, let's assume I'm logged in to my linux box as brad, and I'm using apache which runs under the user www-data. To give apache access to my public_html directory, I'd change ownership for the directory and all of its ...

Mercurial installation woes on Mac OS X 2009-09-30

I started using mercurial around version 1.2, and I'm pretty sure I used the Mac OS X installer (from http://mercurial.berkwood.com/) to install 1.2.1. This placed hg in /Library/Frameworks/Python.framework/Versions/Current/bin/.Now, I've decided to upgrade to 1.3.1, and I again grab the Mac OS X installer (again from http://mercurial.berkwood.com/), which installs hg in /usr/local/bin/.Ok, but my path is set up so that the older version is used by default... Is there a preferred way to remove the older version of mercurial?I just renamed ...

A for AJAX - OR - Dynamically generating options for a select element. 2009-07-14

I don't do a lot of AJAXy web development, but when I do, I usually make use of Prototype. I've recently created a form containing a <select> element whose <option>s are dynamically generated via an AJAX request. The problem however, is that a selected option was already in the form. So before the AJAX request, my HTML looked something like this:<select name="s" id="s"><option value="val1">Value 1</option><option value="val2" selected="selected">Value 2</option><option value="val3">Value 3</option></select>We can then use Ajax.Request method to dynamically add items into ...

My PYTHONPATH bit me. 2009-06-30

I'd just finished the first version of a new django app (myapp), and so I pushed it out to my development server. All the new code was in place, so I ran python manage.py syncdb. The result?Traceback (most recent call last): File "manage.py", line 11, in <module> execute_manager(settings) File "/usr/local/lib/python2.6/site-packages/django/core/management/__init__.py", line 340, in execute_manager File "/usr/local/lib/python2.6/site-packages/django/core/management/__init__.py", line 295, in execute File "/usr/local/lib/python2.6/site-packages/django/core/management/base.py", line 192, in run_from_argv File "/usr/local/lib/python2.6/site-packages/django/core/management/base.py", line 210, in execute File "/usr/local/lib/python2.6/site-packages/django/utils/translation/__init__.py", line 73, in activate File "/usr/local/lib/python2.6/site-packages/django/utils/translation/__init__.py", ...

"Adding" Q objects in Django 2009-06-26

I've got a Django app with the following Model:class Story(models.Model): title = models.CharField(max_length=255) content = models.TextField()The Problem: I wanted to build a simple search feature that OR'ed all the search terms. Essentially, I wanted SQL resembling the following:SELECT * from myapp_stories where title LIKE '%term1%' OR content LIKE '%term1%' OR title LIKE '%term2%' OR content LIKE '%term2%'; The Solution: You can add django's Q objects together! This is a feature not currently discussed in the docs, but I dug through ...

Data Truncated Errors 2009-05-06

I recently ran into some of the Data truncated for column ... errors in my django apps. After a little digging, I've discovered that my particular problem lie in the structure of the underlying MySQL tables. Particularly with varchar columns. I have a model that contains a FileField:class MyModel(models.Model): file = models.FileField(upload_to="files/%Y/%m/%d")Note that the MySQL table generated by this model will look something like the following:+-------------+--------------+------+-----+---------------------+----------------+| Field | Type | Null | Key | Default | Extra |+-------------+--------------+------+-----+---------------------+----------------+| id | ...

Have Apache Force File Downloads 2009-05-01

I have a Django app that lets users upload files. Any kind of file. It's nice that Apache will let me force file downloads based on the files extension. <LocationMatch "\.(gz|tar|pdf|docx|doc|xls|xlsx|bz2|zip)$"> SetHandler None Header set Content-Disposition attachment</LocationMatch>So, in my HTML/templates: all I have to do is this:<a href="SomFile.docx">Some File</a>

Push Your SSH Public keys using Fabric 2009-04-30

This came across my twitter radar today from @bitprophet (aka: Jeff Forcier), who just happens to be the new maintainer for Fabric:def push_key(): keyfile = '/tmp/%s.pub' % env.user run('mkdir -p ~/.ssh && chmod 700 ~/.ssh') put('~/.ssh/id_rsa.pub', keyfile) run('cat %s >> ~/.ssh/authorized_keys' % keyfile) run('rm %s' % keyfile)Everything you need to push your public key to an external server using Fabric.

Restricting Access by Group in Django 2009-04-24

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 that allows you to ...

Dynamically Displaying Fields in a ModelForm 2009-04-21

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 self.foo == 'bar'Normally, if I needed a Form for this Model, ...