Skip to content

Instantly share code, notes, and snippets.

View the-vampiire's full-sized avatar
💭
ive lost a shade of color in my life. rest in peace.

the-vampiire

💭
ive lost a shade of color in my life. rest in peace.
View GitHub Profile
@the-vampiire
the-vampiire / conda_env.txt
Last active October 26, 2018 23:48
github app webhook event types (.json and .txt)
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: osx-64
beautifulsoup4=4.6.3=py36_0
ca-certificates=2018.03.07=0
certifi=2018.10.15=py36_0
libcxx=4.0.1=h579ed51_0
libcxxabi=4.0.1=hebd6815_0
libedit=3.1.20170329=hb402a30_2
libffi=3.2.1=h475c297_4
@the-vampiire
the-vampiire / verify_payload.js
Last active October 26, 2018 20:34
github securing your app webhook example in node js and express
const crypto = require('crypto');
const createComparisonSignature = (body) => {
const hmac = crypto.createHmac('sha1', process.env.OCTO_WIZARD_HOOK_SECRET);
const self_signature = hmac.update(JSON.stringify(body)).digest('hex');
return `sha1=${self_signature}`; // shape in GitHub header
}
const compareSignatures = (signature, comparison_signature) => {
const source = Buffer.from(signature);
@the-vampiire
the-vampiire / main.py
Created October 4, 2018 01:42
studio 8 solution
from flask import request, redirect, render_template, session, flash
import cgi
from app import app, db
from models import Movie, User
# a list of movie names that nobody should have to watch
terrible_movies = [
"Gigli",
"Star Wars Episode 1: Attack of the Clones",
@the-vampiire
the-vampiire / mapAttributes.js
Created August 8, 2018 06:25
Map Attributes for reducing database server load on GraphQL resolver calls
const mapAttributes = (model, { fieldNodes }) => {
const columns = new Set(Object.keys(model.rawAttributes)); // to prevent column doesnt exist error
const rawAttributes = fieldNodes[0].selectionSet.selections.map(({ name: { value } }) => value);
return rawAttributes.filter(attribute => columns.has(attribute));
};
@the-vampiire
the-vampiire / mapAttributes.js
Last active March 20, 2020 05:06
5 line Apollo Server GraphQL Type resolver efficiency proof of concept
const mapAttributes = (model, { fieldNodes }) => {
// get the fields of the Model (columns of the table)
const columns = new Set(Object.keys(model.rawAttributes));
const requested_attributes = fieldNodes[0].selectionSet.selections
.map(({ name: { value } }) => value);
// filter the attributes against the columns
return requested_attributes.filter(attribute => columns.has(attribute));
};
@the-vampiire
the-vampiire / validate_form.py
Created July 20, 2018 05:25
form validation (function and class examples)
def validate_name_as_caps(name):
"""
raise ValueError and pass respective message
"""
if name.upper() != name:
raise ValueError("Name is not capitalized")
return name
validators = {
@the-vampiire
the-vampiire / env_vars.sh
Created April 1, 2018 23:50
shell script for removing Django environment variables on 'conda deactivate' call
# when you call conda deactivate this script will be executed
# removes the environment variables automatically on environment deactivation
# this goes in /anaconda3/envs/ENVIRONMENT-NAME/etc/conda/deactivate.d
unset DEBUG SECRET_KEY DB_HOST DB_PORT DB_NAME DB_USER DB_PASSWORD STATIC_URL
@the-vampiire
the-vampiire / env_vars.sh
Last active April 4, 2018 02:37
shell script for loading Django environment variables on 'conda activate <env name>' call
# when you call conda activate <env name> this script will be executed
# loads the environment variables automatically on environment activation
# this goes in /anaconda3/envs/ENVIRONMENT-NAME/etc/conda/activate.d
# note: you must create etc/conda in the environment directory to place this file
# for windows users: https://conda.io/docs/user-guide/tasks/manage-environments.html#windows
# spread across multiple lines for readability
# you can write them space-separated in one export statement if you want
export DEBUG='True' # "converted" to boolean in settings.py line 5
@the-vampiire
the-vampiire / app.yaml
Last active September 22, 2018 20:28
Django + PostgreSQL Google Cloud Flexible App Engine deployment template
runtime: python
# the PROJECT-DIRECTORY is the one with settings.py and wsgi.py
entrypoint: gunicorn -b :$PORT PROJECT-DIRECTORY.wsgi # specific to a GUnicorn HTTP server deployment
env: flex # for Google Cloud Flexible App Engine
# any environment variables you want to pass to your application.
# accessible through os.environ['VARIABLE_NAME']
env_variables:
# the secret key used for the Django app (from PROJECT-DIRECTORY/settings.py)
@the-vampiire
the-vampiire / settings.py
Created March 28, 2018 06:37
Sample settings.py file for Django deployment on Google Cloud Flexible App Engine
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = os.environ['DEBUG'] == 'True' # environment vars are strings. "convert" to boolean. lol, Python
SECRET_KEY = os.environ['SECRET_KEY']
ALLOWED_HOSTS = [
# TODO: add your Google Cloud Project-ID here
'PROJECT-ID.appspot.com', # must add the app engine (project-id) domain here