Skip to content

Instantly share code, notes, and snippets.

@ratpik
ratpik / basic_auth.py
Created December 30, 2013 13:33
Enable Basic Authetication in your django views. Useful when you have a ajax view accessed by a client that is csrf exempt on HTTP POST
import base64
from django.contrib.auth import authenticate
'''
In your views you can call has_basic_auth(request) to check if request headers have a valid Authorization header or not.
Returns False if header is absent or based on invalid username/password combination.
Handle the result in your view accordingly
@ratpik
ratpik / extra_data_resource.py
Created January 24, 2014 09:44
Add this to the tastypie model to extend the modelresource with other nested resources
def dehydrate(self, bundle):
x = bundle.obj.get_x()
ur = UserResource()
ur_bundle = ur.build_bundle(obj=x, request=bundle.request)
bundle.data['x'] = ur.full_dehydrate(ur_bundle)
return bundle
@ratpik
ratpik / dehydrate_metadata.py
Last active August 29, 2015 13:56
De-serialize json stored as unicode back to JSON for Tastypie
import json, ast
def dehydrate_metadata(self, bundle):
metadata_dict = ast.literal_eval(bundle.data['metadata'])
metadata_dict_str = dict([(str(k), str(v)) for k, v in metadata_dict.items()])
return json.dumps(metadata_dict_str)
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Checkbox grid" />
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
@ratpik
ratpik / 0_reuse_code.js
Created August 19, 2014 12:45
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@ratpik
ratpik / MainActivity.java
Last active July 9, 2018 05:56
PayTM Android Integration
package com.drc.paytm_example;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
@ratpik
ratpik / trie.py
Last active October 13, 2016 13:54
Trie Implementation - A way to search for strings and prefixes given a list of strings
class Node:
def __init__(self, key=None, word_count=0):
self.key = key
self.word_count = word_count #Since word and prefix counts are same
self.edges = [] #List of Nodes
class Trie:
def __init__(self, root=None):
self.root = root
@ratpik
ratpik / BST_traversal.py
Created October 22, 2016 07:06
Spiral Traversal of a Binary Search Tree
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BST:
def __init__(self, root=None):
@ratpik
ratpik / custom_string.js
Last active March 5, 2017 12:17
JS BinRepresentation of a custom String Object// source https://jsbin.com/jibuso
var MyString = function(source){
var arr = [];
for(var index in source){
arr.push(source[index]);
//Public property
this[index] = source[index];
}
//Public property
@ratpik
ratpik / async_recursion_sample.js
Created March 27, 2017 17:01
Async With Recursion
it.only('Async should work with Recursion', function (done) {
var n = 10;
var f = function(callback) {
async.waterfall([
function (cb) {
n--;
cb(null, n);
},
function (b, cb) {