Skip to content

Instantly share code, notes, and snippets.

View dokterbob's full-sized avatar

Mathijs de Bruin dokterbob

View GitHub Profile
@dokterbob
dokterbob / autoslug.py
Created February 15, 2011 18:46
Mixins for automatically setting (unique) slugs for Django models -- also using values from inline classes.
from django.template.defaultfilters import slugify
class AutoSlugMixin(object):
"""
Automatically set slug to slugified version of the name if left empty.
Use this as follows::
class MyModel(AutoSlugMixin, models.Model):
def save(self):
super(MyModel, self).save()
@dokterbob
dokterbob / admin.py
Created February 15, 2011 19:20
Simple Django admin mixin which dynamically adds links to inline admins on the top right of the admin form.
class InlineButtonsAdminMixin(object):
"""
Mixin which dynamically adds links to inline admins on the top right of
the admin form.
"""
class Media:
js = ('js/inlinebuttons.js',)
@dokterbob
dokterbob / admin.py
Created February 15, 2011 20:00
InlineAdmin mixin limiting the selection of related items according to criteria which can depend on the current parent object being edited.
class LimitedAdminInlineMixin(object):
"""
InlineAdmin mixin limiting the selection of related items according to
criteria which can depend on the current parent object being edited.
A typical use case would be selecting a subset of related items from
other inlines, ie. images, to have some relation to other inlines.
Use as follows::
@dokterbob
dokterbob / admin.py
Created February 16, 2011 16:29
Django Admin mixins for dynamic TinyMCE link and image lists.
from django.core.urlresolvers import reverse
from sorl.thumbnail import get_thumbnail
from django.conf.urls.defaults import patterns, url
# This nice little snippet makes the life of someone extending the admin
# functionality a lot easier.
# Find the original at: http://djangosnippets.org/snippets/1804/
@dokterbob
dokterbob / fields.py
Created March 18, 2011 19:07
Email field with domain existence validation for Django.
import logging
logger = logging.getLogger(__name__)
# Note: we need dnspython for this to work
# Install with `pip install dnspython`
import dns.resolver, dns.exception
from django import forms
from django.utils.translation import ugettext as _
@dokterbob
dokterbob / copywriter.py
Created March 21, 2011 22:37
Script for automatically prepending copyright notice to a file
#!/usr/bin/python
import sys
from optparse import OptionParser
def main():
parser = OptionParser(usage="usage: %prog [options] <notice_filename> <code_filename>")
(options, args) = parser.parse_args()
if len(args) != 2:
@dokterbob
dokterbob / listeners.py
Created March 30, 2011 09:25
Class-based listeners, based on Django's class-based generic views.
class Listener(object):
"""
Class-based listeners, based on Django's class-based generic views. Yay!
Usage::
class MySillyListener(Listener):
def dispatch(self, sender, **kwargs):
# DO SOMETHING
pass
@dokterbob
dokterbob / translation_utils.py
Created April 19, 2011 09:08
Translations without using __dict__ :)
import logging, re
logger = logging.getLogger(__name__)
from django.utils.translation import get_language
LANGUAGE_CODE_RE = re.compile(r'_(?P<code>[a-z_]{2,5})$')
class TranslatableMixin(object):
@dokterbob
dokterbob / gist:995013
Created May 27, 2011 10:32
Form mixin class which allows for reordering and hiding fields for normal forms, similar to the way this is possible with ModelForms.
class OrderableFormMixin(object):
"""
Form mixin class which allows for reordering and hiding fields for normal
forms, similar to the way this is possible with ModelForms::
class MyFunkyForm(OrderableFormMixin, Form):
class Meta:
fields = ('my_field1', 'my_field2')
This snippet is based on:
@dokterbob
dokterbob / validators.py
Created May 30, 2011 10:58
Validator which allows for relative URL's as well. Based on Django's URLValidator.
import platform
import re
import urllib2
import urlparse
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.core.urlresolvers import resolve
from django.http import Http404
from django.utils.translation import ugettext_lazy as _