Skip to content

Instantly share code, notes, and snippets.

Brian Jinwright bjinwright

Block or report user

Report or block bjinwright

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bjinwright
bjinwright / permutations_range.py
Created Nov 16, 2017
Permutations based on range
View permutations_range.py
from itertools import permutations
xlist = ["word1", "word2", "word3"]
def perm_range(obj_list,num):
for perm in permutations(obj_list, num):
print(perm)
perm_range(xlist,2)
@bjinwright
bjinwright / loader.py
Last active May 26, 2017
Django Theme Switcher that does not use Django Sites and doesn't use Thread.local
View loader.py
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 = ''
@bjinwright
bjinwright / language.py
Last active May 17, 2017 — forked from beaufour/language.py
Django Middleware to choose language based on subdomain
View language.py
import logging
from django.utils import translation
from django.conf import settings
class DomainBasedLanguageMiddleware(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).
@bjinwright
bjinwright / example.py
Created Feb 16, 2017
Example Click CLI with Callback and Prompt
View example.py
import click
from .cli import CappyCLI
@click.group()
def cappy():
pass
def use_apigw(ctx, param, value):
"""
@bjinwright
bjinwright / create_zappa_user.py
Created Jan 13, 2017
Create super user with management command
View create_zappa_user.py
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
class Command(BaseCommand):
help = 'Echoes AWS Creds'
def handle(self, *args, **options):
get_user_model().objects.create_superuser(username='your_user', password='your_password', email='user@example.com')
@bjinwright
bjinwright / cognito.py
Last active Jun 25, 2019
Example of how to make an authorized call to API Gateway using Boto3, Requests, and AWS4Auth. http://stackoverflow.com/questions/37336286/how-do-i-call-an-api-gateway-with-cognito-credentials-in-python
View cognito.py
import boto3
import datetime
import json
from requests_aws4auth import AWS4Auth
import requests
boto3.setup_default_session(region_name='us-east-1')
identity = boto3.client('cognito-identity', region_name='us-east-1')
account_id='XXXXXXXXXXXXXXX'
@bjinwright
bjinwright / zappa_settings.json
Last active Dec 8, 2016
Zappa settings file with remote environment vars and VPC support
View zappa_settings.json
{
"dev": {
"aws_region": "us-east-1",
"s3_bucket": "yourcode-bucket",
"django_settings": "yourapp.settings",
"remote_env_file":"yourblog-dev.json",
"remote_env_bucket":"yourcode-bucket",
"use_precompiled_packages": true,
"domain": "yourblog-dev-apigw.yourdevdomain.com",
"lets_encrypt_key": "s3://yourcode-bucket/openssl.key",
@bjinwright
bjinwright / zappa-iam.json
Created Dec 8, 2016
Zappa IAM policy - Replace all the references to yourblog (your app name), your-function-bucket (the bucket that Zappa stores your code), and your-static-and-media-files-bucket (where you store your static files)
View zappa-iam.json
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:DescribeSecurityGroups",
"ec2:DescribeSubnets",
"ec2:Describe*"
],
"Resource": [
@bjinwright
bjinwright / custom_storages.py
Created Dec 7, 2016
Custon S3 Django-Storages backend made for use with CloudFront with multiple origins
View custom_storages.py
from django.conf import settings
from django.utils.encoding import filepath_to_uri
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
def url(self, name, headers=None, response_headers=None, expire=None):
return '{}{}'.format(settings.STATIC_URL,filepath_to_uri(name))
@bjinwright
bjinwright / .env
Last active Jul 28, 2019
Django Settings using Environment Variables via envs (https://github.com/bjinwright/envs) project.
View .env
DATABASE_ENGINE=django.db.backends.sqlite3
DATABASE_NAME=db.sqlite3
DATABASE_USER=
DATABASE_PASSWORD=
DATABASE_HOST=
DATABASE_PORT=
SECRET_KEY="asdfdfadsflkjsdflkjadsflkasfakl;"
STATIC_URL=/static/
DEBUG=True
You can’t perform that action at this time.