This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://gist.github.com/bryanchow/45db1ce979b1aae75c996e3e90e6f72c | |
import time | |
from datetime import datetime, timedelta | |
from hashlib import md5 | |
from base64 import b64encode | |
from urllib.parse import urlencode | |
def make_secure_link(url, secret, minutes=5): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
NewerOlder