Skip to content

Instantly share code, notes, and snippets.

View mmerickel's full-sized avatar

Michael Merickel mmerickel

View GitHub Profile
@mmerickel
mmerickel / gist:5286023
Created April 1, 2013 16:32
poor man's rest resource in pyramid
class rest_resource(object):
all_methods = frozenset([
'head', 'get', 'post', 'put', 'patch', 'delete'])
def __init__(self, **kw):
self.kw = kw # view defaults
def __call__(self, wrapped):
# grab the supported methods from the class
methods = self.all_methods.intersection(set(vars(wrapped).keys()))
from pyramid.response import Response
from pyramid.view import view_config
class Test12:
def __init__(self, request):
self.request = request
@view_config(route_name='test1')
def test1(self):
return Response('I am from test 1')
@mmerickel
mmerickel / resources.py
Created February 9, 2011 23:21
Adapted example from rocky on #pylons for wrapping SQLA objects into a resource tree for pyramid's traversal algorithm.
from pyramid.decorator import reify
from pyramid_traversalwrapper import LocationProxy
class traversable_attrs(object):
""" A decorator that adds a "wrap" attribute to the given class
in the form of a dict-like class that does item lookup based on
the attrs given.
"""
@mmerickel
mmerickel / gist:805439
Created February 1, 2011 05:00
How to setup sqlalchemy without scoped_sessions or circular imports.
# tutorial/__init__.py
from pyramid.config import Configurator
from tutorial.model.meta import init_model
from tutorial.request import TutorialRequest
def main(global_config, **settings):
init_model(settings)
@mmerickel
mmerickel / root.py
Created January 27, 2011 00:20 — forked from wwitzel3/root.py
class DayZeroContainer(ModelContainer):
def __init__(self, request, cls):
self.cls = cls
self.request = request
@property
def __acl__(self):
return [
(Allow, 'owner:{0}'.format(cls.ownerid), ('add', 'edit)),
# ... some other acls for this particular resource?
def commit_veto(environ, status, headers):
for good in ('1', '2', '3'):
if status.startswith(good):
break
else:
return True
for header_name, header_value in headers:
if header_name.lower() == 'x-tm-abort':
return True