Skip to content

Instantly share code, notes, and snippets.

View mmerickel's full-sized avatar

Michael Merickel mmerickel

View GitHub Profile
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 / 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
@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 / 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 / __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 / 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 / notfound.py
Created December 1, 2015 04:03
serving cache busted assets without renaming files on disk
_static_regex = re.compile(
r'''
(?P<root>/static/[a-zA-Z0-9._/-]+)
-
(?P<buster>[a-fA-F0-9]+)
(?P<ext>\.[a-zA-Z0-9]+)
$''',
re.VERBOSE,
)
@mmerickel
mmerickel / unity_gzip_tween.py
Last active November 8, 2016 18:34
pre-compressed unity files
UNITY_GZIP_FILES = frozenset([
'js',
'mem',
'data',
'unity3d',
])
def can_gzip_request(request):
if (
request.method in ('GET', 'HEAD') and
@mmerickel
mmerickel / basic_auth1.py
Last active March 6, 2016 17:04
roll your own basic auth in pyramid
"""
Let's pretend Pyramid doesn't have an enterprise-grade auth system
that can support several workflows. Make our own with decorators!
"""
import base64
from pyramid.httpexceptions import HTTPUnauthorized
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
@mmerickel
mmerickel / resolver.py
Created August 3, 2016 21:12
maybe_resolve dotted python path
import inspect
import sys
def caller_module(depth=1):
frm = inspect.stack()[depth + 1]
caller = inspect.getmodule(frm[0])
return caller
def caller_package(depth=1):
module = caller_module(depth + 1)