Skip to content

Quickstart sample

Gonrin edited this page Jun 5, 2018 · 1 revision

For the restapi:

from gatco import Gatco
from gatco_sqlalchemy import SQLAlchemy
from gatco_restapi import APIManager

# Create the Gatco application and the Gatco-SQLAlchemy object.
app = Gatco(name=__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.config['SECRET_KEY'] = 'secret'
app.config['SESSION_COOKIE_SALT'] = 'salt_key'

db = SQLAlchemy()
db.init_app(app)

# Create your Gatco-SQLALchemy models as usual but with the following two
# (reasonable) restrictions:
#   1. They must have a primary key column of type sqlalchemy.Integer or
#      type sqlalchemy.Unicode or type sqlalchemy.dialects.postgresql.UUID
#   2. They must have an __init__ method which accepts keyword arguments for
#      all columns.

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    birth_date = db.Column(db.Date)
    computers = db.relationship('Computer')

class Computer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    vendor = db.Column(db.Unicode)
    purchase_time = db.Column(db.DateTime)
    owner_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    owner = db.relationship('Person')

# Create the database tables.
db.create_all()

# Create the  APIManager.
manager = APIManager(app, sqlalchemy_db=db)

# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(Person, methods=['GET', 'POST', 'DELETE'])
manager.create_api(Computer, methods=['GET'])

# start the Gatco loop
app.run(host="0.0.0.0", port=8000)

Clone this wiki locally