Login

Snippets by pbx

Snippet List

Custom management command to list recent admin actions

On a busy site it can be nice to have a summary of admin activity. Running this command (I call it "adminlog") generates output like this: 2009-07-10 18:06:19: pbx changed flat page: "/yay/ -- Let's All Say Yay" By default it shows the last five actions; pass it a numerical arg to show more or fewer. Run this as a cron job and you can follow a site's admin-side activity without even logging in!

  • admin
  • commands
Read More
Author: pbx
  • 1
  • 3

Management command to list custom management commands

I work with multiple projects, many of which have multiple custom management commands defined. It can be hard to remember them, and slow to pick them out of the "manage.py help" list. This quickie command lists all of a project's custom commands (along with their help text). Writing it was easy after looking at the source of django.core.management. Open questions include: how do you decide which app to put this command in? Should this command list itself?

  • management
  • commands
Read More
Author: pbx
  • 5
  • 8

SQLite database vacuum script

This script can be run periodically (e.g. as a nightly cronjob) to keep a SQLite database with high churn from growing unnecessarily large.

  • db
  • utility
  • sqlite
Read More
Author: pbx
  • 4
  • 8

The chunkmaker

This is a general-purpose utility function, but since it uses lazy sequences via itertools, so it should be suitable for use with Querysets.

  • python
  • utility
  • sequence
  • itertools
Read More
Author: pbx
  • 4
  • 3

Compact idiom for legacy URLs in URLconfs

Taken from the [longer description on my blog][1]: > One of the great things about Django is its simple and flexible URL handling. If your Django work, like mine, includes converting existing sites, you’ll probably be doing some URL cleanup along the way... Just plug your old/new URL pairs into `redirects`. [1]: http://e-scribe.com/news/290

  • urlconf
  • redirect
Read More
Author: pbx
  • 6
  • 9

Import mail into a Django model

A mildly crufty script to slurp mail from an mbox into a Django model. I use a variant of this script to pull the contents of my scammy-spam mbox into the database displayed at <http://purportal.com/spam/>

  • email
  • faceless
  • utility
Read More
Author: pbx
  • 4
  • 11

Random-image template tag

This tag makes it easy to have a random rotation of images on a page. Don't forget to set your MEDIA_URL.

  • template
  • image
  • random
Read More
Author: pbx
  • 3
  • 12

TestCase helpers

I use these helper methods in my unit tests. They turn many simple getting-and-posting tests into one-liners. Definitely a work in progress, and I can't be the only person who has done this sort of thing -- comments are more than welcome.

  • testing
  • tests
  • unittest
Read More
Author: pbx
  • 10
  • 11

Generating vCards using VObject

Use this code to generate downloadable [vCard][] objects. See the [VObject docs][1] for more details on the API. [1]: http://vobject.skyhouseconsulting.com/ [vcard]: http://en.wikipedia.org/wiki/VCard

  • vcard
  • view
Read More
Author: pbx
  • 6
  • 14

Mini issue tracker

Described more fully on [my blog](http://e-scribe.com/news/230), but the gist is: this model becomes a sort of mini-app in your admin, allowing you to record and track issues related to your other applications. Sorting still needs some work. UPDATED 2007-03-14: Fixed repeat in Admin.list_display (thanks, burly!); added Admin.list_filter; changed app list (why did I call that `PROJECTS`, anyway?) to omit "django.*" apps

  • admin
  • model
Read More
Author: pbx
  • 15
  • 31

pbx has posted 10 snippets.