Skip to content

Instantly share code, notes, and snippets.

View bjinwright's full-sized avatar

Brian Jinwright bjinwright

View GitHub Profile
@bjinwright
bjinwright / Cloudant Indexes
Last active August 29, 2015 14:06
Cloudant Indexes
Here are the three JSON documents that I used to create the indexes.
{
"index": {
"fields": ["price","name"]
},
"name" : "price-name-index",
"type" : "json"
}
@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 / custom_storages.py
Created July 12, 2016 18:52
Django Storages S3 custom folder based on settings
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION
Import by filename is not supported.: ImportError
Traceback (most recent call last):
File "/var/task/handler.py", line 302, in lambda_handler
return LambdaHandler.lambda_handler(event, context)
File "/var/task/handler.py", line 110, in lambda_handler
return cls().handler(event, context)
File "/var/task/handler.py", line 159, in handler
app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
File "/var/task/django_zappa_app.py", line 17, in get_django_wsgi
django.setup()
@bjinwright
bjinwright / zappa_settings.json
Last active December 8, 2016 23:22
Zappa settings file with remote environment vars and VPC support
{
"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 / create_zappa_user.py
Created January 13, 2017 20:44
Create super user with management command
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 / example.py
Created February 16, 2017 15:23
Example Click CLI with Callback and Prompt
import click
from .cli import CappyCLI
@click.group()
def cappy():
pass
def use_apigw(ctx, param, value):
"""
@bjinwright
bjinwright / .config
Created May 10, 2013 15:17
AWS Elastic Beanstalk container commands for installing mod_pagespeed. This example uses a Django application but just strip out commands 1 and 2 and it usable by any application. This is the follow up to the https://gist.github.com/bjinwright/5554958 Gist that shows how to setup the Apache conf file that makes this work.
container_commands:
01_collectstatic:
command: "django-admin.py collectstatic --noinput"
02_lesscss:
command: "lesscpy -x static/css/base.less > static/css/base.css"
03_setup_apache:
command: "cp enable_mod_pagespeed.conf /etc/httpd/conf.d"
04_rm_pagespeed:
command: "rm -rf /pagespeed/ebextensions"
05_mkdir_pagespeed:
@bjinwright
bjinwright / enable_mod_pagespeed.conf
Created May 10, 2013 15:01
Example mod_pagespeed conf for AWS Elastic Beanstalk.
# mod_deflate configuration
<IfModule mod_deflate.c>
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xml+rss
AddOutputFilterByType DEFLATE application/x-javascript
@bjinwright
bjinwright / language.py
Last active May 17, 2017 15:43 — forked from beaufour/language.py
Django Middleware to choose language based on subdomain
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).