Skip to content

Instantly share code, notes, and snippets.

View alanhamlett's full-sized avatar

Alan Hamlett alanhamlett

View GitHub Profile
@alanhamlett
alanhamlett / wakatime-admin-__init__.py
Created November 30, 2018 00:11
Flask-Admin Example (replace dash with forward slash in file names)
# -*- coding: utf-8 -*-
"""
wakatime.admin
~~~~~~~~~~~~~~
Flask-Admin.
"""
from .views import admin
@alanhamlett
alanhamlett / requirements.txt
Last active March 21, 2019 00:02
WakaTime's requirements.txt Python dependencies
-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 / oauth_example_decorator.py
Created July 12, 2018 02:22
OAuth permission decorator code snippet from WakaTime blog post
""" 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 / responsive.less
Last active September 30, 2020 18:19
Responsive helper classes for Bootstrap style margins, padding, aligning, and displaying per screen size
/* 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 / ajax_setup.js
Last active January 13, 2021 02:18
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
$.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 / Flask-Login-Example.py
Created April 23, 2014 19:25
Flask-Login Example
# -*- 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 / amqp.py
Last active October 22, 2021 21:18
send an error email when a Celery worker raises an unhandled exception
# -*- coding: utf-8 -*-
"""
wakatime.amqp
~~~~~~~~~~~~~
Setup for Celery distributed task queue.
"""
import socket
@alanhamlett
alanhamlett / logger.py
Created September 6, 2022 00:21
WakaQ example worker error log handler
from logging import getLogger, ERROR, Formatter, Filter
from logging.handlers import WatchedFileHandler
from wakatime import app
from wakatime.background import wakaq
TASK_LOG_FORMAT = '[%(asctime)s] %(hostname)s %(levelname)s in %(task)s args=%(task_args)s kwargs=%(task_kwargs)s retry=%(task_retry)s: %(message)s'
class TaskFilter(Filter):
@alanhamlett
alanhamlett / image_utils.py
Last active September 24, 2022 08:07
Utility functions to resize, make square, and optimize (pngquant) images using Python
from io import BytesIO
from subprocess import PIPE, Popen
from typing import Optional
from PIL import Image, ImageFile, UnidentifiedImageError
from PIL.Image import DecompressionBombError
SUPPORTED_IMAGE_FORMATS = ["PNG", "JPEG", "JPEG2000"]
@alanhamlett
alanhamlett / alembic_env.py
Last active October 7, 2022 05:54
Copy of alembic/env.py from wakatime.com 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__) + '/../')