Django – call url from JavaScript with dynamic parameters

I recently had to use JavaScript to call a URL from Django with dynamic parameter. Here’s what I found that did what I wanted:

function validateDeleteTenant(tenant_pk) {
	submitted = confirm("This would physically DELETE the tenant and all data from the server. Are you sure you want to continue? \n\nPlease note that a log will be submitted to provisioning team for auditing purposes.");
	if(submitted) {
		window.location='{% url deleteTenantFromServer 0 %}'.replace(/0/, tenant_pk);

PHP – Making API calls on the internet via Proxy

I had to make API calls to the internet from one of my servers that didn’t have connectivity to the outside world. I had IT create an internal proxy for me and I used it in the CURL to make the API call. Here’s the generic function that I created that would take care of using the proxy if stated to do so.


//-- Function to POST data to URL
function postArray($url, $arr_data, $setInternetProxy = false) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL, $url);
	if($setInternetProxy) {
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_POSTFIELDS, $arr_data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FAILONERROR,true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	$response = curl_exec($ch);
	$error = curl_error($ch);
	$arr_response = array();
	$arr_response['response'] = trim($response);
	$arr_response['error'] = trim($error);
	return $arr_response;

Changing author name for all git logs

I recently realized I had goofed up with setting up my GIT account with an incorrect name. I found this cool way to update the logs and update the username to the correct one:

Note: This would update all commits so far. If you wanted to modify commits for specific user, then you may have to write a bash script for it. You can find more details here: But the below would work fine if it’s only yourself in the previous logs.
Just execute this command within your GIT home repository:

git filter-branch -f --env-filter "GIT_AUTHOR_NAME='varun.verma'; GIT_AUTHOR_EMAIL=''; GIT_COMMITTER_NAME='varun.verma'; GIT_COMMITTER_EMAIL='';" HEAD 

Bash Alias – Switch user and go to switched user’s home directory

This one was a puzzle to solve. I wanted to create an alias so I can do the two things:
1. Switch to user automation
2. Go to automation’s home directory

I started by creating the following alias:

sa() {
    sudo su automation
    cd /home/automation

But this didn’t work and here’s the explanation.
After the first command “sudo su automation” is executed, the shell is now running as automation and the “cd /home/automation/” will be placed in the original user’s queue. After you exit user automation, the cd command will be picked from the queue and get executed. Therefore, I had to modify the alias to the following:

sa() {
    cd /home/automation
    sudo su automation
    cd ~

This time around, I am navigating to automation’s home directory first and then switching my user so I am already in the directory when I switch to the new user. The last command “cd ~” will be put in queue after “sudo su automation” is executed. And when I exit from automation, the “cd ~” will be executed from the queue and I will end up being in /home/varun.verma after I exit automation.

Postgres dump and restore

I never thought things would get so tricky with postgres dump and restore. Although it’s simple when done manually, but I was trying to automate the process of getting a dump from a remote server and restoring it locally. I was trying to use the “createdb” and “dropdb” command line tools to drop a db before I could restore the new dump – but with both the commands I wasn’t able to get around the password prompt. When I specified the -w (do not prompt for password) argument, the system doesn’t like it and throws an error.

dropdb: could not connect to database postgres: fe_sendauth: no password supplied

[Resolution: The user should be a superuser or owner of database in order to drop the datbase. I used this update “ALTER USER myuser WITH SUPERUSER;” to make the user a superuser and then use dropdb accountdb and createdb accountdb to drop and create the database respectively. After which I was able to restore the database from line 4 without any issue of having data conflicts]

So I had to finally use this technique to do this automated dump and restore process:
Note: .pgpass configuration is required to execute the following commands without password prompts.

1. Manually dump only the db schema from the source system [-w: without password prompt, -s: schema only]. This is a one time activity only.

pg_dump -hlocalhost -Uapiuser -ws accountdb > db_schema.sql

2. Manually create a database on the destination system with the necessary owner within the SQL prompt (assuming the user exists on the destination system)

CREATE DATABASE accountdb OWNER apiuser

3. Execute the following script on the source system to get the db dump [-w: without password prompt, -c: clean (drop) database objects prior to outputting the commands for creating them]

pg_dump -hlocalhost -Uapiuser -wc accountdb > automation_full.sql

4. Get the dump sql file locally on the destination system and restore the dump using the following command [-d: destination database, -f: input dump file]

psql -hlocalhost -Uapiuser -d accountdb -f automation_full.sql

TaDa.. Works like a charm! Try it out.

Linux – adding user with home directory and other settings

Add a new user:

useradd -m automation

The -m option would take care of adding a home directory for the user under /home

After adding a new user, I switched to the user and realized that the command prompt looks different. My own user had the command prompt like varun.verma@server-dev:~$ but for the new user, it only showed a $.

I looked through the changes in the /etc directory and found this different in the passwd file:

varun.verma:x:1002:1002:Varun Verma,,,:/home/varun.verma:/bin/bash

I replaced the sh for user automation with bash and that did the trick.