Last active
March 16, 2021 11:36
-
-
Save thomaxxl/def9cb5a849f3b508ec4aaac66f477c9 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
from flask import Flask | |
from flask_sqlalchemy import SQLAlchemy | |
from safrs import SAFRSBase, SAFRSAPI, jsonapi_attr | |
db = SQLAlchemy() | |
class User(SAFRSBase, db.Model): | |
""" | |
description: User description | |
""" | |
__tablename__ = "Users" | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String) | |
email = db.Column(db.String) | |
@jsonapi_attr | |
def test_attr(self): | |
return 'blah' | |
@test_attr.setter | |
def test_attr(self, val): | |
print(f"Setting test_attr: '{val}'") | |
User.test_attr = test_attr | |
def create_api(app, host="localhost", port=5000, api_prefix=""): | |
api = SAFRSAPI(app, host=host, port=port, prefix=api_prefix) | |
api.expose_object(User) | |
user = User(name="test", email="email@x.org") | |
print(f"Created API: http://{host}:{port}/{api_prefix}") | |
def create_app(config_filename=None, host="localhost"): | |
app = Flask("demo_app") | |
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://") | |
db.init_app(app) | |
with app.app_context(): | |
db.create_all() | |
create_api(app, host) | |
return app | |
host = "localhost" | |
app = create_app(host=host) | |
if __name__ == "__main__": | |
app.run(host=host) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment