Out of the box, Django e-mail fields for both database models and forms only accept plain e-mail addresses. For example, `[email protected]` is accepted.
On the other hand, full e-mail addresses which include a human-readable name, for example the following address fails validation in Django:
Joe Hacker <[email protected]>
This package adds support for validating full e-mail addresses.
**Database model example**
from django import models
from full_email.models import FullEmailField
class MyModel(models.Model):
email = FullEmailField()
**Forms example**
from django import forms
from full_email.formfields import FullEmailField
class MyForm(forms.Form):
email = FullEmailField(label='E-mail address')
I maintain this code in a [GitHub gist](https://gist.github.com/1505228). It includes some unit tests as well.
Simple logging middleware that captures the following:
* remote address (whether proxied or direct)
* if authenticated, then user email address
* request method (GET/POST etc)
* request full path
* response status code (200, 404 etc)
* content length
* request process time
* If DEBUG=True, also logs SQL query information - number of queries and how long they took
Counter tag. Can be used to output and increment a counter.
For usage, see docstring in the code.
This is the first complete tag that I've implemented, I hope that there are no bugs and that it's thread safe.
This is a nice decorator for using the cache to save the results of expensive-to-calculate but static-per-instance model properties. There is also a decorator for when the property value is another model, and the contents of the other model should not be cached across requests.
3 levels of caching implemented:
* outermost: django cache
* middle: obj._cache (for multiple properties on a single object)
* innermost: replace the attribute on this object, so we can entirely avoid running this function a second time.
Unfortunately the built in Django JSON serialzer encodes GeoDjango GeometyrField as WKT text. This snippet extends django's serializer and adds support for GEOJson format.
Built in JSON serializer output:
[{"pk": 1, ... "geopoint": "POINT (-76.5060419999999937 44.2337040000000030)" ... }]
GeoJSON serializer ouput:
[{"pk": 1, ... "geopoint": {"type": "Point",
"coordinates": [-76.503296000000006, 44.230956999999997],
"__GEOSGeometry__": [
"__init__",
[
"SRID=4326;POINT (-75.5129950000000036 44.2442360000000008)"
]
]
}]
Note: the "__GEOSGeometry__" is a class hint as defined by JSON-RCP
and used during deserilization.
Model backend that enables permissions for AnonymusUsers.
I wanted it to be as simple as possible so anonymous users just forward their permission checks
to some fixed user model. This instance can be edited via django admin, assigned to groups, etc.
To control which user will represent anonymous user you use ANONYMOUS_USER_NAME setting in
settings file.
To provide some sensible level of security i enforce following for user that represents
anonymous user:
* This user must have password equal to UNUSABLE_PASSWORD
* This user may not log in
* You cant cange password for this user via admin.
You need to enable this backend by setting AUTHENTICATION_BACKENDS. Please note that you
should not place this backend alongside django ModelBackend. This backend inherits from it.
Better slugify function for national characters. Source of original script: http://trac.django-fr.org/browser/site/trunk/project/links/slughifi.py?rev=47
Django admin orders models by their primary key by default, which can be undesirable on very large tables.
This shows how to disable any ordering on a model.
Note that this behavior is fixed in 1.4 trunk.
A small script that takes a manage.py dumpdata generated json file, and removes fields of the specified models. I needed this because i kept my initial data on a json file and after I removed a field on one of my models, the script wouldn't work anymore.
When using [django debug toolbar](https://github.com/django-debug-toolbar/django-debug-toolbar), I like to be able to turn debugging on and off without having to edit my settings file. This callback makes that possible. Add `?debug=on` to the URL to turn debugging on. It will remain on in the current session until you turn it off with `?debug=off`.
Make sure your session middleware comes before your debug toolbar middleware.
It was based in:
http://djangosnippets.org/snippets/1586/
Instead of doing this:
'attribute_name = forms.CharField(widget=forms.TextInput(attrs={'class':'special'}))`
You can do this in your template:
{{ form|cssclass:"attribute_name:special_class"|cssclass:"other_attribute:special_class" }}
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.