Skip to content

Instantly share code, notes, and snippets.

View richtier's full-sized avatar

Richard Tier richtier

View GitHub Profile
@richtier
richtier / ajax and CSRF
Last active December 25, 2015 22:09
I was asked "I am running into the 403 forbidden issue with django and ajax and was wondering if you could help me a little."
(note, the concept definitely works, but the implementation may have some bug, spelling mistakes etc).
Your problem is your request is missing the Cross Site Request Forgery token (CSRF).
This is a precaution django provide automatically to prevent others doing nefarious stuff.
Non-POST requests don't need the CSRF token, but for POST we need to do some magic:
we need to pass the CSRF token from django to javascript. This is best done like so:
@richtier
richtier / querrystring to object
Last active December 30, 2015 21:29
querrystring to object
/**
* converts querystring to object. Allows multiple values per key
* @param {String} querystring - optional querystring. default address bar
* @return {object}
*/
self.querystringToObject = function(querystring){
var params = {};
(querystring || window.location.search)
.replace(/([^?=&]+)(=([^&]*))?/g, function($0, $1, $2, $3) {
if (params[$1]){
@richtier
richtier / jquery templating
Created January 8, 2014 01:48
quick and dirty jquery templating
// render playlist of songs
var template = '<li/><a>{{title}} by {{artist}}</a></li>';
context = [
{title: 'Killing Me Softly', artist: 'Roberta Flack'},
{title: 'Run Around Sue', artist: 'Dion'},
{title: 'Will You Still Love Me Tomorrow', artist: 'Shirelles'}
];
$('<ul/>', {text: 'songs'}).html(
$.map(context, function(item){
@richtier
richtier / non-model-serialize
Last active January 3, 2016 14:49
serialize non django model
from rest_framework.views import APIView
from rest_framwrork import serializers
class NonDjangoModelSerializer(serializers.Serializer):
class Meta:
fields = ('field1', 'field1',)
field1 = serializers.Field()
field1 = serializers.Field()
@richtier
richtier / gist:8484200
Created January 18, 2014 00:22
append url with slash if not present
function appendSlash(url){
return url + /\/$/.test(url) ? '' : '/';
}
@richtier
richtier / thing.jsx
Last active April 4, 2016 08:26
react material-ui form field validation redux es6
// NewProductDescriptionField.jsx
import React from 'react';
import { connect } from 'react-redux';
import TextField from 'material-ui/lib/text-field';
import {
setFormFieldValidityMessage,
setNewProductData,
} from '../../actions';
import array
import struct
def float_to_16_bit_pcm(raw_floats):
floats = array.array('f', raw_floats)
samples = [sample * 32767 for sample in floats]
raw_ints = struct.pack("<%dh" % len(samples), *samples)
return raw_ints
@richtier
richtier / javascript.js
Created September 18, 2018 09:24
JavaScript webaudio to wav encoder
class WavAudioEncoder {
encode = buffer => {
const channelData = buffer.getChannelData(0);
const length = buffer.length;
const data = []
for (let i = 0; i < length; ++i) {
let x = channelData[i] * 0x7fff;
data.push(Math.round(x < 0 ? Math.max(x, -0x8000) : Math.min(x, 0x7fff)));
}
Your client-side logging is turned on in prod. the logic I see is:
```js
const isDevEnv = () => {
return !!(global.NutmegConfig && global.NutmegConfig.ENVIRONMENT !== 'production');
};
export const logger = ({ interactionPath, properties }, subst = 'info') => {
if (isDevEnv()) {
// do the logging