Last active
December 11, 2015 03:09
-
-
Save binarymatt/4536149 to your computer and use it in GitHub Desktop.
cornice with sqlalchemy
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 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