Skip to content

Instantly share code, notes, and snippets.


Alan Hamlett alanhamlett

View GitHub Profile
alanhamlett /
Last active Sep 6, 2018
Copy of alembic/ from allowing usage of --autogenerate flag when creating new schema migrations
from __future__ import with_statement
from alembic import context
from alembic.util.compat import configparser
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
import os
import re
import sys
basedir = os.path.abspath(os.path.dirname(__file__) + '/../')
alanhamlett / ajax_setup.js
Last active Jan 13, 2021
Sets the X-CSRFToken header for every jQuery ajax non-GET request to make CSRF protection easy. This fixes the example from Django docs here:
View ajax_setup.js
beforeSend: function(xhr, settings) {
if (settings.type == 'POST' || settings.type == 'PUT' || settings.type == 'DELETE') {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
alanhamlett /
Last active Apr 13, 2021
Serialize SQLAlchemy Model to dictionary (for JSON output) and update Model from dictionary attributes.
import uuid
import wtforms_json
from sqlalchemy import not_
from sqlalchemy.dialects.postgresql import UUID
from wtforms import Form
from wtforms.fields import FormField, FieldList
from wtforms.validators import Length
from flask import current_app as app
from flask import request, json, jsonify, abort
# -*- coding: utf-8 -*-
More info:
from flask import current_app, request, render_template, redirect, url_for
from myapp.models import User
alanhamlett /
Last active Jan 3, 2018
send an error email when a Celery worker raises an unhandled exception
# -*- coding: utf-8 -*-
Setup for Celery distributed task queue.
import socket
alanhamlett / responsive.less
Last active Sep 30, 2020
Responsive helper classes for Bootstrap style margins, padding, aligning, and displaying per screen size
View responsive.less
/* responsive.less
* ~~~~~~~~~~~~~~~
* Responsive helper classes for Bootstrap style margins, padding, aligning, and displaying per screen size.
* Works along with Bootstrap3.
//== Media queries breakpoints from Bootstrap3
// Extra small screen / phone
alanhamlett /
Created Sep 22, 2014
django model manager example
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.db import models, transaction
from django.utils import timezone
class Manager(models.Manager):
""" Use this class to define custom methods on models.
def get_query_set(self):
#!/usr/bin/env python
import base64
import requests
# get some JSON, authenticating with url args
r = requests.get('', params={'access_token': ACCESS_TOKEN})
alanhamlett /
Created Dec 8, 2014
Auto update class for a NetBeans IDE plugin
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common

Keybase proof

I hereby claim:

  • I am alanhamlett on github.
  • I am alanhamlett ( on keybase.
  • I have a public key whose fingerprint is 2A17 98FE 2E12 3F7C BCDB E05F 76C4 315D 71A9 FECC

To claim this, I am signing this object: