Skip to content

Instantly share code, notes, and snippets.

LegoStormtroopr /
Last active Sep 22, 2016
Enable pickling of Django QuerySet with abstract intermediate model
import copy_reg
from django.db.models.fields import Field, _load_field, _empty
def _load_field_for_abstract(model, field_name):
return model._meta.get_field(field_name)
View .travis.yml
# Lets run on the new infrastructure
sudo: false
language: python
- "3.5"
- pip install tox codecov coveralls
LegoStormtroopr /
Created May 3, 2017
Handling AWS-ELB terminating a healthy django instance when accessed from an invalid hostname

Handling AWS-ELB terminating a healthy django instance when accessed from an invalid hostname

When spinning up a new service, Amazon Elastic LoadBalancer needs to check if the service is live and running. This check is done from an IP (from any IP in a private IP range) to the service, this is done by the ELB just doing a simple GET request to a specified path, with no host information - for example GET /heatbeat.

If this instance is a Django service, regardless of the page accessed, this call will fail as in a properly setup Django it is very unlikely that the IP will be in Django's settings.ALLOWED_HOSTS settings.

There are two ways around this, either:

a. Add every IP from every private IP range into your Django project's ALLOWED_HOSTS settings

LegoStormtroopr /
Last active Jun 13, 2019
A BackTab Friendly QTextEdit for PyQt. Allows a user to use Tab/Shift-Tab to indent and un-indent text in a QTextEdit - it also works in QPlaintextEdit A workable solution to this StackOverflow question:
import sys
from PyQt4 import QtCore, QtGui
class TabPlainTextEdit(QtGui.QTextEdit):
def __init__(self,parent):
QtGui.QTextEdit.__init__(self, parent)
def keyPressEvent(self, event):
# Shift + Tab is not the same as trying to catch a Shift modifier and a tab Key.
# Shift + Tab is a Backtab!!
LegoStormtroopr /
Last active Jan 23, 2020
'FingerTabs' - Horizontal Text, Horizontal Tabs in PyQt This [trivial fingertab gist]( is released as Public Domain, but boy would it beswell if you could credit me, or tweet me [@LegoStormtoopr]( to say thanks!
from PyQt4 import QtGui, QtCore
from FingerTabs import FingerTabWidget
import sys
app = QtGui.QApplication(sys.argv)
tabs = QtGui.QTabWidget()
digits = ['Thumb','Pointer','Rude','Ring','Pinky']
for i,d in enumerate(digits):
LegoStormtroopr /
Created Aug 31, 2017
Connecting Django/Haystack to AWS ElasticSearch using IAM rotating credentials
import boto3
import os
import requests
from botocore.auth import SigV4Auth
from requests_aws4auth import AWS4Auth
from elasticsearch import RequestsHttpConnection
class AWSRequestsHttpConnection(RequestsHttpConnection):