Monday, August 30, 2010

Change Default Linux Shell

Needs sudo privileges: to change the default shell:

Friday, August 27, 2010

Host a Test email server for Django header debugging

Set the following in

Run the following to host the test webserver
python -m smtpd -n -c DebuggingServer localhost:1025 

Thursday, August 26, 2010

Django Management Commands

Django syncdb without prompts
python syncdb --noinput

Saturday, August 21, 2010

Bash customize autocomplete

Dynamic Fieldsets in Django Admin

Problem: the fieldsets option for the admin interface (classes defined in requires that you explicitly list every single field in the model in the fieldset list that you pass to it.  This gets tedious if you add a field to a model, as you also have to add it to this  Not very DRY.

from django.db import models
class My(models.Model):
    a = models.CharField(max_length=10)
    b = models.CharField(max_length=10)
    c = models.CharField(max_length=10)
    d = models.CharField(max_length=10)
    e = models.CharField(max_length=10)
    f = models.CharField(max_length=10)
    g = models.CharField(max_length=10)
    h = models.CharField(max_length=10)

from django.contrib import admin
from django.forms.models import fields_for_model
from f import models
class MyAdmin(admin.ModelAdmin):
    def __init__(self, *args, **kwargs):
        super(MyAdmin, self).__init__(*args, **kwargs)
        all_fields = set(fields_for_model(models.My))
        fieldset1_fields = ('e', 'f',)
        fieldset2_fields = ('g', 'h',)
        fieldset_fields = set(fieldset1_fields) | set(fieldset2_fields)
        rest_fields = list(all_fields - fieldset_fields)
        self.fieldsets = (
            (None, {
                'fields': rest_fields
            ('Fieldset 1', {
                'classes': ('collapse',),
                'fields': fieldset1_fields
            ('Fieldset 2', {
                'classes': ('collapse',),
                'fields': fieldset2_fields
    models = models.My, MyAdmin)

Thanks for the help lorochka85

Thursday, August 19, 2010

Drill through a Login with Python

import urllib2
import urllib
import BeautifulSoup

# build opener with HTTPCookieProcessor
o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
urllib2.install_opener( o )

# assuming the site expects 'user' and 'pass' as query params
p = urllib.urlencode( { 'username': 'myusername', 'password': 'mypassword' } )

# perform login with params
f = '', p )
data =

# second request should automatically pass back any
# cookies received during login... thanks to the HTTPCookieProcessor
f = '' )
data =

soup = BeautifulSoup.BeautifulSoup(data)

Monday, August 16, 2010

Python String format

Python Format dates into strings
from datetime import date
d1 = date(2010,01,23)
d2 = date(2010,01,23)
"%s - %s" % (d1.strftime("%m / %d / %Y"), d1.strftime("%m / %d / %Y"))

Thursday, August 12, 2010

Python Unicode Convert

Convert weird unicode string characters to string:
import unicodedata

Django module for helping convert things to ascii strings:
from django.utils.encoding import smart_str
smart_str('weird string goes here')

Another hack for removing unicode errors:
unicode('my weird string', errors='ignore')