Skip to content

Instantly share code, notes, and snippets.

View adamchainz's full-sized avatar
🍐
I like pears

Adam Johnson adamchainz

🍐
I like pears
View GitHub Profile
document.addEventListener('DOMContentLoaded', () => {
const ol = document.querySelector('[data-candidates-toc]')
// Get or create a random seed stored in session storage
let seed = sessionStorage.getItem('dsf-board-candidates-2026-shuffle-seed')
if (!seed) {
seed = Math.random()
sessionStorage.setItem('dsf-board-candidates-2026-shuffle-seed', seed)
} else {
seed = parseFloat(seed)
@adamchainz
adamchainz / log.md
Created October 26, 2025 13:49
`Vary: Accept-Encoding` test

User (checkpoint)

I have a question around whether it's correct to send Vary: Accept-Encoding in the case that a (Django) middleware compresses a response, realizes that made it larger, and sends the uncompressed version instead.

  1. Give your brief opinion based on knowledge of the HTTP specs and web browser and client practices.

  2. Test nginx and caddy's behaviour here - run them under Docker with basic config that sends back 1000 random bytes, with gzip compression on, and see if they add the Vary clause.

Assistant

#!/usr/bin/env uv run
# /// script
# requires-python = ">=3.13"
# ///
import json
import subprocess
def main(argv=None) -> int:
query = """
@adamchainz
adamchainz / middleware.py
Created April 3, 2023 19:46
login required middleware
class LoginRequiredMiddleware:
def __init__(self, get_response: GetResponse) -> None:
self.get_response = get_response
def __call__(self, request: HttpRequest) -> HttpResponse:
return self.get_response(request)
def process_view(
self,
request: HttpRequest,
@adamchainz
adamchainz / migrate_postgres_serial_columns.py
Created October 18, 2022 14:42
Django 4.1 serial to identity migration script, alternative version
"""
Alternative version that uses internal-poking technique from:
https://www.enterprisedb.com/blog/postgresql-10-identity-columns-explained
"""
from __future__ import annotations
import argparse
from typing import Any
from django.core.management.base import BaseCommand
@adamchainz
adamchainz / authlib.patch
Created September 14, 2022 08:32
authlib 1.0.1 to 1.1.0 wheel diff
diff --git a/Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/LICENSE b/Users/chainz/Downloads/Authlib-1.1.0-py2.py3-none-any/Authlib-1.1.0.dist-info/LICENSE
similarity index 100%
rename from /Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/LICENSE
rename to /Users/chainz/Downloads/Authlib-1.1.0-py2.py3-none-any/Authlib-1.1.0.dist-info/LICENSE
diff --git a/Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/METADATA b/Users/chainz/Downloads/Authlib-1.1.0-py2.py3-none-any/Authlib-1.1.0.dist-info/METADATA
similarity index 99%
rename from /Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/METADATA
rename to /Users/chainz/Downloads/Authlib-1.1.0-py2.py3-none-any/Authlib-1.1.0.dist-info/METADATA
index da8bbee..58ce39e 100644
--- a/Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/METADATA
@adamchainz
adamchainz / test_migrations.py
Last active June 29, 2023 14:25
Django test for pending migrations
from io import StringIO
from django.core.management import call_command
from django.test import TestCase
class PendingMigrationsTests(TestCase):
def test_no_pending_migrations(self):
out = StringIO()
try:
call_command(
@adamchainz
adamchainz / get_line_offsets.py
Last active June 17, 2022 14:01
Will McGugan's get_line_offsets challenge
def adam_get_line_offsets(text: str) -> list[int]:
offsets = [0]
n = 0
text_index = text.index
offsets_append = offsets.append
while True:
try:
n = text_index('\n', n) + 1
except ValueError:
break
@adamchainz
adamchainz / fix_curly_quotes.py
Created May 30, 2022 15:14
Curly quote fixer script
#!/usr/bin/env python
import argparse
def main(argv=None):
parser = argparse.ArgumentParser(
description="Replace curly quotes with straight versions."
)
parser.add_argument(
"file",