Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
SmileyChris / conf.py
Last active August 29, 2015 13:57
AppSettings
from .conf_base import AppSettings
class Settings(AppSettings):
YOUR_SETTING = True
"""
Give each setting some documentation for sphinx autodoc if you like.
"""
@SmileyChris
SmileyChris / gist:10611277
Last active August 29, 2015 13:59
flatpages template tag
@register.assignment_tag
def flatpages(a, b, *order_first):
"""
Get the flatpages, optionally with specific ones ordered first.
Usage::
{% flatpages '/home/' '/about/' as flatpages %}
{% for flatpage in flatpages %}...{% endfor %}
"""
class UpdateOrCreateMixin(object):
"""
A view mixin to allow views that use SingleObjectMixin to not fail if no pk/slug is provided.
Primarily useful for making an UpdateView that can create objects too. For example::
class MyView(UpdateOrCreateMixin, UpdateView):
...
url(r'^add/$', MyView.as_view(), name='my-add-view'),
import re
array_qs = re.compile(r'(\w+)\[(\d*)\]$')
class UnPHPify(object):
def process_request(self, request):
"""
Iterate request.GET, request.POST and request.FILES and amend the dict
@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())