Skip to content

Instantly share code, notes, and snippets.

@SeanHayes
SeanHayes / settings.py
Created January 6, 2010 01:31
Test Settings/Test Database in Django
#normal settings.py stuff should go above this comment
#if manage.py test was called, use test settings
if 'test' in sys.argv:
try:
from test_settings import *
except ImportError:
pass
@SeanHayes
SeanHayes / website
Created July 2, 2010 18:19
Example Apache config file for enabling HTTP & HTTPS on the same website
#NOTE: This is only used on my laptop for development purposes.
#HTTP config
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName website.localhost
DocumentRoot /web/website
<Directory />
Options FollowSymLinks
@SeanHayes
SeanHayes / signRequest.pseudo_code
Created July 3, 2010 01:21
Pseudo code for signing OAuth requests
function urlEncode(str){
//everything except [a-zA-Z0-9-_~.] must be percent encoded: http://tools.ietf.org/html/rfc5849#section-3.6
}
function hmac_sha1(key, text){
//you'll definitely want a library for this. I used Crypto-JS: http://code.google.com/p/crypto-js/
}
function base64me(key, text){
//I used Crypto-JS for this too.
}
/*
@SeanHayes
SeanHayes / gist:3063179
Created July 6, 2012 22:50
Stop Flash Content from Overlapping Fancybox
$('.fancybox').fancybox({
onClosed: function(){
$('iframe').css('visibility', '');
},
onStart: function(){
$('iframe').css('visibility', 'hidden');
}
});
@SeanHayes
SeanHayes / api.py
Created July 16, 2012 17:43
Custom TastyPie resource that returns the correct HTTP status code when
from tastypie import http
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.resources import ModelResource
class CustomModelResource(ModelResource):
def obj_get(self, request=None, **kwargs):
try:
return super(CustomModelResource, self).obj_get(request=request, **kwargs)
except ObjectDoesNotExist:
# only branch if kwargs were supplied, which indicates filtering
@SeanHayes
SeanHayes / api.py
Created July 16, 2012 18:04
TastyPie: Return 400 Bad Request when deserialization fails
from tastypie import http
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.resources import ModelResource
class CustomModelResource(ModelResource):
def deserialize(self, request, data, format='application/json'):
try:
return super(CustomModelResource, self).deserialize(request, data, format=format)
except Exception as e:
# if an exception occurred here it must be due to deserialization
@SeanHayes
SeanHayes / test.py
Created November 28, 2012 06:16
Mock example usage
def mocked_urlopen_side_effect(url, *args, **kwargs):
"Returns string content in a file-like interface for a variety of possible URLs."
#StringIO is a file-like object, much like we'd expect urlopen to return
#responses is a dict (defined elsewhere) containing various string responses as the values for the keys.
if 'http://search.twitter.com/search.json' in url:
return StringIO(responses['twitter_success_1'])
elif 'http://api.twitter.com/1/geo/search.json' in url:
return StringIO(responses['twitter_geo_success_1'])
elif 'http://api.flickr.com/services/rest/' in url:
@SeanHayes
SeanHayes / fabfile.py
Last active September 27, 2020 20:10
Example Fabric fabfile.py. It's a hodgepodge of old scripts and probably isn't best practice (uWSGI ought to have an init script), but it should give you a practical idea of how to use Fabric. This fabfile relies heavily on custom settings from a Django project in order to be more DRY. Includes commands for setting up a fresh Ubuntu Server insta…
# -*- coding: utf-8 -*-
#Copyright (C) 2013 Seán Hayes
import my_project.settings as dj_settings
from fabric.api import local, run, sudo, env, prompt, settings, cd, parallel, execute
from fabric.contrib.files import exists
from fabric.decorators import hosts, roles, runs_once
import json
import logging
import os
@SeanHayes
SeanHayes / signed_upload_resource.py
Last active April 10, 2016 22:23
Example of a Tastypie Resource which returns a signed upload URL for AWS S3
import base64
from datetime import timedelta
import hashlib
import hmac
import json
import mimetypes
import re
from django.conf import settings
from django.utils import timezone