Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
SmileyChris / fields.py
Created February 14, 2012 00:04 — forked from insin/fields.py
Formatting horrible if
def run_validators(self, value):
if value in validators.EMPTY_VALUES:
return
errors = []
for v in self.validators:
try:
v(value)
except ValidationError, e:
message = None
if hasattr(e, 'code'):
@SmileyChris
SmileyChris / gist:1789453
Created February 10, 2012 12:46
Playing around with creating an image with thumb field (haven't even run this code, so don't blame me if it doesn't work)
import os
from django.db import models
from easy_thumbnails.fields import ThumbnailerImageField
from easy_thumbnails.files import Thumbnailer, ThumbnailerImageFieldFile
import uuid
def _get_file_path(instance, filename):
filename = str(uuid.uuid4()) + os.splitext(filename)
return '/'.join('photo', filename)
PS1="(`basename \"$VIRTUAL_ENV\"`\$(__git_ps1 :%s))$_OLD_VIRTUAL_PS1"
WORKDIR=$HOME/work/`basename $VIRTUAL_ENV`
cd () {
if (( $# == 0 )); then
if [ -d $WORKDIR ] && [ `pwd` != $WORKDIR ]; then
builtin cd $WORKDIR
else
builtin cd $VIRTUAL_ENV
"""
To use these relative storage classes as the default, change the following
settings::
DEFAULT_FILE_STORAGE = 'relative_storage.RelativeFileSystemStorage'
STATICFILES_STORAGE = 'relative_storage.RelativeStaticFilesStorage'
Then, you'll want to make sure that you only use the ``{% static %}`` tag when
referencing static files.
"""
from django.core.files import FileSystemStorage
@SmileyChris
SmileyChris / gist:1249734
Created September 29, 2011 00:54
AddOrEditView
class AddOrEditView(generic.UpdateView):
# The following two _kwarg attributes are introduced in Django 1.4, until
# then, they can exist here without any concern.
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'
def get_object(self, *args, **kwargs):
pk = self.kwargs.get(self.pk_url_kwarg)
slug = self.kwargs.get(self.slug_url_kwarg)
if pk is None and slug is None:
jQuery.fn.selectOptgroup = function(options) {
var settings = jQuery.extend({
optSelect: '_options',
inBetween: '',
optBlank: null,
selectBlank: null,
keepOrphans: true,
showSpeed: 200
}, options);
@SmileyChris
SmileyChris / conf.py
Created May 4, 2011 22:30
Django application specific settings configuration
from django.conf import settings as django_settings
class Settings(object):
DEFAULTS = {
# Define any default settings in here.
}
def __dir__(self):
return dir(django_settings)
from django.utils.datastructures import SortedDict
from django.db.models.fields import AutoField
class EasyFieldsets(object):
"""
A mixin for use with ``ModelAdmin`` and related inline classes allowing
fieldsets to be used without having to manually define all fields
(undefined ones are placed at the end of the ``None`` fieldset).
"""
class SubscriptionForm(forms.ModelForm):
""" Base form to be extended by providers which require authentication. """
def save(self, *args, **kwargs):
self.instance.credentials = self.serialize()
return super(SubscriptionForm, self).save(*args, **kwargs)
@SmileyChris
SmileyChris / postactivate
Created December 8, 2010 20:33
~/.virtualenvs/postactivate: switch to working directory initially if it exists, and allow switiching between the env root and the working directory by typing "cd"
WORKDIR=$HOME/work/`basename $VIRTUAL_ENV`
cd () {
if (( $# == 0 )); then
if [ -d $WORKDIR ] && [ `pwd` != $WORKDIR ]; then
builtin cd $WORKDIR
else
builtin cd $VIRTUAL_ENV
fi
else