Integrates SQLAlchemy with ripozo to easily create sqlalchemy backed Hypermedia/HATEOAS/REST apis
Project description
ripozo-sqlalchemy
This package is a ripozo extension that provides a Manager that integrate SQLAlchemy with ripozo. It provides convience functions for generating resources. In particular, it focuses on creating shortcuts for CRUD type operations. It fully implements the BaseManager class that is provided in the ripozo package.
Example
This is a minimal example of creating ripozo managers with ripozo-sqlalchemy and integrating them with a resource.
from ripozo.decorators import apimethod
from ripozo.viewsets.resource_base import ResourceBase
from ripozo_sqlalchemy import AlchemyManager
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Setup the database with sqlalchemy
Base = declarative_base(create_engine('sqlite:///:memory:', echo=True))
session = session_maker()()
# Declare your ORM model
class Person(Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
# Sync the models wiht the database
Base.metadata.create_all()
# This is the code that is specific to ripozo-sqlalchemy
# You give it the session, a SQLAlchemy Model, and the fields
# You wish to serialize at a minimum.
class PersonManager(AlchemyManager):
session = session
model = Person
fields = ('id', 'first_name', 'last_name')
# This is the ripozo specific part.
# This creates a resource class that can be given
# to a dispatcher (e.g. the flask-ripozo package's FlaskDispatcher)
class PersonResource(ResourceBase):
_manager = PersonManager
_pks = ['id']
_namespace = '/api'
# A retrieval method that will operate on the '/api/person' route
# It retrieves the id, first_name, and last_name properties
@apimethod(methods=['GET'])
def get_person(cls, primary_keys, filters, values, *args, **kwargs):
properties = self.manager.retrieve(primary_keys)
return cls(properties=properties)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ripozo-sqlalchemy-0.1.6b1.zip
(16.1 kB
view hashes)