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.

No comments:

Post a Comment