Skip to content

Instantly share code, notes, and snippets.

@simonluijk
simonluijk / heroku_pgdump.sh
Last active December 18, 2015 20:29
Simple script to backup a Heroku hosted postgres database and download to local directory. Assumes one database per app.
#!/bin/bash
# Copyright (c) 2013 Simon Luijk
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
@simonluijk
simonluijk / gist:2a62fa9ee9157d74b570
Created July 21, 2014 14:34
Django rest framework - Base64 image field
import re
import base64
import uuid
import imghdr
from django.core.files.base import ContentFile
from rest_framework import serializers
class Base64ImageField(serializers.ImageField):
@simonluijk
simonluijk / otp_to_qrcode.py
Created February 21, 2016 18:29
Simple command to create a QR code from an OTP secret
#!/usr/bin/env python
import argparse
import qrcode
def main():
parser = argparse.ArgumentParser(description='Generate QR code for OTP.')
parser.add_argument('user', nargs=1)
parser.add_argument('key', nargs=1)
parser.add_argument('issuer', nargs=1)
@simonluijk
simonluijk / migrate_es.py
Created April 14, 2016 19:14
Allows to easily migrate Elasticsearch indexes
#!/usr/bin/env python
import sys
from argparse import ArgumentParser
from elasticsearch.helpers import reindex
from elasticsearch import Elasticsearch
class MigrateException(Exception):
pass
#!/usr/bin/env python
import sys
from random import choice
from argparse import ArgumentParser
from pprint import pprint
from elasticsearch import Elasticsearch
def auto_relocate(host='localhost'):
@simonluijk
simonluijk / proxy_chain_middleware.py
Created December 19, 2018 09:27
Validates the X_FORWARDED_FOR header in django middleware
from django.conf import settings
from django.core.exceptions import PermissionDenied
from netaddr import IPAddress, IPNetwork
class ProxyChainMiddleware(object):
"""
Validates the X_FORWARDED_FOR header against settings.PROXY_CHAIN. Then
sets REMOTE_ADDR to the IP address of the first untrusted IP in
X_FORWARDED_FOR.
echo "Installing xcode"
xcode-select --install
# Check for Homebrew,
# Install if we don't have it
if test ! $(which brew); then
echo "Installing homebrew..."
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi