Skip to content

Instantly share code, notes, and snippets.

@LegoStormtroopr
LegoStormtroopr / boto_auth_requests.py
Created August 31, 2017 23:06
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):
# Lets run on the new infrastructure
sudo: false
language: python
python:
- "3.5"
install:
- pip install tox codecov coveralls
@LegoStormtroopr
LegoStormtroopr / README.md
Created May 3, 2017 01:26
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
LegoStormtroopr / fields.py
Last active September 22, 2016 23:07
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)
@LegoStormtroopr
LegoStormtroopr / README.md
Last active August 5, 2016 05:10
Generic autocompletes for django-autocomplete-light v3.x
def view_historic_reversion(request, iid, vid):
item = get_object_or_404(MDR._concept, pk=iid).item
if not user_can_view(request.user, item):
if request.user.is_anonymous():
return redirect(reverse('friendly_login') + '?next=%s' % request.path)
else:
raise PermissionDenied
old_item = default_revision_manager.get_for_object_reference(
item.__class__,
@LegoStormtroopr
LegoStormtroopr / token_search_form.py
Created October 9, 2014 00:20
Haystack google-like colon searching
# Released as GPL.
# Attribute where possible :)
from haystack.constants import DEFAULT_ALIAS
from haystack import connections
class TokenSearchForm(SearchForm):
def prepare_tokens(self):
try:
query = self.cleaned_data.get('q')
except:
@LegoStormtroopr
LegoStormtroopr / badge.html
Created August 16, 2014 00:40
Font-awesome login badge
<span class="face">
<i class="fg fa fa-user"></i>
</span>
@LegoStormtroopr
LegoStormtroopr / iassist2014.ddi31.xml
Last active August 29, 2015 14:02
IASSIST 2014 Conference evaluation questionnaire metadata
<?xml version="1.0"?>
<ddi:DDIInstance xmlns:ddi="ddi:instance:3_1" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sqbl="sqbl:1"
xmlns:a="ddi:archive:3_1" xmlns:r="ddi:reusable:3_1" xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:d="ddi:datacollection:3_1" xmlns:l="ddi:logicalproduct:3_1" xmlns:c="ddi:conceptualcomponent:3_1"
xmlns:ds="ddi:dataset:3_1" xmlns:s="ddi:studyunit:3_1" xmlns:g="ddi:group:3_1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="x0" version="0.0.1"
agency="com.kidstrythisathome.ddirepo.legostormtroopr">
<g:ResourcePackage id="x1">
<g:Purpose id="x2">
<r:Content />
@LegoStormtroopr
LegoStormtroopr / pyqt_xml_editor.py
Created September 1, 2013 03:00
This is a basic XML editor that shows of how to do very simple XML validation inside of a PyQT QTextEdit field. All this does is check for document well-formed-ness, but using the LXML library it shouldn't be difficult to extend this to perform Schema or DOCTYPE based validation.
import sys
from lxml import etree
from PyQt4 import QtCore, QtGui
class editor(QtGui.QMainWindow):
def __init__(self):
super(editor, self).__init__()
self.text = QtGui.QTextEdit()
self.setCentralWidget(self.text)
self.text.textChanged.connect(self.validate)