Decorator
[Understanding Python Decorators in 12 Easy Steps!] (http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/)
- decorator
[Understanding Python Decorators in 12 Easy Steps!] (http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/)
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.
Serializes all models of a resource to json. Borrowed from https://github.com/toastdriven/django-tastypie/issues/442
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.
all_models = [ each for each in GetAllModels() ] This produces a list of tuples in format ( app_label's name, model's name) of all the ContentType existing in system.
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"),)`
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.
Позволяет получить типизированный словарь из входных параметров. Может быть использован, например, для дальнейшей передаче параметров в objects.filter(**rez).
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.
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.
Use decode template filter: Sample Use: {{variable|decode:"iso-8859-1"}}
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.
Yeah, I know this is a very basic filter, but I thought I might as well share it with the world...
Script saves payment reason codes text from HTML tables to python dict.
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 %}
3110 snippets posted so far.