Thursday, September 12, 2013

iter() returned non-iterator of type '_timelex'

If you running into issue -  iter() returned non-iterator of type '_timelex', here is the solution -

You got python-dateutil 2.0 with python 2.7 which is not compatible. What you need is, install older version of python-dateutil, so downgrade to python-dateutil==1.5


In order to downgrade the version you can get details over here.

Django - generate list of individual item from queryset result


How to get particular value from Django queryset result set -

Lets say you only one field 'question_id' from Paper object -

> Paper.objects.values('question_id')
>[{'question_id': 311L}, {'question_id': 310L}, {'question_id': 291L}, {'question_id': 302L}, {'question_id': 301L}, {'question_id': 300L}, {'question_id': 299L}, {'question_id': 298L}, {'question_id': 294L}, {'question_id': 293L}, {'question_id': 282L}, {'question_id': 281L}, {'question_id': 235L}, {'question_id': 234L}, {'question_id': 233L}, {'question_id': 47L}, {'question_id': 276L}, {'question_id': 48L}]

Now if you want to make list of this question_ids, one way is to loop thru the list of dict and fetch the value, but easy way is following -

>Paper.objects.values_list('question_id', flat=True)
>[311L, 310L, 291L, 302L, 301L, 300L, 299L, 298L, 294L, 293L, 282L, 281L, 235L, 234L, 233L, 47L, 276L, 48L]

Thursday, August 22, 2013

jquery event on addClass or show/hide

Its often require to fire some event when new class is getting added to the element, or when element is moving from hidden state to visible state or otherwise.

I come across similar scenario while rendering graph using jqplot library. Though if you have div hidden and you try to plot the graph on it, it won't display the graph when you make the parent variable visible. In similar situation it requires replot (which is basically redraw of the element.)

As there is no 'hide' event in DOM, so we will not be able to bind the event. Other way to resolve this issue is by Javascript MutationObserver. In which you can select target node (element) and observer instance.

Though you can find many complex solution on web, here is the simple and neat way to do it using jQuery.

Lets say you have element with class 'xyx', and upon making it to visible you would like to fire few more calls and refresh certain things on the page. You can do it using trigger -

$(".xyz").show();
$("xyz.").trigger('cssClassChanged');

The similar calss cssClassChanged needs to be listen somewhere that its been triggered, so its almost like publisher - subscriber model. Here is the sample -

$('.xyz').bind('cssClassChanged', function(){ perform your steps });


Easy and simple solution.

Monday, July 29, 2013

Setup email notification for team on github commits

Once more than one person start working on some code its good to get notification on what other team mates are committing and with git repo its easy to setup this with help of google groups.

  1. Create a new google group. Add one member - noreply@github.com
    Add the team/users email address which should be same as their github email id, the one they using it to access repo.
  2. Go to Github repository.
  3. Click on Settings from right navigation.
  4. Click on Service Hooks from left navigation.
  5. Click on Email - Provide google group name you have created (@googlegroups.com).
  6. Click Update the settings.
  7. Check its working fine by click on 'Test Hook'.

At the end you should get sample test email with last commit. After this setup all team members/members of google groups will get email upon commit from anyone on the repo.

Thursday, July 25, 2013

Django inline extend from different pages based on condition

Its been sometime I have posted anything on this blog. Recently I came across nice inline syntax if you need to extend the pages based on certain condition. e.g. I have container master page which i want to choose differently if user is logged in or not -

{% extends user.is_authenticated|yesno:"my_home.html,base_home.html" %}

It checks if person is authenticated or not, if authenticated it will extend my_home.html otherwise it will extend base_home.html


Thursday, May 2, 2013

Downgrade the pip installed package



While installing something on my local for my application I run into problem where it replaced (overwrote) one the of the package which wasn’t compatible with other applications I was using. So basically I was dependent on two applications which were dependent on another third party application but not of the same version.

Of coures, I was not ready for that upgrade, so I had to downgrade the version manually for that package, I am sure people run into similar problem often. 

Here are the steps I followed -

In this case Django 1.5 was installed, so it removed my existing Django 1.4.5 and installed 1.5

To downgrade and again go back to Dajngo 1.4.5, you need to locate the egg file for Django which normally you can find it in your vritual env path -

//lib/python2.7/site-packages/Django-1.5.1-py2.7.egg-info

Delete this file, and install 1.4.5 manually with following command -

pip install django==1.4.5

You can follow the same instruction for any other package.

Wednesday, February 27, 2013

Django admin staff authentication on your webapp views


Lets say you have certain views you want to control the access and should be only accessible by staff members.

Its two lines of code change and you are done. 

from django.contrib.admin.views.decorators import staff_member_required

in your module’s views.py

@staff_member_required
def staffdashboard(request):
            ….
            ….
            return render_to_response(‘dashboard.html',context_instance=RequestContext(request))

When user tries to access the view, it will check if user is authorized staff member. If not it will prompt for admin login screen.