Skip to content

Instantly share code, notes, and snippets.

@igniteflow
igniteflow / examples.sh
Last active August 29, 2015 14:15
Ack examples
# search all files ignoring those paths that contain regex_pattern (should be quoted)
ack -v -g regex_pattern | ack -x pattern
# practical Django usage to ignore migrations files
ack --python -v -g 'migrations' | ack -x foo
@igniteflow
igniteflow / gist:2c4b033cc70cf25b8e84
Last active August 29, 2015 14:14
Django: Set all foreign keys to None to prevent cascade deletions
from django.db.models import get_models, get_app
MY_APPS = ('foo', 'bar')
# get all the models
models = []
for app in MY_APPS:
try:
models += get_models(app_mod=get_app(app))
except ImproperlyConfigured:
@igniteflow
igniteflow / gist:a1b438d5aba485cc2709
Last active August 29, 2015 14:06
Add content_as_python attribute to the APIClient.get() returned response object
import json
from rest_framework.test import APIClient
def get(self, *args, **kwargs):
response = super(APIClient, self).get(*args, **kwargs)
try:
response.content_as_python = json.loads(response.content)
except ValueError as e:
@igniteflow
igniteflow / coverage-pycharm.md
Last active April 2, 2016 14:33
How to show coverage in PyCharm

In a terminal:

pip install coverage coverage run --source='.' manage.py test | coverage xml # replace manage.py test with your test run command

In PyCharm go to Tools > Show Code Coverage Data (Ctrl+Shift+6) and select the generated coverage.xml file

@igniteflow
igniteflow / callback.py
Created August 22, 2014 14:43
Sequential method callbacks. In Appengine this would allow a deferred task to defer another task, and this task would defer another etc. so that tasks are completed sequentially
#!/usr/bin/env python
from time import sleep
def simulate_defer(_callable, *args, **kwargs):
"""mocking Appengine's deferred.defer"""
sleep(2)
_callable(*args, **kwargs)
@igniteflow
igniteflow / gist:335ffed5c7a3d909c61e
Last active March 16, 2021 22:15
Hidden delete inline formset base class for Django.
class HiddenDeleteBaseInlineFormSet(BaseInlineFormSet):
"""
Makes the delete field a hidden input rather than the default checkbox
inlineformset_factory(Book, Page, formset=HiddenDeleteBaseInlineFormSet, can_delete=True)
"""
def add_fields(self, form, index):
super(HiddenDeleteBaseInlineFormSet, self).add_fields(form, index)
if self.can_delete:
form.fields[DELETION_FIELD_NAME] = forms.BooleanField(
@igniteflow
igniteflow / git-post-receive-trigger-jenkins-build.md
Last active August 29, 2015 14:02
Setting up a local git server for testing and experimenting with hooks

First create the user and folder structure:

adduser git
su git
mkdir -p ~/repos/myrepo.git
git --bare init ~/repos/myrepo.git

Now our remote is ready to add to our local repository. In the local repo:

@igniteflow
igniteflow / debug.py
Created June 4, 2014 13:53
Useful debugging context managers for Django
import logging
from contextlib import contextmanager
from django.db.backends import BaseDatabaseWrapper
from django.db.backends.util import CursorWrapper
@contextmanager
def turn_off_sql_logging():
@igniteflow
igniteflow / worst-offenders.sql
Created May 21, 2014 17:29
MySQL: Show how much space tables are using in descending order
SELECT `TABLE_SCHEMA`, `TABLE_NAME`, `TABLE_ROWS`, `DATA_LENGTH`, `INDEX_LENGTH`, `DATA_FREE` FROM `TABLES` WHERE `TABLE_SCHEMA` LIKE "my_database" ORDER BY `DATA_LENGTH` DESC;
[color]
ui = auto
[user]
name = Phil Tysoe
email = phil@***.com
[push]
default = simple
[alias]
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
st = status