Skip to content

Instantly share code, notes, and snippets.

Working from home

Peter J. Farrell peterfarrell

Working from home
View GitHub Profile
peterfarrell /
Last active Dec 4, 2020
Defaults for Django-Filters
class DocumentFilterSet(FilterSet):
period = django_filters.ChoiceFilter(
def period_filter(self, queryset, name, value):
if value:
peterfarrell /
Created Jan 9, 2019 — forked from benzkji/
haystack index for django-filer PDFs
# coding: utf-8
from __future__ import unicode_literals
import os
from pdfminer.pdfdocument import PDFEncryptionError
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
# Adapted from:
if [ ! -f /etc/apt/sources.list.d/rabbitmq.list ]
echo "Installing RabbitMQ"
echo 'deb testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list
wget -O- | sudo apt-key add -
apt update
apt -y install rabbitmq-server
peterfarrell /
Created Nov 2, 2018
Django Postgresql DateRange / DateTimeRange upper_inf and lower_inf functions
from django.db.models.lookups import Lookup
class DateRangeLowerInf(Lookup):
Use like:
`MyObject.objects.filter(date_range__lower_inf=True)` for infinity
`MyObject.objects.filter(date_range__lower_inf=False)` for not infinity
View fix celery warm shutdown unack
class CustomTask(Task):
def on_failure(self, exc, task_id, args, kwargs, einfo):
info = '[{0}] failed: {1}'.format(task_id, exc)
logger.exception(info, exc_info=exc)
super(CustomTask, self).on_failure(exc, task_id, args, kwargs, einfo)
def on_success(self, retval, task_id, args, kwargs):
global app
if app.is_warm_shutdown:
app.consumer.connection._default_channel.do_restore = False

Workflows (States) in Django

I'm going to cover a simple, but effective, utility for managing state and transitions (aka workflow). We often need to store the state (status) of a model and it should only be in one state at a time.

Common Software Uses

  • Publishing (Draft->Approved->Published->Expired->Deleted)
peterfarrell / deploy.yaml
Created Dec 22, 2015 — forked from mblarsen/deploy.yaml
Solution for `git clone` using Ansible for repos with private submodules with github deploy keys
View deploy.yaml
# Problem:
# If you use git submodules linking two private github repos, you'll need to create a separate deploy key for each.
# Multiple keys are not supported by Ansible, nor does ansible (when running git module) resort to your `.ssh/config` file.
# This means your ansible playbook will hang in this case.
# You can however use the ansible git module to checkout your repo in multiple steps, like this:
- hosts: webserver
peterfarrell /
Created Oct 28, 2015 — forked from senko/
Singleton Django Model
# Written by Senko Rasic <>
# Released into Public Domain. Use it as you like.
from django.db import models
class SingletonModel(models.Model):
"""Singleton Django Model
Ensures there's always only one entry in the database, and can fix the
peterfarrell /
Created Aug 13, 2015
Select a icheck v2.-0 with Selenium python (javascript version)
def icheck(self, selector, retry=True):
Helper for iCheck radio inputs. N.B. We are using iCheck 2.0.x RC
:param selector: The id of the icheck target.
:type selector: str
:return: None
View drag_and_drop_helper.js
(function( $ ) {
$.fn.simulateDragDrop = function(options) {
return this.each(function() {
new $.simulateDragDrop(this, options);
$.simulateDragDrop = function(elem, options) {
this.options = options;
this.simulateEvent(elem, options);