Installing mod_python on Mac OS X Leopard (10.5.x)

Apache2 comes pre installed on Mac OS X Leopard as does Python (2.5). But it doesn’t come with mod_python. Here is what you need to do to get it to work.

download the latest file from http://www.apache.org/dist/httpd/modpython/ I downloaded (mod_python-3.3.1.tgz)

$ tar xvzf mod_python-3.3.1.tgz
$ cd mod_python-3.3.1
$ ./configure –with-apxs=/usr/sbin/apxs


Now we need to patch the make file so that it will work correctly. edit src/MakeFile


  • Add -arch x86_64 -arch ppc -arch i386 to the end of the LDFLAGS line.

  • Add -arch x86_64 -arch ppc -arch i386 to the end of the CFLAGS line.

  • Under the mod_python.so target add the following to the line after the -c -Wc,"-arch x86_64" -Wc,"-arch ppc" -Wc,"-arch i386" so my complete line looks like. $(APXS) $(INCLUDES) -c -Wc,"-arch x86_64" -Wc,"-arch ppc" -Wc,"-arch i386" $(SRCS) $(LDFLAGS) $(LIBS)



When that is done:
$ make
$ sudo make install

and then:

$ cd /etc/apache2
$ sudo cp httpd.conf httpd.conf.orig
$ sudo emacs /etc/apache2/httpd.conf

Add the following to the httpd.conf file.

LoadModule python_module libexec/apache2/mod_python.so

Restart apache
$ sudo /usr/sbin/apachectl restart


Test configuration
sudo /usr/sbin/apachectl -t


That is all, if there are no errors, it worked!

Read more

Share

Django Query using value from another field in same table

I came across a query that seemed like it should be pretty easy, but it wasn’t.

Suppose you have the following Model Class.

# pseudo code.
class Person(models.Model):
first_name = models.CharField()
last_name = models.CharField()
I would like the query to return all Person’s who have the same first_name and last_name

My first guess was to do the following.

person_list = Person.objects.filter(first_name=last_name)

but that doesn’t work because it wants to have last_name as a variable.

This is what I ended up doing.

person_list = Person.objects.extra(where=[‘first_name=last_name’]

You could also do it using a models.Manager, if that is what you prefer.

Even though this is an edge case, I’m not sure why Django doesn’t support this out of the box, but I am pretty sure there is a pretty good reason.

Read more

Share

Managing Django settings across multiple environments

I don’t know about you, but when I typically work on a Django application I have at least two environments (Local and Production), and a lot of times I am working on these projects with at least one other person.

The way that django currently supports project settings is to have all settings in a settings.py file. This works fine if you are working by yourself in one environment, but once you add something else, it makes managing the settings a pain. One solution, which isn’t very flexible is to make all environments have the same settings. That solution doesn’t usually fly, so I prefer another approach.

I take my settings file and only put settings that will be the same for every install in there. Then I create a local_settings.py file in the same directory and put only settings that are environment specific in that file. Each environment is responsible for providing their own file and setting their own settings.

One important thing to remember, NEVER put the local_settings.py file in source control or else it will just be a matter of time before someone accidentally checks in their file and breaks everyone else. I typically do a Subversion ignore on the file.

I also create a file called local_settings.py.template that has all required settings and examples, and stick that one in source control. When you are setting up a new environment all you need to do is copy the local_settings.py.template and change the values for your ENV. Make sure you keep the local_settings.py.template file up to date.

short example of settings.py and local_settings.py


» settings.py

import local_settings

# there is more above that I removed

DATABASE_ENGINE = local_settings.DATABASE_ENGINE
DATABASE_NAME = local_settings.DATABASE_NAME
DATABASE_USER = local_settings.DATABASE_USER
DATABASE_PASSWORD = local_settings.DATABASE_PASSWORD
DATABASE_HOST = local_settings.DATABASE_HOST
DATABASE_PORT = local_settings.DATABASE_PORT

# there is more below that I removed



» local_settings.py

# there is more above that I removed

DATABASE_ENGINE = ‘mysql’
DATABASE_NAME = ‘dbname’
DATABASE_USER = ‘dbusername’
DATABASE_PASSWORD = ‘dbpassword’
DATABASE_HOST = ’’
DATABASE_PORT = ‘’

# there is more below that I removed

Read more

Share

django common commands

I keep forgetting all of the different django commands so I am creating this cheat sheet for myself.

# start a new project

»> django-admin.py startproject $mysite

# create a new site inside of a project

»> python manage.py startapp $siteName

# start dev server

»> python manage.py runserver

# sync the database with recent model changes.

»> python manage.py syncdb

Read more

Share

iText HTMLWorker Style guide

iText provides a way to convert HTML snippets to PDF using the HTMLWorker class. Unfortunately there isn’t much documentation on what you can do for styles. In order to figure this out I went through the source code. I’m putting it here so that I won’t have to do that again, and so that others will be able to gain something from my pain.

This is from iText 2.0.8

HTMLWorker supported tags

“ol ul li a pre font span br p div body table td th tr i b u sub sup em strong s strike h1 h2 h3 h4 h5 h6 img”










TagAttribute [iText methods]Values
palign [setAlignment()]center, right, justify
pleading [setLeading()]float or float, float
pbefore [setSpacingBefore()]float
pafter [setSpacingAfter()]float
pextraparaspace [setExtraParagraphSpace()]float


If you find any more let me know and I’ll add them to the list.

Ken

Read more

Share

New version of CampRate.com is now Available

I just released a new version of CampRate.com and here are some of the new features / changes.

  • I am about 90% finished with the new image uploader, it uses AJAX to make it look cool.. This just needs to be tested some more, and will be released when we finish with the new add/edit campground flow.
  • 95% finished with the PayPal upgrade. Just need to do some more testing. When complete, people can upgrade their listings to add new features using paypal.
  • Added links to the siteMap page to the google sitemap file, and the yahoo sitemap file.
  • finished working on the Latitude/longitude script, given an address it runs and gets the lat and long and updates the db..
  • Made it easier to add a maintainer to a campground when you don’t have access to the database.
  • When turned on basic listings will have only 1 image.
  • Setup an efax account so that we can get free incoming fax’s
  • If you want to do something that requires you to login, but you need to register, it will now remember what you were doing before you registered, and when complete will forward you to that page.
  • added link to add Campground reviews RSS Feed to Google Reader.
  • Created a Credits page where we can give credit to tools and people that helped us create CampRate.com
  • Changed it so that it says USA instead of campground list in the breadcrumbs
  • Finished adding the rest of the state images
  • Added a new SlideShow option to view the campground images.

CampRate.com - Campground Reviews

Read more

Share

AJAX Struts File Upload Progress Meter

** This Post is a work in progress, and is still being worked on, it hasn’t been edited yet, and may contain errors, use at your own risk **

Over the course of the last year or so I have been hearing a lot about AJAX, and how cool it is, and what neat stuff you can do with it. At first I wasn’t too impressed, mostly because I have heard it all before. It wasn’t until I saw
google maps that I realized how cool it was. I have been itching to use Ajax ever since.

My first attempt at AJAX was using the Google Maps API to add a map to the search results page on CampRate.com. It is fairly simple, the search allows the visitor to search for campgrounds given a zip code and a search radius. I search the CampRate.com database and return the campgrounds in the area, and I then add Site Markers onto the google map so that it is easier to see where the campgrounds are located. This was fairly easy to setup given the fact that Google opened up their API.</span

Once I finished the Google Map hack, I was looking for something else to do. After looking around for a little while I thought of the perfect thing to AJAXify. On CampRate.com we allow Campground Owners to upload images of their campgrounds to the site, and campers are also allowed to upload images of their camping trips for everyone to see. The interface for this is very simple, it is just a regular File browse box and a submit button, once they upload the image, the browse box goes away and a thumbnail of the image is inserted. This is a very basic and a very late 90’s way of doing things. What a prefect thing to AJAXify.

Once I knew what I was going to work on, I needed to figure out how I could improve on the design. After a couple of revisions I came up with the new design and layout. One of the features that I thought would be good to use AJAX for, is a progress bar for showing the status of the upload.

Here is what the new upload page looks like, this is where I would like to add a progress bar.



Progress Bar.
Before I started coding for the progress bar, I decided to search around to see if anyone else had used a progress bar before. I found a bunch of examples, PHP, Ruby, and a good one using Java + DWR. I decided to look at the Java + DWR example since I was already using Java and DWR on CampRate.com. The Java + DWR example was a great start, the only problem was that it didn’t use Struts and once the file was uploaded it changed pages. There were some comments associated with the article that mentioned how to get it to work with Struts, so I decided to take the source code from the article and play with it and see if I could get it to work with Struts.

What I wanted to change in the example.

  • Get the example to work with struts
  • get the file to upload to a selected directory
  • show the status of the file in the progress meter
  • when the file is uploaded don’t leave the page, stay on the same page (no redirect or refresh).
What I did.
  • I downloaded the war file with source code from the article
  • refactored the project directory
  • changed the build script
  • setup struts, log4j
  • added logging statements for easier debugging
All of the new struts code that I added was put into a new package net.kencochrane so that you can easily see what is was originally there, and what was added in order to get struts to work with the original Code.


Struts Changes
  • Added the struts libs to the lib dir in the project
  • added the XML files to the config directory
  • changed web.xml so that it works with struts
  • created some actions, and form

Code Changes
  • Added a MultipartRequestHandler
  • added struts actions to handle all requests.
  • The example now actually stores the uploaded files to a temp directory.
  • Added a new package net.kencochrane

Uploading a file without leaving the page
I looked around to find a way to upload the files without leaving the page, but from what I could find, I guess you can’t do this with javascript because of security reasons. The most common way to do this was to use an iFrame and use this iFrame to hold the upload form, and when the form is submitted the form gets posted like normal, and using some tricks with javascript you can make it look like the file is uploaded without ever leaving the page.

I changed the index.jsp file to include an iFrame, the iFrame holds the upload form. Using javascript I was able to make it looks like the files are being uploaded without leaving the page.

Here is what the new pages look like, the first image looks like the original form, the only difference is that the page is now using an iFrame to hold the upload form.



File upload in progress, it displays the progress bar until it is complete, and then the bar goes away and is replaced with a status message.


The file is finished,
progress bar is hidden, and the status message tells you the file status.



What is left to do
  • add better error checking
  • Check for file being uploaded that is too large.

Now What
Now that I have the example working the way I want it (with struts, DWR, and having the file uploaded without leaving the page.) I can work on adding this code to CampRate.com.

The more that I think about it, I could also use this on PopcornMonsters.com as well, we allow people to add images for movies, and actors to the site, but the interface is very ugly, once I get the code written for CampRate.com I could easily spend the time and get the same code to work on PopcornMonsters.com, that would make the whole adding images experience so much better.

Special Thanks to Pierre-Alexandre Losson for writing the original Article and for posting his code for everyone to use.



I will update this post with some completed pictures once I finish up with all of the code. Feel free to post comments.

Ken Cochrane
http://KenCochrane.net


Read more

Share

Debian Package management

Debian Package management

apt-get update
Run this after changing /etc/apt/sources.list or /etc/apt/preferences. (this debianhelp.org page is helpful); run it periodically anyway, to make sure your source list is up-to-date.

apt-get install packagename
installs a new package: rpm -i package.rpm

apt-cache search string
Searches for string in the list of known packages: like using rpmfind.

dpkg -l package-name-pattern
List packages matching pattern: rpm -q pattern or rpm -qa | grep pattern.
aptitude
Curses viewer of packages installed or available.

apt-cache showpkg pkgs…
Show information about packages.

apt-cache dumpavail
Prints out an available list.

apt-cache show pkgs…
Displays package records, similar to dpkg –print-avail. Like rpm -q packagename.

apt-cache pkgnames
Fast listing of every package in the system.

dpkg -S file
Which installed package owns the file? rpm -qf file.

dpkg -L package
List files in the package. rpm -ql package

apt-file search filename
Search for a package (need not be installed) containing files including the string. apt-file is a package of its own, which you may have to apt-get install first, then run apt-file update. If apt-file search filename shows you too much, try apt-file search filename | grep -w filename (which shows you only the files that contain filename as a whole word) or variants like apt-file search filename | grep /bin/ only files located in directories like /bin or /usr/bin, useful if you’re looking for a particular executable).

apt-get autoclean
Run this periodically to clean out .deb archives from packages which are no longer installed on the system. You can regain lots of disk space that way. If you’re really desperate for disk space, apt-get clean is more radical, and will remove .deb files even for packages currently installed. But most of the time you probably don’t need the .debs any more, so it might be worth it if you’re strapped for megabytes.

apt-cache policy pkg

Read more

Share

CampRate.com online campground directory with reviews

Last summer a bunch of friends and I decided we would go camping. I have been camping before, when I was a kid, so I knew what I was getting myself into. First off I had to find out if I had all of the proper equipment. I found my Tent, sleeping bag, propane grill and some big blue tarps. Once I gathered all of my stuff and dusted it off, I needed to check with my friends to see if they had everything they needed as well. After a few weeks we all gathered all of the stuff we needed, and all agreeed on a weekend that worked for everyone. Once we had that done, we thought it would be a piece of cake to find a campground to fit our needs. Were we wrong.

The first thing we did was hit the internet, we found a bunch of websites for campgrounds in Maine, but they were all marketing hype, no real testimonials from real campers that we could find. We eventually narrowed it down to 3 or 4 different campgrounds and we just had to pick. We picked the one we thought was the best fit for us, in the traveling range that we agreed upon (less than 1 hours drive). We went camping and we used my big blue tarps more then anything else, since it rained off and on the whole weekend. Other than that it was a pretty good experience.

As my friends and I were talking around the campfire, we were wondering why in this day and age, it took us so long for us to find a good campground. We realized that there wasn’t any one website out there where we could go to, and search all of the campgrounds at once, and compare what they had, and then read reviews from people who have actually been there. Since we were all in the software industry, and we all have developed many websites, we decided this might be a good website to develop. It would help people find a campground that they want faster, and you can actually see if the campground is as good as they say it is, on their website.

That is how CampRate.com was born. We wanted to create a website that would list all of the campgrounds in the US and the visitor could pick a state or zip code and search for what amenities that they wanted to have in a campground. Once they got a list back, they could then sort by rating type. For instance, if you wanted good clean bathrooms, then you could sort your results so that all of the campgrounds that had very clean bathrooms come up first. You could also do this for other areas such as Pet and Kid Friendly campgrounds. We figured campgrounds would like it as well since they would be able to see what their visitors are saying, and if they are saying something bad, such as the bathrooms are not clean, then they know that they better clean up their bathrooms or they could lose more customers.

We released CampRate.com on June 1st, 2005 and we are still adding features, and making it look nicer, but right now we are only adding campgrounds for the state of Maine. Once we get all of those added we will slowly move out to the different states, growing slowly until we have the whole USA. One day we hope to make it so that picking a campground won’t be so hard, and you could then spend more of your valuable time doing more important things. If you get a chance you should check us out, and if you can think of anything that would make our site better, please let us know.

CampRate.com - Campground Directory and Reviews

Read more

Share

Getting WinCVS out of flat mode

If you accidentally browse up to the root directory or some other level that is too large for WinCVS to initially deal with and you have the flat mode browser view toggle button of WinCVS on, WinCVS will freeze indefinitely. If killing it in Task Manager and restarting doesn’t fix your problem, you need to hack the registry.

Launch regedit (Start -> Run “regedit”) and browse to:

HKEY_CURRENT_USER/Software/WinCvs/wincvs/CVS settings

In CVS settings, find the variable P_DisplayRecursive in the right hand window. Right-click on P_DisplayRecursive and select Modify from the right-click menu. in the window that gets displayed, change the Value data set with only two numbers in it from the 01 to 00. Then click Ok at the bottom of the window, shut down Registry Editor, start up WinCVS again, and you should now have the flat mode button toggled off.

Some companies don’t let their employees run regedit, but do allow employees to run .reg files. If so, put the following into a file called Unflatten.reg, run it, it should work for you:


Windows Registry Editor Version 5.00 /S
[HKEY_CURRENT_USER\Software\WinCvs\wincvs\CVS settings]
“P_DisplayRecursive”=hex:00

Read more

Share