Login

Snippets by justquick

Snippet List

Unit Test Profiling

A test runner for Django unittests which profiles the tests run, and saves the result. Very useful for diagnosing your apps. Place the top portion of the code into a file called `profiling.py` someplace in your python path. Update your `settings.py` file with the bottom two lines of the code. Now you are ready, so just run `python manage.py test [appnames...]` to test any apps listed with profiling. By default this will just print a nice report after the unittests. If you change the value of `TEST_PROFILE` to a file, the profile will be saved to that file. The latter is recommended because these profiling reports have a lot of info in them, so it is best to tear through them with the `pstats` module.

  • profile
  • unittest
  • cprofile
Read More

Unit Tests That Write Fixtures

This is a skeleton framework of a unittest for an app which will write out a fixture of the test database once the test has been done. I run this once for all apps, but you can limit which apps get serialized by modifying the self.apps value from get_apps (all apps) to a list of only certain apps. This script by default assumes that you have a SVN_DIR setting which points to the current working subversion directory, with a subdirectory of fixtures where it places `tests.json` upon completion. You may change this location as well. After running `python manage test` you can run `python manage loaddata fixtures/tests.json` to load in to the real database all of the test database fixtures. Feel free to edit at will, let me know of any changes that are helpful, and dont forget to fill in the `...`s

  • json
  • unittest
  • fixture
Read More

Google Charts Templatetags (Python)

The core templatetags for my project [google-chartwrapper](http://code.google.com/p/google-chartwrapper/). It is an easy method of creating dynamic GoogleCharts from the [GoogleChartAPI](http://code.google.com/apis/chart/). To get the most recent version: `svn checkout http://google-chartwrapper.googlecode.com/svn/trunk/` and run `python setup.py` in the downloaded trunk directory. There is an included django project there with the [ChartsExamples](http://code.google.com/p/google-chartwrapper/wiki/ChartExamples) all worked out in django templates

  • templatetags
  • google
  • chart
Read More

Google Charts Templatetags (HTML)

Example usage of the GChartWrapper.charts module for creating dynamic charts with templatetags. It is an easy method of creating dynamic GoogleCharts from the [GoogleChartAPI](http://code.google.com/apis/chart/). The project is located at [google-chartwrapper](http://code.google.com/p/google-chartwrapper/). To get the most recent version: `svn checkout http://google-chartwrapper.googlecode.com/svn/trunk/` and run `python setup.py` in the downloaded trunk directory. There is an included django project there with the [ChartsExamples](http://code.google.com/p/google-chartwrapper/wiki/ChartExamples) all worked out in django templates

  • templatetags
  • google
  • chart
Read More

User/IP Banning Middleware

Banning middleware with allow,deny functionality. Can select by User id/username and IP addresses. Returns a HttpResponseForbidden when banning requests. Banning by users is real nice because no matter which IP a pest comes from, they can never retrieve anything that they log into. Very handy for keeping out those unwanted pests! I personally developed this further to use a Ban model to keep track of different bans on different sites. Maybe ill post that eventually, but this runs as is with static vars. Implementation from [HvZ Source](http://www.hvzsource.com)

  • middleware
  • python
  • ban
Read More

justquick has posted 5 snippets.