Thursday, April 26, 2012

fugitive.vim is awesome. So is git interactive rebasing

I have finally started forcing myself to use vim-fugitive. I have to agree that it is "so awesome, it should be illegal". I ran into a problem within the first hour and vim was shouting This is not a preview window everytime I wanted to :Gstatus. I couldn't find a solution for this on the internet and after a few experiments and reading docs, fixed this with :set previewwindow in the Gstatus window. I have always used windows instead of tabs in Vim. But, I started seeing a big use-case for tabs - being able to edit some file without disturbing the existing windows.

I have tried git rebase --interactive ID for a very short time when I was playing githug. Today, I had to use it to remove a change accidentally added in a commit. Interactive rebasing was fun and all I had to do was running the command. Didn't need to read tutorials or go through man pages.

In our custom CMS, we had location details as text fields on multiple models. This has led to fragmentation of city, state, region names. So, we had to convert these text fields to FKs so that the new ones will be through look-up but not manual entry. We have gone one step further and created a Location model which has FKs to City, State, Country and other models and added a reverse generic relation to Location from our models. The immediate problem with this approach was that we had used obj.city, obj.state etc. in our templates. I came up with a simple solution for this -

    class Base(models.Model)
        // model code here

        @property
        def location(self):
            "caching location as it will be needed by 6 properties"
            try:
                return self._location
            except:
                self._location = self.locations.all()[0]
                return self._location

        @property
        def city(self):
            return self.location.city
 
City's __unicode__ method returns its name. We were also getting annotated list of cities and the number of events in the city.

Location.objects.values('city__name').annotate(num_of_events=Count('city__name')

returns dictionaries with city__name key when I wanted those with city key. This was solved by looping through the queryset and processing those dictionaries. I wish I could do something like SELECT name AS city FROM locations_city.

Wednesday, April 25, 2012

Waking up early and some django templating stuff

Ah, another long break. I have been waking up at 7am and reaching office before 9am. This gives me a lot of time to finish my day job. I have a feeling that it is easier to incorporate new habits one or two at a time. Earlier, I used to try to bring too many changes to the daily routine. That never worked and made me feel like I lacked the will to change. This time, I am not biting off more than I can chew. So, for the next month, the resolution is -
I will wake up at 7am and hit the pillow by 12pm
I have been writing a lot of Django templates at work. Recently came across firstof which is a good way to avoid the if-else-endif template. I have always wanted to have a neater way to add context to include templates. So, I used to surround include templates with the with tag. I found that the include tag takes arguments which will be added to the context.

{% include "templates/search_form.html" with placeholder="Search projects.." %}

looks much cleaner than

{% with placeholder="Search projects.." %}
    {% include "templates/search_form.html" %}
{% endwith %}


My ordered-lists were showing with bullets instead of numbers. Learned about list-style css property. I need to understand list styling better.

Phaneendra gave a talk introducing Sencha for mobile app development. Thanks to the framework, he had been pulling his hair almost everyday since he started using it. After the talk, Theju demoed kvm and discussed the problems he had faced getting git and a django project working on Windows. We discussed several ways to make it easy for new people to get started with an old project, but couldn't come to a good conclusion.