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(
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.


Wednesday, February 19, 2014

Raphael js - Uncaught TypeError: Cannot call method 'enable' of undefined

When enabling dragging in Raphael js, if you are using raphael-draggable, then you might run into following error when you try to enable the dragging on any element -

Uncaught TypeError: Cannot call method 'enable' of undefined

That line is -

The issue is in Raphale 2.0 context is broken, and in order to fix that you have can add following function -

// to fix broken context issue with Raphael 2.0

Raphael.fn.fixNS = function(){

    var r = this;

    for (var ns_name in Raphael.fn) {

        var ns = Raphael.fn[ns_name];

        if (typeof ns == 'object') for (var fn in ns) {

            var f = ns[fn];

            ns[fn] = function(){ return f.apply(r, arguments); }



Once you add it, call that function below your Raphael block initialization -

var paper = new Raphael(obj.attr('id'), chart_width, chart_height);

This should be able to fix the issue and you will be able to enable dragging for individual element or your Raphael object.


Sunday, January 12, 2014

Error on upgrading setuptools using pip

Today I upgraded pip using below command as for some reason some of the package it was not able to find and throwing strange error that its not able to find the requested package -

pip install --upgrade pip

Existing installation was 1.0.2 and it installed the 1.5

After the upgrade, it started throwing following error on requesting any install

Wheel installs require setuptools >= 0.8 for dist-info support.
pip's wheel support requires setuptools >= 0.8 for dist-info support.
Storing debug log for failure in /Users/Jaimin/.pip/pip.log

What it means is my pip install 1.0.2 had old setup tools and after upgrading to 1.5 it needs newer version of setup tools.

Js-MacBook-Pro:dj16 J$ pip install --upgrade setuptools
Wheel installs require setuptools >= 0.8 for dist-info support.
pip's wheel support requires setuptools >= 0.8 for dist-info support.
Storing debug log for failure in /Users/Jaimin/.pip/pip.log

So it still throws the error on upgrade of setuptools, you need to use following command to upgrade -

pip install setuptools --no-use-wheel --upgrade

It will be able to upgrade the setuptools and pip should work fine after this.

Thursday, January 9, 2014

Django 1.6 global name 'timezone' is not defined

If you are following the documentation to run the sample app, while doing date time comparison you might run into the issue on following code -

 def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

It complains - global name 'timezone' is not defined

because it needs explicit import -

import datetime
from django.utils import timezone

Once you add it should be resolved. 

Tuesday, December 24, 2013

Size of dictionary in jquery

One would think that they can apply size of length function directly and get it, but its little more than that. You can not apply the length property or size function, both only works with list.

Dict is an Object and it doesn't have size method, and if you do length it will give 'undefined'.

Solution - Get the array of keys and perform length.

> dictObj = Object {PointAArray[23]PointBArray[23]}
> 2

Note - It won't works > IE8

Other robust way is as described in this SO answer, add size function on Object and use it across the app and it will work cross browser.

Short answer is there is no built-in way.

Saturday, October 19, 2013

Site matching query does not exist. Lookup parameters were {'pk': 1}

Django 1.5.1

I created sample app and run the syncdb (python manage.py syncdb) and it created default tables for the application. It didn't created the tables for the app's model. For that I had to run -

python manage.py syncdb --all

It took care of the other tables creation.

Though while accessing the app I run into following error -

Traceback (most recent call last):
  File "/Users/Jaimin/Apps/Github/kqotes/quote/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in get_response
    response = middleware_method(request, response)
  File "/Users/Jaimin/Apps/Github/kqotes/quote/lib/python2.7/site-packages/django/contrib/redirects/middleware.py", line 23, in process_response
  File "/Users/Jaimin/Apps/Github/kqotes/quote/lib/python2.7/site-packages/django/db/models/query.py", line 389, in get
    (self.model._meta.object_name, kwargs))
DoesNotExist: Site matching query does not exist. Lookup parameters were {'pk': 1}
Internal Server Error: /api-auth/login/

Tried different things, though couldn't figure anything.

Solution - After deleting the schema, I run python manage.py syncdb --all

bingo! All worked fine, and magically error is gone.

The issue was first time it didn't created entry for the app in site table (Its part of default tables when you do syncdb.)

select * from django_site

You can add manual entry, or drop the schema and run it again. That will assign the primary key object and app will come up fine.

Any other comments, observation, feel free to drop in comment.