Skip to content

Instantly share code, notes, and snippets.

View gregplaysguitar's full-sized avatar

Greg Brown gregplaysguitar

View GitHub Profile
@gregplaysguitar
gregplaysguitar / slash_middleware.py
Last active August 21, 2023 12:47
Append OR remove slash in django - like APPEND_SLASH but smarter.
from django import http
from django.utils.http import urlquote
from django import urls
from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
class AppendOrRemoveSlashMiddleware(MiddlewareMixin):
"""Like django's built in APPEND_SLASH functionality, but also works in
reverse. Eg. will remove the slash if a slash-appended url won't resolve,
@gregplaysguitar
gregplaysguitar / LICENSE.txt
Last active June 23, 2023 21:46
Auto-generate srcset and sizes attributes for an <img> with Django & sorl-thumbnail
Copyright (c) 2015 Greg Brown
[gregbrown.co.nz](http://gregbrown.co.nz)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
@gregplaysguitar
gregplaysguitar / admin_raw_id.py
Last active October 19, 2022 05:52
Show object description and a link next to django's raw_id_fields, for ForeignKey and ManyToMany fields
"""
Show customisable str representation of the linked objects next to each raw id
field in a django admin change form. Works for ForeignKey, OneToOneField, and
ManyToManyField fields.
Example:
from django.contrib import admin
from admin_raw_id import ImprovedRawIdFieldsAdmin
from .models import MyModel
@gregplaysguitar
gregplaysguitar / .gitignore
Created May 8, 2011 22:00
Some simple Ubuntu server monitoring tools
*.pyc
conf.py
@gregplaysguitar
gregplaysguitar / folding_whoosh_backend.py
Last active May 14, 2022 15:09
Django-haystack Whoosh backend with character folding
# -*- coding: utf-8 -*-
"""
Whoosh backend for haystack that implements character folding, as per
http://packages.python.org/Whoosh/stemming.html#character-folding .
Tested with Haystack 2.4.0 and Whooch 2.7.0
To use, put this file on your path and add it to your haystack settings, eg.
@gregplaysguitar
gregplaysguitar / upgrade_user_admin.py
Last active December 4, 2021 13:37
Enhance the django user admin with enforced unique emails, better filters and list items, and user profiles editable inline.
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.forms.models import inlineformset_factory
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
def upgrade_user_admin(UserProfile=None, unique_email=False,
list_display=None):
@gregplaysguitar
gregplaysguitar / machine.js
Created September 15, 2021 10:05
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@gregplaysguitar
gregplaysguitar / machine.js
Last active August 9, 2021 22:15
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@gregplaysguitar
gregplaysguitar / .gitignore
Last active July 28, 2021 05:06
DynamoDB data streaming with localstack
*.zip
@gregplaysguitar
gregplaysguitar / machine.js
Last active December 18, 2019 02:00
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)