Skip to content

Instantly share code, notes, and snippets.

View jawache's full-sized avatar

Asim Hussain jawache

View GitHub Profile
@jawache
jawache / facebookauthentication.py
Created November 10, 2012 00:06
GETAPIFacebookTokenAuthenticationResource
class GETAPIFacebookTokenAuthenticationResource(ModelResource):
"""
Authenticates the user via facebook and returns an APIToken for them.
"""
class Meta(object):
queryset = ApiKey.objects.all()
resource_name = 'authenticate'
fields = ['user', 'key']
allowed_methods = ['get']
/**
* Stuff
*/
body {
font-family: helvetica;
font-size: 12px;
line-height: 28px;
}
#top {
@jawache
jawache / ratelimit.py
Created March 13, 2013 12:22
Naive rate limiter using django cache backend Rate limits in hour buckets
# -*- coding: utf-8 -*-
import logging
from django.core.cache import cache
from django.utils.datetime_safe import datetime
LOGGER = logging.getLogger(__name__)
class RateLimit(object):
'''
Naive rate limiting helper class.
@jawache
jawache / email.py
Created March 13, 2013 12:24
Django email helper module, uses the temaplating engine to generate both the HTML and PLAIN text emails based on a context provided
import logging
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.template import Context, TemplateDoesNotExist
from django.template.loader import get_template
from django.utils.html import strip_tags
import re
from os.path import commonprefix
@jawache
jawache / base.html
Created March 20, 2013 17:24
Using PIL on Google App Engine to manipulate images and store inthe blobstore
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb">
<head>
<title>Hello World</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
{% block content %}
{% endblock %}
</body>
@jawache
jawache / 0_reuse_code.js
Created October 28, 2013 12:33
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
class SignalModel(models.Model):
"""A Model mixin class that lets you put your signal handler methods back into your Model class."""
@classmethod
def _sig_pre_delete(cls, instance, *args, **kwargs):
"""dispatch the pre_delete method to a regular instance method. """
return instance.sig_pre_delete(*args, **kwargs)
@classmethod
def _sig_post_delete(cls, instance, *args, **kwargs):
def solution(N):
# 1. convert number to binary
# N > 0
# 2. figure out binary period.
# P > 0
# P <= Q/2
# Loop through all values of P until you find one.
words = set()
binStr = bin(N)[2:]
@jawache
jawache / convert-canvas-to-jpeg.coffee
Created November 15, 2014 18:27
How to convert a canvas element to a jpeg
# $(canvas) - is the canvas element we want to export
# svgfix - https://code.google.com/p/svgfix/
svg = svgfix($(canvas).html())
buffer = document.createElement('canvas');
buffer.width = width
buffer.height = height
# canvg - https://code.google.com/p/canvg/
canvg(buffer, svg)
window.setTimeout ->
# Give some time for the canvas to be "drawn" so we can export it
@jawache
jawache / api.py
Created October 1, 2015 22:49
Gist of YelpCoffee Django API
from httplib import HTTPResponse
import json
import logging
import urllib2
from django.conf import settings
from django.http import HttpResponse
import oauth2
from rest_framework.exceptions import PermissionDenied
from rest_framework.filters import SearchFilter, OrderingFilter, DjangoFilterBackend
from rest_framework.views import APIView