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. **
===
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.
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'
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.
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
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/ .
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/)
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.
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.
***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 :)
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.
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.
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.