Sunday, November 29, 2009

Setting up Postgres database server in Ubuntu 9.10

Do all as the superuser.
apt-get install postgresql postgresql-client postgresql-contrib
# To reset password for postgres user
echo "ALTER USER postgres WITH PASSWORD 'postgres';" | sudo -u postgres psql template1
# reset password for system user
passwd postgres
In bottom of /etc/postgresql/8.4/main/pg_hba.conf file set as the following:
# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database
# super user can access the database using some other method.
# Noninteractive
# access to all databases is required during automatic maintenance
# (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by UNIX sockets
local   all         postgres                          ident
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# "local" is for Unix domain socket connections only
local   all         all                               md5
# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
# IPv6 local connections:
host    all         all         ::1/128               md5

Saturday, November 28, 2009

Mount NTFS-partition in Ubuntu Karmic x64

Do all as the superuser.
  • Get location of the ntfs partition
    fdisk -l
    Disk /dev/sda: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors/track, 60801 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x14311431
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1       29089   233657361    7  HPFS/NTFS
    /dev/sda2           29090       32129    24418800   83  Linux
    /dev/sda3           60304       60801     4000185   82  Linux swap / Solaris
    /dev/sda4           32130       60303   226307655   83  Linux
    
    Partition table entries are not in disk order
    OK, /dev/sda1 is the desired location.

  • Create folder in which you want partition will be mounted. E.g.
    mkdir /media/windows
  • Add this line to your /etc/fstab file, and save it after editing. Note, you should input your locale if it's differs from this.
    /dev/sda1       /media/windows    ntfs-3g defaults,locale=en_EN.UTF-8 0 0
  • Test with command
    mount -a

Install Chromium in Ubuntu 9.10 Karmic x64

To install just run this commands as superuser.
echo "deb http://ppa.launchpad.net/chromium-daily/ppa/ubuntu karmic main
deb-src http://ppa.launchpad.net/chromium-daily/ppa/ubuntu karmic main" >> /etc/apt/sources.list

apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xfbef0d696de1c72ba5a835fe5a9bf3bb4e5e17b5

apt-get update

apt-get install -y chromium-browser

Disabled `hg view` command in Ubuntu

To enable `hg view` command it's needs to add
[extensions]
hgk=
in your repository hgrc (.hg/hgrc) or system-wide(/etc/mercurial/hgrc)
Or you might also need to install the tk package.

Convert Ext3 filesystem to Ext4 in Ubuntu 9.04

Suppose your disk is /dev/sda1.

e2fsck /dev/sda1 -y
tune2fs -O extents,uninit_bg,dir_index /dev/sda1
fsck -pf /dev/sda1

Profit :)

Friday, November 27, 2009

Ubuntu One Client in 9.10

If you have troubles with Ubuntu One Client in 9.10 look in its config (~/.config/ubuntuone/syncdaemon.conf). It have to look like this

[bandwidth_throttling]
on = False 
read_limit = -1
write_limit = -1

If in your config `on` = True, change to False, restart UO client and get profit :)

spied here.

Monday, November 23, 2009

Setup memcached cache backend in Django in Ubuntu 9

In order to run them together we have to:
  1. Install necessary packages

    sudo apt-get install build-essential python-dev memcached libmemcache-dev python-dev
    

  2. Install cmemcache (using easy_install)

    easy_install http://gijsbert.org/downloads/cmemcache/cmemcache-0.95.tar.bz2

  3. And, optionally and for test only, run memcached

    memcached -d -m 1024 -i 0.0.0.0 11211

Sunday, November 22, 2009

Django auto language selection

I want user can select language he prefers. But I didn't found how to do it in Django easy. And I stopped on the following decision.

At first we need middleware which will detects language parameter in GET (name is in settings.LANGUAGE_COOKIE_NAME) and sets session's paramater `django_language`.
import settings

class LocaleDetectMiddleware(object):
    """
    This middleware parse GET request and detects the language user chosen
    """
    def process_request(self, request):
        if settings.LANGUAGE_COOKIE_NAME in request.GET:
            request.session['django_language'] = request.GET[settings.LANGUAGE_COOKIE_NAME]
Note, this middleware must be included in settings.INSTALLED_APPS before django.middleware.locale.LocaleMiddleware, because we use Django's standard locale detection routine.

Saturday, November 21, 2009

South for Django, or how to keep database up to date

South is a migration tool for Django.

There two ways to install South: system-wide or per-project. I choose the second way because I always know that it's works with the project.

  1. Get from repository.
    hg clone http://bitbucket.org/andrewgodwin/south/
    hg update -C 0.6.2 #this is stable version now
            
  2. copy south dir to project
  3. Add it to INSTALLED_APPS
  4. In order to create all system models
    ./manage syncdb 
            
  5. Further you should create necessary apps and its models (don't forget to add them to INSTALLED_APPS)
  6. To create initial migration for an application run this command
    ./manage.py startmigration <app name> --initial
            
  7. After you changed the model
    ./manage.py startmigration <app name>
            
    to create migration
  8. To migrate to current database state
    ./manage.py migrate [app]
            

These are the most often operations. More detailed info you get in official documentation.

Javascript SyntaxHighlighter

In this blog for code highlighting I use the cool SyntaxHighlighter. And for installing it I spend some time to way to include in blogspot's template. Now just copy the html-code and paste after <head> tag.

<link href='http://alexgorbatchev.com/pub/sh/2.1.364/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/2.1.364/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushBash.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCpp.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCSharp.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCss.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushJava.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushJScript.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPhp.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPython.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushRuby.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushSql.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushVb.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushXml.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPerl.js' type='text/javascript'/> 
<script language='javascript'> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.1.364/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>

Oh, to escape HTML to form accepted blog engine I use this.

Django authentication

How to login a user in Django? There are two ways.

First, "django way", use the function authenticate which returns User model with very necessary attribute backend. This way is good when you log a user in from a form when you know its username and password. But what do to do if needs to log the user in which loaded directly from User model, when you don't know its password (you just have a strongly encoded password's version)?

After a bit of googling I just was able to found a dirty hack - manually append backend attribute to User's object instance. Does anyone know a "django way" to this?

Here's the "Django way" code snippet

user = authenticate(username='user', password='password')
login(request, user)

And here's 'hack':

user = user_info.user
# a dirty hack ;(
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)

Tuesday, November 17, 2009

Restore GRUB boot loader after other OS has been installed

After updating Windows XP to Windows 7 Grub-loader has been lost. I can't work without Linux. And I found this solution:

At first Load from live-cd
grub
Let's know where os install
find /boot/menu/menu.lst
Output will be like this
(hd0,3)
Tell grub where its files
root (hd0,3)
And install it
setup (hd0)
quit
reboot
After rebooting we can see the GRUB-menu.

Script to easily create a Postgres database

#! /bin/sh
if [ ${#} -ne 3 ]
then
 echo "Usage: $0 db_name username password"
 exit 2
fi

DBNAME=$1
USER=$2
PWD=$3

echo "
CREATE USER $USER WITH PASSWORD '$PWD';
CREATE DATABASE "$DBNAME"
    WITH OWNER "$USER"
    ENCODING 'UTF-8'
    LC_COLLATE = 'en_US.UTF-8'
    LC_CTYPE = 'en_US.UTF-8'
    TEMPLATE = "template0";
GRANT ALL PRIVILEGES ON DATABASE $DBNAME to $USER;
" | psql -U postgres -d template1

or if you want just to exec it once:
echo "
CREATE USER $USER WITH PASSWORD '$PWD';
CREATE DATABASE "$DBNAME"
    WITH OWNER "$USER"
    ENCODING 'UTF-8'
    LC_COLLATE = 'en_US.UTF-8'
    LC_CTYPE = 'en_US.UTF-8'
TEMPLATE = "template0"; GRANT ALL PRIVILEGES ON DATABASE $DBNAME to $USER; " | sudo -u postgres psql -U postgres -d template1

Friday, November 13, 2009

Generate random password in Linux

cat /dev/urandom | tr -d -c 'a-zA-Z0-9' | fold -w 8 | head -1