Skip to content

Instantly share code, notes, and snippets.

View joshourisman's full-sized avatar

Josh Ourisman joshourisman

View GitHub Profile
@joshourisman
joshourisman / change_list.html
Created July 2, 2009 13:53
Code to enable drag and drop reordering on a change_list in the Django admin. Currently causes a 'too many open files' error for some reason.
{% extends "reversion/change_list.html" %}
{% load my_admin i18n %}
{% block extrahead %}
<script type="text/javascript" src="/media/static/js/jquery-1.3.2.js" />
<script type="text/javascript" src="/media/static/js/jquery-ui-1.7.2.custom.min.js" />
<script type="text/javascript" src="/media/static/js/change_list_sort.js" />
{% endblock %}
{% block object-tools %}
@joshourisman
joshourisman / text.py
Created July 14, 2009 20:55
A Django template filter that truncates a string to at most X characters while respecting word boundaries.
from django.template import Library
from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy
from django.template.defaultfilters import stringfilter
register = Library()
def truncate_chars(s, num):
"""
Template filter to truncate a string to at most num characters respecting word
@joshourisman
joshourisman / monthyearfield.py
Created July 30, 2009 19:50
A custom field and widget to provide month and year entry as for a credit card expiration date.
from django import forms
import datetime
class MonthYearWidget(forms.MultiWidget):
"""
A widget that splits a date into Month/Year with selects.
"""
@joshourisman
joshourisman / local_settings.py
Created August 21, 2009 14:10
My basic local_settings.py file for django projects
import os
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_ROOT, '../local.db'),
'USER': '',
@joshourisman
joshourisman / .hgrc
Created September 30, 2009 13:42
My .hgrc file.
[ui]
username = Josh Ourisman <josh@joshourisman.com>
[extensions]
# easy_install hgsubversion
hgsubversion=
# easy_install hg-git
hggit=
fetch=
hgext.mq=
import csv, StringIO
class CSVEmitter(Emitter):
def get_keys(self, input_dict):
keys = []
for item in input_dict.items():
if isinstance(item[1], dict):
keys.extend(self.get_keys(item[1]))
else:
keys.append(item[0])
from struct import pack, unpack
def encode(ip_address):
return unpack('i', ''.join([pack('B', int(sub)) for sub in ip_address.split('.')]))[0]
def decode(packed_address):
return '.'.join(['%d' % unpack('B', sub)[0] for sub in pack('i', packed_address)])
class ThisAdmin(admin.ModelAdmin):
def queryset(self, request):
"""
Filter the objects displayed in the change_list to only
display those for the currently signed in user.
"""
qs = super(ThisAdmin, self).queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(Q(owner=request.user) | Q(editors=request.user))
class ThisAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super(ThisAdmin, self).get_urls()
my_urls = patterns('',
url(r'^all/$',
self.admin_site.admin_view(all_objects),
{'admin': self,},
name='all_initiatives'),
)
return my_urls + urls
def all_objects(request, admin):
return ThisAdmin(admin.model, admin.admin_site, request).changelist_view(request)