Archive for January, 2007

Templates in Vim

January 19, 2007 Leave a comment

I always wanted something that will automatically insert a few header files and macro definitions for newly opened C/C++ files. NOT that i solve too many problems these days(it’s been a looooooong time now 😦 ) but the lazy programmer in me wouldn’t stop craving for one. Lot of people asked me to switch to emacs which apparently has something builtin for this purpose, but G0SUB came to my rescue and showed me this plugin for Vim.

And trust me, it works like a charm 🙂

Categories: Personal, Technology

Custom Change Manipulators in Django(oldforms)

January 17, 2007 2 comments

To tell you quite frankly, I never got to use an inbuilt manipulator while coding for Hackzor. I guess the primary reason being that my forms either had optional fields or i had to deal with two models at a time. I still could have avoided going custom, i guess i didn’t want to clutter the view with manipulation code.

So, on my path to custom manipulatorness, i missed this one HUGE turn called custom change manipulators. The docs didn’t say much about them and the comments weren’t too explicit either. After many hours of torturing unsuspecting victims on #Django, i finally found out that custom manipulators are really ‘custom’ – you’re responsible to storing the object and creating a dictionary for the form wrapper. It kinda sounds dumb right now, but it felt as hard the Da Vinci Code when i didn’t know the solution. So, in aid of all those poor, lost souls like me out there, Here’s how to make your own Custom Change Manipulator

The example i’m going to show you is naturally from Hackzor. I needed a custom change manipulator inorder to be able to put out a form where each contestant can edit his/her team details. List below, is the custom change manipulator that i wrote for the form:

class ChangeDetails (forms.Manipulator):
    ''' Change Membership details '''

    def __init__(self, user_id):
            self.original_object = User.objects.get(id=user_id)
        except User.DoesNotExist:
            from django.http import Http404
            raise Http404
        self.fields = (
                             length=20, maxlength=20,
                             length=20, maxlength=20,

    def isValidEmail (self, field_data, all_data):
        """ Checks if there is an already existing email and raises error if so"""
        except User.DoesNotExist:
        raise validators.ValidationError('The email "%s"
                                                 is already registered.' % field_data)

    def save(self, new_data):
        """ Saves The user object into the database
          with score set to 0 and is_active set to false"""
        self.original_object.first_name = new_data['first_name']
        self.original_object.last_name = new_data['last_name'] = new_data['email']
        print 'Saving UserProfile updation'
        return self.original_object

    def flatten_data(self):
        return {
                'username' : self.original_object.username,
                'email' :,
                'first_name' : self.original_object.first_name,
                'last_name' : self.original_object.last_name,

Points where it differs from an add manipulator:
1. The __init__ function of my manipulator has an extra argument from which i get the primary key of the object to be changed and assign the object to self.original_object
2. save is implemented like in the custom add manipulator, expect that it meddles with the existing object rather than creating a new one.
3. flatten_data is the function that will return a dict of the variables that will eventually be passed to the formwrapper to be loaded on the page. I will be calling this function from my view

Now lets take a look at the view to complete the picture

def change_details(request):
    ''' Change details of existing users '''

    manipulator = ChangeDetails(

    if request.method == 'POST':
        new_data = request.POST.copy()
        print 'By post', new_data
        errors = manipulator.get_validation_errors(new_data)
        if not errors:
            new_user =
            return render_to_response('simple_message.html',
                {'message' : 'Your Details have been updated'}, RequestContext(request))
            print 'Errors'
        errors = {}
        new_data = manipulator.flatten_data()
        print new_data
    form = forms.FormWrapper(manipulator, new_data, errors)
    return render_to_response('change_profile.html',
            {'form': form}, RequestContext(request))

    return render_to_response('register.html', {'form':form}, RequestContext(request))
Categories: Technology

All’s well that ends well

January 15, 2007 1 comment

I’m going to swear again for the n’th time that i will start blogging regularly and that i will do put in some content here. But before that, an update on the Kurukshetra Online Programming Contest. Fate struck us on new year’s eve with our server we hosted the site on crashing for reasons that were beyond our control. we couldn’t get it up on time and had to eventually cancel the contest and hold it again. we rescheduled it again to the 14th of January and this time making sure that we had a kick-ass server running underneath. The acutal contest went well and i could well say that hackzor behaved as well as expected.

Hackzor has grown into a nice little web app now, albeit without the code being in the trunk. The current trunk is HUGELY out of version and my immediate goal is to fix the trunk right now. With me fixing the trunk, we will roll out a version which we will Christen version 0.1. The contest has also shown me a thousand places where i could change my design to improve ease and performance even adopting more simpler ways to do it. I do see good things coming up for hackor at the moment and would also continue to offer it for other Online Programming Contest that want hackzor underneath.

Prashanth has compiled a list of statistics that he has picked up from the server. Do read his article if you’re inclined.

Categories: College, Technology