Jquery UI Autocomplete — Focus event via keyboard clears input content

If mousing over your autocomplete results correctly updates the input box with the currently highlighted value, but doing so via keyboard doesn’t (leaves the input blank) no matter what you put in your focus function, you need to prevent the default behavior from triggering.

For whatever reason, the default behavior works for mouse hovers but not keyboard.

        $("#faq_search").autocomplete({
            source: "/faq/ajax/",
            focus: function( event, ui ) {
                event.preventDefault(); // without this: keyboard movements reset the input to ''
                $(this).val(ui.item.question);
            },
            select: function( event, ui ) {
                document.location.href = ui.item.url;
            },
            context: this
        })

OSX – Install PIL on OSX 10.7 Lion

If the jpeg encoder is not available when PIL is compiled, you should simply remove PIL, and reinstall it after making sure libjpeg successfully installs.

I use homebrew to install libjpeg.

brew install libjpeg

# linking failed for me
sudo brew link jpeg

# sudo pip uninstall PIL if you already have PIL installed.
sudo pip install PIL

How to access PIP -e svn+ specific revision eggs via normal import

PIP lets you install specific revisions from SVN if you use the -e flag.

pip install -E env -e svn+http://code.djangoproject.com/svn/django/trunk/@16406#egg=django

The problem is that unlike installing without the -e flag, it installs the source files in /YOURENV/src/HERE/, meaning if you’re simply adding your virtualenv’s site-packages to your python path, python won’t find these modules.

I found the answer here http://stackoverflow.com/questions/1875037/pip-wsgi-import-errors

The solution

Use the python site module and addsitedir function which automatically searches *.pth files in the directory and includes those paths.

import site
site.addsitedir('path/to/my/site-packages/')

import django
# success

OSX Terminal Colors Suddenly Gone – LSCOLORS CLICOLOR

If for whatever reason your ls command is no longer showing colored directories and looks like so:

Check the output of echo $TERM

$>echo $TERM
xterm-256color

It should be xterm-color

Mine was xterm-256color — probably set when my graphics card bugged out.

Edit your terminal config file (mine is bash, so ~/.bash_profile or ~/.bashrc) and add export TERM=xterm-color somewhere (or just enter into your bash shell for temporary results).

$>export TERM=xterm-color

After setting TERM to xterm-color, my directories look like they used to:

Installing Solr and django-haystack on Ubuntu with OpenJDK

Install django-haystack

This part is easy – django-haystack is just a python module.

pip install django-haystack

Install OpenJDK and Solr

Solr is a Java program – we need the Java Runtime Environment to run our Solr server.

I’ll be using OpenJDK and solr-jetty on Ubuntu.

apt-get install openjdk-6-jre jetty solr-jetty 

Find and configure solr

$ find / -name "solr" 
/var/lib/solr
/var/lib/jetty/webapps/solr
/usr/share/solr
/etc/solr

Now that we know our solr config files live in /etc/solr/conf, have django-haystack generate the solr schema.xml file.

python manage.py build_solr_schema > /etc/solr/conf/schema.xml

Tweak solrconfig.xml

I’ll need to enable the MoreLikeThis handler by adding “” to the solrconfig.xml file.

Modify Jetty to run on port 8983 – default solr port

My jetty defaulted to port 8080 – a port I commonly use for other projects (nginx -> apache). Also, the default solr port is 8983 which my development environment uses. We’ll need to update jetty to use a different port.

I found the config file by searching for the jetty folder (/etc/jetty/jetty.xml) – modify the “port” line to whatever port you have set in your django settings HAYSTACK_SOLR_URL

<Set name="port"><SystemProperty name="jetty.port" default="8983"/></Set> 

Start jetty

Navigate to your jetty folder, and run.

$ java -jar /usr/share/jetty/start.jar

Build your solr indexes

Now we need some data to search against.
Assuming you’ve set up django-haystack, run the rebuild_index management command.

$ python manage.py rebuild_index

Set up cron job to rebuild indexes however often you need

$ crontab -e
$  0 0 * * * python manage.py update_index --age=24 --remove

Unix – Find Largest Files / Free Space

I was looking for places to free up some space on our server – I recalled accidentally having a 12GB tar archive at one point of our custom files.

Check free space

$ df -k
# list all free space
/dev/xvda             15117312  12306456   2196456  85% /
devtmpfs                773972       112    773860   1% /dev
none                    774176         0    774176   0% /dev/shm
none                    774176        56    774120   1% /var/run

Find largest files in a given directory

Use the du command to find the size of a given directory.

Use du -h to get human friendly results

$ du -h
8.3G    .

List all files individually

To list all files individually, use the -a flag

$ du -a -h
16K     ./images_6.jpeg
112K    ./18175_proof.jpg
64K     ./100_3829...jpg
284K    ./19913_final.eps
104K    ./super_heroes.jpg
36K     ./siteyanyo.jpg
8.3G    .

List all files and sort by file size

To sort by file size, pipe the output into the bash sort command. The gotcha is that you can’t use -h as sort thinks 1000K is bigger than 8G.

Use the -B flag to specify block size

From du –help:
SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

$ du -aB M | sort -n | tail -n 4
34M     ./Helvetica.pdf
46M     ./Harleking_TT1.psd
47M     ./Logo_1.psd
8469M   .

Note I used tail to get the last 3 lines.

You can also pass the -r flag to sort to reverse the order and get the first 4 lines via head – if that’s more intuitive as the “top” files.

$ du -aB M | sort -nr | head -n 4
8469M   .
47M     ./Logo_1.psd
46M     ./Harleking_TT1.psd
34M     ./Helvetica.pdf

Grove Server Upgrade

I’m moving Grove’s backend servers to a bigger VPS for some planned extra load on the site.

While the occasional terrible unix-glitch-that-I-don’t-understand happens, I think the risk is worth the reward.

Today’s problem was due to an incorrect static IP address setting leading me on a wild goose chase. That, and trying to SSH to the wrong IP address leading me on yet another wild goose chase messing with my firewall.

Downtime went from a planned 12 minutes to a total of around 30-45 minutes as I swapped the DNS from one server and back a few times.

Lesson learned: do not deal with your server at night. Take it down mid-day when your brain is at its peak. Hah! (I will probably never do that, but it actually makes sense.)

I’ve decided to start going to some Python user groups and hopefully make some friends in the industry. When it comes to server stuff, you really need some friends you can count on calling 🙂

Ubuntu / Unix Server – Linode connection timed out; no servers could be reached.

I’ve been battling this problem for a while.

I opened all firewalls, killed ufw, killed iptables, still I was unable to get any connections to work. Ping / dig / external DNS / all failed.

tcpdump showed incoming connections failing but “something” was making it here.

Turns out the problem was… an incorrect value in my /etc/network/interfaces file for my eth0.

Make sure that your IP address is listed in ifconfig.

ifconfig | grep my_ip_address

My static IP was missing a few digits.

Set the correct value, restart networking, and we’re good to go.

/etc/init.d/networking restart