Skip to content

Instantly share code, notes, and snippets.

View bryanchow's full-sized avatar

Bryan Chow bryanchow

View GitHub Profile
@bryanchow
bryanchow / cache_buster.py
Last active May 27, 2023 00:28
Django cache busting view
# Django cache buster view
# https://gist.github.com/bryanchow/4fd80e38a75be3c4e64560de2e7fd76e
from django.core.cache import cache
from django.utils.cache import get_cache_key
from django.shortcuts import redirect
def bust_cache(request, path):
"""
@bryanchow
bryanchow / cronologuru.py
Last active August 4, 2022 22:54
cronolog-style logging for Django using loguru
# cronolog-style logging for Django using loguru
# https://gist.github.com/bryanchow/99301bff53911f2740bf63d1268c1538
# Example usage:
#
# settings.py:
# CRONOLOGURU_ROOT = "/var/log/django"
#
# views.py:
# from cronologuru import get_logger
@bryanchow
bryanchow / nginx_secure_link.py
Last active August 2, 2022 00:29
Generate an expiring URL compatible with Nginx's http_secure_link_module.
# https://gist.github.com/bryanchow/fb34d7c52d68692b23a26698e5a4ea41
from django.template.engine import Engine
from django.template.loaders.filesystem import Loader
def get_template_source(template_name, dirs=[]):
"""
Return the raw source code of an unrendered Django template.
"""
# https://gist.github.com/bryanchow/b25aeed8db9e82a68c920e66fd192536
try:
from io import BytesIO
except ImportError:
from cStringIO import cStringIO as BytesIO
import requests
from mimetypes import guess_extension
from django.core import files
# A reusable Django Export CSV Command
# https://gist.github.com/bryanchow/e611598bc9391ac854ff427582e5619c
#
# Usage: Import this module in yourapp/management/commands/yourcommand.py
# and subclass ExportCSVCommand like this:
#
# class Command(ExportCSVCommand):
# def get_queryset(self):
# return YourModel.objects.all()
# def handle_exception(self, e, inst):
# https://gist.github.com/bryanchow/8b5bd3c90ab41c82f68d95f7d96f28ed
import shortuuid
from django.conf import settings
DEFAULT_ALPHABET = "0123456789abcdef"
def make_unique_code(length=None, namespace=None, alphabet=None):
@bryanchow
bryanchow / ulize.py
Last active July 16, 2017 23:35
ulize.py: Convert plaintext lists to HTML unordered lists
import re
def ulize(text):
r"""
Convert lists embedded in plain text to HTML unordered lists. Yes, kinda
like Markdown or Textile but without the other junk. Expects each list
item to be prefixed by zero or more spaces, a dash, and a single space.
https://gist.github.com/bryanchow/2b0fa0cc99c2582e5bb65b9f1088473f
# https://gist.github.com/bryanchow/6917ddc1823c09b5cc9f
import re
from django.http import HttpResponseRedirect
from django.conf import settings
SHOULD_REDIRECT_SSL = getattr(
settings, 'SHOULD_REDIRECT_SSL', not settings.DEBUG
)
# https://gist.github.com/bryanchow/d30c0c7021f93b416744
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
def require_settings(*args):
"""
Check django.settings for the presence of the specified keys. Accepts
settings keys as function args, or as a list of strings.