Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
SmileyChris / not_found.py
Created November 29, 2009 09:08
A class which could be used to represent a missing context variable in a Django template
class NotFound(object):
"""
A class representing a context variable which was not found.
TODO: this could be extended to use the ``TEMPLATE_STRING_IF_INVALID``
Django setting for the ``__str__`` and ``__unicode__`` methods.
"""
def __init__(self, name=None):
@SmileyChris
SmileyChris / __init__.py
Created November 30, 2009 20:39
a Django site_settings app
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.cache import cache
from project.apps.site_settings import models
KEY = 'site-settings-%d' % settings.SITE_ID
def get_settings(from_cache=True):
"""
import re
re_float = re.compile('[-+]?(\d+\.\d*|\.\d+)$')
re_decimal = re.compile('[-+]?\d+$')
class CompileError(Exception):
pass
{% if user %}
<div class="left_column">
{% endif %}
{% block content %}
{% endblock %}
{% if user %}
</div>
<div class="right_column">
<div class="sidebar_header user_info">
<h2>Welcome.</h2>
{% if condition
%}{% for item in list %}
{{ item }}{%
endfor %}{%
endif %}
class YourBaseModel(models.Model):
@property
def child(self):
from django.core.exceptions import ObjectDoesNotExist
for related_object in self._meta.get_all_related_objects():
if not issubclass(related_object.model, self.__class__):
continue
try:
return getattr(self, related_object.get_accessor_name())
@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
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)
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).
"""
@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)