Skip to content

Instantly share code, notes, and snippets.

@ruiwen
ruiwen / UtilMixIn.py
Created May 9, 2011 19:45
UtilMixIn with JSON renderer for Django models
# Utility classes
class UtilMixIn():
def __json_render_field(self, f, only_fields=[], mapped_fields={}):
field = self._meta.get_field_by_name(f)[0]
if isinstance(field, models.fields.DateTimeField):
return time.mktime(getattr(self, f).timetuple())
elif isinstance(field, models.fields.related.ForeignKey):
@ruiwen
ruiwen / utils
Created August 28, 2011 13:59
Utility methods for Django
# Various utility functions
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
from django.template import RequestContext, Context, loader
from django.conf import settings
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate, login as django_login
from django.db import models
import json
@ruiwen
ruiwen / console.log shim
Created November 6, 2011 13:21
Silence window.console.log on demand
/**
*
* Neuters console.log, and makes its output dependent on the global boolean DEBUG
*
**/
window.DEBUG = false;
window.console._log = window.console.log; // Make sure we have a pointer to the original function
window.console.log = function(str) {
@ruiwen
ruiwen / setup_init.sh
Created February 8, 2012 13:13
Server setup for Django / uWSGI / nginx
#!/bin/bash
# Check if we're root
if [ 0 -ne `id -u` ]; then
id -u
echo "This script needs to be run as root. Exiting."
exit 1
fi
# Add uwsgi and nginx repositories
@ruiwen
ruiwen / django-piston POSTPUT
Created February 13, 2012 15:38
Enable django-piston to POST/PUT models with ForeignKeys
def flatten_dict(self, dct):
out = {}
for field in dct.keys():
fk = self.model._meta.get_field(field)
if isinstance(fk, ForeignKey):
# Determine the target model of the ForeignKey
Klass = fk.rel.to().__class__
d = { Klass._meta.pk.name : dct[field] }
# Grab the instance
i = Klass.objects.get(**d)
@ruiwen
ruiwen / KeychainItemWrapper.h
Created May 19, 2012 05:13 — forked from dhoerl/KeychainItemWrapper.h
KeychainItemWrapper ARCified - saves NSString, NSArray, NSDictionary
/*
File: KeychainItemWrapper.h
Abstract:
Objective-C wrapper for accessing a single keychain item.
Version: 1.2 - ARCified
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
@ruiwen
ruiwen / RCDirectionalPanGestureRecognizer.h
Last active October 6, 2015 10:27
DirectionalPanGestureRecognizer for iOS
//
// RCDirectionalPanRecognizer.h
//
// Created by Ruiwen Chua on 6/23/12.
// Copyright (c) 2012 thoughtmonkeys.
//
// Reference taken from http://stackoverflow.com/a/7149691
#import <UIKit/UIKit.h>
@ruiwen
ruiwen / timer.py
Created October 2, 2012 14:44
Handy timer for Python functions
# From http://stackoverflow.com/a/1685337
from __future__ import with_statement
import time
class Timer(object):
def __init__(self, autoprint=False):
self.autoprint = autoprint
@ruiwen
ruiwen / parseProfile.py
Created October 3, 2012 12:43
Map-reduce in with MongoDB with PyMongo for Parse timing calls
'''
Sample object:
{
"url": "/1/functions/Klass3",
"_id": {
"$oid": "506c082701d5410ec7000002"
},
"log": [
{
@ruiwen
ruiwen / recordTransaction.js
Created October 31, 2012 11:51
Balance calculation
function recordTransaction(txn) {
// NON FUNCTIONING CODE
// Let 'friend' be the User's friend, the 'other' party in this Activity/Transaction
// Record the origin
var origin = friend.balance;
// Calculate friend balance
if(role == Activity.CREDITOR) {