Tag: Programming

Customizing the admin site listing for the standard Django User model

Django’s admin site is a great tool throughout development. Django also provides an authentication module that also reduces the development time. The problem is that the auth module predefines the admin classes that shows a very simplistic listing in the admin view.

By default this is how the admin view looks for the User model.

It is not easy to see how you can customize this. But if you read through django.contrib.admin.sites then the answers becomes clearer. You can unregister the existing class for the User model and register your own version. Below is what this translates to, put this code in one of your application’s admin.py (if you are calling admin.autodiscover in urls.py) or models.py files.

Read full post...

Facebook bug?

Let’s see if I can get this across with a super blurred photo. Below is a picture of my facebook “home” page. Notice how the same “block” (picture + three comments) is repeated several times, in total it was repeated four times. Knowing how challenging some queries with joins and grouping can be, I don’t blame facebook 🙂

Notice the repeated blocks.

svn status -u

This is a very useful command if you are using subversion for version tracking. I just found out about the -u flag last week and I am loving it. It tells you exactly how an svn update would affect your working copy. This way you can decide whether you want to update to trunk with confidence. Previously there were times when I would update to the latest version only to realize that merging my code would take a little time, now I run this beforehand and decide whether to proceed right now or wait till I have more time. Enjoy 🙂

Interesting language

Cache’ Object Script is an interesting language. Take a look at the following two loops

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
test
	s done=0
	f ln=1:1:10 d  q:done
	. w !,ln
	. i ln=5 s done=1
	w !,"ln after loop "_ln
	q
test2
	s done=0
	f ln=1:1:10 q:done  d
	. w !,ln
	. i ln=5 s done=1
	w !,"ln after loop "_ln
	q

The goal is stop after five iterations and then use the ending index to do something more. Executing the above code results in…

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
PLAY>d test

1
2
3
4
5
ln after loop 5
PLAY>d test2

1
2
3
4
5
ln after loop 6
PLAY>

Can you spot the difference?

I am in love…

…with a font, that is! 🙂 If you haven’t guessed already, I like to write code. So, naturally, the one thing that I see the most during a normal day is fixed width font. Previously I didn’t care so much and used whatever default the operating system had. On Windows this is generally the [Courier New)] font. Recently, I started to look around to “spice up” my environment, and font was the first thing that came to my mind. So, I started looking around for options and ended up on Consolas.

A screen shot of Consolas in VIM

Now, nothing is perfect and Consolas is no different. Consolas was designed with ClearType and pretty much for ClearType because it doesn’t look very good if you don’t have ClearType enabled (checkout the comparison in the wikipedia entry). That is something that I can live with because majority of the coding that I do is either through a shell session running on Windows or in some other Windows programs so it isn’t really an issue for me. For the cases when I do have to use other operating system, there is always the similar looking Inconsolata font.

To really enjoy this font I have all my fixed width programs set to 10pt font size, which I have noticed is the best size for this font. At this size the font looks awesome and you can easily distinguish the similar looking characters like o (lower case o), O (upper case O), and 0 (zero).

So, if you code a lot or look at fixed width fonts all day long for some other reason then I suggest that you should give this font a try, maybe, you too will fall in love with it! =D It comes standard on Windows Vista, for other Windows version you can download the Consolas Font Pack if you have Visual Studio.

If you are looking for some other options you might also want to consider another excellent font that supports box drawing: Envy Code R.

Lambda functions in Python

Python

The lambda functions in Python always seemed very cool to me, but I never thought that I would actually get to use them in my projects. Guess what? Last night I was working on a side project when I had the perfect opportunity to use and very much appreciate this feature!

As part of my move of this site to django I have to move over the “custom” textile format parsing that I created for this site. Basically, it’s mostly textile with the additional feature of allowing you to easily embed the attached image or file. Below is the Python specific regular expression that defines this syntax.

pre.
\[(?Pinline|image):(?P[<>]?)(?P\S+?)(=(?P.*?))?\]

This allows me to easily reference an attached image and instructor the site to resize based on my needs. So, after I upload an image for a blog entry all I have to do is put [image:>0=thumb] (right aligned “standard” thumbnail sized, as predefined by me) or [image:0=200x100] (default alignment with 200×100 resize keeping the original image in proportion) wherever I want an embedded image to be displayed. With this I don’t have to worry about resizing the image or adding the img tag manually, all that is done automatically by my Node class.
Ruby has a useful notation where I could do this in a block notation as…

1
2
3
replaced = txt.gsub(/\[(inline|image):([<>]?)(\S+?)(=(.*?))?\]/m) {
  // do all the custom code in this block.
}

Since python doesn’t have the concept of blocks it uses the approach of passing in functions that return the replacement text.

1
2
3
4
5
6
7
8
def replacement_function(match):
  // interpret the match in this function
	return "...replacement for match." 

def interpret_attachments(text):
  attach = re.compile(r'\[(?P<type>inline|image):(?P<alignment>[<>]?)(?P<positionOrAlias>\S+?)(=(?P<size>.*?))?\]', re.MULTILINE | re.IGNORECASE)
  replaced = attach.sub(replacement_function, text)
  return replaced

Now, the problem comes in if replacement_function needs some extra parameter, which in my case was node_id. How can you pass that to replacement_function?

1
2
3
def replacement_function(node_id, match):
	// interpret the match in this function
	return "...replacement for match." 

That is where lambda function comes in. I ended up encapsulating the call in a lambda function and passing that to the sub() function.

1
2
3
4
def interpret_attachments(text, node_id):
  attach = re.compile(r'\[(?P<type>inline|image):(?P<alignment>[<>]?)(?P<positionOrAlias>\S+?)(=(?P<size>.*?))?\]', re.MULTILINE | re.IGNORECASE)
  replaced = attach.sub(lambda match: get_replacement(node_id, match), text)
  return replaced

I remembered seeing numerous references to lambda functions in the django framework, so now that I understood the need for these, I went back and looked at a few examples. They all make much more sense now!

Here is an excerpt from django/contrib/auth/decorators.py

1
2
3
4
5
6
def permission_required(perm, login_url=None):
    """
    Decorator for views that checks whether a user has a particular permission
    enabled, redirecting to the log-in page if necessary.
    """
    return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url)

…and one more example from django/template/defaultfilters.py

1
2
3
4
5
def title(value):
    """Converts a string into titlecase."""
    return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title())
title.is_safe = True
title = stringfilter(title)

Awesome!

If you are confused about lambda functions then I would suggest reading through the filter, map and reduce section on the python site. That section demonstrates good usage of lambda functions.