Skip to main content

UNKNOWN

Project description

ripozo-sqlalchemy

Travis CI Status 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.

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


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.0.tar.gz (4.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page