Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Peter J. Farrell peterfarrell

🏠
Working from home
View GitHub Profile
@peterfarrell
peterfarrell / filtersets.py
Last active Dec 4, 2020
Defaults for Django-Filters
View filtersets.py
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 Jan 9, 2019 — forked from benzkji/search_indexes.py
haystack index for django-filer PDFs
View search_indexes.py
# 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
View install.sh
# 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 Nov 2, 2018
Django Postgresql DateRange / DateTimeRange upper_inf and lower_inf functions
View postgres_lookups.py
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 task.py
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
View workflows-in-django.md

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 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
vars:
@peterfarrell
peterfarrell / singleton.py
Created Oct 28, 2015 — forked from senko/singleton.py
Singleton Django Model
View singleton.py
# Written by Senko Rasic <senko.rasic@goodcode.io>
# 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
peterfarrell / icheck_selenium_python_js_version.py
Created Aug 13, 2015
Select a icheck v2.-0 with Selenium python (javascript version)
View icheck_selenium_python_js_version.py
def icheck(self, selector, retry=True):
"""
Helper for iCheck radio inputs. N.B. We are using iCheck 2.0.x RC
Example:
self.icheck("#id-element")
: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);
};