Login

Most bookmarked snippets

Snippet List

View decorator providing confirmation dialog

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.

  • views
  • decorators
  • confirmation
Read More

django_bulk_save.py - defer saving of django models to bulk SQL commits

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.

  • sql
  • tool
  • dump
  • save
  • db
  • bulk
  • util
Read More

Django Admin inline preview

Extend this ModelAdmin to get dynamic inline previews in the list admin also lives here: [http://github.com/broderboy/django-admin-preview](http://github.com/broderboy/django-admin-preview)

  • admin
  • jquery
Read More

Dynamically create Django admin actions

With this snippet, I made a set of admin actions for assigning `Quality` objects to `Package` objects. The Django docs for [ModelAdmin.get_actions][1] explain the dictionary of tuples that is returned. [1]: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/actions/#django.contrib.admin.ModelAdmin.get_actions

  • admin
  • admin-actions
  • closures
Read More

Build tags files for emacs and vim

Save this shell script to the root of your Django project as "tags.sh", make it executable with "chmod +x tags.sh", and run it from the project root with "./tags.sh", and you will have a "tags" file for vim and a "TAGS" file for emacs. Tags will be created for Python, JavaScript, and block names found in HTML templates. You may need to change "/usr/share/pyshared/django" to the location of your Django installation. Documentation on [Tags in emacs](http://www.gnu.org/software/emacs/manual/html_node/emacs/Tags.html) | [Tags in vim](http://vimdoc.sourceforge.net/htmldoc/tagsrch.html)

  • tags
  • emacs
  • vim
Read More

Map GPX files to 3D GeoDjango Models

`GPXMapping` is a subclass of `LayerMapping` that imports GPX files into 3D GeoDjango models (requires Django 1.2 or SVN r11742 and higher). Here's an example of GeoDjango models for GPX points and tracks, respectively: from django.contrib.gis.db import models class GPXPoint(models.Model): timestamp = models.DateTimeField() point = models.PointField(dim=3) objects = models.GeoManager() def __unicode__(self): return unicode(self.timestamp) class GPXTrack(models.Model): track = models.MultiLineStringField(dim=3) objects = models.GeoManager() Assuming the above models, then `GPXMapping` may be used to load GPX tracks and waypoints (including elevation Z values): track_point_mapping = {'timestamp' : 'time', 'point' : 'POINT', } track_mapping = {'track' : 'MULTILINESTRING'} gpx_file = '/path/to/file.gpx' lm = GPXMapping(GPXPoint, gpx_file, track_point_mapping, layer='track_points') lm.save(verbose=True) lm = GPXMapping(GPXTrack, gpx_file, track_mapping, layer='tracks') lm.save(verbose=True)

  • gis
  • geodjango
  • 3d
  • gpx
  • layermapping
Read More

django-mptt enabled replacement for SelectBox.js

This snippet is used in conjunction with the code in [#1779](http://www.djangosnippets.org/snippets/1779/) to make an mptt-enabled version of the FilteredSelectMultiple widget. See my blog for full details: [http://anentropic.wordpress.com](http://anentropic.wordpress.com/2009/11/05/more-django-mptt-goodness-filteredselectmultiple-m2m-widget/)

  • widget
  • mptt
Read More

pyText2Pdf - Python script to convert plain text into PDF file. Modified to work with streams.

This is "pyText2Pdf" - Python script to convert plain text into PDF file. Originally written by Anand B Pillai. It is taken from http://code.activestate.com/recipes/189858/ Modified to work with streams. Example: produce PDF document from text and output it as HTTPResponse object. import StringIO input_stream = StringIO.StringIO(text) result = StringIO.StringIO() pdfclass = pyText2Pdf(input_stream, result, "PDF title") pdfclass.Convert() response = HttpResponse(result.getvalue(), mimetype="application/pdf") response['Content-Disposition'] = 'attachment; filename=pdf_report.pdf' return response

  • django
  • tools
  • python
  • pdf
  • converter
  • text2pdf
  • adobe
  • acrobat
  • processing
Read More

yet another digg style paginator

put this code into your application's `__init__.py` it adds a mixin to the `Paginator` class that implements a digg style pagination. the mixin has just one method called `digg_page_range` that takes the current page object as the parameter. this method is an iterator which yields page numbers with `None` values representing gaps. this iterator is similar to the original paginator's method `page_range` and it can be used in your code to emit the needed markup.

  • paginator
  • digg
Read More

HTTP Authorization Middleware/Decorator

Use HTTP Authorization to log in to django site. If you use the FORCE_HTTP_AUTH=True in your settings.py, then ONLY Http Auth will be used, if you don't then either http auth or django's session-based auth will be used. This assumes that the regular auth middleware is already installed. If you provide a HTTP_AUTH_REALM in your settings, that will be used as the realm for the challenge. Having both a decorator and a middleware means that for site-wide http auth, you only need to specify it once, but the same code can be used as a decorator if you want part of your site protected using htty basic auth, and the other bits freely visible. Of course, since this is basic auth, then you need to make sure your site is running under SSL (HTTPS), else your users passwords are effectively transmitted in the clear.

  • middleware
  • decorator
  • http-auth
  • basic-auth
Read More

Django Template "include_raw" tag

At WWU Housing, we started using the [Tempest jQuery plugin](http://plugins.jquery.com/project/tempest) for javascript templating, which has the same {{ var }} syntax as Django's templating. We wanted to be able to use the same templates in our server-side python and our client-side js, so we had to have a way of including the unrendered template for the js. At the same time, for convenience, it had to be modular so we could push the same code from our dev- to our live-server and not worry about absolute paths (which is why the {% ssi %} tag did not work). So the {% include_raw %} tag was born.

  • template
  • django
  • parse
  • include
  • ssi
Read More

is_dirty and dict of changed values

When you call model.changed_columns() you get a dict of all changed values. When you call model.is_dirty() you get boolean whether or not the object has been changed since last save Based on an answer here:http://stackoverflow.com/questions/110803/dirty-fields-in-django but fixed and added is_dirty

  • django
  • models
  • save
  • dirty
Read More

3110 snippets posted so far.