Skip to content

Instantly share code, notes, and snippets.

View thomasyip's full-sized avatar

Thomas Y thomasyip

View GitHub Profile
@thomasyip
thomasyip / pictiles
Created February 25, 2017 05:46
A simple html to combine a number of photos into one
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
}
.row span {
display: inline-block;
position: relative;
@thomasyip
thomasyip / gen.sh
Created July 4, 2015 22:52
Generate self-signed alternate dns (wildcard) ssl certificate with a single script
#!/bin/bash
# file: gen.sh
# usage: ./gen.sh [name]
ROOT=rootCA
NAME=${1:-device}
EXTS=( key cnf csr crt )
ROOT_NAME=rootCA
ROOT_EXTS=( key pem )
@thomasyip
thomasyip / Gruntfile.js
Last active August 29, 2015 14:14
Grunt Multi Task: configmerge
module.exports = function(grunt) {
grunt.initConfig({
compass: {
// see configmerge.compass
},
watch: {
// see configmerge.watch
}
configmerge: {
compass: {
@thomasyip
thomasyip / settings.py
Last active August 29, 2015 14:14
django-pipeline configuration (a SASS / Compass folder per app)
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
PIPELINE_COMPILERS = (
'pipeline_compass.compass.CompassCompiler',
)
PIPELINE_CSS = dict(zip(PROJECT_APPS, [{
'source_filenames': (
(app + '/sass/*.scss'),
),
@thomasyip
thomasyip / gist:5eec33abfb4fa4b588f6
Created December 1, 2014 02:57
Python Utilities -- Save a few line of typing
## Print stack trace
def exception_printer(sender, **kwargs):
import sys, traceback
print >> sys.stderr, ''.join(traceback.format_exception(*sys.exc_info()))
raise
## Set breakpoint
import pdb
pdb.set_trace()
@thomasyip
thomasyip / middleware.py
Last active March 17, 2017 11:42
Accept Tastypie API Key authentication for regular django app.
class TastypieApiKeyUserMiddleware(object):
"""
Middleware for per-request authentication with tastypie
"""
# Name of request header to grab username from. This will be the key as
# used in the request.META dictionary, i.e. the normalization of headers to
# all uppercase and the addition of "HTTP_" prefix apply.
header = 'HTTP_AUTHORIZATION'
method = 'apikey'
apikey_auth = ApiKeyAuthentication()
@thomasyip
thomasyip / jQuery Plugin Factory
Last active December 21, 2015 15:28
Skeleton of jQuery plugin
;(function($, undefined) {
var PLUGIN_NAME = 'plugin';
var DEFAULT_OPTIONS = {
optionA: true,
optionB: false,
'callbackC': function() {
}
};
function plugin(options) {
@thomasyip
thomasyip / CORS_ExpressJS
Created March 20, 2013 08:22
Cheatsheet for adding CORS supports to an ExpressJS app. I found that the most reliable way of sending CORS headers is to echo back what the browser sent to the server. Reminder to test it with browser cache disable and cleared at least one.
// CORS
app.use(function(req, res, next) {
if ('OPTIONS' !== req.method) {
if ('origin' in req.headers) {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
}
next();
} else {
var body = '{}\n';
if ('origin' in req.headers) {
@thomasyip
thomasyip / gist:5010926
Created February 22, 2013 05:21
Heroku Cycling
Running 1 dyno. Does running more dynos get some better treament?
```
2013-02-22T04:49:35+00:00 heroku[web.1]: Cycling
2013-02-22T04:49:38+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-02-22T04:49:41+00:00 heroku[web.1]: Process exited with status 143
2013-02-22T04:49:41+00:00 heroku[web.1]: State changed from up to down
2013-02-22T04:49:41+00:00 heroku[web.1]: State changed from down to starting
2013-02-22T04:49:46+00:00 heroku[web.1]: Starting process with command `newrelic-admin run-program python apps/manage.py runserver 0.0.0.0:40284 --noreload`
2013-02-22T04:49:47+00:00 app[web.1]: Validating models...
@thomasyip
thomasyip / YouModelResource.py
Created August 25, 2012 21:35
Custom `hydrate_m2m` method to workaround tastypie bug on creating model with dependent model
# Say, you have models `Invoice` and `InvoicePart`. The `InvoicePart`
# has a non-nullable field of `Invoice`.
#
# Currently (August 2012), `tastypie` will throw an error when you try
# to create (`HTTP Post`) a new `Invoice` that contains one or more new
# `InvoicePart`.
#
# The problem is that when `tastypie` processes the `InvoicePart`, the
# backlink to `Invoice` is not set.
#