Monday, 11 April 2011

Model form - Get it working

Started playing around with python and django last week. There is a lot of stuff that django provides us to build a basic web app. Despite its very good documentation, I found it hard to get the model form working. Model forms are useful when you have a basic model with some fields defined and you need a form to either add or edit the model. Writing views for such models is redundant, since both of them are going to contain the same information anyway and its prone to human error. Model form comes in handy in such situations.

Lets get started with a basic model that holds an item, its price and the date when it was added:

from django.db import models

class Transaction(models.Model):
item = models.CharField(max_length=200)
price = models.IntegerField()
date_added = models.DateTimeField('date added')

A model form for the above would look like:

from django import forms

class TransactionForm(forms.ModelForm):
        class Meta:
                model = Transaction
        date_added = forms.DateField(initial=datetime.date.today)

To display the model form in a view, we need to have a html file that'll be used to render the model form that is passed as a parameter to that. Using django's shortcuts we can render the form using the code below:

def index(request):
        return render_to_response('managExpenses/transaction_form.html', {'form':TransactionForm}, context_instance=RequestContext(request))

If you are using django's generic views, then the code will look like:

def index(request):
        return create_object(request, form_class=TransactionForm)

In the above code, the create_object method will look for the transaction_form.html placed in the templates folder and use it to render the form.

The transaction_form.html looks something like:

<form action="/some/url/" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Submit"/>
</form>

The model form is replaces the {{form}} in the above html. The csrf_token is used by the CSRF (Cross Site Request Forgery) protection and needs to be included in every form that posts to your internal url. 

You can now go ahead and modify the urls.py to assign a url pattern to the above view and see it working.

Thursday, 7 April 2011

EMU in Chennai - The good, the Bad and the Ugly

Its been more than a year and a half since i started commuting through trains. So, I thought it'd be a good time to just take a look back at stuff I've observed in this journey.

The Good

The electric train in chennai has got a very good onboarding process. If you want to get into a train all that you need to do is to just stand in the vicinity of the entrance to compartment. There are people out there who'll push you harder and apply all sorts of peer pressure to make sure that you are into the train.

Jesting apart. Its an easy way of commuting (forget the crowd factor for a moment). The frequency of trains is yet another WOW factor. They have trains running every 8 mins during the peak hours, without which they wouldn't be able to handle the crowd. The huge advantage is that it skips the road traffic and you exactly know how much time it is gonna take for you to reach your destination.

You get a quality time when you are travelling that you can use to read (newspapers, books), listen to music or you can even think about your next blog post.

The Bad

I did say that listening to music is a good idea, but listening to FM when you are in the train is generally a bad idea. The reception is never good. If at all you try doing that you'll end up hunting for the position and orientation to tap the signals.

One thing that you can never miss when you travel in a local train is the scribblings all over the compartment wall. Looks like people still posses the age old skills of cave or rock art. I don't know when do they get to do that. But, there isn't one single compartment that doesn't have such junk. You can find all sorts of thathuvams and advice written out there. The worst thing is the kind of mistakes those people make. Some examples here:

Girls are rocks
Smart dueds
Ice melts when heated. Eyes melts when hurted.

The Ugly

The most annoying factor while travelling in train is the herd of people who occupy almost half the pathway and sit there. For those lucky people who haven't experienced it yet, here is a sample pic:

Those morons are completely void of common sense and they dont give a damn if the train is crowded or not. You can ask me why cant people just tell them to get up if they are not using their senses. Well, thats quite a tough job and you cant be successful unless you have enough experience in street fights. The best part is when they advice people to go inside the compartment as if they are very much concerned and the rest of the crowd standing there is stupid.

There is yet another category of people who just buy all the edible items sold by any vendor and consume it in the compartment itself. I'm not bothered about what they eat. But am irritated with what they do. Here it goes. They buy a fruit. Peel the skin and drop it in the compartment itself. Eat the fruit. If at all it has a seed, they'd drop that as well in the compartment. What the !@#$?? Will they do the same in their own house?? You have trash bins provided in every damn station. If they cant wait till then, they can at least throw it outside. After all its an organic waste and will get decomposed in the course of time.

Friday, 1 April 2011

Tamil in android

Displaying tamil characters in android can be easily achieved by using TSCII (Tamil Standard Code for Information Interchange) fonts. TSCII is an extended ASCII encoding standard. Unlike ASCII which uses 7 bits for encoding, TSCII makes use of all the 8 bits in a byte. The first 128 bytes remains the same as that of ASCII.  The tamil vowels and consonants along with the common prefixes, suffixes and other special characters fill up the rest of the 128 bytes.


You can place the TSCII font in the assets folder of your android application and set it to any view where you need to display tamil text.

Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/tscFont.ttf");
TextView textView = new TextView(context);
textView.setTypeface(tf);


If you have the text in unicode format, there are online tools available that can help you convert the tamil text from unicode to TSCII format and vice versa.