Create Empty Object in Python

I need to create dummy object in python for testing purpose. Here is how to create dummy object in python on the fly using “type”.

request = type('request', (), {})()  # Create request object on the fly
print request is None                # False
print request                        # <__main__.request at 0x1073f8590>

Reference: http://stackoverflow.com/a/19476841/1936697

Deploying WordPress in Heroku

Download latest version of wordpress.

$ wget http://wordpress.org/latest.zip
$ unzip latest.zip
$ mv wordpress mywebsite # optional if you want to rename the folder.

Create “Procfile” and put this following line.

web: vendor/bin/heroku-php-apache2

Create “composer.json and put this following line.

{"require": {"php": "5.6.27"}}

Configure the wordpress by creating “wp-config.php”. WordPress already had the template, so you just need to rename it.

mv wp-config-sample.php wp-config.php

Edit “wp-config.php” and replace following line.

// ** MySQL settings - You can get this info from your web host ** //
$url = parse_url(getenv('DATABASE_URL') ? getenv('DATABASE_URL') : getenv('CLEARDB_DATABASE_URL'));

/** The name of the database for WordPress */
define('DB_NAME', trim($url['path'], '/'));

/** MySQL database username */
define('DB_USER', $url['user']);

/** MySQL database password */
define('DB_PASSWORD', $url['pass']);

/** MySQL hostname */
define('DB_HOST', $url['host']);

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

define('AUTH_KEY',         getenv('AUTH_KEY'));
define('SECURE_AUTH_KEY',  getenv('SECURE_AUTH_KEY'));
define('LOGGED_IN_KEY',    getenv('LOGGED_IN_KEY'));
define('NONCE_KEY',        getenv('NONCE_KEY'));
define('AUTH_SALT',        getenv('AUTH_SALT'));
define('SECURE_AUTH_SALT', getenv('SECURE_AUTH_SALT'));
define('LOGGED_IN_SALT',   getenv('LOGGED_IN_SALT'));
define('NONCE_SALT',       getenv('NONCE_SALT'));

Now is the deployment process, ensure you have installed heroku toolbelt and already sign in on it.

$ git init
$ git add -A
$ git commit -m "basic setup wordpress heroku"
$ heroku create
$ heroku addons:add cleardb

After that, put following environment variables with new values. You can generate those in here.

heroku config:set AUTH_KEY=''
heroku config:set SECURE_AUTH_KEY=''
heroku config:set LOGGED_IN_KEY=''
heroku config:set NONCE_KEY=''
heroku config:set AUTH_SALT=''
heroku config:set SECURE_AUTH_SALT=''
heroku config:set LOGGED_IN_SALT=''
heroku config:set NONCE_SALT=''

Now we need download all the dependencies, ensure you have composer installed. If you are not, you can install following this.

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install

Last step is deployment.

$ git add composer.lock
$ git commit -m "final step"
$ git push heroku master

Reference: https://ksylvest.com/posts/2014-05-02/deploying-wordpress-to-heroku

Add HTTP_REFERER in Django Test Client

I’m testing my views in my django project and got “None” error because my view returning redirect to “HTTP_REFERER” that I get from HTTP Header.

In django test client apparently all request does not contain any HTTP header.
Here is my views:

class DeleteCategoryView(TemplateView):

    def post(self, request, *args, **kwargs):
        ...
        return redirect(request.META.get('HTTP_REFERER'))

And here is how I add “HTTP_REFERER” in my test client.

def test_delete_view(self):
    url = reverse('category:url-adm-delete')
    redirect_url = reverse('category:url-adm-category')
    response = self.client.post(path=url, data={'id': instance.pk}, HTTP_REFERER=redirect_url)
    self.assertRedirects(response, index_url)

Reference: http://stackoverflow.com/a/11819426/1936697

Register SSH Key To DigitalOcean

I just added new ssh key from my laptop into my digitalocean settings, to ensure next login in my laptop, I don’t need to type my password again and again. But after adding my ssh key and trying to login to my VPS, the ssh still asked me for a password.

Here is how I fix this following the documentation. I assuming that you guys know how to generate new ssh key. So below is next step after generating new ssh key.

$ cat ~/.ssh/id_rsa.pub | ssh root@[your.ip.address.here] "cat >> ~/.ssh/authorized_keys"

After run single command above, I don’t need to type password once logging in to my VPS.

Reference: https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets