Tuesday, March 18, 2014

Use bitly python to shorten the url

We are using bitly to provide short url to users, which is little easy to look at visually and also (probably) motivate user to share instead of long long urls.  You might have seen those on while trying to tweet the link or share it on Facebook.

We are using Django for our web app, and getting short url from bitly while sending certain links in the email to user and provide it on the app to enable sharing.

- Get your code from bitly account, you can get it from settings > Advanced > Legacy API Key



- Install bitly-python-api - Its official api provided by bitly for python.

- Now in following quick steps you can get the short URL:

import bitly_api
con = bitly_api.Connection(
                    'myapp',
                    'R_12345....'
                )
shorten = con.shorten('https://myapp.com/abc/?item=55678')
shorten_url = shorten['url']
//output - https://bit.ly/xyz123

bitly_api takes care of underlying connection complexity, if you using any other platform you can find related packages and instruction over here - http://dev.bitly.com/code_libraries.html


Monday, March 10, 2014

Update Django on Webfaction

We are using webfaction for our Django Application, and its been really awesome service for the Django apps. Most of the things are configurable from their support dashboard.

Now you get to choose the Django and python version when you setup your application first time. However, later if you want to upgrade to newer version of the Django as it made available by Django community, you can follow below steps. In this example my current version of Django is 1.4.1 and I am going for Django 1.4.5.

Please note, following steps are just to update the Django version, if there are functional, API, Model query, settings or any other changes required inside your application, you will need to refer to Django official documentation or migration guide provided on the Django.

Here are the steps to upgrade Django in Webfaction -

1. Go to your App directory. 
> $HOME/webapps/

2. Get the version you want. (here we are extracting 1.4.5)
> wget https://www.djangoproject.com/download/1.4.5/tarball/

3. Extract.
> tar -zxvf Django-1.4.5.tar.gz

4. Rename the existing one. (existing django to django.old)
> mv lib/python2.7/django lib/python2.7/django.old

5. move the new one to lib (now as we have renamed, we can move the content from new django package to lib/python 2.7)
> cp -R Django-1.4.5/django lib/python2.7

6. move the management scripts (copy management script for wsgi, and app creation etc.)
> cp Django-1.4.5/django/bin/* bin
7. Restart and make sure it runs fine.
> apache2/bin/restart

8. delete the extract and tar (only after checking that it works fine, you can remove the floating folder and file from the directory.)
> rm -rf Django-1.4.5*

And that should do the trick! Feel free to drop message or email if you run into any issue.

Thanks.