SRA Lab Open Source Code

27Jan/110

Dynamic OR’d Queryset Creation with Django

To complete posts and comments published on Elf Sternberg's blog, this is the method I found to create dynamic complex queryset with Django (Q and or):

field = 'toto'
qs = Q(**{field + '__slug': 'AL'})
qs |= Q(**{field + '__isnull': True})
Document.objects.filter(qs)

PS: You can start with qs = Q() if you need a loop.

Tagged as: No Comments
13Oct/108

Current Tranformer

At home or at work, you probably have spotlights which indicate only 20W or 35W of power consumption, but sadly these spotlights are powered by a lower voltage and so they require a transformer (hidden in the ceiling, a cupboard or a hood) which in turn consumes around 40W.

I've recently designed an application (with libmodbus, glib and Django) to measure the power consumption from various sensors built by LEM. By doing some tests at home, I've confirmed my suspicions about a transformer permanently plugged into the mains (0.18 * 224 = 40.3W for total of 80W when both spotlights are turned on).

So, I've replaced the shunt plug with a switch between the main and the transformer and once again I have a perfect 0W when off!

Do you know how many electricity transformers do you have around you?

14Jan/090

Limitations of the Django ORM (1.0) in model inheritance

Vertimus has been merged with Damned-Lies and it's wonderful to see how a tiny project is able to receive as much coverage and talks (in geek circles only of course, GNOME Commit Digest 13, planet, GNOME i18n list). I've been spending more time that I've estimated because of Django ORM limitations.

Django is a nice framework with a really good documentation, what I miss the most is a powerful ORM as SQLAlchemy. To code the Design Pattern State in Vertimus, I used two classes StateDb and StateAbstract instead of one to avoid to have too many SQL tables and other drawbacks (same design for actions: ActionDb and ActionAbstract). I've tested many designs before to find the right one which fit my needs. The Django developers are aware of these current limitations and I found a useful document to understand the current Django ORM limitations in model inheritance:

http://www.slideshare.net/LorenDavie/model-inheritance/

I've read this document a bit late I hope you'll find it on time.

Tagged as: No Comments
4Dec/080

Web translations

Almost nothing on GNOME French translations front but I've updated the translations of a few Web applications:

  • roundup (in bug tracker for now)
  • FormEncode
  • Drupal 5
  • TurboGears (in JS)

Claude and me are working to improve the new Damned-Lies, it's really fun to develop this application with Django.

If you're boring to configure manually your virtual hosts and you're looking for a Web server able to serve static and dynamic content faster than the old indian, try Cherokee and forget about the mod_wsgi verruca. If like me you're a bit puzzled about SCGI, WSGI, FastCGI and other flup, see the Cherokee mailing list for a clear explanation.

Oh and Python 3.0 final is released! Good job, guys. I'm now looking for CPU and memory benchmarks...

29Oct/080

Database performance in Web applications

It's more efficient to connect a Web application with an Unix Domain
Socket than TCP/IP one (reduced overhead) so I'll explain the required
configuration with the following pairs:
1 - TurboGears/SQLAlchemy
2 - Django/PostgreSQL
3 - Django/MySQL

1 - TurboGears/SA

SQLObject is dead, isn't it? So With SQLalchemy, the syntax is:

sqlalchemy.dburi="postgres:///dbname?user=mydbuser&password=XXXXXX" ([1])
http://docs.turbogears.org/1.0/DatabasePostgres

2 - Django/PostgreSQL

You just need to define DATABASE_ENGINE = 'postgresql_psycopg2' and DATABASE_NAME. Leave DATABASE_HOST setting empty to use UDS.

3 - Django/MySQL

Create a database in UTF8, either with default-character-set = utf8
under [mysqld] section in the my.cnf file or with an explicit 'create
database bla charset=utf8;'

In settings.py:


DATABASE_HOST = '/var/run/mysqld/mysqld.sock'
DATABASE_OPTIONS = {
'read_default_file': '/etc/mysql/my.cnf',
'init_command': 'SET storage_engine=INNODB'
}

A - Note about PostgreSQL

When the user isn't the same one who runs the process, you must edit the PostgreSQL configuration (/etc/postgresql/8.3/main/pg_hba.conf):
# "local" is for Unix domain socket connections only
local   user     database      md5
local   all      all           ident sameuser

You must create an user with a encrypted password (encrypted by default).

$ CREATEUSER username
$ psql
postgres=# ALTER USER username WITH ENCRYPTED PASSWORD 'my_password';

If you want to be sure, remove the lines with 'host' to deny nonlocal connections.