Skip to content

Instantly share code, notes, and snippets.

View bryanchow's full-sized avatar

Bryan Chow bryanchow

View GitHub Profile
@bryanchow
bryanchow / upload_filename.py
Created July 26, 2012 16:24
Django uploaded filename utility functions
# Django uploaded filename utility functions
# https://gist.github.com/3183057
import os, itertools
from django.core.exceptions import SuspiciousOperation
from django.utils._os import safe_join, abspathu
from django.utils.text import get_valid_filename
from django.conf import settings
# 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.
# 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
)
@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/8b5bd3c90ab41c82f68d95f7d96f28ed
import shortuuid
from django.conf import settings
DEFAULT_ALPHABET = "0123456789abcdef"
def make_unique_code(length=None, namespace=None, alphabet=None):
# 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/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
# 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.
"""
@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.
@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