Skip to content

Instantly share code, notes, and snippets.

View bryanchow's full-sized avatar

Bryan Chow bryanchow

View GitHub Profile
@bryanchow
bryanchow / querysetmanager.py
Created June 15, 2011 00:18
Django QuerySetManager: Add new QuerySet methods using a Model inner class
# Django Query Set Manager
#
# https://gist.github.com/1026248
from django.db import models
class QuerySetManager(models.Manager):
"""
@bryanchow
bryanchow / middleware.py
Created June 20, 2011 06:08
Django SSL Redirect Middleware - Django middleware for automatically redirecting to HTTPS for specific URLs
# https://gist.github.com/1035190
# See also: Snippets 85, 240, 880 at http://www.djangosnippets.org/
#
# Minimally, the following settings are required:
#
# SSL_ENABLED = True
# SSL_URLS = (
# r'^/some/pattern/',
# )
@bryanchow
bryanchow / spine.storejs.js
Created July 5, 2011 19:18
store.js module for Spine.js
// store.js module for Spine.js
// https://github.com/marcuswestin/store.js
// http://maccman.github.com/spine/
(function(Spine, $){
Spine.Model.StoreJS = {
extended: function() {
@bryanchow
bryanchow / backend.py
Created July 21, 2011 21:00
EmailOrUsernameModelBackend - Django authentication backend for logging in using either username or email address
from django.contrib.auth.models import User
class EmailOrUsernameModelBackend(object):
"""
Custom authentication backend against django.contrib.auth.models.User that
allows logging in using either username or email address. Intended to be
used in conjunction with django.contrib.auth.backends.ModelBackend.
"""
@bryanchow
bryanchow / utcisoformat.py
Created September 5, 2011 20:34
Convert Django DateTimeField values to ISO format in UTC
# Convert Django DateTimeField values to ISO format in UTC
# Useful for making Django DateTimeField values compatible with the
# jquery.localtime plugin.
#
# https://gist.github.com/1195854
from pytz import timezone, utc
from django.conf import settings
@bryanchow
bryanchow / gpg.py
Created October 31, 2011 07:30
Django utility function for encrypting data using GPG
# A simple Django utility function for encrypting data using GnuPG
#
# https://gist.github.com/1327072
import os
import subprocess
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
@bryanchow
bryanchow / export_csv_action.py
Last active June 2, 2019 21:30
Django Export CSV admin action factory
# Django Export CSV admin action factory
# https://gist.github.com/1411467
#
# Based on http://djangosnippets.org/snippets/2020/
# and http://djangosnippets.org/snippets/1697/
#
# Example usage:
#
# class MyUserAdmin(admin.ModelAdmin):
# actions = (
@bryanchow
bryanchow / sha256.js
Created January 20, 2012 20:16
JavaScript SHA-256 implementation
// Modified by bryanchow for namespace control and higher compressibility
// See https://gist.github.com/1649353 for full revision history from original
/*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
* in FIPS 180-2
* Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
@bryanchow
bryanchow / accel_redirect.py
Created February 9, 2012 01:16
Django view for serving files using X-Accel-Redirect
# https://gist.github.com/1776202
from os.path import join
from django.http import HttpResponse
from django.views.static import serve
from django.conf import settings
USE_X_ACCEL_REDIRECT = getattr(settings, 'USE_X_ACCEL_REDIRECT', False)
X_ACCEL_REDIRECT_PREFIX = getattr(settings, 'X_ACCEL_REDIRECT_PREFIX', '')
@bryanchow
bryanchow / jquery.jscrollpane-startstop.js
Created March 3, 2012 01:54
Custom start and stop scrolling events for jQuery.jScrollPane
/*jshint browser:true */
/*global jQuery */
/*
jquery.scrollpane-startstop.js
Custom start and stop scrolling events for jScrollPane
https://gist.github.com/1963544
Usage: