Skip to content

Instantly share code, notes, and snippets.

View AndrewIngram's full-sized avatar
🦊
Being foxy

Andy Ingram AndrewIngram

🦊
Being foxy
View GitHub Profile
@AndrewIngram
AndrewIngram / letting_agent.py
Last active January 24, 2023 14:41
Django Queryset capabilites example
"""
This snippet will return all the letting agents, bucketed into discrete bands based on distance from
an origin point (for example, the user's current location), then sorted alphabetically within each
band. The goal being to prioritise agents in close proximity, but not excessively so -- all agents
within the same band should be given equal distance-related priority.
If there's a search term, we then boost the distance score based on the name's similiarity to the
search query.
"""
qs = LettingAgentLocation.objects.select_related("agent").filter(postcode__isnull=False)
@AndrewIngram
AndrewIngram / utils.ts
Created October 27, 2022 18:25
Fix object returned from GraphQL-js to work without intermediate serialization
import { mergeWith } from "lodash";
export function nullPrototypeFix(object: Object): Object {
return mergeWith({}, object, (a: any, b: any) => {
if (b === null || typeof b === "undefined") {
return b;
} else if (typeof b.toJSON == "function") {
return b.toJSON();
}
});
@AndrewIngram
AndrewIngram / toggle.jsx
Last active October 27, 2023 16:05
Naive sketch of mutation API
function getIsLiked(viewerId, entityId) {
// hit a database or something
}
function toggleLikeStatus(viewerId, entityId) {
// hit a database or something
return !currentStatus;
}
@AndrewIngram
AndrewIngram / usage.py
Last active June 30, 2022 11:57
Python Entity or ID Pattern
def my_fun(user_or_id: UserOrId):
user = get_user(user_or_id)
# Do something with user
# These both do the same thing (but the first avoids the extra db hit)
my_fun(user)
my_fun(user.id)
@AndrewIngram
AndrewIngram / session.js
Created February 18, 2022 13:29
next-runtime session middleware
import { json } from "next-runtime";
import jwt from "jsonwebtoken";
const SESSION_KEY = "some-secret";
export function readSession(cookies) {
const sessionCookie = cookies.get("session");
let data = {};
@AndrewIngram
AndrewIngram / utils.ts
Created February 2, 2022 18:22
Apply bitmask and shift
/*
* Given an input and a mask, will return just part we care about
*
* e.g.
* input: 0b11101111
* mask: 0b00110000
* returns: 0b10
*/
function maskedValue(input: number, mask: number): number {
return (input & mask) >> Math.log2(mask & -mask);
@AndrewIngram
AndrewIngram / example.js
Created January 20, 2022 10:39
Stable UUID hack
import { v5 as uuidv5, NIL } from 'uuid';
function stableUuid(namespace, name) {
return uuidv5(name, uuidv5(namespace, NIL))
}
// > stableUuid('Some namespace', 'some name')
// '019761ac-2321-5ae0-9086-70c9557bcc58'
// > stableUuid('Some namespace', 'some name')
// '019761ac-2321-5ae0-9086-70c9557bcc58'
@AndrewIngram
AndrewIngram / formData.js
Created January 18, 2022 22:26
Request/Response form handling with Next.js
import formidable from "formidable";
export default function formData(request) {
const form = formidable({ multiples: true });
return new Promise((resolve, reject) => {
form.parse(request, (err, fields, files) => {
if (err) {
reject(err);
return;
@AndrewIngram
AndrewIngram / gradent.css
Created January 12, 2022 09:48
Gradient thing
.container {
background-color: hsl(346deg, 83%, 51%);
background-image: linear-gradient(
45deg,
hsla(240deg, 100%, 20%, 1) 0%,
hsla(240deg, 100%, 20%, 0) 50%
),
linear-gradient(
45deg,
hsla(55deg, 100%, 50%, 0) 50%,
@AndrewIngram
AndrewIngram / utils.py
Created December 10, 2021 11:43
Django on_save callback
import logging
from django.db import models, transaction
from django.db.models.signals import post_save
logger = logging.getLogger(__name__)
def on_save(obj: models.Model, callback):
"""
Attaches a one-off callback that's triggered after a Django model instance is