finally{}

all will be well finally

Archive for March, 2008

Serving media and static contents in Django

with one comment

Django makes me write web application pretty easily. It is so fast that I could write my first web application in about 30 minutes – including downloading ‘Instant Django’ and setting it up.

On the other hand, understanding MEDIA_ROOT, MEDIA_URL and ADMIN_MEDIA – the ones in settings.py – was confusing. I read the documents, tutorials, comments and forums. Every time I read one of the above, it would appear that ‘voila, that is it; it is so easy’. But when I apply that into the program, it didn’t work. I was missing something seriously.

It was so frustrating to search, read and to code/re-code/re-re-code, only to find out that if admin css are served properly, the site’s css are not served properly. What follows below is how I got all of the pieces together. Surely, this information is available in Django Forums and other documents (which I refer at the end). I am writing this as a reference to myself and also with a hope that it might be useful to other such suffering souls.

Serving Admin CSS/JS (called Admin media)

This is represented in settings.py by ADMIN_MEDIA. It is better to serve this from Django’s admin folder itself, so that you don’t have to copy files, when you ‘svn update’ Django.

If ADMIN_MEDIA = ‘/adminmedia/’, then the development server interprets it to be served from the admin folder of Django (like Python25Libsite-packagesdjangocontribadminmedia). I preferred to keep it that way.

Coming to production, I use Apache alias as discussed in my other article. Here ‘/adminmedia’ should point to Django’s admin media folder. It goes as below:

<Location "/adminmedia">
SetHandler None
</Location>

Alias /adminmedia "C:/Python25/Lib/site-packages/django/contrib/admin/media"
<Directory "C:/Python25/Lib/site-packages/django/contrib/admin/media">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

This is for Apache under Windows. I understand from the forums that the ‘Directory’ directive is not needed under Linux.

Serving site’s media

imageNow let us come to serving media for your site. Most probably you have this stored in a folder called ‘media’. My folder structure as in the picture.

To serve site’s media, I define a variable called ‘SITE_MEDIA’ as below:

import os
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
SITE_MEDIA = '%s' % PROJECT_DIR

and in urls.py, I include this at the end:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.SITE_MEDIA + '/media/'}),
    )

and in base.html

<link rel="stylesheet" type="text/css" href="/media/css/sol.css" />

This fundamentally means that the UrlHandler of Django will serve the ‘media/’ files from the SITE_MEDIA + ‘/media/’ directory.

Coming to production, alias comes to aid. I got the below alias for ‘/media/’.

<Location "/media">
SetHandler None
</Location>

Alias /media "C:/django_projects/cool/media"
<Directory "C:/django_projects/cool/media">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

And of course you need to set DEBUG = False so that media files are served by Apache and not by Django.

Written by Joseph Jude

March 31st, 2008 at 11:28 pm

Posted in Programming

Tagged with

As I drive through chaotic Bangalore Traffic…

with 2 comments

Bangalore is associated with growth of Indian IT industry. Unable to cope with such a tremendous growth, its roads are known for maddening, chaotic traffic. Ask any Bangalorian and they will tell you (horror) stories of ‘getting stuck in traffic for hours’.

When I moved in two years back, I used to get irritated with such traffic jams. Now I sit in my car and listen to new CDs; or make that pending call; or if nothing to do, observe the mad crowd around.

I have spent such unbelievable amount of time in traffic jams that I draw parallels to the traffic around and the corporate world that I drive to, once such traffic jams clear. They might sound wacky; they should be since I thought over them stuck in traffic jams. Next time you are in a traffic jam, give a thought for this.

Risks, Risks, Risks

Regularly the main route becomes congested that every one has an alternate route. Whatever number of routes that you have, all ‘variables’ can come together to laugh at your high planning and cripple you!

It can so happen that on a Monday morning, you decide to leave early to beat the traffic (1st risk mitigation). On the way you find that the ‘state roadways dept’ dug the road in a way that you can’t take it. You tell yourself, “Don’t worry; I got time and an alternate route” and take an alternate route (2nd mitigation). As you drive along you find that the ‘telephone dept’ is busy laying cables and there is a diversion. Hmm…you start to get irritated. Yet not to loose your cool, you think of another alternate route (3rd mitigation) and there you are happily driving through. Not long buddy; there is an accident with a ‘Tata Sumo’ cab that is completely blocking the only route that is left. Now you are stranded. You are so close to office; yet you are so far!

This isn’t further from the truth. Okay, it may not happen on a Monday morning; but it can happen on any day.

Being in IT, I keep relating to risk registers that we maintain and the multiple mitigation plans that we come up with. Even if you’ve been a smart project manager (or a corporate leader) to have thought of multiple mitigation plans, all ‘variables’ can hit you (or your project or your firm) and slide you out of control. Only by keeping ‘cool’ at those times, you can come out of the disaster.

Be Lean

As I get stuck in the traffic, I switch of the engine and watch in despair the cyclist and bike riders wade through the traffic and keep moving. I sit there with a powerful engine in a luxurious car, but I make no progress! With all the luxury and power, I remain without any progress; but these guys make progress, however small it may be, and they reach their destination before me! Don’t get me wrong – I am not saying luxury is not needed; it is of no use in a crisis.

When the going is good, you can have hundreds reporting to you. But when you have to salvage a project in crisis, you need ‘two strong oxen; not hundred chickens’. Unfortunately you can find so many who just doesn’t want to leave their power and luxury even at crisis. It only serves their ego; the group makes no progress.

Effective Enforcement

You will notice the chaos unleashed in its fullest possibility on the Bangalore roads -Jumping signals, Violating one-way and many other lawlessness. What amazes me is that whenever this is discussed by politicians or police, they seem to favor ‘more-rules’; as if there are not enough laws.

As long as police think that every law is an opportunity for black money, any number of law will not achieve orderliness.

And as long as every politician want themselves to be an exception to the same rules that they devise, the common man continue to suffer.

Instead of devising ‘more laws’, enforce the existing ones firmly, effectively and without an exception. I’ve heard of one Kiren Bedi towing the illegally parked vehicle of a Prime Minister. She is retired and we have only that incident (and only one Crane Bedi) to celebrate!

I see a similar thought process in the corporate management. Despite their claims of employee-friendly and building family-like environment, the current corporates have so many restrictions – one can’t take a photo of the campus; one can’t bring their parents to show their workplace; one can’t browse to social network sites and so on and so forth.

Google earth (and similar other sites) could be your security threat; not the photos that I take inside the campus. If you want me to feel proud of the company that I work for, I want to bring my family members and show them where I work; and if I decide to be unproductive, there are many ways of doing so; not just by browsing to social networking sites.

I know I’m making it sound so simple and there is another side of the story. But my point is instead of having a simple and effective guideline, the corporate culture is driven (in a madly fashion) with all restrictions. No wonder there is no creative solutions coming from here!

In a smaller scale, it is true of the design of enterprise systems and applications. There are so many restrictions placed on these systems that they have become so complex to use for end users while the management get away with exceptions.

Active Participation – the other side of the coin

I’ve watched with contempt those ‘recently returned onsite’ guys violating the well known rules without missing a heart beat. These guys will not stop a second praising the fantastic American/Singaporean/European roads as they zoom past the red signal. I’ve come to believe that infrastructure is not the major cause of chaos but we, the road users are. There are times that I go highly irritated and loose my cool, but mostly I say to myself, ‘just because the other guy behaves as a son-of-a-bitch doesn’t mean I need to behave one myself’. I’m convinced that if at least 10% of the road users become responsible users, the chaos will reduce to a large extent.

Coming to the corporate side, do you enjoy your work? Do you love what you do? I do. However, this is not true of so many of the millions in IT.

I keep telling my guys, either find a job that you enjoy or you enjoy the job that you get. Instead of complaining, you either quit or initiate or participate in activities that make your work and other’s work enjoyable. Make yourself a better person to work with and make your workplace a better place to work for. I’m talking from experience and let me tell you it is possible.

This has been a long ramble. I’ve not attempted, either to solve Bangalore traffic woes or to bring a better corporate culture. I just drew parallel to each other.

Written by Joseph Jude

March 26th, 2008 at 11:05 pm

Posted in personal MBA

Tagged with

Life’s little lessons

without comments

As I go through the ups and downs of life, these words keep me going with sanity.

This too shall pass

I’ve come across many difficulties in life – disappointments, physical ailments, academic failures, broken relationships and financial difficulties. As I went through each of the tough phase in my life, I’d tell myself – ‘This too shall pass’ and so did each of them.

There was a time when I used to think that, "I don’t need to do anything; with time, this situation will go away or get better". I’ve realized that it is a lazy attitude.

Some situations may go away or get better as time goes. In other cases, you have to change your perspective or take a solid action before the situation get any better. However, most often, it is, ‘take-enough-action-and-then-wait-with-hope’.

It has guided me not only during my bad days. These words have guided me during my ‘feeling-damn-good-buddy’ days as well.

In essence, these words have kept me going with hope one one side and humility on the another.

Life is not a zero-sum game

I’ve not understood game theory well. All that I know is, "I don’t have to necessarily loose for others to win; and others don’t have to necessarily fail for me to win". Both can win. Or in corporate parlance, it is a ‘win-win’ situation.

In one of the mail forwards, I read the below and it challenged me to think with this ‘win-win’ perspective.

A shoe salesman is sent to an island. He returns back saying, "None wears shoes; No opportunity". The company sends another. He returns saying, "None wears shoes; great opportunity".

I’ve been in many disastrous situations – in personal and professional lives – sometimes when others have explicitly given up hope. I don’t mean to say I can solve the issue. Not always. But something better can come out of it, if someone takes the pain to think.

Life is a bitch and at the end you have to die

I bet you did not expect this after all this optimistic talk. Right? Well, let me say one thing: I’m somewhere in between being-optimistic and being-pessimistic, which I call ‘being-realistic’.

When I get attached to life (when the going is good) and try to be a good person myself, I start to expect that life will be fair to me. Let us get this straight: life can be beautiful; life can be fun; life can be anything but fair. Life is unfair: sometimes in favor of you; most often ruthlessly against you.

I accept the brutal fact that life is unfair (rather a bitch). That doesn’t depress me; rather it challenges me to be stronger to take action (life is not a zero-sum game; think differently to win) with a hope (this too shall pass).

I keep repeating this (AA) prayer quite often:

Grant me the strength
To change the things that I can
Grant me serenity
To accept the things that I can’t change
And wisdom
To know the difference

Life is a paradox. Beauty lies in knowing to sail through that.

Written by Joseph Jude

March 26th, 2008 at 8:09 am

Posted in personal MBA

Tagged with ,

My Django workspace

without comments

I’ve come to enjoy developing in Django – I can concentrate on the logic of the program rather than worrying about the intricacies of the language or the framework.

So I’ve come to setup my windows box as below so as to be quick in playing with Django. As I am a newbie myself, I am still finding out the better way to do these. But so far, it is good.

For development under windows, Instant Django (update: Instant Django seems to have gone out of the world) is the best bet. Download, Unzip into a folder, click on start.bat to play with your favorite framework. Simple isn’t it? It can’t get any better. By the way, do an (svn) update before you start so that you get the latest Django sources.

Since Django is about developing for web, it is necessary to have a decent browser – Firefox is my choice. Of course just because your application displays well in Firefox doesn’t necessarily mean that it will display well in IE.

Another reason to love Firefox is because of the vast number of efficient plug-ins. Two of such relevant plug-ins are Firebug and SQLite Manager. Firebug helps me in designing CSS and also to locate mistakes quickly. It is an awesome tool. As I use SQLite for development purposes, SQLite Manager is a handy extension. If you are wondering whether to download Sqlite, you don’t need to. ‘Instant Django’ package comes along with Python 2.5 and Sqlite is part of it.

So I’ve gotten, Python, Sqlite, Django and Firefox with its extensions. What else is pending?

A good source editor.

I use ‘Programmer’s Notepad’ for source code editing. If you are used to Visual Studio type of editor with all fancy additions to development, PN will disappoint you for editing source files. It would be nice to pop-in the Python/Django command with the parameters and so on. But to tell you the truth, I didn’t miss any such feature.

That is all needed for development. However, you might want to setup a staging environment to closely match your production environment. I setup apache/postgresql/django stack using the instructions provided by John. Fundamentally it involves the below steps. (PIL was not included by John).

  1. install python
  2. install PIL (imaging library)
  3. install apache
  4. install postgresql
  5. install Psycopg
  6. install pgadmin
  7. install Tortoise SVN client
  8. install Django (I didn’t install Django; rather copied the Django folder from Instant Django)
  9. install mod_python
  10. configure apache for Django

Each of these installations went on pretty well. The aspect that I appreciated in John’s article was that, not only he explained the installation pretty well; but the steps were accompanied by screenshots, which makes it easy to comprehend.

If you are copying the Django files (as in step 8), delete the pyc (compiled python files) files. They will be generated freshly in the destination as and when they are invoked.

In addition to the apache configuration that John explains, I included aliases (by suggestion by Kellen from irc; Thank you Kellen), so that I don’t have to copy the media (css/js) directories every time I update Django or I make modification to the application media. I understand from apache docs that <Directory> directive is not needed under non-windows platform. One is the path to Django admin media and the next one is to your application media.

To summarize below are added to the apache conf file:

#load mod_python
LoadModule python_module modules/mod_python.so

<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE cool.settings
PythonDebug On
PythonPath "['c:/django_projects'] + sys.path"
</Location>

<LocationMatch ".(jpg|gif|png)$">
SetHandler None
</LocationMatch>

<Location "/media">
SetHandler None
</Location>

<Location "/adminmedia">
SetHandler None
</Location>

Alias /media "C:/django_projects/cool/media"
<Directory "C:/django_projects/cool/media">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Alias /adminmedia "C:/Python25/Lib/site-packages/django/contrib/admin/media"
<Directory "C:/Python25/Lib/site-packages/django/contrib/admin/media">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

and this is an extract of the settings.py

MEDIA_ROOT = 'c:/django_projects/cool'
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/adminmedia/'

That is it, to play with Django.

Written by Joseph Jude

March 17th, 2008 at 10:49 pm

Posted in Programming

Tagged with

Striking a balance

without comments

This world is full of people with strong opinions. The people who think that they are on the ‘absolutely’ right side and all others, who don’t agree with them, are on the ‘wrong’ side. For them it is, ‘either you are with us or you are against us’ – a ridiculously immature attitude.

Not even for a moment, they can entertain the idea that there is tiny teeny possibility that they could be wrong (borrowing on the words from the character Phoebe in Friends series).

Take a simple case of, enjoying the present to worrying about the future. Should I spend all my money (or effort or whatever) enjoying the present or save for the future?

Another scenario that I quite often find myself is whether to leave things to ‘fate’ or ‘to take control of life’.

Same applies to ‘religion’ and ‘science’. Or ‘Christianity’ vs ‘Islam’ vs ‘Hinduism’ vs any other numerous regions on the face of the earth.

I have been on both sides of the argument and have come to realize that there is no absolute position.

I need to live at the present; yet be concerned about the future.

I need to plan and do my best; yet understand that there will be events beyond my control.

I won’t even get in to the religious topics. That is where the balance is needed the most.

I believe it is a paradox of life that one needs to be balanced.

Of course I agree that, that line of balance changes to one to the other; or as we age.

Point is, do you consider that you are at the right side and all others are just plain wrong?

So is striking a balance always right? And all others wrong?

Written by Joseph Jude

March 15th, 2008 at 12:34 pm

Posted in personal MBA