Welcome to ripozo-sqlachemy’s documentation!

Contents:

ripozo-sqlalchemy

test status test coverage Documentation Status

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.

Full Documentation

Example

This is a minimal example of creating ripozo managers with ripozo-sqlalchemy and integrating them with a resource.

First we need to setup our SQLAlchemy model.

from ripozo import apimethod, ResourceBase

from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base

# Setup the database with sqlalchemy
engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base()

# 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()

Now we can get to the ripozo-sqlalchemy part.

from ripozo_sqlalchemy import AlchemyManager, ScopedSessionHandler

# A session handler if responsible for getting
# And handling a session after either a successful or unsuccesful request
session_handler = ScopedSessionHandler(engine)

# 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):
    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(session_handler)
    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)

Easy Resources

Alternatively, we could use the create_resource method which will automatically create a manager and resource that corresponds to the manager.

from ripozo import restmixins
from ripozo_sqlalchemy import ScopedSessionHandler, create_resource

session_handler = ScopedSessionHandler(engine)
person_resource_class = create_resource(Person, session_handler)

By default create_resource will give you full CRUD+L (Create, Retrieve, Update, Delete, List). Although there are many options that you can pass to create_resource to modify exactly how the resource class is constructed.

After you create your resource class, you will need to load it into a dispatcher corresponding to your framework. For example, in flask-ripozo

from flask import Flask
from flask_ripozo import FlaskDispatcher
from ripozo.adapters import SirenAdapter, HalAdapter # These are the potential formats to return

app = Flask(__name__)
dispatcher = FlaskDispatcher(app)
dispatcher.register_adapters(SirenAdapter, HalAdapter)
dispatcher.register_resources(person_resource_class)
# or in the first style of generating resources
# dispatcher.register_resources(PersonResource)

app.run()

1.0.1 (unreleased)

  • Easy resources updated to use create_fields, update_fields, and list_fields options.

1.0.0 (2015-06-30)

  • Added _COLUMN_FIELD_MAP for determining python type. Transparent change.

1.0.0b1 (2015-06-29)

  • Fixed bug in retrieve_list with improperly named link to previous (“prev” –> “previous”)
  • Removed all fields flag.
  • Renamed alcehmymanager to alchemymanager
  • easy resources added. By simply calling create_resource with a session_handler and sqlalchemy model, you can automatically create a full resource. and immediately add it to a dispatcher.

0.2.0 (2015-06-08)

  • Tests fixed.

0.2.0b1 (2015-06-05)

  • Breaking Change: You are now required to inject a session handler on instantiation of the manager.

0.1.6b1 (2015-06-04)

  • Sessions are only grabbed once in any given method. This allows you to safely return a new session every time
  • Added a method for after a CRUD statement has been called.

0.1.5 (2015-04-28)

  • Optimization for retrieving lists using AlchemyManager.list_fields property for retrieving lists
  • Retrieve list now properly applies filters.
  • meta links updated in retrieve_list. They now are contained in the links dictionary
  • previous linked rename to prev in retrieve_list meta information

0.1.4 (2015-03-26)

  • Nothing changed yet.

0.1.3 (2015-03-26)

  • Nothing changed yet.

0.1.2 (2015-03-24)

  • NotFoundException raised when retrieve is called and no model is found.

0.1.1 (2015-03-15)

  • Added convience attribute for using all of the columns on the model.

Indices and tables