Skip to content

Instantly share code, notes, and snippets.

View mmerickel's full-sized avatar

Michael Merickel mmerickel

View GitHub Profile
@mmerickel
mmerickel / cors.py
Last active December 6, 2022 14:13
cors in pyramid
from pyramid.security import NO_PERMISSION_REQUIRED
def includeme(config):
config.add_directive(
'add_cors_preflight_handler', add_cors_preflight_handler)
config.add_route_predicate('cors_preflight', CorsPreflightPredicate)
config.add_subscriber(add_cors_to_response, 'pyramid.events.NewResponse')
class CorsPreflightPredicate(object):
@mmerickel
mmerickel / __init__.py
Last active November 22, 2018 03:08
decorate a bunch of items in a package and expose them as a public api
def _build_facade():
import sys
from .meta.api import scan
this = sys.modules[__name__]
registry = {}
scan(this, registry=registry)
globals().update(registry)
_build_facade()
del _build_facade
@mmerickel
mmerickel / static.py
Last active July 11, 2016 07:48
static asset detection
class StaticFactory(object):
def __init__(self, request):
request.is_static_asset = True
config.add_static_view('static', static_pkg, factory=StaticFactory)
config.add_request_method(lambda r: False, 'is_static_asset', reify=True)
_default_vary = set([
'Cookie',
'Accept',
@mmerickel
mmerickel / security.py
Last active October 9, 2015 15:57
simple token-based authentication policy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.security import (
Authenticated,
Everyone,
)
log = __import__('logging').getLogger(__name__)
class OAuthAuthenticationPolicy(object):
def unauthenticated_userid(self, request):
@mmerickel
mmerickel / login.py
Last active March 30, 2016 20:47
bind the session lifecycle to external request properties like authenticated user
from pyramid.httpexceptions import HTTPSeeOther
from pyramid.view import view_config
@view_config(...)
def login_view(request):
# ... validate the user
user = # ...
next_url = request.route_url('home')
headers = remember(request, user.id)
# bind the session to the user upon login
import collections
class DictProxy(collections.Mapping):
"""
A proxy for a dictionary that allows attribute access to underlying keys.
You may pass a custom ``wrapper`` to override the logic for wrapping
various custom types.
"""
@mmerickel
mmerickel / App.js
Last active August 29, 2015 14:23
redux bindActions decorator
import React from 'react';
import {connect} from 'redux/react';
import {bindActions} from './utils/bindActions';
import * as Actions from './actions';
@connect(state => ({
activePage: state.activePage,
}))
@bindActions({
@mmerickel
mmerickel / models:__init__.py
Last active August 29, 2015 14:22
model with separate modules
from .meta import DBSession
from .users import User
from .entries import Entry
@mmerickel
mmerickel / env.py
Last active August 29, 2015 14:20
parsing settings from a pastedeploy-style ini file
import logging.config
import os.path
from alembic import context
from sqlalchemy import create_engine, pool
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
@mmerickel
mmerickel / main.py
Last active August 17, 2016 20:09
sqlalchemy with no threadlocals
from pyramid.config import Configurator
from pyramid.view import view_config
def main(global_config, **app_settings):
config = Configurator(settings=app_settings)
config.include('.model')
config.scan(__name__)
return config.make_wsgi_app()