Skip to content

Instantly share code, notes, and snippets.

@binarymatt
Last active December 11, 2015 03:09
Show Gist options
  • Select an option

  • Save binarymatt/4536149 to your computer and use it in GitHub Desktop.

Select an option

Save binarymatt/4536149 to your computer and use it in GitHub Desktop.
cornice with sqlalchemy
from cornice.resource import resource
def dbresource(**kw):
"""Class decorator to declare CRUD classes.
"""
def wrapper(klass):
setattr(klass, kw.pop('mapping'))
setattr(klass, kw.pop('session'))
setattr(klass, kw.pop('match_key', 'id'))
setattr(klass, kw.pop('primary_key', 'id'))
if klass.mapping is None:
raise ValueError('You need to define a mapping when calling @crud')
if klass.session is None:
raise ValueError('You need to define a session when calling @crud')
klass.dbsession = klass.session()
klass.cols = klass.mapping.__table__.c.keys()
klass = resource(**kw)(klass)
return klass
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment