Login

Most bookmarked snippets

Snippet List

Retrieve Latitude & Longitude for an Address from Google Geocoder V3

Very simple python class for querying Google Geocoder. Accepts a human-readable address, parses JSON results and sets lat and lng. (Full JSON results are stored to `results` property of GoogleLatLng for access to other attributes.) This could easily be expanded to include the XML output option, etc. **Requires PycURL and simplejson.** Example: >>> location = "1600 Amphitheatre Parkway, Mountain View, CA 94043" >>> glatlng = GoogleLatLng() >>> glatlng.requestLatLngJSON(location) >>> print "Latitude: %s, Longitude: %s" % (glatlng.lat, glatlng.lng) Latitude: 37.422782, Longitude: -122.085099` ** Do not forget the usage limits, which include request rate throttling, otherwise, Google might ban you. ** ===

  • google
  • geocode
Read More

Generate ICalendar Files with django-events

This is a fairly straightforward view to generate iCalendar (.ics) files, with a unique UUID, a proper filename, and the basic fields needed to export an event from a public calendar (using the django-events-calendar app). While it can certainly be extended and adapted, it works very well as-is.

  • calendar
  • ical
  • icalendar
Read More

Exclude Apps When Testing

This allows you to exclude certain apps when doing standard tests (manage.py test) by default. You set the settings/local_settings variable EXCLUDE_APPS and it will exclude those apps (like django, registration, south... etc). This makes running tests much faster and you don't have to wait for a bunch of tests you don't care about (per say). You can override it by adding the app to the command line still. So if 'south' is in the excluded apps you can still run: 'python manage.py test south' and it will run the south tests. You will also need to tell django to use this as the test runner: TEST_RUNNER = 'testing.simple.AdvancedTestSuiteRunner'

  • testing
  • tests
  • exclude
Read More

SOAP web service with soaplib 0.9+

The popular [soaplib snippet](http://djangosnippets.org/snippets/979/) works only with older soaplib (0.8, for example). This snippet is modifed to work with newer soaplib: it was tested with 0.9.2-alpha3 and 1.0.0-beta4. I've tested suds python client and .NET client.

  • soap
  • soaplib
  • wsdl
Read More

Silk icon tags

This simple template tag can be used to add famfamfam's silk icons easily to any element in your template.

  • templatetags
  • icons
  • silk
Read More

Amazon's CloudFront streaming signed urls

You can use this code to sign urls for streaming distributions or change it a bit and sign normal distribution's urls. Available settings: CLOUDFRONT_KEY - path to private key file CLOUDFRONT_KEY_PAIR_ID - key pair id CLOUDFRONT_EXPIRES_IN - expiration time in seconds CLOUDFRONT_DOMAIN - domain name

  • s3
  • amazon
  • aws
  • cloudfront
Read More

Overwriting file storage

The title says it all — a subclass of FileSystemStorage which will overwrite files. Note that saves which fail part way though will leave the original file intact (see `test_upload_fails`). Based roughly on http://djangosnippets.org/snippets/2044/ .

  • file
  • storage
  • filestorage
  • file-storage
Read More

Encode emails as URIs

Put this snippet in a file in a templatetags/ directory and load it in your templates. Then use it to encode your emails: `{{"[email protected]"|html_encode_email}}` Or if you want some control over the anchor tag: `<a href="mailto:{{"[email protected]"|html_encode}}&subject=Feedback">Send Feedback</a>` From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)

  • templatetag
  • email
  • spam
  • encode
Read More

Reset / Send account details email

I needed to create a feature for administrators to create accounts and email the *new account was created* email using a button. Remember to change the default template and subject to match your needs. The code is directly taken from `django.contrib.auth.forms.PasswordResetForm.save()`. Notice that the function raises `ValueError` if the user is missing an email address.

  • users
  • reset-password
Read More

Filter to generate QR codes

You can convert any string to a QR code image as easy as use a simple filter, thanks to google charts api. Common use: <img src="{{object.attribute_to_encode|qr:"120x130"}}" /> This will create a 120(width) x 130(heiht) image with the value of the attribute_to_encode encoded in a QR coded image.

  • filter
  • templatetag
  • templatefilter
  • barcode
  • qr
Read More

Database cleanup

***About*** I tried to dump data from my database (manage.py dumpdata) and I couldn't do it because of error: User matching query does not exists I found out that my database was filled with garbage: entries those foreigners were deleted. My table's engine is MyISAM so it allows for these lost entries to exist. I had to cleanup my database before I do datadump, so I've written a script which worked fine for me. ***Usage*** Place this script in same directory with your settings.py and run it: `python db_cleanup.py` ***Disclaimer*** Backup your data :)

  • dumpdata
Read More

Arbitrary auto-generated primary keys

Auto-incremented primary keys are the default in Django and they are supported natively in most databases but for anything more complex things are less trivial. DB sequences are not standard, may not be available and even if they are, they are typically limited to simple integer sequence generators. This snippet bypasses the problem by allowing arbitrary auto-generated keys in Python. The implementation needs to determine whether an IntegrityError was raised because of a duplicate primary key. Unfortunately this information is not readily available, it can be found only by sniffing the DB-specific error code and string. The current version works for MySQL (5.1 at least); comments about how to determine this in other databases will be incorporated in the snippet.

  • mysql
  • sequence
  • uuid
  • auto decrement
  • auto increment
  • primary key
Read More

Per-Instance On-Model M2M Caching

If you are like me and you find yourself often using M2M fields for tons of other on-model methods, in templates, and views alike, try using this quick and dirty caching. I show the use of a "through" model for the m2m, but that is purely optional. For example, let's say we need to do several different things with our list of beads, the old way is... # views.py necklace = Necklace.objects.get(id=1) bead = Bead.objects.get(id=1) if bead in necklace.beads.all(): # this bead is here! # template necklace.html {% for bead in necklace.beads.all %} <li>{{ bead }}</li> {% endfor %} ...which would hit the database twice. Instead, we do this: # views.py necklace = Necklace.objects.get(id=1) bead = Bead.objects.get(id=1) if bead in necklace.get_beads(): # this bead is here! # template necklace.html {% for bead in necklace.get_beads %} <li>{{ bead }}</li> {% endfor %} Which only does one hit on the database. While we could have easily set the m2m query to a variable and passed it to the template to do the same thing, the great thing is how you can build extra methods on the model that use the m2m field but share the cache with anyone down the line using the same m2m field. I'm by no means an expert, so if there is something here I've done foolishly, let me know.

  • models
  • cache
  • m2m
  • caching
Read More

3110 snippets posted so far.