Sep. 17th, 2009

elfs: (Default)

We frequently write little functions that populate the Django context, and sometimes we want that context to be site-wide, and we want every page and every Ajax handler, basically everything that takes a request and spews a response, in our application to have access to that information.  It might the user’s authentication, or his authorization, or some profile information.  Or it might be environmental: a site might have figured out what time it is on the user’s site, and will adjust backgrounds and themes accordingly.

The context might be a simple variable.  I have an example right here: is the browser you’re using good enough?  (I know, this is considered Bad Form, but it’s what I have to work with) .  The function has the simple name, need_browser_warning.  The context key may as well have the same name.  Using a constant for the context key is the usual pattern; this ensures the Django programmer won’t get it wrong more than once, at least on the view side.  (The template is another issue entirely.  Set your TEMPLATE_STRING_IF_INVALID in settings.py!)

I wanted something more clever in my context processor.  Here’s sickly clever:

import inspect
def need_browser_warning(request):
    return { inspect.currentframe().f_code.co_name:
        not adequate_browser(request.META.get('HTTP_USER_AGENT')) }

Yeah, that’s a little twisted.  It guarantees that the name of the context key is “need_browser_warning“, and the value is True or False depending upon what the function “adequate_browser” returns, which is what we want, so it’s all good.

Obviously, this isn’t good for everything.  Some context processors handle many, many values.  But for a one-key, this is a nifty way of ensuring name consistency.

This entry was automatically cross-posted from Elf's technical journal, ElfSternberg.com
elfs: (Default)
Kung-Fu Monkey on the missle defense shield that Obama just cancelled:
At $200 billion [the allocated cost for the missle defense shield from 1999 through 2009] we could literally give every person in the Middle East a thousand bucks to look the other way, maybe buy some Nike knock-offs and chill*. If we Fermi Problem it, figuring on just men, 50% down, then just adult men between , say 17-40, knock it down a quarter, then assume fairly effective terrorists need a smattering of English and some technical knowledge, that's down to maybe 10% of our working number ... that's between $50,000 to $80,000 to every reasonably viable terrorist to go out and not vaporize himself. That's good money. That's "let's focus more on the inventing-algebra parts of Islam and slightly less on the jihad aspects while we watch the game on my new big-screen" money. Sure, you're gonna get your martyrs, but hey, that's what the Dept. of Homeland Security is for.

Yes, that suggestion is crazy. It also has EXACTLY THE SAME REAL-WORLD SUCCESS RATIO OF THE MISSILE DEFENSE SYSTEM.


Read it all: I Miss Republicans.

Now realize that Kung-Fu Monkey wrote that in 2004.

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 9th, 2026 01:07 pm
Powered by Dreamwidth Studios