Login

Most bookmarked snippets

Snippet List

A RegexpField that clean the regex match using the desired format

I wanted a way to allow flexible phone number validation while making sure the saved data was uniform. ex. With: RegexFormatField(r'^\(?(?P<area>\d{3})\)?[-\s.]?(?P<local>\d{3})[-\s.]?(?P<subscriber>\d{4})$', format='%(area)s %(local)s-%(subscriber)s') input: (444) 444-4444 444 444-4444 444-444-4444 444.444.4444 4444444444 output: 444 444-4444

  • regex
  • format
  • field
Read More

create_template_postgis-ubuntu_lucid

The template creation script referenced [here] (http://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#ubuntudebian) doesn't work on Ubuntu Lucid, where the default PostgreSQL version is now 8.4 and some things have been moved around. I've edited the script to work on Ubuntu Lucid.

  • postgis
  • ubuntu
Read More

ModelValue for djagno-livesettings

Extension for django-livesettings project - http://bitbucket.org/bkroeze/django-livesettings/ Allow to specify the model instance in settings Usage: config_register( ModelValue(BASE_GROUP, 'TestValue', queryset = Value.objects.all(), required=False)

  • livesettings
Read More

One step up from __icontains

The [REGEX and IREGEX](http://docs.djangoproject.com/en/dev/ref/models/querysets/#iregex) operators were added in Django 1.0 and I'm sure you can think of fancier ways of doing word delimiting and things like that but this was all I needed to make a user-friendly autocomplete search function.

  • ORM
  • QuerySet
Read More

Get current user without a request object

Mechanism to obtain a `request.user` object without the `request` object itself. Requires `LocalUserMiddleware` in `MIDDLEWARE_CLASSES` settings variable. **Important**: works under assumption that within a web server each request is handled by a separate thread (as for example in the Apache HTTP server). **Beware**: [security threat](http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser), although ["thread locals only appears to be a security threat if a system has already been seriously compromised, at which point there'd be easier attacks to execute"](http://groups.google.com/group/django-users/browse_thread/thread/e7af359d7d183e04). **Dev note**: works fine with one-threaded Django's development server, each request resets current user; no worries 'bout many media requests - they won't (at least shouldn't) be using Django on the production server. **Ref**: originally found in the gatekeeper app.

  • middleware
  • user
  • request
  • local thread
Read More

Checkbox or radio iterator as template filter

This snippet is based on [Bill Freeman's MultiSelect checkbox iterator template filter](http://djangosnippets.org/snippets/2151/). Usage: See docstring

  • checkbox
  • choice
  • templatefilter
  • iterator
  • checkbox-input
  • form-choice
  • radio-button
  • radio-input
Read More

change settings locally in an individual test

So you need to change some settings when running an individual test in a test case. You could just wrap the test between `old_value = settings.MY_SETTING` and `settings.MY_SETTING = old_value`. This snippet provides a helper which makes this a bit more convenient, since settings are restored to their old values automatically. Example usage: class MyTestCase(TestCase): def test_something(self): with patch_settings(MY_SETTING='my value', OTHER_SETTING='other value'): do_my_test()

  • settings
  • testing
  • unittest
Read More

JSON fixtures of Intl. country codes & dial-codes

Just a dump of a countries data, including standard codes & dial-codes, based on the following model: - Country -- name : CharField -- code : CharField -- dial_code : CharField Note that countries aren't unique, as some may have several intl. dial codes. You can parse it using script or load it using the loaddata command.

  • json
  • fixtures
  • data
  • country
  • countries
  • codes
  • dial_codes
  • international
Read More

PostgreSQL fulltext with language translations

Consider following models: class Product(models.Model): code = modeld.CharField() class ProductTrans(models.Model): product = models.ForeignKey('Product') language = models.ChoiceField(choices=settings.LANGUAGES) title = models.ChaField() description = models.ChaField() With this snippet is possible search through all translations of product at the same time (using string concatenation in trigger): Product.objects.extra( where = ['product_product.fulltext @@ to_tsquery(%s)'], params = [ 'someproduct' ] ) For PostgreSQL >=8.4 only.

  • sql
  • models
  • translations
  • model
  • full-text
  • postgres
  • postgresql
  • language
  • fulltext
  • translation
Read More

[UPDATE]Filter to resize a ImageField on demand + RATIO OPTION + CLEANUP

This is the snippet of rafacbd but upgraded... (http://djangosnippets.org/snippets/955/) Now support keep the aspect ratio or not, changing the size string (x1, x0) this choice use pil.resize or pil.thumbnail. Remember change the variable CAPS NAMES of the cleanup code. This cleanup function get the original main image file name and create a regexp similar to "[filename]_[width]x[height]x[ratio].[ext]" and remove all thumbs. By example if the main image is spain_rulez.jpg the script remove by example spain_rulez_100x100x1.jpg spain_rulez_200x150x0.jpg etc... etc...

  • template
  • image
  • thumbnail
  • cleanup
Read More

3110 snippets posted so far.