Skip to content

Instantly share code, notes, and snippets.

@mtigas
mtigas / middleware.py
Created February 13, 2012 01:14
An SSL-enforcing middleware
from django.conf import settings
from django.http import HttpResponsePermanentRedirect, get_host
class ForceSSLMiddleware(object):
"""
Redirects all (non-DEBUG) requests to go through SSL.
Picks up the `HTTP_X_FORWARDED_PROTO` proxy header set by Heroku.
Also sets the "Strict-Transport-Security" header for 600 seconds so that
@mtigas
mtigas / .gitignore
Created February 12, 2012 06:03
A gitignore, based on GitHub's
# A decent default .gitignore
# Same as GitHub's at https://github.com/github/gitignore/blob/master/Python.gitignore
# except keeps /bin directory.
*.py[co]
# Packages
*.egg
*.egg-info
dist
@mtigas
mtigas / pylibmc_backend.py
Created January 27, 2012 00:08
Subclass of `django.core.cache.backends.memcached.PyLibMCCache` that uses the binary protocol.
from django.core.cache.backends.memcached import PyLibMCCache
class PyLibMCBinaryCache(PyLibMCCache):
"""
Someday, they will accept my patch in
https://code.djangoproject.com/ticket/15815
Until then, this works.
class once(object):
"""
only allows the decorated function to run once (per unique args; can change
this behavior in __call__ as necessary)
based on http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
"""
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
from django.utils.encoding import smart_str
from django.utils.hashcompat import md5_constructor
def short_key(key, key_prefix, version):
"""
Like the default `KEY_FUNCTION`, but hashes strings longer
than 250 characters for safety with memcached. Uses python hash()
only when key is too long for memcached. Does not do anything
about memcached's restricted character set, only the key
@mtigas
mtigas / gist:1335214
Created November 2, 2011 22:57
Because you've always wanted a terminal screen tracking a live stream of "bieber" tweets.
@mtigas
mtigas / gist:1304751
Created October 21, 2011 19:44 — forked from ryanpitts/gist:1304725
GROUP BY and Select MAX from each group in Django, 2 queries
'''
given a Model with:
category = models.CharField(max_length=32, choices=CATEGORY_CHOICES)
pubdate = models.DateTimeField(default=datetime.now)
<other fields>
Fetch the item from each category with the latest pubdate.
'''
#!/bin/bash
while true; do; if [ -z "`curl -s http://store.apple.com/| grep backsoon1`" ]; then; open http://store.apple.com/; say -v cellos "go go gadget iphone"; break; fi; done;
@mtigas
mtigas / install_homebrew.rb
Created July 15, 2011 22:27 — forked from mxcl/install_homebrew.markdown
Installs Homebrew to /usr/local so you don't need sudo to `brew install`
#!/usr/bin/ruby
#
# This script installs to /usr/local only. To install elsewhere you can just
# untar https://github.com/mxcl/homebrew/tarball/master anywhere you like.
#
#
# 30th March 2010:
# Added a check to make sure user is in the staff group. This was a problem
# for me, and I think it was due to me migrating my account over several
# versions of OS X. I cannot verify that for sure, and it was tested on
@mtigas
mtigas / gist:1007030
Created June 3, 2011 19:45 — forked from n0nick/gist:1006549
wget-1.12-subjectAltNames.diff
diff --git a/src/openssl.c b/src/openssl.c
index b55ca8b..b036a3b 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -39,7 +39,7 @@ as that of the covered work. */
#include <string.h>
#include <openssl/ssl.h>
-#include <openssl/x509.h>
+#include <openssl/x509v3.h>