07.11.06
Posted in clepy, python, turbogears at 6:52 pm by Ian Maurer
IBM has published my second article (2 of 2) on Web Development frameworks for Python. This one is on the TurboGears framework.
Check it out here:
http://www-128.ibm.com/developerworks/linux/library/l-turbogears/
Permalink
06.06.06
Posted in clepy, python, django at 11:40 pm by Ian Maurer
The first article in my 2 part series on Python web development is now up on the IBM Developerworks website. This article features Django and the next one will be about TurboGears.
http://www-128.ibm.com/developerworks/linux/library/l-django/
You can also digg it if you are so inclined.
If you want to download the actual source code, you can grab it from here:
http://itmaurer.com/djproject.tar.gz
It is only going to work using the ‘python manage.py runserver’ command since it uses relative paths for both the sqlite database and the templates directory. The admin user is “admin” with a password of “password”.
If you have any trouble, please send me an email (ian -at- itmaurer -dot- com).
Permalink
01.09.06
Posted in clepy, python at 10:45 pm by Ian Maurer
Below is a link to my presentation tonight on Jython that I did for the Cleveland Area Python Users Group (Clepy):
Introduction to Jython
Permalink
10.09.05
Posted in python, django at 10:19 am by Ian Maurer
Looking through the unit tests for the Django project, I have been able to extract a relatively simple way of creating tests for my django models that do not effect production data or need to be cleaned up after each run. For my unit tests, I leverage sqlite and use an in-memory database that is thrown away after the tests complete.
First, I created a ‘tests’ module in my application with a new settings file that sets up the database.
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = ':memory:'
Then at the beginning of my unit test I need to prepare the database. The very first thing I needed to do was to replace the DJANGO_SETTINGS_MODULE module in the os.environ dictionary which will tell Django to load my test settings rather than my normal project settings.
Since the database is in memory and new on each run, the init and install commands need to be run. These commands are available through the django.core.management module. Below is the example startup code before my tests:
# Globals
SETTINGS_MODULE = 'apps.myapp.tests.settings'
TEST_MODELS = ('model_one', 'model_two')
# Settings Set First
import os
os.environ['DJANGO_SETTINGS_MODULE'] = SETTINGS_MODULE
# Install Models
from django.core import management, meta
management.init()
for model in TEST_MODELS:
management.install(meta.get_app(model))
# start tests
import unittest
class test_model_one(unittest.TestCase):
....
The most important thing is that the settings file gets set before importing anything from Django because Django will automatically load the ‘normal’ settings file the first chance it gets.
In order to use sqlite in python, you need the pysqlite project, which can be downloaded here:
http://initd.org/tracker/pysqlite
The following page includes the install directions which includes a simple test that can be run in the intepreter to make sure your install is correct:
http://initd.org/pub/software/pysqlite/doc/install-source.html
UPDATE:
I created a method that lets me now skip the creation of a settings file and reduces the amount of duplicate code. The trick is reloading the db module of Django.
def enable_inmemory_testing_db(models):
# Change Settings
from django.conf import settings
settings.DATABASE_NAME = ':memory:'
settings.DATABASE_ENGINE = 'sqlite3'
# Reload DB module
from django.core import db
reload(db)
# Install Models
from django.core import management, meta
management.init()
for model in models:
management.install(meta.get_app(model))
Also, a VERY nice side-effect that I didn’t realize at first is that I can now refactor my models and run my tests without having to drop and re-install my models using the django-admin script. Obviously, you still need to go thru the process for running your site but the TDD process of write a test, run it, modify, run it was slowed down a lot by the SQL admin stuff.
This was my biggest nit with Django and now it is eliminated.
Permalink
10.02.05
Posted in clepy, python, django at 10:20 am by Ian Maurer
On Monday night, I am doing a presentation on Django to the Cleveland Area Python Interest Group (Clepy). Here is the presentation:
Python Web Development with Django
Update:
The zipfile that contains the sample project discussed in the presentation can be downloaded here:
clepy.zip
Permalink