Skip to content

Instantly share code, notes, and snippets.

View benjaoming's full-sized avatar

Benjamin Balder Bach benjaoming

View GitHub Profile
@benjaoming
benjaoming / brute_force_mixin.py
Last active August 29, 2015 14:00
Block brute force attempts with a Mixin intended for FormView
import logging
from django.core.cache import cache
from django.utils import timezone
from datetime import timedelta
logger = logging.getLogger('name-your-logger')
class BruteForceMixin():
"""
Use this in a FormView and call count_failure() in form_invalid
@benjaoming
benjaoming / pyedit_autopep8.py
Last active February 2, 2017 08:55
PyDev with autopep8 formatting upon CTRL+SHIFT+F
"""
This code is public domain.
The original author is Bear Huang (http://bear330.wordpress.com/).
Uploaded to GIST and adapted for autopep8 by github/benjaoming
"""
# Guide for installing a code formatter (like this one) in PyDev:
# http://bear330.wordpress.com/2007/10/30/using-pythontidy-in-pydev-as-code-formatter/
# Ensure that your autopep8.py is executable (chmod +x).
@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 / shotwell_regenerate_thumbnails.sh
Last active April 10, 2017 01:56
Regenerate Shotwell Thumbnails
#!/bin/bash
# Based on http://gagor.pl/2014/01/regenerate-thumbnails-in-shotwell-for-last-month/
THUMB_ROOT=~/.cache/shotwell/thumbs
# Remove " > date('now','start of month','-1 month')" if you want to re-generate everything
sqlite3 ~/.local/share/shotwell/data/photo.db \
"select id||' '||filename from PhotoTable where date(timestamp,'unixepoch','localtime') > date('now','start of month','-1 month') order by timestamp desc" |
while read id filename; do
@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."
)
#!/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\///')

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:

@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
"""
@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 / 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.