Posts

Showing posts from January, 2014

Django - Use the Django admin app's FilteredSelectMultiple widget in form

Image
To use the FilteredSelectMultiple widget of the Django admin app you have to do several things:


0. Include FilteredSelectMultiple widget:

from django.contrib.admin.widgets import FilteredSelectMultiple


1. Define the form, for example:

class MyForm(forms.Form):
first_name = forms.CharField(max_length=100)
groups = forms.ModelMultipleChoiceField(\
widget=FilteredSelectMultiple("Groups", is_stacked=False),\
queryset=models.Group.objects.all())


class Media:
css = {'all': ('/static/admin/css/widgets.css',),}
js = ('/admin/jsi18n',)

Notes:

* /static/ is my STATIC_URL setting and I also copy the static admin folder from Django source.
* You also need to define the url pattern for jsi18n path in urls.py:
...
(r'^admin/jsi18n/$', 'django.views.i18n.javascript_catalog'),
...

2. Include necessary css and js in the template:

...
<script type="text/javascript" src="/static/admin/js/jquery.min.js"></script>
<script type="text/j…

Drupal - Enable Clean URLs in Drupal 7 with Nginx

To enable Clean URLs in Drupal 7 with Nginx, using this following rule:

>> /etc/nginx/sites-available/mysite

server {

        listen 80;
        server_name [host];
        root /var/www/[document_root]; ## <-- Your only path reference.

        # Enable compression, this will help if you have for instance advagg module
        # by serving Gzip versions of the files.
        gzip_static on;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        # This matters if you use drush prior to 5.x
        # After 5.x backups are stored outside the Drupal install.
        #location = /backup {
        #        deny all;
        #}

        # Very rarely should these ever be accessed outside of your lan
        location ~* \.(txt|log)$ {
                allow 192.168.0.0/16;
                deny all;
        }

       …

Drupal - Using Drush to update Drupal core

Drupal does not allow us to upgrade Drupal core using the admin interface the way Wordpress does. We have to manually do it ourself. I meant, backup, download the drupal-core source file, copy the new files and folders exclude the "sites" directory into the old drupal root path. Quite a lot of work.

Luckily, a genius ( :D) has created a tool name DRUSH to do the management stuffs including upgrade drupal core, modules...completely in command line. The core developers of drush has published the source code of drush in github:

https://github.com/drush-ops/drush

"Drush is a command-line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those who spend their working hours hacking away at the command prompt."

This morning, I've just upgrade my drupal site to the latest drupal core (7.26)  by running the following drush command:

$ cd /path/to/drupal/site
$ drush pm-update drupal-7.26

The output is something like:

Updat…

Preparing for my PyCon2014 trip

Image
I will attend the PyCon 2014 conference in Montreal, Canada, this April. I can meet the creator of Python and any other superstars in the Python world. And to be able to be there, I need to do something.

First of all, purchase the conference ticket. Luckily, I'm granted the Financial Aid of PyCon2014, so I can attend the conference for free (just the 3 days conference).

Secondly, I have to buy a flight ticket to Montreal to be able to apply for the VISA. The ticket price is so high (around $1600 USD). So, I am applying for the PD fund of my school to support me this. That PD fund plus $100 USD travel grants from the PyCon organizer might give me a break.

Next thing I need to care about is to find a place to stay in Montreal. Fortunately, some friends from the Foulab Makerspace (in Montreal) are willing to accommodate me. I'm just so thankful for that!

And finally, my trip will only be successful if I can get the VISA to Canada. There are a lot of paperworks. So, the only obst…

Ubuntu - Fix "W: GPG..." key error

Sometimes, You run "sudo apt-get update" and get this kind of error:

W: GPG error: http://extras.ubuntu.com precise Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 16126D3A3E5C119
2

It is because your apt misses that public key. So, to fix this issue, just run the following command to add the need key:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 16126D3A3E5C1192
then run update again:
$ sudo apt-get update

If the above method does not work, try this:

$ sudo apt-get clean
$ cd /var/lib/apt
$ sudo mv lists lists.old
$ sudo mkdir -p lists/partial
$ sudo apt-get clean
$ sudo apt-get update

Drupal - Install OpenAcademy distribution with Nginx, php5-fpm, and MariaDB in Ubuntu12.04

OpenAcademy is a Drupal distribution which is used for higher education (or whatever you want :D). This blog post is about installing and setting up the full stack (including nginx, php5-fpm, mariadb, drupal, openacademy) to make a working website:


1. Install MariaDB, create a database for drupal:
Go to https://downloads.mariadb.org/mariadb/repositories, and select the appropriate mirror for your mariadb installation. In my case, I install the dbms in Ubuntu 12.04, mariadb 5.5, so the installation procedure is as following

$ sudo apt-get install python-software-properties
$ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
$ sudo add-apt-repository 'deb http://ftp.kaist.ac.kr/mariadb/repo/5.5/ubuntu precise main'
$ sudo apt-get update
$ sudo apt-get install mariadb-server


2. Install and  config php5-fpm:

* Install php5-fpm:

$ sudo apt-get install php5-fpm php5-mysql

* Open php config file:

$ sudo nano /etc/php5/fpm/php.ini

change the following line…

Nginx + Wordpress + SSL configuration

Sometimes you need to harden your wordpress blog with ssl. It's pretty simple.



1. Create your self-signed ssl certificate (as in previous blog post):

$ sudo mkdir /etc/myssl_cert
$ cd /etc/myssl_cert
$ openssl genrsa 1024 > wordpress.key
$ sudo chmod 400 wordpress.key
$ openssl req -new -x509 -nodes -sha1 -days 1780 -key wordpress.key > wordpress.crt
$ openssl x509 -noout -fingerprint -text < wordpress.crt > wordpress.info


2. Configure nginx to support ssl connection:

/etc/nginx/sites-available/wordpress:

...

# HTTPS server
#
server {
        listen 443;
        server_name your.domain.com;

        root /var/www;
        index index.php index.html index.htm;

        ssl on;
        ssl_certificate /etc/myssl_cert/wordpress.crt;
        ssl_certificate_key /etc/myssl_cert/wordpress.key;

        ssl_session_timeout 5m;

        ssl_protocols SSLv3 TLSv1;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
        ssl_prefer_server_ciphers on;


        location / {
      …

Setting up Web2py + Gunicorn + Nginx + SSL

Deploying web2py applications is not so hard if you've already deployed Django apps. It's just the same.


1. Prepare folders and download web2py source code, install nginx, install gunicorn:

* Create project folder:

$ mkdir /home/www-data/
$ cd /home/www-data

* Clone web2py source code:

$ git clone https://github.com/web2py/web2py.git

* Install nginx:

$ sudo apt-get install nginx-full

* Install gunicorn:

$ sudo apt-get install python-pip
$ sudo pip install gunicorn


2. Create self-signed ssl certificate:

$ sudo mkdir /etc/nginx/ssl
$ cd /etc/nginx/ssl

$ openssl genrsa 1024 > web2py.key
$ sudo chmod 400 web2py.key
$ openssl req -new -x509 -nodes -sha1 -days 1780 -key web2py.key > web2py.crt
$ openssl x509 -noout -fingerprint -text < web2py.crt > web2py.info


3. Configure nginx:

* Create a configuration file names /etc/nginx/sites-available/we2py:

upstream gunicorn {
        server 127.0.0.1:8000 fail_timeout=0;
}

server {
        listen 80 default;
        client_max_body_size 4G;
        server…

A first look at vSphere SDK for Python

VMware've just recently released the vSphere SDK for Python, the pyVMomi, at last year Christmas. And that is a great news to me. The source code can be found at:

pyVmomi Github Repository:
https://github.com/vmware/pyvmomi

pyVmomi Pypi package:
https://pypi.python.org/pypi/pyvmomi


I've just dig around the sample scripts and found it pretty cool. For example, I ran the geallvms.py to get all virtual machine info :

python sample/getallvms.py --host vmware.host.domain --user vmadmin --password VMAdminPass

It showed me something like:

Name       :  server1
Path       :  [iSCSI LUN 536Gb-A] server1/server1.vmx
Guest      :  Ubuntu Linux (64-bit)
State      :  poweredOn
IP         :  172.55.1.67

Name       :  server2
Path       :  [iSCSI LUN 536Gb-B] server2/server2.vmx
Guest      :  Ubuntu Linux (64-bit)
State      :  poweredOn
IP         :  172.56.1.56

Name       :  server3
Path       :  [iSCSI LUN 536Gb-B] server3/server3.vmx
Guest      :  Ubuntu Linux (64-bit)
State      :  poweredOn
IP         : …

Linux - Delete all files with a specific file type

In linux, we can use the following command to delete all files with a specific file type:


$ find /my/file/path -type f -iname "*.pyc" -exec rm -f {} \;

SSH tunnelling

Image
Currently, I wanted to ssh to a remote server from my computer. But, the network where my computer resides has blocked the ssh port. I could not do it the normal way. So, I used the SSH tunnelling technique  to bypass this strictly policy.

Here is how I did it:

1. Create a SSH tunnel from my machine (localhost) through a un-blocked server (e.g. the firewall), the only computer that can make ssh connections with outside world, to the server I want. (Luckily, I have access to the firewall of the network)

$ ssh -L 2022:myremoteserver.com:22 firewall_user@firewalldomain.com

This will ask me to provide the password of the firewall_user. It will open a tunnel from my local machine at port 2022 to the ssh port (22) of the firewall. Keep the terminal window open and move on the next step.

2. Open another terminal window and SSH through the tunnel by the following command:

$ ssh -l remote_user -p 2022 localhost

This command will ask me password of the remote_user user in the myremoteserver.com, (b…

A funny conversation about REST by Ryan Tomayko

This is a super hilarious conversation about REST by Ryan Tomayko. In this article, he explained to his wife, a non-tech woman, how the Internet works and especially what the REST (Representational state transfer) architecture is in a funny way. You will find there your relaxation and a quick over view of REST after reading it.

Because the original article has been closed by the author, I have to go to web archive to read:

http://web.archive.org/web/20130116005443/http://tomayko.com/writings/rest-to-my-wife




MS Active Directory - List all groups DN in an OU

Type this command in the command prompt of a AD Domain Controller to list all the groups in an OU:

dsquery group "OU=My OU,DC=MYDomain,DC=COM" -name * | dsget group -dn

Linux - List all nameservers

In linux, Ubuntu, to list all nameservers the computer is using, run the following command:

$ nm-tool | tail -n 8


The results will be something like:

    Prefix:          16 (255.255.0.0)
    Gateway:         172.18.3.1

    DNS:             172.18.1.2
    DNS:             172.17.1.2
    DNS:             172.16.1.2



MS Active Directory - Move multiple users to a new OU using commandline

This is a pretty handy technique to move a bunch of users to a new OU in Active Directory using command line:

1. Create a list of usernames, each username per line: users.txt

username1
username2
username3
...

2. In the AD Domain Controller, open command prompt and enter this command:

for /f %i in (users.txt) do dsquery user -samid %i | dsmove -newparent "OU=New OU,DC=Mydomain,DC=COM"

Just finished the Web Development Course on Udacity

Image
I've just finished the Web Development Course on Udacity (in 7 days :D). I've had a great time to review the process of building a website (a blog) and many other techniques with the experienced instructor, Steve Huffman (founder of Reddit and Hipmunk):

+ Design the database.
+ Design application's workflow
+ Implement handler for each request
+ Mapping request
+ User's data validation (form)
+ Organize code
+ Using memcache
+ Using Google App Engine
...


The course even gave me a chance to do things that I've never had to create myself before:

- Session management
- User authentication

Because the web framework (Django) does all of those for me.

And greater, I also had an over view of Reddit's architecture, how it works, etc... (everything is open-source)




About the final, I have to do one little trick to make the grader accept my blog:

* sleep 1 second after every query, for example:

...
class HistoryPage(Handler):
    def get(self, permalink):
        wikis = g…

Google App Engine Datastore - To render an object's id in the template using jinja2

To render an object's id in the template using jinja2:


{{myobject.key().id()}}