Skip to content

Instantly share code, notes, and snippets.

View allanlei's full-sized avatar
:shipit:
What's up?

Allan Lei allanlei

:shipit:
What's up?
View GitHub Profile
@allanlei
allanlei / app.yaml
Created February 6, 2012 22:49
Config files for using Google App Engine(Python) as CDN
application: APP_ID
version: 1
runtime: python
api_version: 1
default_expiration: "60d"
handlers:
- url: /favicon\.ico
static_files: build/favicon.ico
@allanlei
allanlei / Procfile
Created February 9, 2012 02:06
Gunicorn config for Django application on Heroku with SSL
web: python manage.py run_gunicorn -b 0.0.0.0:$PORT -c config/gunicorn.conf --settings $DJANGO_SETTINGS_MODULE
@allanlei
allanlei / gravatar.py
Created March 24, 2012 08:33
Gravatar Template Tag
from django import template
import urllib, hashlib
register = template.Library()
@register.simple_tag(takes_context=True)
def gravatar_image_url(context, email, default='mm', size=40, extension='jpg', force_default=False, rating='g'):
return '{host}/avatar/{hash}.{extension}?{params}'.format(
host='https://secure.gravatar.com' or '//www.gravatar.com',
@allanlei
allanlei / gist:2299620
Created April 4, 2012 08:08
Mount Amazon S3 bucket if not mounted
mkdir -p MYBUCKET && mountpoint -q MYBUCKET || s3fs MYBUCKET MYBUCKET -o use_cache=/tmp -o use_rrs=1
@allanlei
allanlei / views.py
Created April 23, 2012 15:39
Datatables mixin for Django *incomplete*
#from django.db.models import Q
#from django.template.loader import render_to_string
#from django.http import HttpResponse
#from django.utils.cache import add_never_cache_headers
#from django.utils import simplejson
class DataTablesMixin(object):
@allanlei
allanlei / fields.py
Created January 10, 2013 07:58
Subclass of mongoengine's GenericEmbeddedDocumentField. Allows GenericEmbeddedDocumentField to read a custom field instead of _cls to construct the EmbeddedDocument.
from mongoengine.fields import GenericEmbeddedDocumentField as MDBGenericEmbeddedDocumentField
class GenericEmbeddedDocumentField(MDBGenericEmbeddedDocumentField):
def __init__(self, *args, **kwargs):
self.cls_field = kwargs.pop('cls_field', '_cls')
super(GenericEmbeddedDocumentField, self).__init__(*args, **kwargs)
def to_python(self, value):
cls = None
if isinstance(self.cls_field, (str, unicode)):
@allanlei
allanlei / auto_create_schema.sql
Last active December 12, 2015 08:08
Postgresql triggers to handle CRUD with rows and schemas
-- Trigger: auto_create_schema on landlord_tenant
-- DROP TRIGGER auto_create_schema ON landlord_tenant;
CREATE TRIGGER auto_create_schema
AFTER INSERT
ON landlord_tenant
FOR EACH ROW
EXECUTE PROCEDURE create_schema();
@allanlei
allanlei / middleware.py
Last active December 16, 2015 03:29
Force HTTPS middleware. Parts taken from rdegges/django-sslify and kennethreitz / flask-sslify. Excludes checking for HTTP_X_FORWARDED_PROTO, as that should be handled by Django 1.4+ by SECURE_PROXY_SSL_HEADER setting. For Django 1.4<, the SECURE_PROXY_SSL_HEADER should be implemented through a sperate middleware. Should be using django-appconf …
from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed
SSLIFY_ENABLED = getattr(settings, 'SSLIFY_ENABLED', not settings.DEBUG)
SSLIFY_HSTS_AGE = int(getattr(settings, 'SSLIFY_HSTS_AGE', 60 * 60 * 24 * 365))
SSLIFY_INCLUDE_SUBDOMAINS = getattr(settings, 'SSLIFY_INCLUDE_SUBDOMAINS', False)
SSLIFY_PERMANENT = getattr(settings, 'SSLIFY_PERMANENT', False)
if SSLIFY_PERMANENT:
from django.http import HttpResponsePermanentRedirect as HttpResponseClass
#!/bin/bash
# Procedure
# 1. Stop instance
# 2. Detach volume
# 3. Make Snapshot
# 4. Make new volume from snapshop with new size
# 5. Attach volume to instance
# 6. Start instance
instanceid=INSTANCE_TO_RESIZE
@allanlei
allanlei / gunicorn.py
Created June 11, 2013 04:34
Config for gunicorn on Heroku. Fixes HTTPS(wsgi.url_scheme, Django's request.is_secure() and Flask's request.is_secure) and REMOTE_ADDR
forwarded_allow_ips = '*'
x_forwarded_for_header = 'X-FORWARDED-FOR'
secure_scheme_headers = {
'X-FORWARDED-PROTO': 'https',
}