Skip to content

Instantly share code, notes, and snippets.

View benjaoming's full-sized avatar

Benjamin Balder Bach benjaoming

View GitHub Profile
#!/usr/bin/env python3
import random
nouns = [
"sten",
"et glashus",
"perler",
"svin",
"hunden",
@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:
#!/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 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\///')
@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 / kolibri_legacy_restore.py
Created October 16, 2017 11:58
Restoring a <0.6 database dump generated with >=0.6
#!/usr/bin/env python
try:
import kolibri
except ImportError:
print(
"Could not find Kolibri in the system path, if you are using a PEX "
"file, please run `./file.pex manage shell` and then copy-paste all "
"contents of this script into the command line of Kolibri."
)
@benjaoming
benjaoming / zinnia_to_wagtail.py
Created March 21, 2017 13:53
Management command: Export django-blog-zinnia to wagtail_blog
"""
This command exports blog posts, categories and tags from django-blog-zinnia
to wagtail_blog.
Usage:
0. Carefully consider if you want custom models for wagtail_blog before
starting the import. You may for instance want to retain an FK relation in
the database between old Zinnia Entry objects and new BlogPage objects.
1. Copy this script to yourapp/management/commands/zinnia_to_wagtail.py
@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
"""