Saturday, March 15, 2014

Filtering django admin inline dropdown for a foreignkey based on the parent

The original concept is explained here. But overriding the method 'formfield_for_dbfield' as in that link doesn't show up the column name. To avoid that override the 'formfield_for_foreignkey' with same logic but just build the queryset as necesssary.

Following is the snippet from my code:

class NewsBentInline(admin.TabularInline):
    model = NewsBent
           
    def __init__(self, model, admin_site, dist=False):
        self.dist = dist
        super(NewsBentInline, self).__init__(model, admin_site)
           
    def formfield_for_foreignkey(self, field, request, **kwargs):
        parent_article = self.get_object(request, Article)
        queryset = None
        if field.name == "parliament":
            if parent_article.district == 'District_name1':
                queryset = Parliament.objects.filter(dist__name="District_name1")
            else:
                queryset = Parliament.objects.all()
        if queryset is not None:
            kwargs["queryset"] = queryset
        return super(NewsBentInline, self).formfield_for_foreignkey(field, request, **kwargs)

    def get_object(self, request, model):
        object_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
        return model.objects.get(pk=object_id)

Thursday, March 13, 2014

Kibana on ubuntu linux

Install Java:
sudo apt-get install openjdk-7-jre

Download logstash from http://www.elasticsearch.org/overview/logstash/.
It comes bundled with elasticsearch and kibana.

Setup/Installation instructions for crawler - scrapy for scraping pages and Django for frontend

Requirement:

Ubuntu 12.04 LTS.
Python version: 2.7.3


Setup:

sudo apt-get install python-pip
sudo pip install virtualenvwrapper

copy the following to .bashrc:
"""
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/projects
source /usr/local/bin/virtualenvwrapper.sh
"""

mkvirtualenv crawler
workon crawler

Dependent Packages Installation:
sudo apt-get install libxml2-dev
sudo apt-get install libxslt-dev
sudo apt-get install python2.7-dev
sudo apt-get install python-scrapy
sudo apt-get install libffi-dev
pip install Scrapy

If the above packages are already available in global site packages you can use them by running following virtual env command, more info here:
toggleglobalsitepackages

mysql:
sudo apt-get install python-mysqldb
sudo apt-get install mysql-client-core-5.5
sudo apt-get install mysql-server-core-5.5
sudo apt-get install mysql-server
pip install SQLAlchemy

Django:
pip install Django==1.6.1

BeautifulSoup:
pip install beautifulsoup4

NodeJS for running javascript on terminal/commandline:
http://nodejs.org/download/

Set the environment variable:
Be careful about the ordering of paths, virtual env should be followed by system installed python
export PYTHONPATH="/home/vjonnak/.virtualenvs/crawler/local/lib/python2.7/site-packages:/usr/lib/python2.7/dist-packages"

Create the DB to support utf8 encoding:
create database news_db  DEFAULT CHARACTER SET utf8   DEFAULT COLLATE utf8_general_ci;





Adjust resolution of Oracle VM virtualbox with ubuntu

To adjust Screen resolution:

Click on 'Devices' on the virtualbox menu -> 'Insert Guest additions CD Image'
Login to Ubuntu:
Devices -> VBOXADDITIONS
On the top right end of explorer click the button "Open Autorun prompt"
Restart Ubuntu

Sunday, March 9, 2014

Extend/Reuse the power of Django Admin model


urls.py:

"""
urlpatterns = patterns('',
  url(r'articleview/$', 'myapp.views.admin_reuse'),
  url(r'^admin/', include(admin.site.urls)),
)
"""

admin.py:

"""
from myapp.models import Article

class ArticleAdmin(admin.ModelAdmin):
    <...>

admin.site.register(Article, ArticleAdmin)
"""

views.py:

"""
from django.contrib.admin.sites import AdminSite
from myapp.models import Keyword, Article
from myapp.admin import ArticleAdmin

def admin_reuse(request):
   article_admin = ArticleAdmin(Article, AdminSite(), dist)
   return article_admin.changelist_view(request)
"""