Skip to content

Instantly share code, notes, and snippets.

View bjinwright's full-sized avatar

Brian Jinwright bjinwright

View GitHub Profile
@bjinwright
bjinwright / encode.py
Created July 16, 2024 20:44 — forked from cameronmaske/encode.py
base64 that actually encodes URL safe (no '=' nonsense)
"""
base64's `urlsafe_b64encode` uses '=' as padding.
These are not URL safe when used in URL paramaters.
Functions below work around this to strip/add back in padding.
See:
https://docs.python.org/2/library/base64.html
https://mail.python.org/pipermail/python-bugs-list/2007-February/037195.html
"""
@bjinwright
bjinwright / ajax-dialog.js
Created September 28, 2023 14:53
Ajax Dialog Web Component
class AjaxDialog extends HTMLElement {
connectedCallback() {
// Store the original children (the slot content)
const originalChildren = this.innerHTML;
this.open = this.getAttribute('open') || false;
this.loaded = this.getAttribute('loaded') || false;
this.content = this.getAttribute('content') || '';
this.url = this.getAttribute('url') || '';
this.button_class = this.getAttribute('button-class') || 'bg-green-600 text-white';
this.dialog_class = this.getAttribute('dialog-class') || 'bg-white';
@bjinwright
bjinwright / create_role.py
Created August 1, 2021 23:04
Create Role in Fauna
result = real_client.query(
q.create_role({
"name": "new-role-c",
"privileges": [
{
"resource": q.function("create_user"),
"actions": {
"call": True
}
}
@bjinwright
bjinwright / permutations_range.py
Created November 16, 2017 18:50
Permutations based on range
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 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 = ''
@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).
@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 / 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='[email protected]')
@bjinwright
bjinwright / cognito.py
Last active January 18, 2022 00:25
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
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 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",