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
@peterfarrell
peterfarrell / install_latest_vagrant_via_ansible
Last active February 5, 2017 16:06
Install latest Vagrant with Ansible (x86_64)
- name: Compute Vagrant latest version
sudo: yes
shell: wget -qO - https://dl.bintray.com/mitchellh/vagrant/|sed -n 's/.*href=\"\([^"]*\).*/\1/p'|grep x86_64\.deb|tail -1|cut -d'#' -f2
register: vagrant_file_name
- name: Download Vagrant latest version
shell: "wget https://dl.bintray.com/mitchellh/vagrant/{{ vagrant_file_name.stdout }} -O {{ vagrant_file_name.stdout }}"
args:
chdir: "./bootstrap/files"
creates: "{{ vagrant_file_name.stdout }}"
- name: Download Telegram
shell: "wget https://tdesktop.com/linux -O telegram.tar.xz"
args:
chdir: "./bootstrap/files"
creates: "telegram.tar.xz"
- name: Unarchive Telegram
sudo: yes
unarchive: src=telegram.tar.xz dest=/opt

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)