Created
August 9, 2019 10:38
-
-
Save antonbezkrovny/226d3e1c63c8f578affe3ea78465198f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| from flask import Flask | |
| from flask_cors import CORS | |
| from flask_rest_jsonapi import Api, ResourceDetail, ResourceList, ResourceRelationship | |
| from flask_sqlalchemy import SQLAlchemy | |
| from marshmallow_jsonapi.flask import Schema, Relationship | |
| from marshmallow_jsonapi import fields | |
| import os | |
| basedir = os.path.abspath(os.path.dirname(__file__)) | |
| # Create the Flask application | |
| app = Flask(__name__) | |
| cors = CORS(app) | |
| app.config['DEBUG'] = True | |
| app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.db') | |
| db = SQLAlchemy(app) | |
| class Band(db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| name = db.Column(db.String) | |
| description = db.Column(db.String) | |
| songs = db.relationship('Song') | |
| class Song(db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| name = db.Column(db.String) | |
| rating = db.Column(db.Integer) | |
| bands_id = db.Column(db.Integer, db.ForeignKey('band.id')) | |
| bands = db.relationship('Band', backref=db.backref('song')) | |
| db.create_all() | |
| class BandSchema(Schema): | |
| class Meta: | |
| type_ = 'bands' | |
| self_view = 'band_one' | |
| self_view_kwargs = {'id': '<id>'} | |
| self_view_many = 'band_many' | |
| id = fields.Integer(as_string=True, dump_only=True) | |
| name = fields.Str(required=True) | |
| description = fields.Str() | |
| songs = Relationship( | |
| self_view='band_songs', | |
| self_view_kwargs={'id': '<id>'}, | |
| many=True, | |
| include_resource_linkage=True, | |
| schema='SongSchema', | |
| type_="songs",) | |
| class SongSchema(Schema): | |
| class Meta: | |
| type_ = 'songs' | |
| self_view = 'song_one' | |
| self_view_kwargs = {'id':'<id>'} | |
| self_view_many = 'song_many' | |
| id = fields.Integer(as_string=True, dump_only=True) | |
| name = fields.Str(required=True) | |
| rating = fields.Integer() | |
| bands = Relationship( | |
| schema='BandSchema', type_= 'bands') | |
| # Create resource managers | |
| class BandMany(ResourceList): | |
| schema = BandSchema | |
| data_layer = {'session': db.session, | |
| 'model': Band} | |
| class BandOne(ResourceDetail): | |
| schema = BandSchema | |
| data_layer = {'session': db.session, | |
| 'model': Band} | |
| class SongMany(ResourceList): | |
| schema = SongSchema | |
| data_layer = {'session': db.session, | |
| 'model': Song} | |
| class SongOne(ResourceDetail): | |
| schema = SongSchema | |
| data_layer = {'session': db.session, | |
| 'model': Song} | |
| class BandSong(ResourceRelationship): | |
| schema = BandSchema | |
| data_layer = {'session': db.session, | |
| 'model': Band} | |
| # Create en | |
| api = Api(app) | |
| api.route(BandMany, 'band_many', '/bands') | |
| api.route(BandOne, 'band_one', '/bands/<int:id>') | |
| api.route(SongMany, 'song_many', '/songs') | |
| api.route(SongOne, 'song_one', '/songs/<int:id>') | |
| api.route(BandSong,'band_songs', '/bands/<int:id>/relationships/songs' ) | |
| if __name__ == '__main__': | |
| # Start application | |
| app.run(debug=True) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
flask-json-restapi for rock and roll with emberjs book - https://www.balinterdi.com/rock-and-roll-with-emberjs/