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
@yuekui
yuekui / group_filter_set.py
Created March 2, 2022 21:54
GroupFilterSet to reduce inner joins for performance.
from django.db.models import Q
from django.db.models.constants import LOOKUP_SEP
from django_filters import rest_framework as django_filters
from django_filters.constants import EMPTY_VALUES
class GroupFilterSet(django_filters.FilterSet):
def filter_queryset(self, queryset):
"""
Group the fitlers by the first join table to
@sameerkumar18
sameerkumar18 / yourapp_admin.py
Last active October 7, 2024 18:21
Django Admin: Celery Retry Task by ID
# Add this code in any Django app's admin.py
# Works for all Task Statuses; you can filter them in line 12.
import ast
import importlib
import json
from django.contrib import admin
from django.contrib import messages
from django.utils.safestring import mark_safe
@russmatney
russmatney / django_filter_grouping_impl.py
Created November 30, 2021 21:36
Django-filters group nested queries impl
class GroupFilter:
"""
The work here is largely cherry-picked from this unmerged django-filters PR:
https://github.com/carltongibson/django-filter/pull/1167/files
"""
def __init__(self, filter_names):
self.filter_names = filter_names
def set_parent(self, parent):
@alysivji
alysivji / graph_transitions.py
Created March 30, 2021 06:05
Exporting Mermaid Markdown for django-fsm
# -*- coding: utf-8; mode: django -*-
import graphviz
from optparse import make_option
from django.core.management.base import BaseCommand
from django.utils.encoding import force_text
from django_fsm import FSMFieldMixin, GET_STATE, RETURN_VALUE
try:
@loilo
loilo / pass-slots.md
Last active October 16, 2024 18:16
Vue: Pass Slots through from Parent to Child Components

Vue: Pass Slots through from Parent to Child Components

The Situation

  • We've got some components A, B and C which provide different slots.
    const A = {
      template: `<div><slot name="a">Default A Content</slot></div>`
    }

const B = {

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
@benzkji
benzkji / search_indexes.py
Created December 8, 2015 07:15
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
// XPath CheatSheet
// To test XPath in your Chrome Debugger: $x('/html/body')
// http://www.jittuu.com/2012/2/14/Testing-XPath-In-Chrome/
// 0. XPath Examples.
// More: http://xpath.alephzarro.com/content/cheatsheet.html
'//hr[@class="edge" and position()=1]' // every first hr of 'edge' class
@Kartones
Kartones / postgres-cheatsheet.md
Last active November 15, 2024 21:14
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

Some interesting flags (to see all, use -h or --help depending on your psql version):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)
  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)
@Nagyman
Nagyman / workflows-in-django.md
Last active October 19, 2024 22:03
Workflows in Django

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)