Monday, June 29, 2009

Adding a field to the django auth user.

I've been trying to do this for a while and I keep finding how-to's such as Extending the Django User model with inheritance, but the problem with these methods is that you still cant sign-in to the django admin with any users you create (such as the admin you set up at the beginning of the project).

I finally got the monkey-patching method to work by doing the following.

You can add fields to the user class with the method add_to_class instead of inheriting the User class by doing the following in your models.py:
User.add_to_class('new_field', models.BooleanField(default=False))

Once that is done, in admin.py, modify the UserAdmin and add your new field
from django.contrib.auth.admin import UserAdmin

UserAdmin.list_display += ('new_field',)
UserAdmin.list_filter += ('new_field',)
UserAdmin.fieldsets[1][1]['fields'] = ('first_name','last_name','email','new_field')
VoilĂ , with that done, you're new fields should show up in the auth user on the django admin page.

Stupid Adventures in Djangoland

After converting a project and doing some directory wide find-replace's, I kept getting this error in django:

TemplateSyntaxError at /admin/ Caught an exception while rendering: maximum recursion depth exceeded
A little background, all my projects are usually named 'backend', with the front end code (usually flash) in a separate directory 'frontend'. So for example in settings.py, I install apps in django by adding 'backend.app' to INSTALLED_APPS. Anyway, after commenting out huge chunks of code I eventually turned to one of the last things I could turn to to start commenting stuff out. So things Amongst the urlpatterns I found a line (r'^app/', include('backend.urls')). Some how, after the find-replace session, this line was setup to constantly reference the urls.py that it resided in, which resulted in the error stated above.

Tuesday, June 16, 2009

Tall in the bathroom stall.

You know what would suck about being 7 foot tall? In most public bathrooms you can see over the stall walls and look at the guy who's currently wiping his ass.

Course you never hear 7 foot tall people complaining about this because usually they play for the NBA and are evil rich and don't have to use public restrooms. Thus, if you see a 7 foot tall guy looking over the stall at you while you take a dump, take pitty on him since he's most likely so rich that he's going to hell.

Monday, June 15, 2009

Adages

Red sky in morning, sailors take warning;
Red sky at night, sailors delight.

or was it
Red sky in morning, sailors in mourning;
Red sky at night, sailors in plight.

Yeah, that second one is it.. it basically means if you're a sailor and the sky is Red, your screwed.

And yes if the sky is red instead of blue for anyone else, it means you're going to hell.

Monday, June 8, 2009

TypeError: not enough arguments for format string

Got this error in some python code the other day:

TypeError: not enough arguments for format string


Essentially it means you have something like the following in your code:

"%s,%s,%s" % ('foo','bar')


Essentially it's saying there aren't enough strings to fill the placeholders. But it also means that there might be an errant % in the template string that you weren't anticipating such as:

"%s has 42% of the shares." % ('Bob')


which is annoying. A regex you can run on your code to roughly handle this is:

regex1 = re.compile('%(?![\(])')
regex2 = re.compile('%%+')
cooked_html = regex1.sub('%%', regex2.sub('%', raw_html))
I say roughly since if you're starting to run into these kinds of situations, you really need to start using a templating engine like jinja or mako. I'll switch to mako in the future.

Wednesday, June 3, 2009

Running functions on marked files in dired mode for emacs.

Source

If you have some function, say some custom refactoring operation, you can call it from a dired buffer, and have it run on each marked file, using the code below.

Go into a dired buffer, mark some files, and then do M-x and run your command. In this case it would be test-for-each-dired-marked-file


;;; iterating over files in dired

;;; usage example - for-each-dired-marked-file returns a filename and path
;;; for each marked file, so this is what a function using it looks like
(defun view-stuff(filename)
"opens up the file and gets the length of it, then messages the result"
(let (fpath fname mybuffer len)
  (setq fpath filename)
  (setq fname (file-name-nondirectory fpath))
  (setq mybuffer (find-file fpath))
  (setq len (buffer-size))
  (kill-buffer mybuffer)
  (message "Buffer length %d %s" len (buffer-file-name mybuffer))))

; Usage example
(defun test-for-each-dired-marked-file()
(interactive)
(for-each-dired-marked-file 'view-stuff))

(defun for-each-dired-marked-file(fn)
"Do stuff for each marked file, only works in dired window"
(interactive)
(if (eq major-mode 'dired-mode)
   (let ((filenames (dired-get-marked-files)))
     (mapcar fn filenames))
 (error (format "Not a Dired buffer \(%s\)" major-mode))))