If you inherit from ValidatedModel instead of from models.Model, then full_clean() will be called before save().
So, add validators to your field definitions, and all your fields will be validated before they go to the database.
The same thing can be accomplished with a pre_save signal, but the code is quite a bit messier than the simple inheritance above.
The code here will take an EmailMessage from django.core.mail and replace the sourcing of any images served by the application with attached image content. Note: This expects a valid closing tag of **/>** on img elements, it will not properly handle filenames with **'** characters in it, and it does not handle if invalid image sources are listed.
Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) doesn't know pickle, but json yes.
I've updated the `DjangoSoapApp` class from [this popular soaplib snippet](http://djangosnippets.org/snippets/2210/) so the snippet will work properly with soaplib 2.0.
Usage is the same as before:
my_soap_service = DjangoSoapApp([MySOAPService], __name__)
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
Basically the idea is to import/update model instances from a json data that closely matches the model structure (i.e. identical field names)
From my answer to this question: [http://stackoverflow.com/a/8377382/202168](http://stackoverflow.com/a/8377382/202168)
See the original question for sample data format.
Have you ever wanted a decorator that you could apply either straight-out:
@mydec
def myfun(...):
...
or with special arguments:
@mydec(special=foo)
def myfun(...):
...
? Well, decorate it with this metadecorator, and voila.
(I had this idea independently, but it's been done before as decorator_withargs: http://osdir.com/ml/python.ideas/2008-01/msg00048.html. My version is actually useful because it deals with signatures and calling directly.)
As http://www.siafoo.net/article/68 points out, the standard decorator module has too much magic: the "@decorator" decorator expects a wrapping function, not a working decorator. This module fixes that.
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.
Based on code from [mihelac.org](http://source.mihelac.org/2010/02/19/django-time-widget-custom-time-shortcuts/)
Modified to work in Django 1.3.1. Put it in templates/admin/app_label/model/change_form.html
if you have multiple choice field in front and you need just a string contains chosen values this is it! Unicode encoding row related with another snippet - "Switched value typed choice field", In typical cases, you do not need to call additional methods.
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.
Trying to build a state-machine that stores state in the model or in settings.py rather then in the database, I wrote this small generic pre_save hook that lets me leave all the data in the Model.
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.