Skip to content

Instantly share code, notes, and snippets.

View vdboor's full-sized avatar

Diederik van der Boor vdboor

View GitHub Profile
@vdboor
vdboor / caching.py
Created January 4, 2023 14:14
A @cache_results decorator for functions that gives full control over bypassing/refreshing/clearing the cache
import functools
import logging
from django.core.cache import DEFAULT_CACHE_ALIAS, caches
logger = logging.getLogger(__name__)
class CachedFunction:
"""
The CachedFunction is a proxy object that implements cache-wrapper logic around
@vdboor
vdboor / sqlmigrate_custom.py
Last active December 15, 2022 14:18
Running the Django migration engine for custom models
from django.core.management import BaseCommand
from django.db import connection, models
from django.db.migrations import Migration
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.graph import MigrationGraph
from django.db.migrations.questioner import InteractiveMigrationQuestioner
from django.db.migrations.state import ModelState, ProjectState
class PatchedModelState(ModelState):
@vdboor
vdboor / dmesg-dsn.py
Last active August 17, 2018 08:39
Show dmesg IPTABLES messages with DNS name resolving (needs apt install python3-dnspython or pip3 install dnspython)
#!/usr/bin/env python3
#
# This runs dmesg to show iptable logs, and replaces the IP addresses with
# actual DNS names. Resolved names are cached for speedup.
#
# Uses dnspython for proper resolver timeout handling:
# pip3 install dnspython (or apt install python3-dnspython)
from collections import defaultdict
import dns.exception

Keybase proof

I hereby claim:

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

To claim this, I am signing this object:

@vdboor
vdboor / kubernetes_add_service_account_kubeconfig.sh
Created December 13, 2017 19:46 — forked from innovia/kubernetes_add_service_account_kubeconfig.sh
Create a service account and generate a kubeconfig file for it - this will also set the default namespace for the user
#!/bin/bash
# Add user to k8s 1.6 using service account, no RBAC (must create RBAC after this script)
if [[ -z “$1” ]] || [[ -z “$2” ]];then
echo “usage: $0 <username> <environment (stg|prod)>”
exit 1
fi
USER=$1
environment=$2
@vdboor
vdboor / admin_filters.py
Last active December 13, 2016 14:00
In the admin sidebar, only show related objects that are in use by the objects shown in the list
from django.contrib import admin
from django.contrib.admin.utils import get_model_from_relation
from django.utils.encoding import smart_text
class LimitedRelatedFieldListFilter(admin.RelatedFieldListFilter):
"""
Limit the filter choices in the admin sidebar to objects
which are actually used by the objects shown in the list.
@vdboor
vdboor / _media_debug.scss
Created September 1, 2016 12:36
Place a <div id="media-debug"></div> in the HTML, and this Sass code will show the active Bootstrap media query.
// Debugging for media queries
#media-debug {
position: fixed;
left: 0;
bottom: 20px;
color: #000;
font-weight: bold;
padding: 2px 5px;
z-index: 1001;
}
@vdboor
vdboor / patch_migrations.py
Created August 19, 2016 09:03
Avoid the `verbose_name` and `help_text` in Django migrations for fields.
"""
Patch the creation of database migrations in Django
Import this early from `__init__.py``.
- Don't want verbose_name changes in the migrations file.
- Don't want help_text in the migrations file.
"""
from functools import wraps
from django.db.models import Field
@vdboor
vdboor / create_update_mixin.py
Last active September 29, 2019 03:18
A `CreateOrUpdateMixin` for Django, to support both creating and updating an object in a single view.
from django.utils.functional import cached_property
from django.views.generic import UpdateView
class CreateOrUpdateMixin(object):
"""
A mixin to make an ``UpdateView`` behave like an ``CreateView`` when the ID argument is missing.
The ``is_add`` attribute allows views to make small changes depending on the way the view is called.
Note this mixin is designed to save code. When the create and update logic differs a lot,
write them as separate views.
@vdboor
vdboor / forms.py
Last active June 10, 2016 08:41
Django list filters - provide a way to have filtering in lists. The behavior is completely form-driven, making it possible to write custom `filter_FIELDNAME()` methods in the form if needed.
from django import forms
from django.db.models import QuerySet, Q
class ListFilterForm(forms.Form):
"""
Base form for lists to add a search feature.
Tips: