Skip to content

Instantly share code, notes, and snippets.

Avatar

Alan Hamlett alanhamlett

View GitHub Profile
@alanhamlett
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
alanhamlett / api.py
Last active Sep 22, 2020
Serialize SQLAlchemy Model to dictionary (for JSON output) and update Model from dictionary attributes.
View api.py
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
View Flask-Login-Example.py
# -*- coding: utf-8 -*-
"""
More info:
http://flask.pocoo.org/docs/patterns/wtforms/
http://pythonhosted.org/Flask-SQLAlchemy/
https://flask-login.readthedocs.org/en/latest/
"""
from flask import current_app, request, render_template, redirect, url_for
from myapp.models import User
@alanhamlett
alanhamlett / ajax_setup.js
Last active Apr 25, 2020
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: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
View ajax_setup.js
$.ajaxSetup({
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
alanhamlett / heartbeats-to-durations.js
Last active Dec 4, 2019
Turn an array of heartbeats into durations by combining heartbeats that are within Timeout minutes from each other.
View heartbeats-to-durations.js
/*
BSD 3-Clause License
Copyright (c) 2019 Alan Hamlett.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
@alanhamlett
alanhamlett / oauth_example_decorator.py
Created Jul 12, 2018
OAuth permission decorator code snippet from WakaTime blog post
View oauth_example_decorator.py
""" Example for Blog Post:
https://wakatime.com/blog/34-part-3-flask-api-decorators-and-helpers
"""
def oauth(required_scopes=[]):
def wrapper(func):
@wraps(func)
def inner(*args, **kwargs):
# don't check oauth tokens if user already logged in with session cookie
@alanhamlett
alanhamlett / requirements.txt
Last active Mar 21, 2019
WakaTime's requirements.txt Python dependencies
View requirements.txt
-e git://github.com/kennethreitz/inbox.py.git@551b4f44b144564504c687cebdb4c543cb8e9adf#egg=inbox
alembic==0.8.3
amqp==1.4.9
anyjson==0.3.3
boto==2.39.0
braintree==3.20.0
cairosvg==1.0.19
celery==3.1.20
@alanhamlett
alanhamlett / wakatime-admin-__init__.py
Created Nov 30, 2018
Flask-Admin Example (replace dash with forward slash in file names)
View wakatime-admin-__init__.py
# -*- coding: utf-8 -*-
"""
wakatime.admin
~~~~~~~~~~~~~~
Flask-Admin.
"""
from .views import admin
@alanhamlett
alanhamlett / alembic_env.py
Last active Sep 6, 2018
Copy of alembic/env.py from wakatime.com allowing usage of --autogenerate flag when creating new schema migrations
View alembic_env.py
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
alanhamlett / UpdateHandler.java
Created Dec 8, 2014
Auto update class for a NetBeans IDE plugin
View UpdateHandler.java
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* 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
You can’t perform that action at this time.