Archive for January, 2009

Edit textarea with Vi in any browser

25 Jan 2009 18:22 | 0 comments | 0 pingbacks | , ,

I prefer to use Vi for my day to day programming tasks. When I’ve installed this blog, I’ve tried to use a textarea in the Django admin in order to write new posts. While, it is possible, it is not as convenient as writing them with Vi. If I used Firefox as my browser of choice, the solution would be quite simple: Mozex. It is a nice simple Firefox extension which allows you to use any external editor to edit content of your textares.

But my favorite browser is WebKit Nightly. It is fast, supports advanced CSS and passes Acid3 test. It also has a Web Inspector — analog of Firebug, built right into the browser. Luckily, there is a simple Javascript implementation of Vi.

Several lines of code, and it is in my Django admin.

apps/blog/admin.py

class PostAdmin(admin.ModelAdmin):
    class Media:
        css = {"all":(settings.STATIC_URL + 'vi/vi.css',)}    
        js += (settings.STATIC_URL + 'vi/vi.js', settings.STATIC_URL + 'vi/vi_attach.js',)

vi_attach.js

window.onload=function(){
  var ta = document.getElementById('id_text');
  var label = ta.previousSibling;
  while (label.previousSibling && label.nodeName == "#text") {
      label = label.previousSibling;
  }
  label.innerHTML+='<br /><a href="#" onclick="editor(document.getElementById(\'id_text\'));'+ 
                   'return false;">Edit with vi</a>';
}

Django and Spawning

24 Jan 2009 3:18 | 0 comments | 0 pingbacks | , ,

This blog is running on Django/Byteflow, using Spawning as WSGI server. After reading Eric’s post about Spawning, I’ve decided to give it a try, and found it convenient to work with.

Install

First thing, you need to install development version of python (python-dev package). Setuptools and libssl-dev are also required (python-setuptools, libssl-dev packages).

Other requirements are:

  • eventlet (requires python-dev)
  • greenlet
  • PasteDeploy
  • pyOpenSSL
  • simplejson

Running Django site

The simplest way is to run

spawn --factory=spawning.django_factory.config_factory settings

from the Django project directory.

With Spawning 8.3 this method didn’t always work, because there was a bug with connected with adding current directory path to the PYTHONPATH variable. Apparently, it was fixed in the latest Spawning version from PyPi.

So alternative method to run Django and Spawning would be using Python scripts:

spawn_byteflow.py

#!/usr/bin/python
from spawning.spawning_controller import run_controller
import os.path, os
import sys

#workaraund for Spawning issue with PYTHONPATH 
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)
os.environ['PYTHONPATH']=':'.join(sys.path)

factory_args = {
        'host': '',
        'port': 9090,
        'num_processes': 1,
        'processpool_workers': 0,
        'threadpool_workers': 10,
        'watch': [],
        'dev': True,
        'deadman_timeout': 120,
        'args': ['byteflow_wsgi.application'],
        }

run_controller('spawning.wsgi_factory.config_factory', factory_args, None)

byteflow_wsgi.py

import os, sys

#Calculate the path based on the location of the WSGI script.
workspace = os.path.dirname(__file__)
sys.path.append(workspace)
sys.path.append(os.path.dirname(workspace))

#Add the path to 3rd party django application and to django itself.

os.environ['DJANGO_SETTINGS_MODULE'] = 'byteflow.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

The first script corrects Spawning 8.3 issue with a path, and runs the web server. The second script is a standard way to run Django using WSGI.