Skip to content

Instantly share code, notes, and snippets.

View marteinn's full-sized avatar
✖️
🥕

Martin Sandström marteinn

✖️
🥕
View GitHub Profile
@marteinn
marteinn / debug_model_admin.py
Created February 25, 2025 06:02
How to debug a ModelAdmin view in Django
from django.contrib import admin
from django.contrib.admin.options import ModelAdmin
from django.db import connection
class MockRequest:
pass
class MockSuperUser:
def has_perm(self, perm, obj=None):
return True
@marteinn
marteinn / pgpy_example.py
Last active February 7, 2025 07:56
Example on how to sign and encrypt messages in PGPY
import pgpy
# Keys
encryption_pubkey, _ = pgpy.PGPKey.from_file('/myapp/pgp-encryption-public-key.asc')
encryption_privkey, _ = pgpy.PGPKey.from_file('/myapp/pgp-encryption-secret-key.pgp')
signing_pubkey, _ = pgpy.PGPKey.from_file('/myapp/pgp-signing-public-key.asc')
signing_privkey, _ = pgpy.PGPKey.from_file('/myapp/pgp-signing-secret-key.pgp')
message = pgpy.PGPMessage.new("My test message")
@marteinn
marteinn / utils.py
Created May 29, 2024 04:33
Convert firebase BatchResponse to a JSON serializable struct
from typing import Dict, List
from firebase_admin.messaging import BatchResponse, SendResponse
def batch_response_to_dict(batch_response: BatchResponse) -> Dict:
responses: List[SendResponse] = batch_response.responses
return {
"responses": [
@marteinn
marteinn / info.md
Created May 6, 2024 04:13
How to solve cache busting issue in django-tinymce 4.0

Having issues with the latest 4.0 version of django-tinymce where assets are not refreshed in the browser?

  1. Make sure you add a cache busting query param to tinymce.min.js
APP_VERSION = "1.0.0"
...
TINYMCE_JS_URL = f"/static/tinymce/tinymce.min.js?app_version={APP_VERSION}"
  1. Add cache suffix so any asset loaded by tinymce gets the same cache busting query.
@marteinn
marteinn / info.md
Created February 13, 2024 13:31
How to add json field to django-constance

How to add a json field to django-constance

  1. Install django-jsonform

    pip install django-jsonform
  2. Add django-jsonform to installed apps

@marteinn
marteinn / list-80.md
Last active July 10, 2024 06:53
Best albums of the 80:s (very opinionated)
  1. Tom Waits - Rain Dogs
  2. Tom Waits - Swordfishtrombones
  3. The The - Soul Mining
  4. Cocteau Twins - Treasure
  5. Talk Talk - Spirit Of Eden
  6. Ryuichi Sakamoto - Merry Christmas Mr.Lawrence
  7. This Heat - Deceit
  8. Waterboys - This Is The Sea
  9. Sonic Youth - Daydream Nation
  10. XTC - Skylarking
@marteinn
marteinn / cookietractor-match.js
Created October 11, 2023 12:37
Match category in cookietractor cookie
var data = "%7B%22user%22:%22bfce3822-d2ce-4327-a813-00e616f4773b%22,%22consents" +
"%22:%5B%22undefined%22,%22necessary%22,%22statistical%22%5D,%22avail" +
"ableConsents%22:%5B%22undefined%22,%22necessary%22,%22marketing%22,%" +
"22statistical%22%5D,%22utc%22:1697017078669%7D"
////////////////////
// Match using regex
////////////////////
var altStatistical1 = /%22consents%22:.*statistical.*%22availableConsents%22/
var altStatistical2 = /\%22consents\%22:.*statistical.*\%22availableConsents\%22/
@marteinn
marteinn / middleware.py
Created July 5, 2023 03:29
Generate a LogEntry every time a user is inspecting a object in the django admin.
import json
import re
from datetime import timedelta
from django.contrib.admin.models import ADDITION, LogEntry
from django.contrib.contenttypes.models import ContentType
from django.utils import timezone
admin_change_path_match = r"^admin:([a-z_]*)_([a-z]*)_change$"
@marteinn
marteinn / test.js
Created January 30, 2023 13:59
Read local file from jpg, pass to gotenberg libreoffice and then download result as pdf
const fs = require('fs/promises');
const { createWriteStream } = require("fs");
const util = require('util');
const stream = require('stream');
const got = require('got'); // third party
const FormData = require('form-data'); // third party
const pipeline = util.promisify(stream.pipeline);
const main = async () => {
@marteinn
marteinn / list-90.md
Last active August 29, 2024 04:12
Best albums of the 90:s (very opinionated)
  1. Mercury Rev - Deserter Songs
  2. Nick Cave - Let Love In
  3. Cocteau Twins - Heaven Or Las Vegas
  4. Pulp - This Is Hardcore
  5. This Mortal Coil - Blood
  6. Spiritualized - Ladies And Gentlemen We Are Floating In Space
  7. Tom Waits - The Black Rider
  8. Coil - Loves Secret Domain
  9. Smashing Pumpkins - Mellon Coolie & The Infinite Sadness
  10. Wilco - Summerteeth