2General http://www.2general.com/blog/ My blog en-us Fri, 23 Aug 2013 00:00:00 +0300 http://www.2general.com/blog/2013/08/23/keep-that-module-out.html http://www.2general.com/blog/2013/08/23/keep-that-module-out.html Keep that module out!

Keep that module out!

We usually include a local_settings.py file in our Django projects. We use the file whenever some Django settings need to be tweaked according to the environment or specific requirements of individual developers.

It was a challenge to find a good way to exclude that file from being installed in production.

Read more...]]>
Fri, 23 Aug 2013 00:00:00 +0300
http://www.2general.com/blog/2013/08/05/easier_way_to_change_django_cache_backend_between_test_cases.html http://www.2general.com/blog/2013/08/05/easier_way_to_change_django_cache_backend_between_test_cases.html An easier way to change Django cache backend between test cases

An easier way to change Django cache backend between test cases

In Changing Django cache backend between test cases I showed how to use the Mock library to activate a different cache backend for individual tests.

In the comments for that article, Diederik van der Boor pointed out that the same effect can be achieved in a cleaner way by using a custom “proxy” cache backend.

I took the challenge and created a proxy cache backend and a decorator for switching the effective backend on the fly.

Read more...]]>
Mon, 05 Aug 2013 00:00:00 +0300
http://www.2general.com/blog/2013/05/17/django_settings_split.html http://www.2general.com/blog/2013/05/17/django_settings_split.html Splitting up settings in Django

Splitting up settings in Django

By default all Django settings are in one monolithic settings.py file. A single big file is hard to read and hard to maintain.

Django users have found many ways to split up the settings into multiple files, all of them with their pros and cons. In our latest projects, we have developed yet another way, which uses file inclusion instead of importing Python scripts.

The main features of django-split-settings are:

  1. Settings can be split into files and directories.
  2. Files later in the list can modify configurations, for example add or remove apps from INSTALLED_APPS.
  3. The main settings file lists the files that make up the project’s settings.
  4. Files can be marked optional. Optional files can be used to override settings per instance.
  5. Wildcards can be used in file paths.
  6. Maintains support for Django’s runserver auto-reloading.
Read more...]]>
Fri, 17 May 2013 00:00:00 +0300
http://www.2general.com/blog/2012/08/09/changing_django_cache_backend_between_test_cases.html http://www.2general.com/blog/2012/08/09/changing_django_cache_backend_between_test_cases.html Changing Django cache backend between test cases

Changing Django cache backend between test cases

It’s a good practice to run tests for a Django project with a dummy cache backend. This eliminates side effects of one test from affecting the results of other tests.

Here’s how to activate the dummy backend in a Django settings file:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache'
    }
}

However, sometimes it’s also necessary to test how an application uses the cache. In this article, we’ll show how to replace the dummy cache with a real cache backend separately for individual test cases.

Read more...]]>
Thu, 09 Aug 2012 00:00:00 +0300
http://www.2general.com/blog/2012/06/07/selective_restore_from_database_backups_with_django.html http://www.2general.com/blog/2012/06/07/selective_restore_from_database_backups_with_django.html Selective restore from database backups with Django

Selective restore from database backups with Django

Scream by eflon, on Flickr

When things go wrong and you lose your database, backups will help save the day. If the whole production database is corrupted or lost, it’s simple to throw it away and restore it in its entirety from the latest backup.

If data loss caused by a user error has remained unnoticed for some time, valuable data may since have been stored, and restoring a complete backup is not an option. In such cases it’s useful to be able to do a partial restore of one or more tables while keeping the rest of the database.

For these complex cases, I’m going to describe a technique for restoring a subset of the data in one or more relational database tables using Django.

Read more...]]>
Thu, 07 Jun 2012 00:00:00 +0300
http://www.2general.com/blog/2012/05/30/flot_stacked_bar_chart.html http://www.2general.com/blog/2012/05/30/flot_stacked_bar_chart.html Creating stacked bar charts with Flot

Creating stacked bar charts with Flot

Flot is a powerful JavaScript plotting library for jQuery. It uses <canvas> tag for creating beautiful graphical plots. Out of the box it supports lines, points, filled areas, bars and any combinations of these. With plugins you get pie charts, stacked charts and more.

Today we’ll take a look at Flot’s stacked charts support. The first thing you must notice is that you have to pass the data in the right format. Also, Flot has a bug in creating stacked charts that may require some tinkering with Flot’s code. Luckily there’s a patch available.

Read more...]]>
Wed, 30 May 2012 00:00:00 +0300
http://www.2general.com/blog/2012/05/22/django_mediagenerator_dustjs.html http://www.2general.com/blog/2012/05/22/django_mediagenerator_dustjs.html Compiling Dust.js templates with django-mediagenerator

Compiling Dust.js templates with django-mediagenerator

Here at 2General we’ve used django-mediagenerator as the asset manager in many of our latest projects. It combines the best parts of Django’s own staticfiles implementation and the older django-compressor app.

Dust.js is a fast, asynchronous JavaScript-based templating engine which runs both in browsers and on Node.js. We haven’t used Dust.js extensively yet, but we immediately noticed that if we’re planning to use it, we need to compile Dust.js templates during our normal build process, i.e. be able to add them in our mediagenerator bundles.

Read more...]]>
Tue, 22 May 2012 00:00:00 +0300
http://www.2general.com/blog/2012/05/22/virtualenv_install_node_gem.html http://www.2general.com/blog/2012/05/22/virtualenv_install_node_gem.html How to install Node.js packages and Ruby gems in virtualenv

How to install Node.js packages and Ruby gems in virtualenv

Virtualenv is a superb tool for creating isolated environments for development with Python. However, if your project requires libraries in other languages, you may want to install them in your virtual environment as well. Here’s how to do it with Node.js packages and Ruby gems.

If you’re using virtualenvwrapper (and you should), you can put this in your postactivate script. Otherwise make sure some other way that these variables are set:

# node.js packages
export npm_config_prefix=$VIRTUAL_ENV

# ruby gems
export GEM_HOME=$VIRTUAL_ENV
export GEM_PATH=""
Read more...]]>
Tue, 22 May 2012 00:00:00 +0300