Posts

Showing posts from September, 2012

Django System Configuration via Admin Interface

We wanted to allow system administrators to easily configure a number of parameters through the web interface itself. We also wanted to do this without too much additional coding, or installing any more dependencies. So we hacked together the following solution using a standard django model that's part of your app, which can be edited through the standard interface. It's not entirely elegant, but it works. Step 1 - Create configuration model
Each setting has it's own field. This makes it easy to edit everything on the one screen. The downside is that adding new settings will require adding new columns to the db table using a migration script, or manual db manipulation. The __unicode__ method defines what shows up in the admin interface when users click on the corresponding model. ################################# class SystemConfiguration(models.Model): some_setting = models.IntegerField('Some Setting', help_text="What does it do.") some_sett…

Django Custom Log Handler to DB Model

Django uses the standard python logging framework, which allows you to fire log events at different handlers, such as console, file, email, etc. We wanted to be able to store CRITICAL and ERROR messages to a custom model as part of our Django app, so that we could use the admin interface to easily view these critical alerts. To do so requires creating a custom log handler, as documented here.

Step 1:
Create your django model for where you want to store your error messages. For example, copy this in your application models.py file:
############################################## class SystemErrorLog(models.Model): level = models.CharField(max_length=200) message = models.TextField() timestamp = models.DateTimeField('timestamp', null=True, blank=True) ############################################## Step 2:
Create the custom log handler class. For example, copy this into a new loggers.py file that sits in your application folder, along side the models.py file.
############…

Screenshot a Webpage via Command Line Script

We've recently had a requirement on one of our projects to periodically take a screenshot of an external URL every few minutes and cache the image on our server, which acts as an intermediary to a set of dedicated clients. Here are a few notes on how we made this work:

The external URL generated a dynamic map using javascript, css and canvas elements. To take a screenshot of this on our headless server, we used wkhtmltoimage, which is part of the wkhtmltopdf package.This can be installed using the following recipe:First, check the link for latest version and select either 32bit or 64bit for your machine. If you're not sure what architecture your server is, use the command "file /sbin/init" -- the output should give you all the information you needThen:wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2tar xvjf ./wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2chown www-data:www-data ./wkhtmltoimage-i386chmod +x ./wkhtmltoimage-i386mv …