UNKNOWN
Project description
sqlalchemy_traversal
====================
This is a pyramid extension that allows you to use traversal with SQLAlchemy objects
Demo App here: https://github.com/eventray/sqlalchemy_traversal_demo
To use this you just have to include sqlalchemy_traversal in your pyramid application
by either putting it in your development.ini:
pyramid.includes =
sqlalchemy_traversal
or by including it in your main:
config.include('sqlalchemy_traversal')
Then you just register your SQLAlchemy session and declarative base:
from sqlalchemy_traversal.interfaces import ISASession
from sqlalchemy_traversal.interfaces import ISABase
config.registry.registerUtility(DBSession, ISASession)
config.registry.registerUtility(Base, ISABase)
And then place the TraversalMixin on any SQLAlchemy class and it will automatically
be traversed:
from sqlalchemy_traversal import TraversalMixin
class User(TraversalMixin, Base):
pass
Now you will be able to hit the URL /traverse/user to get all the users in your database
You can also tell it to load relationships via the _json_eager_load property:
class User(TraversalMixin, Base):
_json_eager_load = ['permissions']
Saving
==================================
If you want to be able to create data with your API but the content
coming back doesn't exactly match your model or you want to run it through
schema validation first you can use the register save decorator:
@register_save(MyModel, MySchema):
def saving_my_model(request, data):
data['my_prop'] = 'NEW DATA'
return data
You can also handle data exceptions with exception_handlers:
def handle_integrity_error(model, exception):
return {
'errors': {
'message': 'That data is not unique'
}
}
@register_save(
MyModel
, MySchema
, exception_handlers={
IntegrityError: handle_integrity_error
}
):
def saving_my_model(request, data):
data['my_prop'] = 'NEW DATA'
return data
====================
This is a pyramid extension that allows you to use traversal with SQLAlchemy objects
Demo App here: https://github.com/eventray/sqlalchemy_traversal_demo
To use this you just have to include sqlalchemy_traversal in your pyramid application
by either putting it in your development.ini:
pyramid.includes =
sqlalchemy_traversal
or by including it in your main:
config.include('sqlalchemy_traversal')
Then you just register your SQLAlchemy session and declarative base:
from sqlalchemy_traversal.interfaces import ISASession
from sqlalchemy_traversal.interfaces import ISABase
config.registry.registerUtility(DBSession, ISASession)
config.registry.registerUtility(Base, ISABase)
And then place the TraversalMixin on any SQLAlchemy class and it will automatically
be traversed:
from sqlalchemy_traversal import TraversalMixin
class User(TraversalMixin, Base):
pass
Now you will be able to hit the URL /traverse/user to get all the users in your database
You can also tell it to load relationships via the _json_eager_load property:
class User(TraversalMixin, Base):
_json_eager_load = ['permissions']
Saving
==================================
If you want to be able to create data with your API but the content
coming back doesn't exactly match your model or you want to run it through
schema validation first you can use the register save decorator:
@register_save(MyModel, MySchema):
def saving_my_model(request, data):
data['my_prop'] = 'NEW DATA'
return data
You can also handle data exceptions with exception_handlers:
def handle_integrity_error(model, exception):
return {
'errors': {
'message': 'That data is not unique'
}
}
@register_save(
MyModel
, MySchema
, exception_handlers={
IntegrityError: handle_integrity_error
}
):
def saving_my_model(request, data):
data['my_prop'] = 'NEW DATA'
return data
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file sqlalchemy_traversal-0.5.0.tar.gz
.
File metadata
- Download URL: sqlalchemy_traversal-0.5.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af26d48a045b331a78a36a41d346de01d8e06fb4c321b333747f65e5f9fba023 |
|
MD5 | fc11e5a60d70f2737194d2e0ae2c439d |
|
BLAKE2b-256 | 6d621a4cc4f4dec407df0500cbba3ca29271d8183d14a2fd3df05fe3866ca154 |