Skip to content

Instantly share code, notes, and snippets.

@goldhand
goldhand / Django + Ajax dynamic forms .py
Last active September 29, 2023 06:32
Django form with Ajax. A simple Task model that can be updated using a CBV with an AJAX mixin. The view sends post data with ajax then updates the view with a callback to a DetailView with a json mixin.There is an abstract CBV, AjaxableResponseMixin, based on the example form django docs, that is subclassed in the TaskUpdateView CBV. TaskUpdateV…
#models.py
class Task(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
def __unicode__(self):
return self.title
@goldhand
goldhand / SVGPath.React.js
Created July 12, 2016 20:42
An svg <path> react component
import React, {Component, PropTypes} from 'react';
const DEFAULT_STROKE_WIDTH = 0.15;
const DEFAULT_STROKE_COLOR = '#e1e1e1';
/**
* SVGPath is an svg <path> element with utitlities
*
* @param {object[]} points - Array of Point objects - {x, y} - to plot this path
* @param {string} color - stroke color of path
@goldhand
goldhand / parseHtmlStyles.js
Last active March 18, 2023 16:28
Parse html style string for a react component
/**
* @function parseStyles
* Parses a string of inline styles into a javascript object with casing for react
*
* @param {string} styles
* @returns {Object}
*/
const parseStyles = styles => styles
.split(';')
.filter(style => style.split(':')[0] && style.split(':')[1])
@goldhand
goldhand / staticencoder.py
Last active May 17, 2022 21:00
Django template tag for encoding images in base64 and rendering with server
from django import template
from django.contrib.staticfiles.finders import find as find_static_file
from django.conf import settings
register = template.Library()
@register.simple_tag
def encode_static(path, encoding='base64', file_type='image'):
"""
@goldhand
goldhand / :nth-child React Component Factory .js
Created June 9, 2016 19:56
Factory for creating React components with nth-child css selector like properties.
import React, {Component, PropTypes} from 'react';
/**
* createNthChild is a factory for NthChild components.
*
* Can set conditional styling meant to simulate css's nth-child pseudo selector
*
* @param {object} [options] - styles options to pass into the icon panel
* @param {function} [styleFirst] - if isFirst, invoke styleLast(options.styles)
@goldhand
goldhand / immutableMove.js
Created March 8, 2017 00:52
Immutable move item in an array
/**
* Immutable move item
*/
const move = (arr, from, to) => {
const clone = [...arr];
Array.prototype.splice.call(clone, to, 0,
Array.prototype.splice.call(clone, from, 1)[0]
);
return clone;
};
@goldhand
goldhand / compose.js
Created December 13, 2017 19:22
ATA JS (Day 2) Functions - Daily Assignment Solutions reviewed in class
const reduce = require("./reduce");
/**
* Performs right-to-left function composition. The rightmost function may have
* any arity; the remaining functions must be unary.
* compose(f, g)(x) >> f(g(x))
*
* @example
* const doubleNegative = compose(x => x * -1, x => x * 2);
* doubleNegative(5); // -10
@goldhand
goldhand / rootReducer.js
Created June 30, 2016 17:40
Redux root reducer that uses webpack's require.context to create a rootReducer that dynamically imports reducers in a directory
// this file should actually be in reducers/index.js
import {combineReducers} from 'redux';
const rootReducer = combineReducers(
allReducers(require.context('.', false, /^\.\/(?!index)\w+$/))
);
export default rootReducer;
@goldhand
goldhand / spawn.js
Last active January 8, 2019 22:18
Use a generator to make async stuff look sync.
/**
* Turns generators into async demons
*
* Within the generator function, any "yield" operator becomes enhanced with async powers
* allowing them to yield promises.
*
* @example
* spawn(function *() {
* const data = yield fetch('/foo.json'); // will be resolved and assigned to "data" var
* console.log(data); // write like its sync but its acually async ;)
@goldhand
goldhand / resolveModuleFromLocations.js
Created January 1, 2019 02:00
Resolves a module given multiple locations.
import * as R from 'ramda';
const resolveModuleFromLocation = R.curry((location, next) => {
try {
return require.resolve(location) && require(location);
} catch (err) {
if (next && err && err.code === 'MODULE_NOT_FOUND') {
return next();
}
}