Skip to content

Instantly share code, notes, and snippets.

View peterfarrell's full-sized avatar
🏠
Working from home

PJ Farrell peterfarrell

🏠
Working from home
View GitHub Profile
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@peterfarrell
peterfarrell / machine.js
Last active May 28, 2021 22:49
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@peterfarrell
peterfarrell / filtersets.py
Last active December 4, 2020 17:16
Defaults for Django-Filters
class DocumentFilterSet(FilterSet):
period = django_filters.ChoiceFilter(
choices=Periods.choices,
label='Period',
field_name='period',
method='period_filter',
)
def period_filter(self, queryset, name, value):
if value:
@peterfarrell
peterfarrell / search_indexes.py
Created January 9, 2019 17:15 — forked from benzkji/search_indexes.py
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
@peterfarrell
peterfarrell / install.sh
Created November 26, 2018 19:04
RabbitMQ Install
# Adapted from: http://www.rabbitmq.com/install-debian.html
if [ ! -f /etc/apt/sources.list.d/rabbitmq.list ]
then
echo "Installing RabbitMQ"
echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
apt update
apt -y install rabbitmq-server
fi
@peterfarrell
peterfarrell / postgres_lookups.py
Created November 2, 2018 15:41
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
"""
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
peterfarrell / deploy.yaml
Created December 22, 2015 14:37 — forked from mblarsen/deploy.yaml
Solution for `git clone` using Ansible for repos with private submodules with github deploy keys
# 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
vars:
@peterfarrell
peterfarrell / singleton.py
Created October 28, 2015 18:23 — forked from senko/singleton.py
Singleton Django Model
# Written by Senko Rasic <[email protected]>
# 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