Javascript

Jquery Datatables modify fnserverData and pagination

28 Jan , 2015  

Here is a quick snippet for custom AJAX data for Jquery datatables :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 var table = $('#table-index').dataTable({
                "serverSide": true,
                "searching": false,
                "ordering": false,
                "sAjaxSource": url,
                "bStateSave": true,
                "fnStateSave": function (oSettings, oData) {
                    localStorage.setItem( 'DataTables', JSON.stringify(oData) );
                },
                "fnStateLoad": function (oSettings) {
                    return JSON.parse( localStorage.getItem('DataTables') );
                },
                "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
                 oSettings.jqXHR = $.ajax( {
                   "url": sSource,
                   "data": {'page': Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1},
                   "success": fnCallback,
                   "error": function (e) {
                       console.log(e.message);
                   }
                 });
               },
                "columns": [
                    { "data": "name" },
                    { "data": "gender" },
                    { "data": "location" },
                    { data: null,
                      className: "center",
                      defaultContent: '<a href="#" class="edit_data"><i class="fa fa-pencil"></i> Edit</a> |  <a href="" class="remove_data"><i class="fa fa-trash"></i> Remove</a>'
                    }
                ],
            });

For pagination, the key is :

1
 "data": {'page': Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1},

Python

Using format instead of %s for string replacement

23 Jan , 2015  

Usually we use “%s” for string replacement in Python:

1
"asddas %s" % ('hello',)

There is better way to do it:

1
2
3
"hello {a} {b} {c}".format(a='error', b='hello', c='world')
"hello {0} {1} {2}".format("hello", "world", "fine")
"hello {}".format("hello")

Uncategorized

Decoder jpeg not available in Ubuntu 14.04

15 Jan , 2015  

To solve this issue :

1
sudo apt-get install libjpeg-dev libfreetype6-dev zlib1g-dev libpng-dev xfonts-base xfonts-75dpi libjpeg8-dev libjpeg-turbo8-dev

Django

Your branch and ‘origin/master’ have diverged, and have 1 and 1 different commit each, respectively.

15 Jan , 2015  

I got this problem:

1
2
3
4
5
6
λ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean

Solution

1
2
λ git reset --hard origin/master
HEAD is now at 63813ef fix localdb

, ,

Django

Django 1.7 reset migration solved

12 Jan , 2015  

Here is a quickstep to reset database migration in Django 1.7 :

1
2
python manage.py makemigrations --empty your_app
python manage.py makemigrations your_app

Django

Git rollback last commit in remote

12 Jan , 2015  

To rollback last commit :

1
2
git reset HEAD~1 --hard
git push -f

Django

Git checkout track from remote branch

12 Jan , 2015  

Let’s say somebody already push their branch in remote called “finances”. We want to fetch and working on this branch from our local. So, the solution :

1
2
3
4
5
git fetch --all

Fetching origin
From github.com:polatic/vasham-erp
 * [new branch]      finance    -> origin/finance

Then, we checkout with tracking

1
2
3
git checkout -t origin/finance
Branch finance set up to track remote branch finance from origin.
Switched to a new branch 'finance'

Now ready for working :

1
git checkout finance

Django

Accelerate django development server with django-devserver

9 Jan , 2015  

As we know what django development server running on single thread. This is make development a bit slow. There is some alternative, by using django-devserver

Here is the quickstep:

1. Install the app

1
pip install git+git://github.com/nealtodd/django-devserver

2. Edit your local_settings.py

1
2
3
4
5
INSTALLED_APPS =  ('devserver',) + INSTALLED_APPS

MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
   'devserver.middleware.DevServerMiddleware',
)

3. Then run

1
python manage.py runserver

Django

Ubuntu Check Memory Usage by Process

9 Jan , 2015  

Here is a quick snippet for check memory usage by process :

1
ps aux | sort -rn -k 5,6 | less

Django

Solve Django Filter queryset result duplicates with Pagination

5 Jan , 2015  

When we filter using multiple filter and mixed with django pagination, somehow the results are duplicates. This is incorrect. Apparently, looking at the source code https://github.com/alex/django-filter/blob/master/django_filters/filters.py#L105, I found this line:

1
2
3
4
q = Q()
for v in value:
    q |= Q(**{self.name: v})
return qs.filter(q).distinct()

By default, django-filter not using “AND” condition and that’s why it’s showing duplicates.

To fix this, then we can use distinct when passing object_list in paginator, example:

1
results = paginator(request, PlanningOrder, 'farmseason', object_list.qs.distinct())

Django

Setup Django and Timezone follow server datetime (Ubuntu)

5 Jan , 2015  

Here is quickfix to setup Django working with server timezone.

1. Set Server Timezone

Check by :

1
2
date
Mon Jan  5 10:54:38 WIB 2015

If this not correct or you want to change server timezone, then edit “/etc/timezone”

1
Asia/Jakarta

Then do :

1
2
sudo dpkg-reconfigure --frontend noninteractive tzdata
date

Now you should see the updated server time.

2. Django
We need to edit settings.py add edit this line:

1
2
TIME_ZONE = 'Asia/Jakarta'
USE_TZ = True

Restart your django server (uwsgi or wsgi) and do testing