Skip to content

Instantly share code, notes, and snippets.

@defrex
defrex / jquery.500frame.js
Created April 11, 2011 19:06
pop-up any 500s in an iframe using jQuery. Especially useful for Django errors.
$(document).bind('ajaxError', function(e, jqXHR){
if (jqXHR.status == 500){
var erframe = document.createElement('iframe');
$('body').append(erframe);
$(erframe).css({
'position': 'absolute',
'top': '5%', 'left': '50%',
'width': '90%', 'height': '90%',
'marginLeft': '-45%'
}).attr('id', 'errorframe');
@defrex
defrex / pretty_request.py
Last active September 8, 2023 14:33
A simple function to print a Django request the way requests are meant to be printed.
def pretty_request(request):
headers = ''
for header, value in request.META.items():
if not header.startswith('HTTP'):
continue
header = '-'.join([h.capitalize() for h in header[5:].lower().split('_')])
headers += '{}: {}\n'.format(header, value)
return (
@defrex
defrex / markup.py
Created August 9, 2013 15:23
`django.contrib.markup` is deprecated in Django 1.5. Here is a simple replacement for the markdown filter.
import markdown as mkdn
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter()
def markdown(value):
function parseUrl(url) {
var urlParser = document.createElement('a');
urlParser.href = url;
return {
hostname: urlParser.hostname,
pathname: urlParser.pathname,
search: urlParser.search,
hash: urlParser.hash,
port: urlParser.port,
@defrex
defrex / gist:10008795
Created April 6, 2014 17:07
Keybase Proof

Keybase proof

I hereby claim:

  • I am defrex on github.
  • I am defrex (https://keybase.io/defrex) on keybase.
  • I have a public key whose fingerprint is 1486 B752 395C B552 C0DF 8604 D2BB 926C 932E D7E4

To claim this, I am signing this object:

from storages.backends.s3boto import S3BotoStorage
class StaticS3BotoStorage(S3BotoStorage):
location = 'static'
class MediaS3BotoStorage(S3BotoStorage):
location = 'media'
@defrex
defrex / shell.py
Created September 30, 2014 19:14
Add ptpython support to Django's shell command
from django.core.management.commands.shell import Command as ShellCommand
class Command(ShellCommand):
shells = ['ptpython', 'ipython', 'bpython']
def ptpython(self):
from prompt_toolkit.contrib.repl import embed
embed(globals(), locals(), vi_mode=False, history_filename=None)
@defrex
defrex / print_request.py
Created January 20, 2015 22:01
A couple utility functions for pretty printing Django request and response objects
from __future__ import unicode_literals, print_function
import re
import colorama
header_regex = re.compile('^HTTP_')
def print_header(key, value):
{
"title": "Spendwell JS Test",
"posts": [
{
"title": "Power",
"gif": "http://i.giphy.com/WmDxBPSHdEBtS.gif"
},
{
"title": "Disgusting",
"text": "http://i.giphy.com/xThuWgQbkshXOAlUHu.gif"
@defrex
defrex / Repitition.test.ts
Created January 21, 2020 22:14
TypeORM Repitition Model: encodes an arbitrary repeating period
import { subDays, subWeeks } from 'date-fns'
import { Repitition } from './Repitition'
describe('Repitition', () => {
test('findByDate', async () => {
const base = new Date()
const twoWeeksAgo = subWeeks(base, 2)
const twoWeeksOneDayAgo = subDays(twoWeeksAgo, 1)
const weeklyStartingTwoWeeksAgo = await Repitition.create({