Skip to content

Instantly share code, notes, and snippets.

View dokterbob's full-sized avatar

Mathijs de Bruin dokterbob

View GitHub Profile
@dokterbob
dokterbob / dashlane-pass-import.py
Created April 28, 2015 09:46
Python 3 script to import passwords from Dashlane CSV-export
# Import passwords from Dashlane's CSV-export into pass
# Note: you might need to manually clean up the typically malformatted Dashlane CSV export
import sys
import csv
import argparse
import subprocess
def main(argv=None):
@dokterbob
dokterbob / admin.py
Created February 15, 2011 19:20
Simple Django admin mixin which dynamically adds links to inline admins on the top right of the admin form.
class InlineButtonsAdminMixin(object):
"""
Mixin which dynamically adds links to inline admins on the top right of
the admin form.
"""
class Media:
js = ('js/inlinebuttons.js',)
@dokterbob
dokterbob / admin.py
Created February 16, 2011 16:29
Django Admin mixins for dynamic TinyMCE link and image lists.
from django.core.urlresolvers import reverse
from sorl.thumbnail import get_thumbnail
from django.conf.urls.defaults import patterns, url
# This nice little snippet makes the life of someone extending the admin
# functionality a lot easier.
# Find the original at: http://djangosnippets.org/snippets/1804/
@dokterbob
dokterbob / listeners.py
Created March 30, 2011 09:25
Class-based listeners, based on Django's class-based generic views.
class Listener(object):
"""
Class-based listeners, based on Django's class-based generic views. Yay!
Usage::
class MySillyListener(Listener):
def dispatch(self, sender, **kwargs):
# DO SOMETHING
pass
@dokterbob
dokterbob / translation_utils.py
Created April 19, 2011 09:08
Translations without using __dict__ :)
import logging, re
logger = logging.getLogger(__name__)
from django.utils.translation import get_language
LANGUAGE_CODE_RE = re.compile(r'_(?P<code>[a-z_]{2,5})$')
class TranslatableMixin(object):
@dokterbob
dokterbob / gist:995013
Created May 27, 2011 10:32
Form mixin class which allows for reordering and hiding fields for normal forms, similar to the way this is possible with ModelForms.
class OrderableFormMixin(object):
"""
Form mixin class which allows for reordering and hiding fields for normal
forms, similar to the way this is possible with ModelForms::
class MyFunkyForm(OrderableFormMixin, Form):
class Meta:
fields = ('my_field1', 'my_field2')
This snippet is based on:
def sanitize_log_data(secret, data=None, leave_characters=4):
"""
Clean private/secret data from log statements and other data.
Assumes data and secret are strings. Replaces all but the first
`leave_characters` of `secret`, as found in `data`, with '*'.
If no data is given, all but the first `leave_characters` of secret
are simply replaced and returned.
"""
@dokterbob
dokterbob / compare.py
Created February 17, 2012 13:56
Compare versions Python PIP requirements
oldfile = open('freeze_old.txt')
newfile = open('freeze_new.txt')
packages = {}
def splitpackage(line):
result = line.split('==')
if not len(result) == 2:
result = line.split('@')
@dokterbob
dokterbob / snippet.py
Created March 7, 2012 14:50
Default validator
from django.core.exceptions import ValidationError
...
def default_validator(value):
""" Make sure only one instance of MyModel has default=True """
if value and MyModel.objects.filter(default=True).exists():
raise ValidationError('A default has already been defined.')
@dokterbob
dokterbob / models.py
Created June 21, 2012 14:45
Example of loosely coupling Django apps using settings.
from django.db import models
# Import the PROJECT_MODEL string from the current app's settings
from .settings import PROJECT_MODEL
class Task(models.Model):
"""
Instead of explicitly giving the model for the FK as a Python object or
explicitly stating a string in the form '<app>.<modelname>', we'll use