The code shown allows you, in GeoDjango, to reduce the number of points in your polygons. It helps reduce storage needs and makes queries run faster, at the cost of some precision. It provides a variation on the simplify() method that comes with the GEOS API, allowing you to specify a number of points instead of a distance tolerance.
It is set up as a management command so that you can run it with python manage.py. See the django docs for how to set that up.
The example shown assumes a table called CountyBorders with fields "name" and "mpoly." It should be straightforward to adapt it for your needs. Look at the first three lines of the simplify() method in particular for customization. The rest of the code is pretty generic, and it should run fast enough in a one-time batch process for most needs.
The algorithm tries to keep the 75 points that provide the most definition for the shape. Each point in a polygon defines a triangle with its immediate neighbors. If that triangle has no area (the degenerate case), it is a midpoint on the segment between its neighbors and adds no value whatsoever. This principle is extended to say that the larger the triangle, the more value the point has in defining the shape. (You can find more refined algorithms, but this code seems to work fine by visual inspection.)
This table tag helps with render tables, which can be fairly complex.
I updated the previous table tag (296).
I added support for oddrow,evenrow,lastcellinrow,oddcol,evencol. And made a few minor adjustments to syntax formatting, and some non needed if conditionals
These are all of the supported variables available in the context
{{table.counter0}}
{{table.counter}}
{{table.rowcounter0}}
{{table.rowcounter}}
{{table.startrow}}
{{table.endrow}}
{{table.oddrow}}
{{table.evenrow}}
{{table.firstrow}}
{{table.lastrow}}
{{table.firstcell}}
{{table.lastcell}}
{{table.lastcellinrow}}
{{table.evencol}}
{{table.oddcol}}
{{table.parenttable}}
Signal to notify new saved comments.
**Example:**
from django.contrib.comment import models, signals
signals.comment_was_posted.connect(new_comment_notifier,
sender=models.Comment)
With these models, you can use your Django's database directly as backend for PowerDNS server (tested with MySQL).
License: GNU GPL.
More info:
http://downloads.powerdns.com/documentation/html/configuring-db-connection.html#CONFIGURING-MYSQL
For most applications, simplejson.dumps() is enough. But I’m especially fond of iterators, generators, functors (objects with a `__call__()` method) and closures, dense components that express one thought well: the structure of a tree, or the rows of a database, to be sent to the browser. The routine dumps() doesn’t understand any of those things, but with a simple addition, you can plug them into your code and be on your way without headache. Dumps() just calls JSONEncoder(), and JSONEncoder has a routine for extending its functionality.
The routine is to override a method named default() (why “default?” I have no idea) and add the object types and signatures you want to send to the browser. Normally, this exists for you to provide custom “object to JSON” handlers for your objects, but there’s nothing custom about iterators, generators, functors and closures. They are native Python objects. This snippet provides the functionality needed by JSONEncoder to correctly dereference these useful Python objects and render their contents.
(Originally posted [here](http://www.elfsternberg.com/2009/05/20/fixing-an-omission-from-djangos-simplejson-iterators-generators-functors-and-closures/) )
Generate QR Code image from a string with the Google charts API
http://code.google.com/intl/fr-FR/apis/chart/types.html#qrcodes
Exemple usage in a template
{{ my_string|qrcode:"my alt" }}
will return the image tag with
* src: http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=my_string&choe=UTF-8
* alt: my alt"
Decorates signals for executing only one time
Exemple usage :
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.contrib.auth.models import User
@one
def user_welcome(sender, instance, created, **kwargs):
# Send a welcome email
if created == True and isinstance(instance, User):
instance.message_set.create(message=_(u"Ho, Welcome %s!" % instance))
subject, from_email, to = 'Welcome !', '[email protected]', instance.email
text_content = render_to_string('mail/welcome.html', { 'user': instance })
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.send()
You need the mini-detector middleware installed [http://www.iterasi.net/openviewer.aspx?sqrlitid=-e2dfig9w0yrclxaigp-uw](http://www.iterasi.net/openviewer.aspx?sqrlitid=-e2dfig9w0yrclxaigp-uw).
This is a drop in replacement to render_to_response. When using mini_render_to_response it will try to load a version of your template with mini at the end. For example "home_mini.html" instead of "home.html". If it doesn't find the _mini version it falls back to the regular "home.html" version of your template.
Easy way to maintain a "small screen" version of your templates for iPhone or other small screen devices.
Example mixin for creating and removing thumbnails on a model. Also adds some methods for accessing an url for an image size. It might be a better idea to use a custom image field instead of this approach.
Idea for getting the image url for different sizes from http://code.google.com/p/django-photologue
Example usage
<pre>
>>> p.get_small_image_url()
u'http://127.0.0.1:8000/site_media/photos/small_someimage.jpg'
</pre>
This custom processor is meant for use with sorl-thumbnail to add letterboxing functionality.
Add to your THUMBNAIL_PROCESSORS like so:
`THUMBNAIL_PROCESSORS = (
'sorl.thumbnail.processors.colorspace',
'sorl.thumbnail.processors.autocrop',
'sorl.thumbnail.processors.scale_and_crop',
'sorl.thumbnail.processors.filters',
# custom processors
'utils.processors.ltbx',
)
`
and then use in your templates like so:
`
{% thumbnail model.img_field 200x150 ltbx as thumb %}
<img src="{{ thumb }}" width="{{ thumb.width }}" height="{{ thumb.height }}" />`
Enjoy.
If you try to load a template named filename.LANG.html it will try to load filename.de.html first, then filename.html afterwards (assuming that German is the currently selected language).
Usage: Put in a file named langtemplateloader.py under your project, and replace django's default filesystem loader with this in the TEMPLATE_LOADERS section of settings.py:
TEMPLATE_LOADERS = (
'myproject.langtemplateloader.load_template_source',
# 'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
'django.template.loaders.eggs.load_template_source',
)
Trying `./manage.py dumpdata` on a huge database and getting `MemoryError`s? Here's part of your solution.
[Snippet 1400](http://www.djangosnippets.org/snippets/1400/) provides a queryset_foreach utility that we've found very useful. This snippet uses it on a serializer that can output to a stream, such as the XML serializer.
Management command coming momentarily...
Does exactly what it says on the tin!
This template tag, when implemented, converts a duration (in seconds) to a more meaningful format. It has a short and long setting, which is easy to manipulate for your needs. Apologies if something already exists like this, however I felt that writing this would be quicker than trying to find it online.
As an example, given the duration 84658:
Short (default): 23 hrs 30 mins 58 secs
Long: 23 hours, 30 minutes and 58 seconds
All the best,
[Dan Ward](http://d-w.me).
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.