I don't like not having the `range()/xrange()` in JavaScript — particularly when working with [Underscore.js](http://documentcloud.github.com/underscore/) and other such libraries — so I wrote it.
It's not rocket science, but it might help make the world a slightly less annoying place for a couple of people.
This decorator is for views that one wants only users of the site to logout based on few conditions
Just add the decorator and it should logout anyuser who doestnot match the condition
btw I borrowed the code from django`s source code
Ok let's descrive what i have done
I subclassed the django admin to create a form that makes you choose if activate a delete and replace login inside your admin.
Then i have added a form with a modelChoiceField to make you select another model instance when you are selecting an istance to delete. If you select another instance the current instance will be replaced.
A custom templatetag for inlining image in the browser.
The principe is to base64 encode the image and avoid a http request.
There is a cache handling, you just have to specify a writable directory.
An example of the utilisation (template part): [http://djangosnippets.org/snippets/2267/](http://djangosnippets.org/snippets/2267/)
The explication on [http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/](http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/)
Just a quick solution to import product data from a excel spreadsheet to satchmo.
Supports
* hierarchical and multi categories
* product attributes
* product variations (configurable product, options)
* product price
* product image
Shows difference between two json like python objects. May help to test json response, piston API powered sites...
Shows properties, values from first object that are not in the second.
Example:
import simplejson # or other json serializer
first = simplejson.loads('{"first_name": "Poligraph", "last_name": "Sharikov",}')
second = simplejson.loads('{"first_name": "Poligraphovich", "pet_name": "Sharik"}')
df = Diff(first, second)
df.difference is ["path: last_name"]
Diff(first, second, vice_versa=True) gives you difference from both objects in the one result.
df.difference is ["path: last_name", "path: pet_name"]
Diff(first, second, with_values=True) gives you difference of the values strings.
This is an update to Simon Willison's snippet http://djangosnippets.org/snippets/963/, along with one of the comments in that snippet.
This class lets you create a Request object that's gone through all the middleware. Suitable for unit testing when you need to modify something on the request directly, or pass in a mock object.
(note: see http://labix.org/mocker for details on how to mock requests for testing)
Example on how to use:
from django.test import TestCase
from yourapp import your_view
from yourutils import RequestFactory
class YourTestClass(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
# Create your own request, which you can modify, instead of using self.client.
def test_your_view(self):
# Create your request object
rf = RequestFactory()
request = rf.get('/your-url-here/')
# ... modify the request to your liking ...
response = your_view(request)
self.assertEqual(response.status_code, 200)
Suggestions/improvements are welcome. :)
This is an addition to Django's cache_page decorator.
I wanted to cache pages for anonymous users but not cache the same page for logged in users.
There's middleware to do this at a site-wide level, and it also gives you the ability to partition anonymous users, but then you have to tell Django which pages not to cache with @never_cache.
A simple django-admin filter to replace standar RelatedFilterSpec with one with the "All"/"Null"/"Not null" options.
It applies to all relational fields (ForeignKey, ManyToManyField).
You can put the code in the `__init__.py` or wherever you want.
The `_register_front` idea is copied on [this snippet](http://djangosnippets.org/snippets/1963/)
launch python manage.py yourcommand
and command output paste into http://www.yuml.me/diagram/scruffy/class/draw
todo:
1.implement other relation attributes
2.add class inheritance
3.download image automatically
4.generate diagram for specific app
When you neeed to do redirect and request object is not available, you can do it with exception.
Put exception handler somewhere request is available, for example to middleware or ModelAdmin.
Raise exception, where request is not available.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.