Skip to content

Instantly share code, notes, and snippets.

View shacker's full-sized avatar
💭
Djangonaut, tree hugger

Scot Hacker shacker

💭
Djangonaut, tree hugger
View GitHub Profile
# General pattern for a data "chunking" process to prevent a server from hitting memory limits
# when calling .update() or .delete() on large amounts of data. In this example, we enter an
# infinite loop, then keep deleting 1000 records at a time until the records are exhausted,
# then exit the loop. You can't call .update() or .delete() after taking a slice, hence the need
# for two queries rather than one.
t = Territory.objects.get(name="some-territory")
while True:
loc_ids = Location.objects.filter(territory=t)[:1000].values_list("id", flat=True)
@shacker
shacker / .gitconfig
Last active May 28, 2018 06:51
shacker .gitconfig aliases
[alias]
alias = config --get-regexp ^alias\\.
br = branch
bra = branch -a -v -v
cm = commit -a
co = checkout
cp = cherry-pick
dev = checkout develop
find = "!f() { git ls-files |grep $@; }; f"
last = log -1 HEAD
@shacker
shacker / test_views.py
Created November 22, 2017 18:41
Convert output of django-extensions' `show_urls` to a python list of "simple" URLs
import re
from django.core.management import call_command
from test_plus.test import TestCase
class SmokeTestAllViews(BaseUserTestCase):
def test_all_view_respones(self):
"""
@shacker
shacker / gist:f5b814762f373614653f10a2abbb658a
Created November 21, 2017 18:27
Example gravatar implementation for Python 2 *and* 3, using libgravatar
# For just python:
pip install libgravatar
# libravatar documentation/options at:
# http://libgravatar.readthedocs.io/
# Then:
from libgravatar import Gravatar
@shacker
shacker / admin.py
Last active April 11, 2017 06:17
ORM Logging boilerplate
from dirapp.models import Log
from django.contrib import admin
class LogAdmin(admin.ModelAdmin):
search_fields = ['user__username', 'action']
list_display = ('user', 'anon_username', 'action', 'target', 'status', 'ip_addr', 'timestamp',)
list_filter = ['target', 'status']
admin.site.register(Log, LogAdmin)
@shacker
shacker / stream_blocks.py
Last active November 26, 2016 19:07
BlockQuoteBlock for use with Wagtail Streamfields
from django import forms
from django.utils.encoding import force_text
from django.utils.html import format_html
from wagtail.wagtailcore.blocks import FieldBlock
class BlockQuoteBlock(FieldBlock):
def __init__(self, required=True, help_text=None, max_length=None, min_length=None, **kwargs):
self.field = forms.CharField(
@shacker
shacker / gist:2fdf105f2d51bebedc8c3ecd99176677
Last active November 8, 2017 11:57
Blog: github version template tag 1
@register.simple_tag
def git_ver():
'''
Retrieve and return the latest git commit hash ID and tag as a dict.
'''
git_dir = os.path.dirname(settings.BASE_DIR)
try:
# Date and hash ID
@shacker
shacker / import_drupal_pages.py
Created August 20, 2015 18:44
Django management command to import flat Drupal page sets into a wagtail tree
from datetime import datetime
import json
import requests
import sys
from bs4 import BeautifulSoup
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
@shacker
shacker / gist:87908e13c9ee6655ce90
Last active October 5, 2023 17:46
Using the Workday API with Python and the suds client library
import sys
from suds import client
from suds.wsse import Security, UsernameToken
from suds.sax.text import Raw
from suds.sudsobject import asdict
from suds import WebFault
'''
Given a Workday Employee_ID, returns the last name of that employee.
@shacker
shacker / keybase.md
Created November 14, 2014 06:17
keybase.io identity proof

Keybase proof

I hereby claim:

  • I am shacker on github.
  • I am shacker (https://keybase.io/shacker) on keybase.
  • I have a public key whose fingerprint is D28A CA5D AE23 7727 D8EB 8F92 3844 B708 0026 C9C3

To claim this, I am signing this object: