Skip to content

Instantly share code, notes, and snippets.

View bjinwright's full-sized avatar

Brian Jinwright bjinwright

View GitHub Profile
@bjinwright
bjinwright / settings.py
Created February 15, 2013 16:36
So part of being a 12-factor app is storing your config in your environment (http://12factor.net/config). This is how I do that using Django settings files. This is the code companion for a blog post (http://blog.ipoots.com/post/43154090079/how-to-achieve-12-factor-app-config-glory-with-django). A similar blog post is http://www.wellfireinteract…
# Django settings for dnsly project.
from unipath import FSPath as Path
import os
def env(key, default=None):
"""Retrieves env vars and makes Python boolean replacements"""
val = os.getenv(key, default)
if val == 'True':
val = True
@bjinwright
bjinwright / loader.py
Last active May 26, 2017 17:39
Django Theme Switcher that does not use Django Sites and doesn't use Thread.local
from django.core.exceptions import SuspiciousFileOperation
from django.template import Origin
from django.template.loaders.filesystem import Loader
from django.conf import settings
from django.utils._os import safe_join
class ThemeLoader(Loader):
themes = [theme.get('theme') for theme in settings.SITE_CONFIGS.values()]
current_theme = ''
@pgolding
pgolding / dynamo_expression_builder.md
Last active August 22, 2017 15:40
DynamoDB Expression builder for update_item method in Boto3 (Python)

The update_item method in Boto3 requires preparing something like this:

try:
    result = table.update_item(
        Key={
            'id': event['pathParameters']['id']
        },
        ExpressionAttributeNames=attr_names,
        ExpressionAttributeValues=expression_vals,
"""
@author Jay Taylor [@jtaylor]
@date 2010-11-01
Copyright Jay Taylor 2010
"""
#import socks
import socket
@girasquid
girasquid / s3store.py
Created February 9, 2010 19:46
Turn S3 into a key/value store for JSON objects.
import simplejson
from boto.s3.connection import S3Connection
from boto.s3.key import Key
class S3KeyStore(object):
def __init__(self, access_key, secret_key, bucket):
self.conn = S3Connection(access_key, secret_key)
self.bucket = self.conn.create_bucket(bucket)
def get(self, key):
@zbnauj
zbnauj / Readme.md
Created October 12, 2019 20:19 — forked from colllin/Readme.md
FaunaDB User Token Expiration (for ABAC)

Auth0 + FaunaDB ABAC integration: How to expire Fauna user secrets.

Fauna doesn't yet provide expiration/TTL for ABAC tokens, so we need to implement it ourselves.

What's in the box?

3 javascript functions, each of which can be imported into your project or run from the command-line using node path/to/script.js arg1 arg2 ... argN:

  1. deploy-schema.js: a javascript function for creating supporting collections and indexes in your Fauna database.
@tovbinm
tovbinm / fauna-graphql-relations.gql
Last active March 6, 2022 14:38
FaunaDB Relations: GraphQL schemas, mutations and resulting documents
****************************************************************************
**** FaunaDB Relations: GraphQL schemas, mutations and resulting documents *
****************************************************************************
**** One to One Relation ***************************************************
SCHEMA:
type User { name: String! car: Car }
type Car { plate: String! owner: User }
MUTATION:
mutation Create {
createUser(data: {
@dokterbob
dokterbob / validators.py
Created August 31, 2011 15:03
Validator for files, checking the size, extension and mimetype.
from os.path import splitext
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
class FileValidator(object):
"""
Validator for files, checking the size, extension and mimetype.
@beaufour
beaufour / language.py
Created November 30, 2012 16:00
Django Middleware to choose language based on subdomain
import logging
from django.utils import translation
class SubdomainLanguageMiddleware(object):
"""
Set the language for the site based on the subdomain the request
is being served on. For example, serving on 'fr.domain.com' would
make the language French (fr).
@simonw
simonw / gist:7000493
Created October 15, 2013 23:53
How to use custom Python JSON serializers and deserializers to automatically roundtrip complex types.
import json, datetime
class RoundTripEncoder(json.JSONEncoder):
DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
def default(self, obj):
if isinstance(obj, datetime.datetime):
return {
"_type": "datetime",
"value": obj.strftime("%s %s" % (