Developing Django app on both Windows and Linux

I was doing most of my development on Linux so far. My desktop is dual boot, so I can log into linux or Windows at whim. This is a small inconvenience. However the bigger problem is when I am travelling as my laptop is by necessity a Windows one. This problem occupied my mind this weekend and the following is how I am getting it to work.

First I used dropbox (in case you want dropbox, please drop me a line as referrals get me more space :) ) and installed on both Windows and Linux. Next step was to move the folder where I was developing into Dropbox. This helps immensely as I dont have to worry while travelling and my code is always replicated into the 3 locations that I work in.

Next I setup git - this is an optional step for you, but I found that it helps that even on my local projects I have version system to go and look back at what changes I have made.

Now the things were working on Linux ( you can look at my earlier posts about the other problems that I had ), so I logged into windows and tried to work out the problems.

On Windows to use Django I use Bitnami Django Stack. It is quite quick and painless to use. The only problem is that the mysql install does not allow you to have root password to have special characters - which is quite common for me. The work around is to install with normal password and then start the django stack and use the mysqladmin to change the root password. I went through all this because I use root for django settings. Yeah, I know i should change it - and will probably do later when I am ready to deploy.

Next on the Bitnami shell do a syncdb and runserver.

On navigating to the http://127.0.0.1:8000 URL, I got a template not found error for index.html

Looking at the settings.py file I found the culprit immediately. The settings for Dirs was very linux specific


TEMPLATE_DIRS = (
"/home/vibhu/website/templates",
)

Same was for the STATIC_DIRS settings.

The solution to this is not to replace it with windows directory as that will just mean that when I log into linux I will again get the problem and have to change back. Searching through the django site and internet I finally got the right solution ( some books have it wrong and the examples were not working). The following seems to work for me:

Edit the settings.py file with the following somewhere at the top



#hack to accomodate Windows
import os
CURRENT_PATH=os.path.abspath(os.path.dirname(__file__))



And then in the relevant sections change the path lines :


STATICFILES_DIRS = (
os.path.join(CURRENT_PATH,'static'),
)
TEMPLATE_DIRS = (
os.path.join(CURRENT_PATH,'templates'),
)



Make sure that you have the trailing comma else the admin login view gets screwed.

Simple enough when you know it.

Login URLs in Django

Django is a pain.

I know - its a great framework. Its also supposed to be robust and highly recommended by a few friends when I asked around for what should I use as I wanted to learn how to develop a website.

I have been dilly-dallying with making the website for quite some time, but now I am trying to put in a few hrs every week into it. I enjoy it so far... but then I run into some stupid roadblock.

Today I ran into another one. If I do not document it, I am sure that I will make the same mistakes again.

Today's problem was with the login redirect URL.

I have a function with the decorator as follows :

@login_required
def foo(request, template_name="foo.html"):
    return render_to_response(template_name,locals(),context_instance=RequestContext(request))

I changed the default account redirect to /account so my urls.py has the following:
    (r'^account/', include('account.urls')),

Now, when I tried to click on the link which requires the login ( and I am not logged in) I got the following error :

The current URL, accounts/login/, didn't match any of these.

See the s in accounts. I tried searching everywhere where I had the accounts word. grep did not turn up anything and I was quite frustrated.

Finally, I reasoned that it has to be something with the way django does anything and started reading the settings documentation. Sure enough, @login_required requires a url which defaults to /accounts/login !

Why the Django documentation has to be so vague with login is something I don't understand.

So, I added the following lines in the settings.py :
LOGIN_URL='/account/login/'

Not it works !

Gah !

Lvl 25 Indian Guild in World of Warcraft

This is good news !



Most of my readers would know that I play World of Warcraft. Well, I used to - till a few months back when work became overwhelming to really put in enough time in the game.

When I had started playing Wow in 2006, I was always looking for people from India. I found people from far away places like Brazil but no Indians. Then I started a google group and got a few people in it.

At the end of 2010 I started a guild with the people there after a particularly bad day in office. I named it 'Vinash'. We got the guild to a good start with Indian people joining in. Unfortunately we had a couple of real bad eggs who wanted to run the guild and I was not really Guild Leader material. Sad to say that guild disintegrated.

Enter Sumantho who rolled a new Guild. Since I was on the google group and the facebook group I got excited about it and joined it. The guild got more and more people. It was originally rolled on the horde side on Skullcrusher, called Astra, and I rolled a Paladin figuring that I would make a Tank, and levelled him to 85. Turned out, I made a lousy tank :D

Sometime in between, people decided to move to servers in Oceania as the ones in US , due to the distance of the continents , were not giving good latency numbers. Another factor was that on SK we were just not enough people and getting random people to PUG with us was not happening. So different servers were being checked out for people availability and AH prices. Also, the main people were kinda wanting to go Alliance ( me too ). We had some late night calls over this and ultimately a decision was taken to move to Frostmourne and do a faction change. The guild was named Yuva. I moved my Shaman over to the server and being a healer was gearing it up till work and life caught up. After a month of hardly playing I had to sadly stop playing.

But I continued trolling the facebook group.

Then yesterday it got posted that the guild had levelled up to 25 (the max level as of now in WoW). Felt good. Kudos to the team - Brats, Purva, Bobby, and all the others. GG.

Ubuntu no sound fix for USB audio

Ubuntu 11.10 seems to be quite a step back in terms of usability. Things which worked in 10 are broken now. One of the things that was driving me crazy was the audio was not working sometimes.

My system uses a USB audio system (Bose Companion 5). I believe this issue is there on other systems using USB audio also.

When I used to boot, the system would come up but there was no audio. The audio would start once I would slide the volume slider down and up. But if I start VLC video player, the sound would go away all together and the only way it would work was to reboot the system. This was nuts.

After a lot of searching, i finally got the solution from Ubuntu Forums . Here's the relevant part of the solution.It involves un-installing and re-installing the audio systems.

Open a terminal and enter the following:
sudo apt-get remove --purge alsa-base
sudo apt-get remove --purge pulseaudio
sudo apt-get clean && sudo apt-get autoremove
sudo apt-get install alsa-base
sudo apt-get install pulseaudio


For some reason while removing alsa-base it removes the ubuntu-desktop. So you need to install it again.
sudo apt-get install ubuntu-desktop

Now reboot your system. Once it comes up you will see that the audio icon on the top bar is no longer there. But now all sound should be working without fiddling around.

Drop me a comment if you had this problem and this change worked for you.

Edit: Seems there is still some reboot issue as pointed out by Albin in the comments below. Once you reboot - you will need to go to Settings -> Sound and slide the volume indicator up and down to get the sound. :(

Redmine Ubuntu Installation Error

One of the most amazing software tools that I have found which meet almost all my needs in the day to day working is Redmine (hard to convince people in the office though). However I ran into a problem recently which seems to be quite prevalent going by the searches thrown up.

Cannot start Ruby on Rails application
The directory "/usr/share" does not appear to be a valid Ruby on Rails application root.

Earthquake Delhi

Delhi had a tremor in Sep last week and had another one today. People got something to gossip and tweet about. 

Today's earthquake measured 5.2 on the Richter Scale and lasted a few seconds but was enough to get people to run out of their offices. For my part, I did not even feel it and was oblivious to it until I got asked by someone. Thats when I got to know about the quake and checked out the news channels - including facebook and twitter. 

This was a small quake. 

I am scared if a big quake came. 

For comparison, the Latur Earthquake in 1993 was 6.2 , and the more recent Haiti earthquake was 7. 

Delhi falls in Zone IV. If , Delhi was to ever get a high intensity earthquake which lasted more than a few seconds, the city is going to see massive destruction. Most of the city is made up of shanties with no proper planning going into making of houses. People point at Old Delhi and say it will collapse, I think most of the modern Delhi will also collapse. 

The picture I see in my mind is quite grim.
  • Most of the water piping is old piping. It does not need an earthquake to break those pipes and every few months we see news reports of a broken pipe flooding a road in some part of Delhi. These will all happen on the same day. The basic necessity of life will not be available. 
  • Electricity will be the other big hit. People in many parts of the city steal electricity by hooking the overhead lines. Just raise your head above the streetline in any congested locality and you can see the messy mesh of wires. Sparking will cause massive blackouts and fires in most parts of the city. 
  • Houses constructed dont follow the guidelines laid down for earthquake safety. In Many places you have a large area of slums. These will all collapse.
At all this, there is hardly any disaster plan in place. Delhi had a mock earthquake drill a few weeks back - but that assumes that people in Delhi will help out the people in Delhi. There is no plan for getting aid from other parts of the country into Delhi. 

All in all, if this becomes a reality - we are in Deep Shit. 

Hindi on an Android Phone

This is just an app I am developing on the side. It uses Devanagari scripts to render the text, rather than displaying images as most of the similar apps in the Android Markets do.

Part 1 of the problem was how to load the hindi font in the code and get the display to render it. If you try this on a phone without Hindi support - which is the vast majority of the phones out there, then all you see is squares instead of the text.



This was pretty simple. If you want to load a custom font, this is what you do :


  1. Get the specific font you want to bundle with your application. Lets call it Hindi.ttf
  2. Put it in your project under the "assets/fonts" folder. You will need to create the folder 'fonts'. Not necessary to have the exact name, but I use it as it denotes what the files int the folder are. 
  3. Now you need to tweak your code a bit as you need to load the font before you render the text. This is quite simple - use something like the following :


//Font work
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Hindi.ttf");
// Get the UI element handles
mText = (TextView)findViewById(R.id.body_text);
// Set the title and read the file for the text
mText.setText(readRawTxt(pName));
mText.setMovementMethod(new ScrollingMovementMethod());
mText.setTypeface(tf); // setting font for the text. 
 Now when you run the program, you will get the fonts loaded and rendered, which brings use to Part 2 of the problem.

Part 2 of the problem is that the maatras are not getting aligned properly. For this I have so far not been able to figure out how to fix it. If you know , let me know. If I get to know about it, I will update this post.