This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyramid.authorization import ACLAuthorizationPolicy | |
from pyramid.security import ( | |
Authenticated, | |
Everyone, | |
) | |
log = __import__('logging').getLogger(__name__) | |
class OAuthAuthenticationPolicy(object): | |
def unauthenticated_userid(self, request): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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, | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UNITY_GZIP_FILES = frozenset([ | |
'js', | |
'mem', | |
'data', | |
'unity3d', | |
]) | |
def can_gzip_request(request): | |
if ( | |
request.method in ('GET', 'HEAD') and |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |