Posts

Showing posts from September, 2013

Django - Migrating function-based generic date_based views

I used Django's generic date_based views in Django v1.1:

urls.py:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

from coltrane.models import Entry

entry_info_dict = {
    'queryset': Entry.objects.all(),
    'date_field': 'pub_date',
}

urlpatterns = patterns('',
     (r'^admin/', include(admin.site_urls)),
     (r'^search/$', 'cms.search.views.search'),
     (r'^weblog/$', 'django.views.generic.date_based.archive_index',entry_info_dict),
     (r'^weblog/(?P<year>\d{4}/$', 'django.views.generic.date_based.archive_year', entry_info_dict),
     (r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/$', 'django.views.generic.date_based.archive_month', entry_info_dict),
     (r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/(?P<day>\d{2})/$', 'django.views.generic.date_based.archive_day', entry_info_dict),
     (r'^we…

Ubuntu - Fix mount NTFS partition error

If you see this error when trying to mount a NTFS partition (/dev/sda5 for example) in Ubuntu:

Error mounting: mount exited with exit code 13: ntfs_attr_pread_i: ntfs_pread failed: Input/output error Failed to read NTFS $Bitmap: Input/output error NTFS is either inconsistent, or there is a hardware fault, or it's a SoftRAID/FakeRAID hardware. In the first case run chkdsk /f on Windows then reboot into Windows twice. The usage of the /f parameter is very important! If the device is a SoftRAID/FakeRAID then first activate it and mount a different device under the /dev/mapper/ directory, (e.g. /dev/mapper/nvidia_eahaabcc1). Please see the 'dmraid' documentation for more details

Run this command line to fix and remount again:

sudo ntfsfix /dev/sda5

ntfsfix is provided by the ntfs-3g package of Ubuntu.

nginx - Hide the identity and version of nginx

Image
Whenever a user access a restricted page without a appropriate privilege, nginx server will redirect her to an error page:



It will show its identity and version (nginx ). For the security's sake I want to hide those information and show my custom page instead of that default page.

So, do the following:

1. Hide the nginx version: open the /etc/nginx/nginx.conf, edit the line:

erver_tokens off; 

save and exit.

2. Direct those 40x error to my custom page in /usr/share/nginx/www/40x.html: in the site's configuration (/etc/nginx/site-available/default):

        error_page 401 402 403 404 /40x.html;                                                                                                                                    
        location = /40x.html {                                                                                                                                                   
                root /usr/share/nginx/www;
        }

Create a custom page in /usr/share…

Python - *args and **kwargs

This is really useful:


Passing a list as an argument and prefixing it with a single asterisk (*) will cause each item of the list, in order, to be used as a separate positional argument. Passing a dictionary and prefixing it with two asterisks (**) will cause the keys of the dictionary to be used as names for separate keyword arguments and the dictionary’s values to become the values of these arguments.
When a Python function needs to accept arbitrary sets of optional arguments, or to accept many different arguments based on different situations, it’s common to define it like this:


def my_func(*args, **kwargs):
The function will then have access to a list named args containing all the positional arguments passed to it and a dictionary named kwargs containing all the keyword arguments passed to it. The function can then look at those variables to work out what it needs to do.



Reference:
Practical Django Projects, 2nd edition, James Bennet, Apress.

php5-ldap - Useful ldap snipet

This is a super useful php snipet to debug the ldap search:

#==============================================================================
# Configuration
#==============================================================================
# LDAP
$ldap_url = "ldaps://ldap.server1 ldaps://ldap.server2";
$ldap_binddn = "cn=manager,dc=example,dc=com";
$ldap_bindpw = "P@ssw0rd";
$ldap_base = "dc=example,dc=com";
#$ldap_filter = "(&(objectClass=person)(uid={login}))";
$ldap_filter = "(&(objectClass=user)(sAMAccountName={login})(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";


$login = "genius";


# Connect to LDAP
$ldap = ldap_connect($ldap_url);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

# Bind
if ( isset($ldap_binddn) && isset($ldap_bindpw) ) {
$bind = ldap_bind($ldap, $ldap_binddn, $ldap_bindpw);
} else {
$bind = ldap_bind($ldap);
}

$errno = ldap_errno($ldap);
if ( $errn…

Xubuntu - Some default shortcut keys to remember

Xubuntu 12.04 released New default shortcut keysAlt F5
Max window horiz. Alt F6
Max. window vert. Alt F7
Max. window (h+v) In addition, you can stick a window (making it appear in all workspaces) with Alt F8. The shortcuts for moving and resizing windows have been removed in favor of more powerful mouse gestures. Naturally, if you need these shortcuts, you can enable them from the Settings Manager. Super W
Firefox Super M
Thunderbird Super F
Thunar Super T
Terminal The aforementioned shortcuts are bound to exo-open launchers. This means they will always open any browser, mail reader, file manager or terminal emulator you have set as the default. In addition, there is six new application shortcut keys: Super R for Application finderSuper E for LeafpadSuper 1 for GmusicbrowserSuper 2 for PidginSuper 3 for AbiwordSuper 4 for Gnumeric(source: http://xubuntu.org/news/12-04-release/)

* To show desktop: CTRL + ALT + D

Django seminar - September and October

Image
I had planed for my Django presentations in September and October (see the plan here). But, according to my agreement with the teacher at the University a week ago, plan has to be changed:


1. September
Topic: A crash course on Django

Attendants: students

Date Time: 6PM September 30th, 2013

Venue:room C102 Saigon University, Bà Huyện Thanh Quan street, Ho Chi Minh City.


2. October: I will do the presentation at 2 Universities, on the same topic


a. Saigon University:
Topic: A crash course on Django

Attendants: students

Date Time: 6PM October 1st, 2013

Venue: room D104 Saigon University, AN Dương Vương street, Ho Chi Minh City.

b. University of Information Technology:
Topic: A crash course on Django

Attendants

LDAP - Filter syntax example

Example:

ldap_filter = "(&(objectClass=user)(|(sn=*{lastname}*)(givenName=*{firstname}*))(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"


& => AND
|  => OR
!  => NOT


References:

http://www.zytrax.com/books/ldap/apa/search.html

Sublime Text - How to show White Space and Indentation Guides

To show White Space and Indentation Guides (Tab) in Sublime Text:

* Open Preferences menu, choose Setting - User to open the Preferences.sublime-settings file, add this line:

"draw_white_space": "all",

php5-ldap - Search a user in Active Directory

This php snipet helps me to produce an ldap search against an Active Directory using php5-ldap:

<?php
#==============================================================================
# Configuration
#==============================================================================
# LDAP
$ldap_url = "ldaps://<ldap server 1 ip> ldaps://<ldap server 2 ip>";
#$ldap_binddn = "cn=manager,dc=example,dc=com";
$ldap_binddn = "CN=Admin,OU=ArtificialUsers,DC=MyDomain,DC=COM";
$ldap_bindpw = "P@ssw0rd";
$ldap_base = "DC=MyDomain,DC=COM";
$ldap_filter = "(&(objectClass=user)(sAMAccountName={login})(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";

#==============================================================================
# POST parameters
#==============================================================================
# Initiate vars
$result = "";
$login = "";
$ldap = "";
$userdn = "";
$firstname …

Active Directory - Great free tool to reset password in bulk

I have to reset multiple accounts passwords. I's looking for a way to use the powershell to read csv file and then update the password just like I did before (http://iambusychangingtheworld.blogspot.com/2013/08/activedirectory-update-user-info-from.html). Unfortunately, the Active Directory Module for Windows PowerShell is only available in Windows Server 2008 R2 and Windows 7 which I don't have now (only Server 2008 SP2). So, I's googling around and then I found this:

Bulk Password Control

http://www.wisesoft.co.uk/software/passwordcontrol/bulk_password_control.aspx

This free tool can help me update all the users passwords which are auto generated. It even let me export the information to csv file. It's so cool!

Genius!!!


Note:install this tool inside the Active Directory Domain Controller.

Apache - Setup basic authentication when Apache is behind a cache server?

To set a basic authentication for a directory in Apache, add the following lines to your Apache config file (usually in /etc/apache2/site-available/default):

        <Directory "/var/www/myapp/">
                Order deny,allow
                Allow from 172.0.0.0/8
                Deny from all   

                AuthType Basic
                AuthName "Authentication Required"
                AuthUserFile "/etc/apache2/pwd-reset.conf"
                Require valid-user
        </Directory>

With the above setting, apache will only allow internal IP address access /var/www/myapp/ directory, and deny all request from outside that IP range (172.0.0.0/8).

For more options, read the Apache documentation: http://httpd.apache.org/docs/2.2/mod/mod_auth_basic.html


But, the question is:
what if Apache stands behind a cache server (e.g. Varnish)?
If there is a proxy or cache server placed between users and the apache, all the requests come to apache will be reco…

Django - SlugField

If you are working with Apache and some CMS (e.g. Wordpress), you will find yourself at least do the url rewrite thing once to make the url friendly. Something like translate:

this:
http://mydomain.com/?q=abcd

to this:
http://mydomain.com/abcd/


In Django, you dont have to worry about it because when creating apps, Django lets you define the url pattern (regex) for a particular view. It is used in a pre-defined view. How about the user-defined view, like a blog post which will be posted by a non-programmer user using a WYSIWYG editor of your app? What will the url look like?

Django core developers (who develop the Django core) know that situation and create a Model Field name SlugField which will solve our problem:


James Bennett's definition from the book "Practical Django projects":

SlugField is a bit more interesting. It’s meant to store a slug: a short, meaningful piece of text, composed entirely of characters that are safe to use in a URL. You use SlugField when you genera…

Django - Check variables in POST/GET request

When a form is submitted, I wanna get the value of a key (variable) which is in the POST/GET data. For example:

myvalue = request.POST['mykey']

But if for some reason, the POST/GET data does not contains that data ('mykey': 'myvalue'). In this case, I have to check if the key 'mykey' is sent in POST/GET data and check if that key is not empty:

if 'mykey' in request.POST.keys() and request.POST['mykey']:
      myvalue = request.POST['mykey']

There is a Python method available on any Python dictionary names get(). get() lets you ask for the value of a particular key and specify a default to fallback on if the key doesn't exist. And, POST and GET are dictionaries.

myvalue = request.POST.get('mykey', '')

That get() method will save me (and you) so much time!

Python - Rename files in batch

I have a directory which contains a lot of photos. But, for some reasons, the files's names do not have file extension. Something like:

myphotos/
-------------photo1
-------------photo2
-------------photo3
...
-------------photo70

Because of that, I cannot upload those photos to facebook. The file-chooser window does not recognize them as images. So, I have to add .JPEG extension for all photos. Python helps me this time (and so many other times). Run this python script inside the myphotos directory;

#! /usr/bin/python

from os import listdir, rename
from os.path import isfile, join

def get_files_list(path):
return [ f for f in listdir(path) if (isfile(join(path,f)))]

def add_ext_batch(files_list):
for fn in files_list:
tem_fn = fn + '.JPEG'
rename(fn, tem_fn)

if __name__=="__main__":
       file_list = get_files_list('.')
       add_ext_batch(file_list)




Nginx - Basic authentication

Image
To create a basic authentication layer for a specific page with nginx, you can do the following steps:

1. Add these lines to your nginx config, /etc/nginx/sites-available/myconf:

...
server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;

        root /myapp/root/path;
        index index.php index.html index.htm;

        server_name localhost;

        location / {
                try_files $uri $uri/ /index.html;
                auth_basic                     "Restricted";
                auth_basic_user_file   /mnt/DATA/projects/myproject/.htpasswd;
        }
....

2. Create a .htpasswd file which contains username and password, in the form of username:password ( or username:password:comment). Password must be in encrypted form:

a. Encrypt my desired password (mypassword) for myusername by running the following command line:
trinh@trinh-pc$(PASSWORD="mypassword";SALT="$(openssl rand -base64 3)";SHA1=$(printf "$PASSWORD$SALT" | openssl dgs…

PHP - Using php5-ldap to interact with Microsoft Active Directory from Linux

Image
To make the php5-ldap library contactable with a Microsoft Windows Active Directory server, make sure:

1. Install OpenLDAP library:

sudo apt-get install slapd ldap-utils
2. Modify the config file of ldap at /etc/ldap/ldap.conf, !important:

## LDAP Defaults## See ldap.conf(5) for details# This file should be world readable but not world writable.#BASE   dc=example,dc=com#URI    ldap://ldap.example.com ldap://ldap-master.example.com:666#SIZELIMIT      12#TIMELIMIT      15#DEREF          never# TLS certificates (needed for GnuTLS)# TLS_CACERT    /etc/ssl/certs/ca-certificates.crt# add thisTLS_REQCERT never
3. Write a test script and run:

<?php $ldap = ldap_connect("ldaps://172.18.1.2"); ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); $password="P@ssw0rd"; $binddn = "CN=Admin,OU=ArtificialUsers,DC=MyDomain,DC=COM"; if($bind = ldap_bind($ldap, $binddn,$password )) { echo "logged in"; } else { echo &q…

Javascript - Browser detection

Image
Yesterday, a user of my website notified me that she didn't see anything on the website. What? I checked, and found that she used IE v9 to access the website. My website works perfectly in the current Chrome, Firefox and IE10 (or newer).

There're two solutions I can think about to deal with this issue:

1. Detect the user's browser, if it is Internet Explorer and the version is older than 10, notify her and suggest to install a newer browser.

2. Add IE9 (or older) support for my website. And this will be so much pain.


I tried the solution number 1, so I did the following steps:

1. Dectect the user's browser and version using Peter-Paul Koch's script:

var BrowserDetect = { init: function () { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "…

InternetExplorer - Test website in different IE versions with IE10's Dev Tool

Image
In Internet Explorer 10 you can use Developer Tool's Brower Mode to see what your website looks like in different IE versions (IE 9, 8, 7).



Crap is crap!

Linux - Test website in Internet Explorer on a Linux machine

Image
One of the most common issues in web development world is Internet Explorer compatibility. It's a pain in the ass. And It's even more annoying when Ubuntu is the only machine I have. So, I have to install a virtual machine (VirtualBox) to scratch the itch.

Luckily, Microsoft has acknowledged that problem and provide us some pre-built VM images at http://www.modern.ie/en-us/virtualization-tools#downloads . In my case, I download the IE10 - Win7 image. Follow the instructions in the link to install the VM:


Instructions
Download the SFX and all RAR files for the VM (smaller VMs may not have files with RAR extension). In each set below that contains a split archive, the provided text file (.txt) contains URLs to all files in the set, and this can be used directly with the 'wget' command in Linux. From the terminal, enter wget -i [URL TO TEXT FILE]. For Windows XP single file downloads, use wget [URL TO DOWNLOAD FILE] instead.
Example 1: wget -i https://az412801.vo.msecnd.…