Decorator for views that need confirmation page. For example, delete
object view. Decorated view renders confirmation page defined by template
'template_name'. If request.POST contains confirmation key, defined
by 'key' parameter, then original view is executed.
Context for confirmation page is created by function 'context_creator',
which accepts same arguments as decorated view.
When called, this module dynamically alters the behaviour of model.save() on a list of models so that the SQL is returned and aggregated for a bulk commit later on. This is much faster than performing bulk writing operations using the standard model.save().
To use, simply save the code as django_bulk_save.py and replace this idiom:
for m in model_list:
# modify m ...
m.save() # ouch
with this one:
from django_bulk_save import DeferredBucket
deferred = DeferredBucket()
for m in model_list:
# modify m ...
deferred.append(m)
deferred.bulk_save()
Notes:
* - After performing a bulk_save(), the id's of the models do not get automatically updated, so code that depends on models having a pk (e.g. m2m assignments) will need to reload the objects via a queryset.
* - post-save signal is not sent. see above.
* - This code has not been thoroughly tested, and is not guaranteed (or recommended) for production use.
* - It may stop working in newer django versions, or whenever django's model.save() related code gets updated in the future.
Want to display the remaining characters on a text field in admin? (based off of maxlength or an override)
also lives here: [http://github.com/broderboy/django-admin-remainingcharacters](http://github.com/broderboy/django-admin-remainingcharacters)
Template to make a galleriffic gallery for django photologue.
Uses the settings from this demo
http://www.twospy.com/galleriffic/example-2.html
This is now the default template for the django-cms photologue plugin
http://www.django-cms.org/en/extensions/cmsplugin-photologue/detail/
as with all things related to monkeypatching, the caveat is to use things like these for good, and not for evil.
I wrote these decorators because I did not want to rewrite all of [PyAWS](http://github.com/IanLewis/pyaws) -- instead I use these to add some standard/useful methods to the Bag collection that PyAWS uses internally.
AN EXAMPLE:
class PatchMyMonkey:
pass
@monkeypatch(PatchMyMonkey)
def throwfecesat(self, who="nobody"):
print "Throwing Feces At: %s" % who
@monkeypatch(PatchMyMonkey)
def nicoderm(self, why="no reason"):
print "Trying To Quit Because: %s" % why
return {'why':str(why)}
trampstamp = PatchMyMonkey()
trampstamp.throwfecesat(who="another monkey")
print trampstamp.nicoderm(why="cigarettes are pricey")
A LESS SILLY EXAMPLE:
from pyaws import ecs
@monkeypatch(ecs.Bag, fname='get')
def get(self, param, *args, **kw):
return self.__dict__.get(param, *args, **kw)
@monkeypatch(ecs.Bag, fname='clearurls')
def clearurls(self, *args, **kw):
for k, v in self.__dict__.items():
if isinstance(self.__dict__[k], ecs.Bag):
self.__dict__[k].clearurls(*args, **kw)
if type(v) == type([]):
[ii.clearurls() for ii in self.__dict__[k]]
if type(v) == type(u''):
if self.__dict__[k].count(u'http://') > 0:
self.__dict__[k] = "<URL>"
(amazon's URLs are extremely long, and can muddle your test/log output, hence that last function.)
based on sample code from [here](http://pypi.python.org/pypi/decorator) and [here](http://mail.python.org/pipermail/python-dev/2008-January/076194.html).
Extra field for slugs that does the work.
If the slug value is given, the value is not recreated but correctness is ensured. If value is not given, the field regenerates the slug.
Adds a hidden footer to the bottom of every text/html page containing a list of SQL queries executed, the view function that was used and all templates that were loaded.
Each template path is a clickable URL of the type txmt://open/?url=file://FILENAME&line=LINENO as is the view function.
This lets you open the file very quickly in TextMate which speeds up development considerably and relieves the mind of much fetching-lookup tedium.
This version works on SVN 12002 (1.2 alpha) and earlier.
The previous version was: http://www.djangosnippets.org/snippets/1033/ which got caught but some edge case in certain modules.
It is based on the original by Simon http://www.djangosnippets.org/snippets/766/
To use save this as 'debug_middleware.py' somewhere on your PYTHONPATH and add 'debug_middleware.DebugFooter' to your MIDDLEWARE_CLASSES setting.
- Felix (the Third) hey wouldn't it be great if you could retrieve your password on djangosnippets ? somebody should volunteer to upgrade it
A Django Form for creating a browser-based upload form that pushes files to Amazon S3. See http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434
OrderField for models from http://ianonpython.blogspot.com/2008/08/orderfield-for-django-models.html and updated to use a django aggregation function. This field sets a default value as an auto-increment of the maximum value of the field +1.
I had an issue with the django templating system, where by if I included several different files at the same level on one template.. and all the included templates extended the same base template. It would render the defined blocks for the first inclusion for all. i.e. everything in the parent that is being extended would be not updated for the subsequent inclusion.
So, if anyone else has this problem. I have a solution that I sorta wanted for other reasons anyway. It's called a decorator tag, you can include and extend a template at the same time - in-line, with local context scoping so it only affects that tag.
This is also a nice way to avoid creating .html files for simple extending purposes.
You have some tree-like structure in your models, for example:
`class MyModel(models.Model):
parent = models.ForeignKey('self', verbose_name=u'Parent', \
null=True, blank=True, related_name='children')
name = models.CharField(u'Раздел', max_lengtch=255)
position = PositiveSmallIntegerField(u'Позиция', default=0)
class Meta:
ordering = ('position',)`
To see it as a tree in admin's object list(you also can sort items, move to another parents by drag-n-drop; and rename them) add this to admin.py:
`class MyModelAdmin(admin.ModelAdmin):
ordering = ('position',)
list_display = ('pk','name','parent','position')
raw_id_fields =('parent',)
list_per_page = 900 #we sould have all objects on one page
list_editable = ('name','position','parent')
def parent_id(self,obj):
return obj.parent and obj.parent.id or '0'
class Meta:
model = MyModel
class Media:
js = [settings.MEDIA_URL + s for s in ('lib/jquery-1.3.2.min.js',
'lib/jquery.tree.min.js',
'lib/plugins/jquery.tree.contextmenu.js',
'lib/mymodel_admin.js',)]
css = {
'all':(settings.MEDIA_URL+'css/nestedsortablewidget.css',)
}`
mymodel_admin.js is the code listed here, if you have different title field(not "name"), change var title_column in javascript, list_display and list_editable.
jstree can be obtained here: [jstree](http://www.jstree.com/)
screenshot is in [my blog](http://tabed.org/blog/2010/01/06/jstree-in-django-admin/)
Ok... this is really a hack. But I love it. I hate setting up all of my test cases into suites, and making sure that I remember to add them each time I add a new python file... annoying! This allows me to have a tests package and then just add python files and packages to that test package (mirroring my app setup). Each of the files are then dynamically imported and every test case is automatically executed. If you don't want one to execute, add it to the ignore list. If you add 'views' to the ignore list, it will ignore all views, otherwise you would have to specify 'package.views' if it is in a package.
So... in short this is a bit ghetto, but it saves me a lot of time just setting up all my tests... now I can just write them! Hope it's useful to someone.
Greg
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.