def user_profile_edit(request):
    """Edit user's/Coordinators profile.

    Arguments:
    - `request`:
    """
    context = RequestContext(request)
    print request.user
    user = get_object_or_404(User, username=request.user)
    old_username = user.username
    print user.first_name
    print user.last_name

    coordinator = get_object_or_404(Coordinator, user=request.user)
    print coordinator.contact
    print coordinator.picture

    aakashcentre = get_object_or_404(AakashCentre, coordinator=coordinator)
    print aakashcentre.name

    if request.method == 'POST':
        print "We've a request to register"
        #aakashcentreform = AakashCentreForm(data=request.POST)
        coordinatorform = CoordinatorForm(data=request.POST, instance=coordinator)
        userform = UserForm(data=request.POST, instance=user)

        if coordinatorform.is_valid() and userform.is_valid():
            print "Forms are Valid"
            user = userform.save(commit=False)
            if old_username == user.username:
                print "Username unchanged"
            else:
                print "Username changed!. Deactivating old user."
                old_username = get_object_or_404(User, username=old_username)
                old_username.is_active = False
                old_username.save()
            # print user.username
            # print user.first_name
            # print user.last_name
            user.set_password(user.password)
            user.save()

            coordinator = coordinatorform.save(commit=False)
            # print coordinator.contact
            if 'picture' in request.FILES:
                coordinator.picture = request.FILES['picture']
            coordinator.user = User.objects.get(username=user.username)
            coordinator.save()

            # Save Aakash Centre details with the new Coordinator's profile.
            # aakashcentre = aakashcentreform.save(commit=False)
            aakashcentre.coordinator = Coordinator.objects.get(user=coordinator.user)
            aakashcentre.save()
            print aakashcentre.ac_id

            messages.success(request, "Profile updated successfully.")
            # return HttpResponseRedirect('/user/profile/')
        else:
            if coordinatorform.errors or userform.errors:
                print coordinatorform.errors, userform.errors
    else:
        # aakashcentreform = AakashCentreForm(instance=aakashcentre)
        coordinatorform = CoordinatorForm(instance=coordinator)
        userform = UserForm(instance=user)

    context_dict = {'coordinatorform': coordinatorform,
                    'userform': userform}
    return render_to_response('profile_edit.html', context_dict, context)
def register(request):
    """Registeration Form.
    
    Arguments:
    - `request`:
    """
    context = RequestContext(request)
    
    if request.method == 'POST':
        print "We've a request to register"
        aakashcentreform = AakashCentreForm(data=request.POST)
        coordinatorform = CoordinatorForm(data=request.POST)
        userform = UserForm(data=request.POST)

        if aakashcentreform.is_valid() and coordinatorform.is_valid() and userform.is_valid():
            print "Forms are Valid"
            user = userform.save(commit=False)
            print user.username
            print user.first_name
            user.set_password(user.password)
            user.save()

            coordinator = coordinatorform.save(commit=False)
            print coordinator.contact
            if 'picture' in request.FILES:
                coordinator.picture = request.FILES['picture']
            coordinator.user = User.objects.get(username=user.username)
            coordinator.save()
            
            aakashcentre = aakashcentreform.save(commit=False)
            aakashcentre.coordinator = Coordinator.objects.get(user=coordinator.user)
            aakashcentre.save()
            print aakashcentre.ac_id
            email_subject="[aakashlabs.org] New Aakash Center has been registered."
            email_message="""
New Aakash Center has been registered.

Details:
Name: """ + aakashcentre.name + """
City: """ + aakashcentre.city + """
State: """ + aakashcentre.state + """
Coordinator's Name: """ + coordinator.name.first_name + coordinator.name.last_name + """

Waiting for you approval"""
            send_mail(email_subject, email_message,
                      '*****@*****.**',
                      [
                          '*****@*****.**',
                          '*****@*****.**',
                          '*****@*****.**',
                      ],
                      fail_silently=False)
            messages.success(request, "Form successfully submitted. Waiting for\
            activation from admin.")
            return HttpResponseRedirect('/ac/register/')
        else:
            if aakashcentreform.errors or coordinatorform.errors or userform.errors:
                print aakashcentreform.errors, coordinatorform.errors, userform.errors
    else:
        aakashcentreform = AakashCentreForm()
        coordinatorform = CoordinatorForm()
        userform = UserForm()
        
    context_dict = {'aakashcentreform': aakashcentreform,
                    'coordinatorform': coordinatorform,
                    'userform': userform}
    return render_to_response('ac/register.html', context_dict, context)