Login

Snippets by menendez

Snippet List

Better Static Image Serving With Fallback

Serves images from a local directory, but if it doesn't find it locally, then go look for it on a server. This is useful for sites with very large amounts of static content. Instead of copying all your prod images to dev every time you update the database, simply use this in your URL patterns to serve the images: `(r'^(?P<path>.*)$', 'static_fallback.serve', {'document_root' : '/path/to/my/files/'})` By default, it caches the images locally in the path it was supposed to find them. The fallback server URL can be specified in urls.py or settings as FALLBACK_STATIC_URL. Thanks to Johnny Dobbins for the idea. Based on the Nginx pass through image proxy concept. For more info [http://menendez.com/blog/using-django-as-pass-through-image-proxy/](http://menendez.com/blog/using-django-as-pass-through-image-proxy/)

  • image
  • static
  • images
  • fallback
Read More

Dynamically maintain local_constants.py from South migration

Allows you to dynamically maintain a local_constants.py file from a migration tool like South. Example of usage: set_constant('/home/projects/sample/local_constants.py', 'STAMP_MW_ID', 42, 'Set from sample.0007_add_constants.py') More more information, see [Allows you to dynamically maintain a local_constants.py file from a migration tool like South. Example of usage: set_constant('/home/projects/sample/local_constants.py', 'STAMP_MW_ID', 42, 'Set from sample.0007_add_constants.py') More more information, see [http://menendez.com/blog/maintain-contants-through-south-data-migration/](http://menendez.com/blog/maintain-contants-through-south-data-migration/).

  • migration
  • data
  • south
Read More

Profanity Check

Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc. This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.

  • tag
  • profanity
Read More

mini_render_to_response

You need the mini-detector middleware installed [http://www.iterasi.net/openviewer.aspx?sqrlitid=-e2dfig9w0yrclxaigp-uw](http://www.iterasi.net/openviewer.aspx?sqrlitid=-e2dfig9w0yrclxaigp-uw). This is a drop in replacement to render_to_response. When using mini_render_to_response it will try to load a version of your template with &#157;mini at the end. For example "home_mini.html" instead of "home.html". If it doesn't find the _mini version it falls back to the regular "home.html" version of your template. Easy way to maintain a "small screen" version of your templates for iPhone or other small screen devices.

  • templates
  • render
  • iphone
  • mini
Read More

Load a local settings file for dev/test environments

Add the snippet to your settings.py. If you have a settings_local.py it will load that one. Can be used in development environments where you might have different settings for your dev sandbox. You should exclude settings_local.py from SVN. By Rudy and Ed Menendez

  • settings
  • environment
  • production
Read More

Create breakpoints to time code at

Include in your code like this: t=Timer() Then use it like this: t.tick('Some optional description') It will output the time spent between the tick and the previous tick (or inception) and the total time spent since it began tracking time. Can be placed multiple times in a long segment of code. Can be used to break out the amount of time being spent on various parts of your code so you can focus on optimizing those sections.

  • time
  • high-performance
  • profiling
  • timer
  • optimize
Read More

get_cache_or_query - Shortcut to common cache signature

Replaces something like this: cache_key = 'game1' the_game = cache.get(cache_key) if not the_game: the_game = Game.objects.get(id=1) cache.set(cache_key, the_game, 60*24*5) With this: the_game = get_cache_or_query('game1', Game, seconds_to_cache=60*24*5, id=1)

  • cache
  • optimization
  • get_cache_or_object
Read More

Choices datatype for model

This class will automatically create a django choices tuple like this: STATUS_CHOICES = django_choices(Draft=1, Public=2, Closed=3) Additionally, it includes a method that converts the choices tuple to a dictionary. Like this: STATUS = STATUS_CHOICES.to_dict() Those types can come in handy when you need to use those magic values in your code. Best done within the model once so everyone can use it. Code based on: http://www.djangosnippets.org/snippets/455/. By the way, if you want to just have the method without having to convert to the newer syntax.. it's backward compatible. Just add django_choices in front of the first paren for your choices tuple.

  • choices
  • model
Read More

menendez has posted 8 snippets.