Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Archives for code

Installing PostGIS on Ubuntu 13.04 Raring Ringtail

2 Comments

It's as simple as
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt-get install postgresql-9.1-postgis-2.0-scripts

You might see alternate instructions asking you to install Sharpie's PPA, but it works only for Ubuntu Precise. The instructions in the PostGIS trac wiki is also broken ‐ you will get an error like so :
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 postgis : Depends: libgdal1h (>= 1.9.0) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
This problem has been raised and a solution found in the PostGIS Trac Issue tracker. The first two lines are what needs to be done.
Filed under code
Aug 2, 2013

ForeignKey to custom user model in Django

0 Comments

Came across this problem when running a syncdb in Django (>=1.5) :

CommandError: One or more models did not validate:
APP.ModelName: 'user' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.

So you define a custom user model and add the appropriate line to the settings.py file. Then you add a ForeignKey from a model in your app the way the Django doc tells you to. And then you see this (the one above) error when you run a syncdb.

We hit upon this problem. The reason this happened was that the AUTH_USER_MODEL setting was below the INSTALLED_APPS in the settings.py file. Django will not handle forward referencing in this case. The solution is to simply move the AUTH_USER_MODEL setting above the INSTALLED_APPS settting.

Filed under django
Jul 16, 2013

Django 1.4 on CentOS 5.8

0 Comments

The default python version (2.4.3) on Centos 5.8 is not supported for Django 1.4 . Here's how to get Django 1.4 running on Centos 5.8 . Steps here are probably useful for future/different versions.

Step 1 : Enable EPEL

EPEL has newer version of Python available in it's repo. The first step is to enable it. The command you need to enable the EPEL repo is :
# rpm -ivh http://mirror.chpc.utah.edu/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
Once you install the package you've enabled EPEL.

Step 2 : Install Python 2.6 and easy_install

#yum install python26-distribute
This will install python 2.6 and easy_install

Step 3 : Install pip

#easy_install-2.6 pip
Now that you've pip installed, we can get started on install virtualenv and virtualenvwrapper.

Step 4 : virtualenv and virtualenvwrapper

#pip install virtualenvwrapper
will install virtualenvwrapper and virtualenv. Now you need to add some lines to your .bashrc
export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python26
source /usr/bin/virtualenvwrapper.sh

(Make sure to create the ~/.virtualenvs directory if it isn't already created.)
Now activate the changes to your bashrc by doing
#source ~/.bashrc

Step 5 : Create a virtual environment for your Django project

You can do this by running
# mkvirtualenv my-env
Once you run this you will automatically switch to the new virtualenv. Your shell prompt will be prefixed with (my-venv)

Step 6 : Install Django

Change to the directory you want to install Django in. Then run :
(my-venv)#pip install Django

Bonus!

1. Installing MySQL-python :

First install
#yum install mysql-devel
and then install MySQL-python (my-venv)#pip install MySQL-python

2. Installing Gunicorn

(my-venv)#pip install gunicorn

3. requirements.txt

It's often easier to just have a list of pip packages to install for a particular project in a file. Conventionally, it's named requirements.txt
(my-venv)#pip install -r requirements.txt
will install all the packages given in the requirements.txt
(my-venv)#pip freeze
Will show you a list of packages installed by pip currently. Something like this :
Django==1.4
MySQL-python==1.2.3
wsgiref==0.1.2

You can take this list directly and put it into the requirements.txt
(my-env)# pip freeze > requirements.txt

Filed under django, Linux
Aug 24, 2012

How to solve the “local edit, incoming delete upon update” issue in SVN

0 Comments

When I removed a file from svn it caused me lot of problems. This particular file was in CamelCase and I needed to replace it with a file in lower case. I svn deleted the CamelCase file and committed a copy of the lower case file. All this happened in a windows system. When I updated the checked out branch I got the error :

At revision 17582.
!  +  C FooBar.php
>   local edit, incoming delete upon update

Multiple attempts at svn cleanup, svn update etc. failed. I came across this stack overflow thread which recommended svn resolve. This did not work for me. Further down the thread I came across another solution which worked.

  1. create FooBar.php ( $touch FooBar.php )
  2. svn revert it ( $svn revert FooBar.php )
  3. Check status ( $svn status ) . It should say ?FooBar.php
  4. Now remove FooBar.php ( $rm FooBar.php )

That’s it, you’re done :)

Filed under code
Aug 11, 2011

SQLyog on Linux

0 Comments
Run SQLyog on Linux with WINE. You can download the sqlyog windows installer and just run it with WINE. SQLyog is my favourite GUI client. It is lightweight and powerful. I use the community edition which comes free. The paid versions of the app are awesome. I particularly miss the autocomplete feature. This feature autocompletes table names, field names, query commands etc. on tab press. I have used the MySQL query browser, but there's no comparison. The problem with SQLyog being that it runs only on windows :( . I tried alternatives - phpmyadmin, MySQL query browser - but none of them quite measured up against SQLyog. So I decided to try and get it working on my linux box ( Ubuntu Maverick - 10.10 ). I use Wine version wine-1.3.11 . Downloaded the latest installer from the community edition project page. Ran the installer with Wine and voila! That was it :) sqlyog running on wine in Ubuntu
Filed under code
Jan 16, 2011

How to update a field in a table with a field from another table

0 Comments

I needed a table which gave the map between the URLs and nodes in drupal. I ended up creating a table like this :

 

CREATE TABLE `nid_url_map` (
`nid` int(10) NOT NULL DEFAULT '0',
`url` varchar(255) DEFAULT NULL,
`type` varchar(32) DEFAULT NULL,
PRIMARY KEY (`nid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

To populate this table I used the url_alias table :

CREATE TABLE `url_alias` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`src` varchar(128) NOT NULL DEFAULT '',
`dst` varchar(128) NOT NULL DEFAULT '',
`language` varchar(12) NOT NULL DEFAULT '',
`nid` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`pid`),
UNIQUE KEY `dst_language` (`dst`,`language`),
KEY `src_language` (`src`,`language`),
KEY `nid` (`nid`)
) ENGINE=MyISAM AUTO_INCREMENT=267714 DEFAULT CHARSET=utf8

This is the query I ran at first to populate the table :

INSERT IGNORE INTO nid_url_map (nid,url)
SELECT SUBSTR(src,6) AS nid,dst AS url FROM url_alias
WHERE src LIKE 'node/%';

The url_alias table maps the internal url ( in the form of node/ID ) to the pretty URL of the page ( eg. comedy/monty-python-spam ) . Now what remained was the type. The node type is available in the node table :

CREATE TABLE `node` (
`nid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`vid` int(10) unsigned NOT NULL DEFAULT '0',
`type` varchar(32) NOT NULL DEFAULT '',
`language` varchar(12) NOT NULL DEFAULT '',
`title` varchar(255) NOT NULL DEFAULT '',
`uid` int(11) NOT NULL DEFAULT '0',
`status` int(11) NOT NULL DEFAULT '1',
`created` int(11) NOT NULL DEFAULT '0',
`changed` int(11) NOT NULL DEFAULT '0',
`comment` int(11) NOT NULL DEFAULT '0',
`promote` int(11) NOT NULL DEFAULT '0',
`moderate` int(11) NOT NULL DEFAULT '0',
`sticky` int(11) NOT NULL DEFAULT '0',
`tnid` int(10) unsigned NOT NULL DEFAULT '0',
`translate` int(11) NOT NULL DEFAULT '0',
`titlehash` char(32) DEFAULT '',
PRIMARY KEY (`nid`),
UNIQUE KEY `vid` (`vid`),
KEY `node_changed` (`changed`),
KEY `node_created` (`created`),
KEY `node_moderate` (`moderate`),
KEY `node_promote_status` (`promote`,`status`),
KEY `node_status_type` (`status`,`type`,`nid`),
KEY `node_title_type` (`title`,`type`(4)),
KEY `node_type` (`type`(4)),
KEY `uid` (`uid`),
KEY `tnid` (`tnid`),
KEY `translate` (`translate`),
KEY `title` (`title`),
KEY `titlehash` (`titlehash`)
) ENGINE=MyISAM AUTO_INCREMENT=197121 DEFAULT CHARSET=utf8

I referred the MySQL man page on the UPDATE query. Found a useful query in the comments there. In case I need to use this one again, here's where I'm gonna refer :)

UPDATE nid_url_map,node
SET nid_url_map.type = node.type
WHERE nid_url_map.nid = node.nid;

 

There you go, easy peasy :)

Filed under code
Dec 7, 2010

IRCTC disabled right click

7 Comments

Many readers of my post on booking tatkal tickets with Firefox's autofill forms extension have reported that IRCTC has now disabled right clicks. Not only does this hamper saving the form, it is also very annoying. I've found a workaround for this. After you fill in the form do not directly go and right click to save the form as a profile. Instead paste this code into the Firefox address bar -

javascript:void(document.oncontextmenu=null) .

This will re-enable the disabled right click. Now proceed as before.

This code can be used on any site that disables right click

Hat tip to this post on "Re-Enable Right-Click When Web Pages Turn It Off"

Filed under code, misc
Jun 9, 2010

How to programmatically create and log in drupal users

0 Comments

Creating a new user is very easy in Drupal 6. Here’s how.

<p>><p>$new_user = array(</p><p>'name'                   => $username,<br /> 'mail'                     => $mail,<br /> 'pass'                    => user_password(),<br /> 'status'                  => 1,<br /> 'auth_MODULENAME' => $username<br />)</p><p>$user = user_save(NULL,$new_user)</p><p>// log the user in</p><p>$user = user_authenticate($new_user)</p><p>

Now for the explanation. We create a $new_user array with values we want the newly created user to have. We pass this along to the user_save function and set the 1st parameter as NULL. From the code comments in the user module -

* @param $account
* The $user object for the user to modify or add. If $user->uid is
* omitted, a new user will be added.
*
* @param $array
* (optional) An array of fields and values to save. For example,
* array(‘name’ => ‘My name’); Setting a field to NULL deletes it from
* the data column.

So setting the 1st parameter as NULL creates a new user.

The parameters name, mail, pass and status are all self explanatory. The user_password function generates a random password (by default with a length of 10).

The (optional) ‘auth_MODULENAME’ element will record the user as being created externally. This will result in an entry in the authmap table like this -

“aid” “uid” “authname” “module”
“2″ “20″ “USERNAME” “MODULENAME”

The user_authenticate function logs the user in. This function expects an array as a parameter. It first loads the user in using the user_load function and in case of no errors logs the user in.

This approach of logging in a user is useful when we have the array with us which contains the raw values used to create the user. If all you have is the uid of the user, then logging the user in is very simple. Just use the global $user object.

</p><p>global $user</p><p>$account = user_load( array('name' => 'USERNAME') ); // or user_load(UID)</p><p>$user = $account</p><p>

Don’t use the user_authenticate function here as it expects the raw values(form values). This wil not work -

</p><p>$account = user_load( array('name' => 'USERNAME') ); // or user_load(UID)</p><p>user_authenticate((array)$account)</p><p>

It will take whatever password is stored in the user object(the raw password), md5 it and then run a query to load that user. Since the password in the query is not the raw password but the md5, the query will return nothing. This will cause an error in the user_authenticate function.

For a direct code example for programmatic log in, check the devel module’s devel_switch_user function.

Filed under drupal
Feb 22, 2010

How to dump or export all the table definitions in a MySQL database

0 Comments

So you have  a database with loads of tables. You want the table definitions of all of them. You don’t really need the data. mysqldump to the rescue!

mysqldump -u USERNAME --password=PASSWORD --no-data --opt DB1 > DUMPFILE

That’s it! Easy as a pie :) The key here is the –no-data option. It dumps all the table definitions, but not the table data.

Want to do this for multiple databases? No problem.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt --databases DB1 DB2 > DUMPFILE

The –databases option allows you to add multiple databases.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt --all-databases > DUMPFILE

The –all-databases option allows you to dump all the databases.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt DB1 --ignore-table DB1.TABLENAME1 --ignore-table DB1.TABLENAME2 > DUMPFILE

–ignore-table option allows you to skip dumping certain tables. Do not forget to specify the databasename when using this option.

 

Know any more tricks? Let us know in the comments below :)

Filed under code
Feb 5, 2010

Firebug opens automatically for all pages

0 Comments

Faced this bizzare issue today in Firefox. Firebug was opening automatically for all pages. Even on closing firebug, all I had to do was click on a new link in the same tab for it to appear again. This happened for all tabs.

Turns out I had accidently turned on “On for all Web Pages” option. Right clicked on the Firebug icon to turn it off. Problem solved :D

Right click Firebug

Right click Firebug to disable "On For All Web Pages"

Filed under code
Jan 20, 2010