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 / date_trunc.py
Last active August 19, 2019 08:22
Allow to use DATE_TRUNC('month', "date_field") in PostgreSQL for Django 1.8+. I'm using this for django-oscar reports per month/week/year.
from django.db.models import Func, Value
class DateTrunc(Func):
"""
To support using DATE_TRUNC('text', "field") in SQL
Example::
order_totals = (orders
@vdboor
vdboor / pgbouncer_user.py
Created April 7, 2016 13:08
Manage the `userlist.txt` for PgBouncer in ansible. Place this file in the `library` folder.
#!/usr/bin/env python
#
# (c) 2015, Edoburu, GPLv3+ licensed
#
# Based on https://github.com/ansible/ansible-modules-core/blob/devel/web_infrastructure/htpasswd.py
#
DOCUMENTATION = """
module: htpasswd
short_description: manage PgBouncer userlist.txt entries
description:
@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:
@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 / 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 / _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 / 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 / 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

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 / 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