Login

Most bookmarked snippets

Snippet List

Validating Model subclass

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.

  • model
  • validation
  • subclass
Read More

Take a django.core.mail email message and replace any MEDIA_URL-served images with attached images

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.

  • email
  • attachment
Read More

JSON instead of pickle for memcached

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.

  • memcache
  • cache
  • json
  • memcached
  • pickle
Read More

SOAP web service with soaplib 2.0

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__)

  • soap
  • soaplib
  • wsdl
Read More

another request logging middleware with request time and extra info

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

  • middleware
  • request-path
  • time
  • request
  • logging
  • time-logging
Read More

create or update, then get, model instances from JSON/py dict

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.

  • json
  • import
Read More

decorator for decorators with optional args

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.

  • decorator
  • decoratordecorator
  • metadecorator
Read More

Cached model property decorator (like @property)

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.

  • property
  • cache
  • decorator
Read More

Custom Times for the Django Admin Time Widget

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

  • template
  • javascript
Read More
Author: caa
  • 0
  • 1

Support for permissions for anonymous users in django ModelBackend

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.

  • user
  • auth
  • permissions
  • anonymous
Read More
Author: jb
  • 1
  • 1

3110 snippets posted so far.