Skip to content

Instantly share code, notes, and snippets.

View walkness's full-sized avatar

Walker Angell walkness

View GitHub Profile
@walkness
walkness / PasswordField.py
Last active August 3, 2016 21:18
Django Rest Framework PasswordField
from rest_framework import serializers
from django.contrib.auth import password_validation
class PasswordField(serializers.CharField):
def __init__(self, *args, **kwargs):
kwargs['write_only'] = True
kwargs['style'] = {'input_type': 'password'}
self.validate_password = kwargs.pop('validate_password', True)
@walkness
walkness / processServerError.js
Last active January 11, 2017 20:10
Simple function to process DRF errors in JS
function processServerError(error, fields) {
const fieldErrors = {};
let otherErrors = [];
if (typeof error === 'object') {
for (const field of fields) {
const value = error[field];
if (value) {
if (value.constructor === {}.constructor) {
Object.keys(value).forEach(key => {
fieldErrors[`${field}.${key}`] = value[key];
@walkness
walkness / DRFRequestMaybeInitializedMixin.py
Last active January 18, 2017 18:32
Simple mixin for DRF generic viewsets to prevent RawPostDataException error
from rest_framework.request import Request
class RequestMaybeInitializedMixin(object):
def initialize_request(self, request, *args, **kwargs):
drf_request = super(
RequestMaybeInitializedMixin, self).initialize_request(
request, *args, **kwargs)
@walkness
walkness / query.py
Last active June 28, 2022 12:25
Django regex replace for PostgreSQL
from django.db.models import Func, F, Value
from images.models import Image
Image.objects.all().update(original_filename=Func(F('file'), Value('^.*\/(.*)$'), Value('\\1'), function='regexp_replace'))
@walkness
walkness / domainsFromHar.js
Last active May 4, 2020 23:47
Provides a simple way to extract a list of unique domains from a HAR file (e.g. as downloadable from Chrome dev tools)
const fs = require('fs')
const urlLib = require('url')
function domainsFromHar(filePath) {
const rawData = fs.readFileSync(filePath)
const json = rawData && JSON.parse(rawData)
const { log } = json || {}
if (!log) return
const { pages, entries } = log
@walkness
walkness / importS3Resources.js
Last active May 22, 2023 21:30
Generates Terraform Import Commands for v4 AWS S3 Bucket Configuration Changes
const fs = require("fs");
const rawData = fs.readFileSync("plan.json");
const data = JSON.parse(rawData);
const changes = data.resource_changes.filter(
(c) =>
c.change.actions.indexOf("create") !== -1 &&
c.type.startsWith("aws_s3_bucket_")