Vixiom Axioms

July 26, 2006

Code Highlighting

Filed under: Misc. Alastair @ 2:22 am

Google and TextMate come through again.

Found this post on using TextMate for coloring code in blog posts. Works great - except for a couple of tab issues, see post blow the comments should be aligned and the bottom ‘if’ statement is kinda wacky - I swear in my code it’s all indented properly! :P

Now I have to fix my earlier posts, thankfully the blog is only a few days old.

Digg! submit Code Highlighting to stumbleupon.com submit Code Highlighting to del.icio.us submit Code Highlighting to reddit.com Like this post? subscribe to the feed.

Rails StringIO File Upload

Filed under: Ruby on Rails Alastair @ 12:09 am

# UPDATE

It turns out the method below works unless your file is below a certain file size (which I haven’t figured out yet but it’s around 15k). If your file is too small it will be StringIO not a Tempfile. So how do we check it then? By the file size, if it’s 0 then don’t run the image upload code.
Replace

@image = Image.create params[:image] unless params[:image][:file_data].kind_of? StringIO

with

@image = Image.create params[:image] unless params[:image][:file_data].size == 0

# END UPDATE
Coming from PHP getting file uploads to work with Rails was a bit of a mind funk.

The biggest issue is that a file upload field returns different objects depending on if you browsed for a file or left it blank. If you browsed and chose a file the object will be a ‘Tempfile’, if it’s blank it will be ‘StringIO’. That took a couple of hours to figure out.

The next step was figuring out how to check the object type, that’s when I stumbled upon some beauty Ruby code

object.kind_of?

Simple once you know how.

The file field from the form in my view:

<%= file_field(“image”, “file_data”) %>

Here’s my controller code for updating data from the form (the controller is products_controller.rb so I’m setting a relationship to the Image ‘product_id’):

def update #save image
  params[:image][:product_id] = params[:id]
#upload it unless it’s StringIO
  @image = Image.create params[:image] unless params[:image][:file_data].kind_of? StringIO #save product params
  @product = Product.find(params[:id])
#update params that have changed
  if @product.update_attributes(params[:product])
#flash and redirect
    flash[:notice] = ‘Product was successfully updated.’ redirect_to :controller => ‘cms’, :action => ‘index’ else render :action => ‘edit’ end end

The line to notice is

    @image = Image.create params[:image] unless params[:image][:file_data].kind_of? StringIO

It translates to; save the image model unless file_data is a StringIO object (if it’s not StringIO then it’s a Tempfile)

Digg! submit Rails StringIO File Upload to stumbleupon.com submit Rails StringIO File Upload to del.icio.us submit Rails StringIO File Upload to reddit.com Like this post? subscribe to the feed.

July 25, 2006

Django issues (or issues with Django)

Filed under: Misc. Alastair @ 11:51 pm

My initial exuberance with Django has been tempered a bit by trying to get it to run on a production server. Getting Rails to run was a snap compared to getting Django to run on a Plesk server.

Once I realized I had to install an earlier version of mod-python (3.1.4) and finally got it running I was confronted with the “The Dreaded Segmentation Fault” which crashed Apache.

Patching expat as described in the article was easy enough but I still had trouble running any pages that accessed the database. So I might have had both install issues at the bottom of the Django deployment page. Even when serving up non-dynamic pages the server resources where going throught the roof.
It seems mod_python and PHP just don’t want to play nice together, and there’s now way I can give uninstall PHP too many of our old sites use it. Maybe running Django under FastCGI is the way to go.

Digg! submit Django issues (or issues with Django) to stumbleupon.com submit Django issues (or issues with Django) to del.icio.us submit Django issues (or issues with Django) to reddit.com Like this post? subscribe to the feed.

July 23, 2006

Unix commands 101

Filed under: OS X Alastair @ 8:22 pm

Some basic Unix/Linux commands that are also helpful on OS X’s Terminal.

‘ls’ lists the contents of a directory

ls‘cd’ change directory (go into a directory)

cd [directory name]‘mv’ move a file or directory

mv [directory] [new/location]‘rm’ remove a file or directory (directory must be empty)

rm [fileToDelete]‘rm -rf’ remove a directory AND it’s contents (without confirmation)

rm -rf [aDirecotyFullofStuff]

‘du -s -k’ get the size of a directory in KBytes

du -s -k [aDirecotry]

Digg! submit Unix commands 101 to stumbleupon.com submit Unix commands 101 to del.icio.us submit Unix commands 101 to reddit.com Like this post? subscribe to the feed.

How To: Install Django on OS X (Tiger 10.4)

Filed under: OS X Alastair @ 6:56 pm

Django is a web framework similar to Ruby on Rails, it’s crown jewel is a built in admin/CMS system that saves tons of time when setting up a dynamic site.

Django is pretty easy to install, if you have the correct version of Python, if you have MySQLdb for Python, if you know how add apps/scripts to your $PATH, if you haven’t installed a previous version, if you have Subversion installed, and especially if you’re a programmer rather than a designer who programs (I’m the latter).

“There’s two many ifs in that sentence Johnny”

~Reese Witherspoon as June Carter in ‘Walk the Line’

Let’s try and remove those IFs by going through an install step by step. Getting Django running locally the first time took me about four hours, following these instructions it should take you less than one hour. Prerequisites are that you have Apple’s Developer Tools (on the CD that came with your mac or here), MySQL (here), and Subversion (here) installed. you don’t HAVE to have subversion installed but it makes life easier.

Here we go…

  1. Install the latest greatest Python (2.4.3 at this date)
  2. Django of course needs a database so install MySQLdb (you have MySQL installed localy right?)
  3. Test your python installation, open up Terminal.app and type
    python

    you should see text similar to this

    Python 2.4.3 (…) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. >>>
  4. Test MySQLdb. Type
    >>> import MySQLdb

    (Note: >>> is the Python prompt) If the python interpreter doesn’t complain you have MySQLdb installed properly.

  5. Next up is Django itself. These are slightly modified instructions from the Django site. Go back to the Terminal, most likely Python is still running hit ‘control-D’ to exit. You should be in your ‘Home’ directory this is where the Django souce will be installed (it will actually just live here and links will be set up between Django and Python). Enter the following
    svn co http://code.djangoproject.com/svn/django/trunk/ django_src

    This downloads the latest Django source through Subversion into a folder called ‘django_src’

  6. Create a ’symlink’ (symbolic link) so Python knows where to look for Django. This is where we part ways with the Django website’s tutorial, Python2.4 on Tiger lives in a much different directory than the one their tutorial uses (do not enter this)
    ln -s `pwd`/django_src/django /usr/lib/python2.3/site-packages/django

    our Python install’s ’site-packages’ is located at

    /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/

    because Python 2.4 was installed as a Framework. So our symlink looks like this

    ln -s /users/[USERNAME]/django_src/django/ /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django

    Note: [USERNAME] should be your username (wait what’s my username?! it’s your login, also shows up in the terminal on every line before the $)

  7. Test your django installation in the Terminal type
    python

    then

    >>> import django

    if you get no complaint from the Terminal Django is installed!

  8. Well…. not quite, Django uses
    django-admin.py startproject mysite

    to start a new Django app, however django-admin.py is not on your path so Python doesn’t know where to find it. Open up .bash_profile from your root directory - if you have TextMate or BBEdit you can open it from the Terminal.

    mate .bash_profile

    add the following line to your .bash_profile file.

    export PATH=/Library/Frameworks/Python.framework/Versions/2.4/bin:$PATH

    Now django-admin.py is on your path, prove it by going to the terminal and echoing your path

    echo $PATH

    you’ll get a response like this

    /Library/Frameworks/Python.framework/Versions/2.4/bin:/users/[USERNAME]/django_src/django/bin:/bin:/sbin:/usr/bin:/usr/sbin

Now go to the Django site and get cracking on the first tutorial.

Digg! submit How To: Install Django on OS X (Tiger 10.4) to stumbleupon.com submit How To: Install Django on OS X (Tiger 10.4) to del.icio.us submit How To: Install Django on OS X (Tiger 10.4) to reddit.com Like this post? subscribe to the feed.

Powered by WordPress