Setting up a Django project with an ORM connected to an existing MySql instance

1.  Install Python:

I had to add the following to my %PATH% variable


2.  Install Django:  pip should have been installed with python so type

pip install Django==1.7.7

**Django comes with it’s own light-weight web-server, so there is no need to install one for development.

I’m going to follow for the following steps.

3. Create the project:  Run the following command in the directory you wish to use for development.  It will create the project.

$ startproject CourseOptimizer

4.  Install the C++ .0 python compiler at:

5.  Add the MySql libraries

pip install MySql-Python (I spent about an hour before I found out it's very difficult to get it to work on windows)

pip install PyMySQL

To use it, you must go to the django directory, exactly in this directory
and do a search and replace of MySQLdb with pymysql
in the two files "" and  "

6.  Connect to database:  In the file, change the DATABASES variable to:

 'default': {
 'ENGINE': 'django.db.backends.mysql',
 'USER': 'XXXX',
 'HOST': '',
 'PORT': '3306',

7. Scaffold the database schema for a Django site:

$ python migrate

8. Verify it runs:

$ python runserver

Go to

9. Create your first Models:

$ python startapp CourseCatalog

10. Map the legacy database to the of the CourseCatalog app:

$ python inspectdb > coursecatalog/

11. Open a python shell and verify the data has been mapped:

$ python shell
>>> from CourseCatalog.models import Course, Specialization, SpecializationCourse   # Import the model classes we just wrote.

# No questions are in the system yet.
>>> Course.objects.first().coursename
It should return the first row in the database's coursename

12. Create a view:

In the CourseCatalog/ file add the following and save:

from django.shortcuts import render
from django.http import HttpResponse

from CourseCatalog.models import Course

def index(request):
 course_list = Course.objects.all()[:5]
 output = ', '.join([p.coursename for p in course_list])
 return HttpResponse(output)

13. Map the view to a route

Create a file CourseCatalog/ file add the following and save:
from django.conf.urls import patterns, url

from CourseCatalog import views

urlpatterns = patterns('',
 url(r'^$', views.index, name='index'),
In the CourseOptimizer/ file add the following and save:
 url(r'^coursecatalog/', include('CourseCatalog.urls')),

14. Create a template directory and index.html template

The template structure is /App/templates/App/index.html, so in this instance, create /CourseCatalog/templates/CourseCatalog/index.html and add the following:
{% if course_list %}
 {% for course in course_list %}
 <li>{{ course.coursename }}</li>
 {% endfor %}
{% else %}
 <p>No courses are available.</p>
{% endif %}

15. Run the server and verify you get a list of courses

The template structure is /App/templates/App/index.html, so in this instance, create /CourseCatalog/templates/CourseCatalog/index.html and add the following:
$ python runserver
Browse to and see the courses

A simple static page web server using node js


I’m building a SPA using AngularJS, and Chrome doesn’t allow Angular to load the html partials because of cross origin request errors.  Firefox works, but I just prefer to develop using Chrome, so I needed to host the SPA somewhere.  IIS seemed like overkill, so I turned to Node which turned out to be very simple.

  1. Assuming you have node and NPM installed, install connect and serve-static with NPM
    $ npm install connect serve-static
  2. Create a server.js and place it in the same directory as the entry point of your SPA
    var connect = require('connect'); 
    var serveStatic = require('serve-static');
  3. Start the web server
    $ node server.js
  4. Browse to your SPA entry page:


That’s it!  I really love some things about Node, and the simplicity of setting up a web server is one of those things.

How to add polygon search capabilities to Apache SOLR


This is another post to document for myself how to add polygon search capabilities to solr since I do it very seldomly.


  1. >wget
  2. >sudo unzip -d jts-1.13
  3. >sudo cp lib/* /var/lib/tomcat6/webapps/solr/WEB-INF/lib
  4. Add the following field type to your solr schema:
    <fieldType name=”location_rpt” class=”solr.SpatialRecursivePrefixTreeFieldType”
  5. Add a field of type “location_rpt” to index the lat/lng data:
    <field name=”geo” type=”location_rpt” indexed=”true” stored=”true” multiValued=”true” />
  6. Restart Solr
  7. Index the data in solr
  8. Query to ensure you have the ability to search a polygon:*%3A*&fq=Geo:%22Intersects(POLYGON((-84.113824%2034.085196,-84.003009%2034.146633,-83.856827%2034.032769,-84.188127%2033.993178,-84.113824%2034.085196)))%22+&wt=json&indent=true

How to increase solr jvm heap memory


I’m really only documenting this for myself, so I know where to look when I need to do this again in 6 months, but maybe it will help someone else as well.

I was getting an exception:

java.lang.RuntimeException: java.lang.OutOfMemoryError: Java heap space

After digging around on StackOverflow for a while, I found several methods of increasing the default memory allocation of 128m.

The one that worked for me was changing the JAVA_OPTS value in the script and restarting tomcat.

Step 1.  Locate

> ps aux | grep catalina

Look for the catalina.home.  In my case it was "/usr/share/tomcat6". will be located in the bin subdirectory.

Step 2:  Edit the following lines to set the JVM heap size:

if [ -z "$LOGGING_MANAGER" ]; then
   JAVA_OPTS ="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms256m -Xmx512m"

Arduino Garage Door Opener Part 3

So, I’m finally back healthy enough to be coding in my free time, and I have made tremendous progress on the garage door opener.  Here is how it currently works:

I’m using magnetic reed switches to sense the state of the doors, then send it over to the controller via rf (315 mhz).

When the garage door installers put the garage door opener in, they were nice enough to use Cat5 cable, and only used 2 pairs per safety sensor.  That means I can use the other pairs to connect the reed switches without running additional wires.  After I’m done, the door sensors will not even be noticeable!

Now to the arduino code…

I opted to use a 2 character code to represent the state of both doors.

  • 00 represents both doors being open.
  • 11 represents both doors closed.
  • 10 represents door 1 being closed.
  • 01 represents door 2 being closed.

I have updated the code on GitHub:

There are 2 arduino projects (an opener and a sensor), and a phonegap mobile app.

The opener receives the rf signal indicating the status, and in turn, serves a json result to web requests indicating the status of the doors.  It also listens for web requests to open and close the doors.

The sensor sends the 2 digit code indicating the door status.

The phonegap app requests the door status every 10 seconds, and provides 2 buttons to open and close each door.

How to set up a Solr 4.0 GA instance on Windows Azure using an Ubuntu VM


I’ll assume that whoever is reading this has a basic knowledge of setting up a VM on Azure.  So the first step is to set up the Ubuntu Server VM.

Now we’ll delve into how to actually set up the Solr 4.0 GA instance.  I don’t know if it’s right or wrong, I just know it helps in my instance, so once I set the VM up, I use Putty to ssh into the DNS name of the server, and log in with the credentials I created.  I then issue the sudo su command, and give the same password I used to login to gain sudo rights.

At this point if all has gone well, you’ll be looking at the /home/username directory, which doesn’t contain anything.

First things first, we’re going to be running Solr on Tomcat 6 (I have tried with Tomcat7, and did not have success, so I went back to Tomcat6), which is the java servlet that we’ll be using.  There is also Jetty, but I have found Tomcat to have more documentation around it, and it has worked great for me in the past.  To begin with, go ahead and install the openjdk with the following command:

#apt-get install openjdk-7-jdk

To verify that it installed correctly, run:

#java -version

and it should show you the JDK version information.

Next we need to install tomcat6 and the webadmin using apt-get:

#apt-get install tomcat6 tomcat6-admin

Next, and this is big:  In the Azure Portal, you need to set and endpoint for the VM to port 8080 so that you can reach the admin interface via the web (I know this because it has bitten me twice).

At this point you should be able to reach the tomcat admin gui by using your VM [DNS Name]:8080.  To enable users, next  go into the /etc/tomcat6/tomcat-users.xml file and edit the users to add a role name of “manager-gui” and a user with that role.

At this point, I like to stop Tomcat with the following command:

# /etc/init.d/tomcat6 stop

Next we are going to install Solr 4.0 GA.

You’ll need a directory from which to run you solr instance.  I usually do this under /var/lib/tomcat6/solr, but it’s really your choice, and many people choose /opt/solr for their instances.

Download the file from one of the mirrors. In my instance:


Then issue:

# tar xvfz apache-solr-4.0.0.tgz

In the apache-solr-4.0.0 directory, there is a sub directory called dist, and another called example that we’ll be using to get this instance up and running.

In the dist directory, look for apache-solr-4.0.0.war, and copy and rename the file solr.war into the /var/lib/tomcat6/webapps directory.  Once tomcat6 starts back up, it will automatically unpack the war into a folder named solr.

In the example directory, copy the solr/* directory into the directory that you set up as your solr home directory above.

This will create a bin and collection1 directory under your solr home, as well as solr.xml and zoo.cfg files.

You’ll also need to create a data directory for solr to store the index, so issue:

mkdir data

You also need to give tomcat6 permissions on that folder, so issue:

sudo chown tomcat6 /var/lib/tomcat6/solr/data

Drill into the collections/conf directory and open the solrconfig.xml file.

Edit the <dataDir>{}</dataDir> directory to point to your data directory:


Now cross your fingers and restart tomcat6: # /etc/init.d/tomcat6 start

If all goes well, you should be able to reach solr via:  [DNS Name]:8080/Solr

If not, look at the catalina logs in your tomcat6 instance for exceptions.

Good luck, it seems each setup is a little different.