Login

All snippets

Snippet List

Internal view decorator

The `internal_view` decorator makes a view accessible only from `INTERNAL_IPS`. Handy for exposing internal APIs.

  • view
  • decorator
  • internal_ips
Read More

Per-Instance On-Model M2M Caching

If you are like me and you find yourself often using M2M fields for tons of other on-model methods, in templates, and views alike, try using this quick and dirty caching. I show the use of a "through" model for the m2m, but that is purely optional. For example, let's say we need to do several different things with our list of beads, the old way is... # views.py necklace = Necklace.objects.get(id=1) bead = Bead.objects.get(id=1) if bead in necklace.beads.all(): # this bead is here! # template necklace.html {% for bead in necklace.beads.all %} <li>{{ bead }}</li> {% endfor %} ...which would hit the database twice. Instead, we do this: # views.py necklace = Necklace.objects.get(id=1) bead = Bead.objects.get(id=1) if bead in necklace.get_beads(): # this bead is here! # template necklace.html {% for bead in necklace.get_beads %} <li>{{ bead }}</li> {% endfor %} Which only does one hit on the database. While we could have easily set the m2m query to a variable and passed it to the template to do the same thing, the great thing is how you can build extra methods on the model that use the m2m field but share the cache with anyone down the line using the same m2m field. I'm by no means an expert, so if there is something here I've done foolishly, let me know.

  • models
  • cache
  • m2m
  • caching
Read More

base class to easily expose json based web services

*JsonWebService.jsonresponse JsonWebService provides the property jsonresponse which marks the method it is applied to, and also serializes to json the response of the method. *JsonWebService.urls Scans the marked methods and build a urlpattern ready to use in urls.py

  • json
  • webservice
Read More

Middleware to resolve current URL to module and view

Add it to MIDDLEWARE_CLASSES and you will get request.current_view getting values like "connection.views.index" or "django.views.static.serve" - one example usage is to show different help text based on current view name. EDIT: actually, you can use process_view middleware function where you automatically get current_view as first parameter.

  • middleware
  • view
  • url
  • resolve
Read More

Update applications

This snippet is based on 928. I've added the support to update a custom folder, using shell arguments. It requires the argparse module. You can install it with: pip install argparse Usage: # Updates repositories in the specified <folder path> # The default is the ./ folder update_repos --path <folder path> # List available options update_repos -h Alessandro Molari

  • update
  • applications
  • svn
  • git
  • baazar
  • mercurial
  • repositories
Read More

iTunes Podcast RSS Feed

The syndication documentation gives a very basic beginning to creating an iTunes Podcast Feed, but leaves a lot of work left to be figured out. This is a completed example. Because I needed to obtain the duration of the podcast file programmatically (not part of this code) I limit the podcast to mp3 files. If you want to obtain durations some other way you can set the mime_type in the same manner as the other properties. This is what I'm using and should be plenty for you to customize from.

  • feed
  • rss
  • syndication
  • iTunes
  • podcast
Read More

Simple Syslog Logging Class with Decorator

Simple logging decorator. Logs the function name along with the message. `Jul 14 16:10:42 mtzion test_func: 1 two` Define SYSLOG_FACILITY in settings.py. import syslog SYSLOG_FACILITY = syslog.LOG_LOCAL1

  • decorator
  • logging
  • syslog
Read More

Admin: ordering by multiple fields in a column sort

The new changelist supports clicking on a column header to order by that column, like iTunes. Unlike iTunes, which sorts by track number if you click the Artist or Album column header, it can only order by the column clicked on. By adding a property to my ModelAdmin, and subclassing ChangeList, I was able to make it also sort by track number when sorting by Artist or Album. [Blog post](http://python-web.blogspot.com/2010/07/sorting-by-more-than-one-field-in-admin.html) [Repository with full example](http://github.com/benatkin/tuneage)

  • admin
  • ordering
  • changelist
Read More

TestCase base class to easily temporarily change module values

1. Base your test case off `ModuleTestCase` and set a class attribute containing a dictionary of modules which you want to be able to revert the values of. 2. Use `self.modulename.attribute = something` in your `setUp` method or test cases to change the module's attribute values. 3. The values will be automatically restored when each test case finishes. For the common case of reverting the settings module, just use the `SettingsTestCase` as your base class.

  • tests
Read More

Template filter to turn Twitter names into links

Quick and simple twitterize filter to turn Twitter usernames into profile links on your own sites. Add the filter code to your own template tags (http://docs.djangoproject.com/en/dev/howto/custom-template-tags/).

  • twitter
  • twtterize
Read More

3109 snippets posted so far.