Skip to content

Instantly share code, notes, and snippets.

View tobiasmcnulty's full-sized avatar

Tobias McNulty tobiasmcnulty

View GitHub Profile
@tobiasmcnulty
tobiasmcnulty / writecache.py
Created September 2, 2013 20:11
Simple write-through cache for Django querysets
def get_cache_key(model, pk):
"""
Generates a cache key based on ``WRITE_CACHE_PREFIX``, the cache key prefix
defined in the settings file (if any), the Django app and model name, and
the primary key of the object.
"""
params = {
'prefix': getattr(settings, 'WRITE_CACHE_PREFIX', ''),
'app': model._meta.app_label,
'model': model._meta.object_name,
@tobiasmcnulty
tobiasmcnulty / decorators.py
Last active May 5, 2019 07:29
Django database router, based on django-balancer, that sends reads for the specified models to the RoundRobinMasterSlaveRouter router in django-balancer. Uses thread locals to determine whether or not the it has been enabled for the current request. Will raise an exception if views or other code attempt to use it for write queries.
from functools import wraps
from routers import ForceReadForModelsRouter
__all__ = ['uses_forced_read_router']
def uses_forced_read_router(func):
@wraps(func)
def wrapper(req, *args, **kwargs):
@tobiasmcnulty
tobiasmcnulty / generate_factories.py
Last active October 27, 2020 15:12
Django management command to generate factory-boy boilerplate factory definitions for all the models in a given app. The code is not intended to be usable right off the bat, it just provides a few sane defaults based on the structure of your models.
from django.core.management.base import BaseCommand, CommandError
from django.db.models import get_models, get_app, fields
from django.db.models.fields import related
class Command(BaseCommand):
help = """Generate factory-boy factories for the given app"""
def handle(self, *args, **options):
assert len(args) == 1, 'Must specify app name as first and only argument'
@tobiasmcnulty
tobiasmcnulty / logfilters.py
Last active December 18, 2015 22:59
Sample Django logging configuration for adding static contextual fields to logging records to be sent to Graylog2 via graypy.GELFHandler. Also included is a filter to remove the Django request object from logging records, which may not be picklable or you may not always want to send to Graylog2 (since it can contain sensitive information).
import logging
class QuotelessStr(str):
"""
Return the repr() of this string *without* quotes. This is a
temporary fix until https://github.com/severb/graypy/pull/34 is resolved.
"""
def __repr__(self):
return self
@tobiasmcnulty
tobiasmcnulty / kannel.conf
Created January 31, 2012 12:31
Sample Kannel configuration for talking to RapidSMS server
#
# CONFIGURATION FOR USING SMS KANNEL WITH RAPIDSMS
#
# For any modifications to this file, see Kannel User Guide
# If that does not help, see Kannel web page (http://www.kannel.org) and
# various online help and mailing list archives
#
# Notes on those who base their configuration on this:
# 1) check security issues! (allowed IPs, passwords and ports)
# 2) groups cannot have empty rows inside them!
@tobiasmcnulty
tobiasmcnulty / elasticsearch.conf
Created January 22, 2012 22:07 — forked from rbscott/elasticsearch.conf
upstart job for elastic search
# ElasticSearch Service
description "ElasticSearch"
start on (net-device-up
and local-filesystems
and runlevel [2345])
stop on runlevel [016]