Skip to content

Instantly share code, notes, and snippets.

View benjaoming's full-sized avatar

Benjamin Balder Bach benjaoming

View GitHub Profile
#!/bin/bash
# Fixes a downloaded apt mirror
# See: https://github.com/apt-mirror/apt-mirror/issues/113#issuecomment-464932493
## first parameter is optional mirror list file
if [ "$1" != "" ]
then
mirrorlist=$1
else
#!/usr/bin/env python3
import random
nouns = [
"sten",
"et glashus",
"perler",
"svin",
"hunden",
@benjaoming
benjaoming / models.py
Last active July 11, 2022 06:37
Django non-db field properties
"""
Ever got tired of having loads of migrations that have no effect on
the actual database?
This little mockup presents how we can move properties of fields into
a scope where they can easily be ignored by the migration system
Would it work?
"""
from django.db import models
@benjaoming
benjaoming / fastmail_create_todo_list.py
Created May 18, 2022 14:56
Create a TODO calendar on Fastmail
#!/usr/bin/python3
#
# If you are using Thunderbird and other todo clients that do not support
# creation of new calendars on a CalDav backend (such as Fastmail), you'll
# need to tell Fastmail directly via CalDav.
#
# Dependencies: On Debian/Ubuntu, install python3-caldav:
#
# sudo apt install python3-caldav
#
@benjaoming
benjaoming / migratemigrations2django2.py
Last active February 27, 2021 09:30
Adds required keyword on_delete to ForeignKey and OneToOneField in Django migrations
#!/usr/bin/python3
"""
This script adds on_delete to Django migrations. It works on multi-line
definitions of ForeignKey/OneToOneField which may be in your codebase if you
have black'ened the migration files.
It's not perfect, but it doesn't create syntax errors and you can run black on
the code again afterwards.
First version:
@benjaoming
benjaoming / custom_user_auth_south_refactor.py
Last active August 1, 2020 17:04
Refactor South migration directory to respect custom User models. Backwards compatible with <django 1.5
# -*- coding: utf-8 -*-
"""Refactor South migrations to use settings.AUTH_USER_MODEL.
Inserts a backwards-compatible code-snippet in all
your schema migration files and uses a possibly customized user
model as introduced in Django 1.5.
Please note that this has nothing to do with changing
settings.AUTH_USER_MODEL to a new model. If you do this, stuff
will very likely break in reusable apps that have their own
migration trees.
@benjaoming
benjaoming / awake_or_suspend.py
Last active November 15, 2019 09:46
Automatically suspend using your own custom timing, unlike screen savers.
"""
Automatically suspend computer when the user has fallen asleep. But using your
own custom timing.
Add to your crontab with `crontab -e`
# m h dom mon dow command
0 2 * * * python3 /path/to/awake_or_suspend.py
This command either needs that your user can invoke `sudo` without a password or that it's launched with sude permissions.
@benjaoming
benjaoming / add_user_info_to_request_middleware.py
Last active February 28, 2019 12:26 — forked from sidmitra/add_user_info_to_request_middleware.py
Middleware to add user info to the current django request
class ExceptionUserInfoMiddleware(object):
"""
Adds user details to request context on receiving an exception, so that they show up in the error emails.
Add to settings.MIDDLEWARE_CLASSES and keep it outermost(i.e. on top if possible). This allows
it to catch exceptions in other middlewares as well.
Origin: https://gist.github.com/646372
"""

Keybase proof

I hereby claim:

  • I am benjaoming on github.
  • I am benjaoming (https://keybase.io/benjaoming) on keybase.
  • I have a public key ASCa077dJpvd_GlrDxPvWAQUkdoRhqhU_mHh9eg1zSQxWgo

To claim this, I am signing this object:

#!/usr/bin/env bash
# get the upstream repository username
upstream_repo=$(git config remote.upstream.url | sed 's/^.*://' | sed 's/\/.*$//')
# open the PR against develop, or another branch specified as the first command-line argument
upstream_branch=${1:-develop}
# branch
branch=$(git symbolic-ref HEAD | sed 's/refs\/heads\///')