Showing posts from August, 2013

Django - Utility bash script to reset the db

The following bash script is used when I want to reset my Django app's db to a fresh state (with some initial data, fixtures) in some test cases:

#!/usr/bin/env bash
mysqladmin -u myuser -p'myuser_pwd' -f drop dj_ecap
mysqladmin -u myuser -p'myuser_pwd' create dj_ecap
/home/.venv/myenv/bin/python syncdb
/home/.venv/myenv/bin/python loaddata path/to/my/fixtures/*.json
mysql -u myuser -p'myuser_pwd' -h localhost mydb < path/to/my/fixtures/group.sql

The last line is to import some intial groups into django.contrib.auth.models.Group table.

MySQL - Using mysqladmin to do some jobs outside the mysql terminal

You can do some mysql jobs outside the mysql terminal using mysqladmin util:

For example:

1. Drop a table:

$ mysqladmin -u myuser -p'myuser_password' -f drop mytable

The '-f' parameter is to proceed the command without prompting (y/N).

2. Create a new table:

$ mysqladmin -u myuser -p'myuser_password' create my_newtable

For more use cases,  read this article:

Django - Get a list of users of a certain group

To get a list of users of a certain group (let's call it 'MyGroup'):

from django.contrib.auth import User, Group

users = User.objects.filter(groups=Group.objects.get(name='MyGroup'))


users = User.objects.filter(groups__name__in=['MyGroup'])

MySQL - Dump tables data

If you want to backup only some tables of your database, use the following command:

$ mysqldump -u myuser -p'myuser_pwd' my_database my_table1 my_table2 > my_backupdb.sql

dpkg - Searching installed packages

To search a package (let's call it 'abcd') installed in a linux sytem, run the following command:

sudo dpkg -l | grep 'abcd'

It is very useful for me to find a package name to remove.

Django & Ajax - Disable Django's Cross-Site-Request_Forgery protection on a view

This is an example about how to make an ajax call from a django template without applying the cross-site-request-forgery mechanism of Django, using @csrf_exempt annotation.


from apps.testajax import models
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from django.http import HttpResponse
import json

def home(request):
my_models = models.MyModel.objects.all()
return render_to_response('home.html', locals(), context_instance=RequestContext(request))

def del_model(request):
msg = 'Fail'
if request.is_ajax():
if 'models_to_del' in request.POST.keys() and request.POST['models_to_del']:
models_to_del_pks = request.POST['models_to_del'].split(',')
if '' in models_to_del_pks:
msg = 'Models were deleted'
msg …

Django - Error caused by Mariadb/MySQL's BINLOG_FORMAT = STATEMENT

Today, when I was trying to run the celery task to make changes to a bulk of data (Mariadb5.5), I got the following error and the task cannot be done:

"Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. Statement is unsafe because it accesses a non-transactional table after accessing a transactional table within the same transaction."

After a while of asking and Googling, found that  there are 2 possible reasons of this error:

1. By default, Mariadb5.5/MySQL5.5 enable the replication between db servers (master/slave)

2. And by default, Mariadb5.5/MySQL5.5 using innodb engine. Maybe not all of my tables were created under the same engine (I re-used some 3rd party django apps).

To solve this issue, set binlog_format as mixed:


Git - Using SSH key to push code without entering password

I had to enter password every time I push my commits to git server. I can avoid doing it again and again by using SSH key:

1. In my computer, generate SSH Key:

$ cd ~/.ssh
$ ssh-keygen -t rsa -C ""

The above command will create 2 files, id_rsa and is your public key. Send it to the git server.

2. Copy the public key to the remote server:

$ ssh-copy-id -i ~/.ssh/ myusername@remotehost

The command will prompt me to enter password of my user account in the remotehost. Enter and done.

+ After that, if I can ssh to the remote server without error, I can assume that it works:

trinh@mylocalpc$ ssh myusername@remotehost

+ If I get the error "Agent admitted failure to sign using the key", run the following command and try to ssh again:

$ ssh-add

From now on, whenever I push code to remote git server, I don't have to input my password anymore.


Planning for the next Django Seminars (September & October)


1. September: In September I will do two seminars at the University of Information Technology (Vietnam National University). So, I guess topics are the same with the previous event. This time, I think I will have more time to discuss with students on web development:

* Topic: A crash course on Django

* Attendants: students

* Date Time: (scheduling...)

* Venue: University of Information Technology, Linh Trung Ward, Thu Duc Province, Ho Chi Minh City (room# will be updated later...)

2. October: I will focus on the deployment of Django, and take OpenStack private cloud as an example:

* Topic: Deploying Django on an OpenStack cloud

* Attendants: everyone

* Date Time: 9AM Saturday, October 26, 2013

* Venue: (will be updated...)


Ubuntu - Solve "cannot find -lxxx" errors

In Linux (Ubuntu), whenever you install a package and you see the following error:

/usr/bin/ld: cannot find -lxxx

It means that your system is missing the "xxx" lib. In my case, the error occurs when I install MySQL-python package:

/usr/bin/ld: cannot find -lssl  

/usr/bin/ld: cannot find -lcrypto

So, the solution here is install those missing libraries. For my computer, I'just need to install the OpenSSL development lib, the libssl-dev:

sudo apt-get install libssl-dev

And everything will be OK.

Linux - How to verify that your CPU supports Virtualization

To verify that your CPU supports Virtualization technology:

* Intel CPU:

$ sudo grep --color vmx /proc/cpuinfo

If the output has vmx flag, your Intel CPU supports VT.


$ sudo grep --color svm /proc/cpuinfo

If the output has svm flag, your AMD CPU supports VT.

Now, you can enable the Virtualization Technology from the BIOS:

Cool!!! My Laptop supports VT!!!! \m/\m/\m/


P/S: an alternative way is to use kvm-ok:

$sudo apt-get install cpu-checker $sudo kvm-ok

Python - Generate random string

If you want to create a random string in python, here you are:

import string
import random

def id_gen(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for x in range(size))

#100: Django Seminar 08/17/2013 Write Up

So, my first Django Seminar (08/17/2013) has been over. It was more successful than I'd thought. There were about 13 people attended, most of them were students.
I worked through some must-known general ideas in web development such as:

* MVC architecture, and how it was implemented in Django as MTV
* Steps to build a Django application in my recommend environment, Ubuntu:

ActiveDirectory - Export Data to CSV

1. To export all data from Active Directory into a file called test.csv

csvde -f test.csv

2. Export data in a specific DN

csvde -d "OU=MyOU,DC=domain,dc=local" -f test.csv

3. Export with filters

csvde -f test.csv -r "(&(objectClass=user)(objectCategory=person)"

4. Export data with filters and specific DN

csvde -d "OU=Directors,DC=domain,dc=local" -f test.csv -r "(&(objectClass=user)(objectCategory=person)"

5. Get data of some fields only

csvde -d "OU=Directors,DC=domain,dc=local" -f test.csv -r "(&(objectClass=user)(objectCategory=person)" -l " givenName, sn, mail"

For more information, read this article:

Before attending the Django Seminar (08/17/2013)

After receiving some questions about the Django Seminar this Saturday (08/17/2013 - Click to see the detail of the seminar), I decide to write something for the students who will attend this small study group. This will make our meet-up more successful:

1. This is not a Python Workshop. Instead, We will discuss some general ideas around the web development and the greatness of Django.

2. You should bring your own laptop. You and I will work through some basic practical Django-related projects. You will get more than just listening.

3. Googling around the Django topic and preparing some questions, any questions before attending this event.

Good luck and see you this Saturday!

Python - Rename a file

To rename a file using python:

>>> import os
>>> os.rename(oldfilepath, newfilepath)

It's just useful!

ActiveDirectory - Update User Info From CSV

We will update the Department of our users using PowerShell:

1. Create a CSV file contains sAMAccountName column and all needed information, e.g. C:\userlist.csv:

supergenius,Super Man Department
ironman,The Advanger

2. In AD Controller, open Active Directory Module for Windows PowerShell, and run the following command:

Import-Csv C:\userlist.csv | foreach { Get-ADUser -Identity $($_.sAMAccountName) | Set-ADUser -Department $($_.department) }

Update:the Active Directory Module for Windows PowerShell is only available in Windows Server 2008 R2 and Windows 7.

A private cloud in my USB stick

I've just tried the Ubuntu Open Stack Live USB. The tools are so great and easy to use. Now I have a private cloud in my USB stick. Here are some screenshots:

Django Seminar - August 17, 2013

Next Saturday (August 17 2013), I will organize a seminar about Django and its greatness. I want to share my interest in web development with Django and some of my experience. This event is made for Vietnamese students, and web developers, so all the content will be Vietnamese. But, if you can understand, join me next Saturday:


1. Mục đích:
- Giới thiệu framework Django. - Xu hướng công nghệ web hiện nay - Giao lưu trao đổi kinh nghiệm.
2. Đối tượng tham gia:
- Sinh viên (ưu tiên) - Các nhà lập trình web có hứng thú
3. Nội dung:
- Giới thiệu framework Django. - Xây dựng ứng dụng mẫu. - Giới thiệu dự án đang làm với Django - Hỏi đáp, giao lưu
4. Người trình bày:
- Nguyễn Trọng Đăng Trình (Trang cá nhân:
5. Thời gian: 9h-11h sáng thứ bảy ngày 17/08/2013
6. Địa điểm: Quán cafe Dứa Gào, 187/15 Hoàng Hoa Thám, Bình Thạnh, TP. Hồ Chí Minh (có máy lạnh, máy chiếu). Xem bản đồ, điểm đánh dấu số (1)

7. Hình thức tham dự: Miễn Phí. (Mỗi bạn chỉ phải …

Django - Using Upstart to run Django built-in dev server

Sometimes, You only want to have a small django server to run a not-important-app, and you dont want to install any extra stuff. So, my solution is just use the Django built-in development app and make it start as a service at startup with Upstart (in Ubuntu).

Here is how:

Ubuntu - Printing multiple pdf files in one go

I have some pdf files to print (about 158), and I want to print all of them in one go.

Change directory to the containing folder, and run the command:

for FILE in *.pdf ; do lpr "$FILE" ; done

M$ DHCP Server - Reserved IPs synchronization between servers

Although the M$ DHCP Server does not allow you to synchronize the reserved IP(s) with other DHCP servers, but If you want, you can do it with a small trick:

1. Export the dhcp config of the master server by the command (in the command prompt windows):

netsh dhcp server \\master-server-name scope > C:\master.cfg

The config file are something like this:

Ubuntu - Add an application to the dashboard

In order to add an application (let's call it my_app) to the Ubuntu's dashboard (Unity):

+ Create a .desktop file in ~/.local/share/applications/:

sudo nano ~/.local/share/applications/my_app.desktop

with these line:

[Desktop Entry] Name=the name you want shown Comment= Exec=command to run Icon=icon name Terminal=false Type=Application StartupNotify=true

+ Logout and Login again, open the dashboard, and my_app will show up.

Ubuntu - How to add a dir to PATH

In Ubuntu 12.04 or newer, edit the ~/.bashrc to add a directory to PATH. Add the following line to the end of the file:

export PATH=/my/path:$PATH

For the changes to take effect:

$ source ~/.bashrc

The Original Innovator

"The people who are crazy enough to think they can change the world are the ones who do."

~Steve Jobs.

(image's src: