Skip to content

Instantly share code, notes, and snippets.

@nealtodd
nealtodd / gist:a8f87b0d95e73eb482c5
Created June 10, 2015 11:10
Django management command to detect missing migration files.
import sys
from django.apps import apps
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connections
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.state import ProjectState
from django.db.utils import OperationalError
@nealtodd
nealtodd / settings_test_snippet.py
Last active November 14, 2019 01:25
Skip migrations for a Django 1.7 test run
# If your test settings file doesn't import any other settings file
# then you can use the function directly:
def prevent_tests_migrate(db):
import django
from django.db import connections
from django.db.migrations.executor import MigrationExecutor
django.setup()
ma = MigrationExecutor(connections[db]).loader.migrated_apps
return dict(zip(ma, ['{a}.notmigrations'.format(a=a) for a in ma]))
@nealtodd
nealtodd / deletable_objects_snippet.py
Last active August 10, 2020 06:49
Django helper for showing related objects that would be cascade deleted on deletion of an object.
from django.conf import settings
from django.db.models.deletion import Collector
from django.db.utils import ConnectionRouter
def deletable_objects(obj):
"""
Return a generator that yields (model, instance) tuples
of instances related to obj (including obj itself).
Essentially, programmatic access to the data Django Admin
@nealtodd
nealtodd / wagtail-on-zappa.md
Last active July 30, 2021 17:44 — forked from tomdyson/wagtail-on-zappa.md
Wagtail on AWS Lambda, with Zappa

Wagtail on AWS Lambda, with Zappa

Aim

A demonstration of Wagtail running on AWS Lambda + API Gateway using Zappa for deployment.

This is not a Production solution, it is an insecure setup focusing solely on getting Wagtail to run on Lambda in the simplest way possible (and at zero cost if it is not used beyond this demonstration (or close to zero depending on how much you exercise the S3 bucket)).

caveat emptor!

@nealtodd
nealtodd / decorator.py
Created April 25, 2012 13:15
Python profiling decorator
from cProfile import Profile
import pstats
def profile(sort_args=['cumulative'], print_args=[10]):
profiler = Profile()
def decorator(fn):
def inner(*args, **kwargs):
result = None