Skip to content

Instantly share code, notes, and snippets.

@aaugustin
aaugustin / djangoauth.py
Created July 29, 2014 12:49
WSGI middleware that authenticates against a Django user database.
"""
WSGI middleware that authenticates against a Django user database.
DJANGO_SETTINGS_MODULE should point to a valid Django settings module.
In addition, the following settings are available:
- BASIC_AUTH_LOGIN_URL: DjangoAuth will trigger basic authentication on this
URL. Since browsers only propagate auth to resources on the same level or
below, this URL will usually be '/<something>' without a trailing slash.
<!DOCTYPE html>
<html>
<head>
<title>Vega - bug 259</title>
<script src="http://vega.github.io/vega/lib/d3.v3.min.js"></script>
<script src="http://vega.github.io/vega/vega.min.js"></script>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript">
@aaugustin
aaugustin / gist:3012318
Created June 28, 2012 16:29
Quick'n'dirty faster test runner for Django (not tested)
from optparse import make_option
from django.test.simple import DjangoTestSuiteRunner
def fake_create_test_db(self, verbosity=1, autoclobber=False):
"""Simplified version of BaseDatabaseCreation.create_test_db."""
test_database_name = self._get_test_db_name()
if verbosity >= 1:
@aaugustin
aaugustin / gist:3164559
Created July 23, 2012 16:25
Migrate from CMS_REDIRECTS=True to django.contrib.redirects
from cms.models import Title
from django.contrib.redirects.models import Redirect
titles_with_redirect = Title.objects.exclude(redirect__isnull=True).exclude(redirect=u'')
for title in titles_with_redirect.select_related('page__site'):
Redirect.objects.create(
site=title.page.site,
old_path=u'/%s/' % title.path,
new_path=title.redirect,
@aaugustin
aaugustin / build_svn_to_git_mapping.py
Created July 26, 2012 08:52
Script used to create the svn_to_git.py file for code.djangoproject.com
"""Build a mapping between svn commits and git changesets.
Usage: python build_svn_to_git_mapping.py > svn_to_git.py
This script should be run in a clone of the git repository,
with a checkout of https://code.djangoproject.com/svn/django in ../django-svn.
"""
import os
import pprint
#!/usr/bin/env python3.4
"""Git pre-commit hook"""
import os
import subprocess
import sys
def run_linter(cmd, files):
@aaugustin
aaugustin / gameaboutsquares.py
Last active November 4, 2015 05:02
Quick'n'dirty solver for http://gameaboutsquares.com/.
#!/usr/bin/env python
# Copyright (c) 2014 Aymeric Augustin
# Released under the WTFPLv2 - http://www.wtfpl.net/txt/copying/
"""
Quick'n'dirty solver for http://gameaboutsquares.com/.
Install requests or save http://gameaboutsquares.com/game.c.js next to this
file. Then run: python gameaboutsquares.py. Tested with Python 2.7 and 3.4.
@aaugustin
aaugustin / i18n.py
Created December 13, 2011 15:05
Cache Django's javascript_catalog
import datetime
import hashlib
from django.views.decorators.cache import cache_page
from django.views.decorators.http import condition
from django.views.i18n import javascript_catalog
def get_version():
# Write your own! That depends on your deployment strategy.
# This example won't work if you release more than once a day.
@aaugustin
aaugustin / dates.js
Created April 21, 2017 09:31
Date arithmetic in JavaScript
export function addYears(date, numYears) {
// Make a copy to avoid mutating the date argument
date = new Date(date.getTime())
// If adding years to a February 29th ends up in a non-leap year, this
// returns March 1st, which is the expected result.
date.setFullYear(date.getFullYear() + numYears)
// DST can also mess with us. Thanks, JavaScript, for not having dates.
if (date.getHours() === 23) {
@aaugustin
aaugustin / isValidDate.js
Created February 15, 2017 15:12
isValidDate in JavaScript
export const DAYS_IN_MONTH = [null, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
function daysInMonth(year, month) {
// isValidDate checked that year and month are integers already.
// February of leap years. Assumes that the Gregorian calendar extends
// infinitely in the future and in the past.
if (month === 2 && (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0))) {
return 29
}