Login

Most bookmarked snippets

Snippet List

Decorator

[Understanding Python Decorators in 12 Easy Steps!] (http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/)

  • decorator
Read More

Controller Class for Views

I wanted to be able to share common code among a subset of views without copy-and-pasting the code or the same function into each view, so I decided to wrap a class around the views so that the common code (i.e. loading a model that each of the views would affect) can go in the __init__ of that class. I created the controller_view function above to allow the urls to access those class methods. It would be called something like this: url(r'^someview$', controller_view(SomeController, 'someview'), name='someview'), Where the SomeController inherits (or is structured like) the Controller class above and implements __init__ and someview as methods. I'm new to Django so it's entirely possible I've missed something that already does this or that makes this unnecessary. If so, let me know so that I can figure out how to do this right, otherwise, hopefully this is helpful to someone else out there.

  • urls
  • views
Read More

Active Directory Authentication With Groups

This is a authentication backend for Django, to authenticate via Active Directory. This pulls all group information out of Active Directory and allows for multiple group memberships.

  • Django
  • Python
  • Active-Directory
  • Authentication
  • Groups
Read More

Retrieve human-readable value from choices tuple or value from dict

Will help you retrieve the value from a dictionary with a supplied key, or the human-readable value from a choices tuple. Works as follows: To retrieve the value of a dict: `{{ crime_rates_dict|getval:"Chicago" }}` <-- will return value of `crime_rates_dict["Chicago"]` To retrieve the human-readable value from a choices tuple: `{{ country.COUNTRIES|getval:"US" }}` <-- will return "United States" in `COUNTRIES = (("US", "United States"),)`

  • template
  • templatetag
  • choices
  • dict
  • tuple
Read More

decorator to add GUID Field to Django Models

A decorator to add a GUID Field to a Django Model. There are other bits of code out there that do similar things, but it was important for the field to have a unique value _before_ it is saved in the database. The contribute_to_class method therefore registers the field class with the post_init signal of the class it's being added to. The handler for that signal is where field initialization is done.

  • models
  • fields
  • decorators
Read More

fast non-locale aware float format

Django's `floatformat` is a good way to format a number if you require a specific amount of decimals. It is, however, very slow. In testing each `floatformat` call took 200–250 us, which means it'll take a second to render a page that floatformats 4000 numbers. Much of the time comes from using `Decimals`. I looked at using the `cdecimal` module, and while it improved the speed, each call still clocked in at between 80 and 100 us. `fast_floatformat` is not locale aware, and doesn't look at Django settings for USE_THOUSAND_SEPARATOR, but it'll take between 1.2 and 3 us per call for ints, floats and strings, and about 12 us per call for Decimals, giving you up to 800000 floatformatted numbers per second.

  • floatformat
Read More

Coffeescript compilation

All I wanted was for one to one compilation of coffeescript to javascript. * Without special templatetags * Without specifying explicit bundles * Served dynamically in development * Compiled by collectstatic for producton This code is the minimum required for this. There are two things to take into account: * list method to find coffeescript files to compile for collectstatic * find method to find coffeescript equivalent for a js file for django.contrib.staticfiles.views.serve. The list method will use the list method on all finders that come before it in STATICFILES_FINDERS to find all the files that end with .coffee and will return the equivalent .js path with a storage class that knows how to compile the coffeescript. The find method will use the find method on all finders that come before it in STATICFILES_FINDERS to locate the coffeescript file that is actually being requested. It will then compile the coffeescript into a file in settings.CACHE_DIR before serving that file.

  • development
  • staticfiles
  • coffeescript
  • collectstatic
Read More

Django cycle tag that doesn't continue where it left off

This tag is equivalent to {% cycle %} but resets when we exit the containing loop. See Django ticket "Cycle tag should reset after it steps out of scope" [https://code.djangoproject.com/ticket/5908](https://code.djangoproject.com/ticket/5908) This code is a lightly modified version of Simon Litchfield's attachment on that ticket.

  • forloop
  • cycle
Read More

Link If templatetag

Django templatetag wraps everything between ``{% linkif %}`` and ``{% endlinkif %}`` inside a ``link`` if ``link`` is not False. Sample usage:: {% linkif "http://example.com" %}link{% endlinkif %} {% linkif object.url %}link only if object has an url{% endlinkif %}

  • link
Read More

3110 snippets posted so far.