How to use the safrs.db.SAFRSBase function in safrs

To help you get started, we’ve selected a few safrs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github thomaxxl / safrs / examples / v1_examples / demo_flask_admin.py View on Github external
if __name__ == "__main__":
    HOST = sys.argv[1] if len(sys.argv) > 1 else "0.0.0.0"
    PORT = 5000
    app = Flask("SAFRS Demo Application")
    CORS(app, origins="*", allow_headers=["Content-Type", "Authorization", "Access-Control-Allow-Credentials"], supports_credentials=True)
    app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://", DEBUG=True, SECRET_KEY="secret")
    db.init_app(app)
    db.app = app
    # Create the database
    db.create_all()
    admin = Admin(app, url="/admin")
    admin.add_view(UserView(User, db.session))
    admin.add_view(BookView(Book, db.session))
    SAFRSBase.db_commit

    API_PREFIX = "/api"
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)
    logging.getLogger(__name__).setLevel(logging.DEBUG)
    builtins.log = log

    with app.app_context():
        # Create a user and a book and add the book to the user.books relationship
        user = User(name="thomas", email="em@il")
        book = Book(name="test_book")
        user.books.append(book)
        db.session.add(user)
        db.session.add(book)
        db.session.commit()  # COMMIT because SAFRSBase.db_commit = False
github thomaxxl / safrs / examples / v1_examples / demo_hashid.py View on Github external
import sys

from flask import Flask, redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, String
from safrs.db import SAFRSBase, documented_api_method, SAFRSSHA256HashID
from safrs.jsonapi import SAFRSRestAPI, SAFRSJSONEncoder, Api
from flask_swagger_ui import get_swaggerui_blueprint


app = Flask("demo_app")
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://", DEBUG=True)
db = SQLAlchemy(app)

# Example sqla database object
class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    __tablename__ = "users"
    id = Column(String, primary_key=True)
    name = Column(String, default="")
    email = Column(String, default="")
    id_type = SAFRSSHA256HashID

    # Following method is exposed through the REST API
    # This means it can be invoked with a HTTP POST
    @documented_api_method
    def send_mail(self, email):
        """
            description : Send an email
github thomaxxl / safrs / examples / v1_examples / demo_flask_admin.py View on Github external
import builtins
from flask import Flask, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_swagger_ui import get_swaggerui_blueprint
from flask_cors import CORS
from sqlalchemy import Column, String, ForeignKey
from safrs.db import SAFRSBase, documented_api_method
from safrs.jsonapi import SAFRSJSONEncoder, Api

from flask_admin import Admin
from flask_admin import BaseView
from flask_admin.contrib import sqla

db = SQLAlchemy()
# Needed because we don't want to implicitly commit when using flask-admin
SAFRSBase.db_commit = False

# Example sqla database object
class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    __tablename__ = "Users"
    id = Column(String, primary_key=True)
    name = Column(String, default="")
    email = Column(String, default="")
    books = db.relationship("Book", back_populates="user", lazy="dynamic")

    # Following method is exposed through the REST API
    # This means it can be invoked with a HTTP POST
    @documented_api_method
github thomaxxl / safrs / examples / v1_examples / demo_cors.py View on Github external
app = Flask("demo_app")
CORS(
    app,
    origins="*",
    # methods=['GET', 'POST', 'PATCH', 'DELETE', 'OPTIONS'],
    allow_headers=["Content-Type", "Authorization", "Access-Control-Allow-Credentials"],
    supports_credentials=True,
)

app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://", SQLALCHEMY_TRACK_MODIFICATIONS=False, DEBUG=True)
db = SQLAlchemy(app)

# Example sqla database object
class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    __tablename__ = "users"
    id = Column(String, primary_key=True)
    name = Column(String, default="")
    email = Column(String, default="")

    crossdomain_kwargs = {"origin": "*", "methods": ["GET", "PATCH", "OPTIONS"], "headers": ["Content-Type"]}
    # Following method is exposed through the REST API
    # This means it can be invoked with a HTTP POST
    @documented_api_method
    def send_mail(self, email):
        """
            description : Send an email
github thomaxxl / safrs / examples / v1_examples / demo_flask_admin.py View on Github external
    @documented_api_method
    def send_mail(self, email):
        """
            description : Send an email
            args:
                email:
                    type : string
                    example : test email
        """
        content = "Mail to {} : {}\n".format(self.name, email)
        with open("/tmp/mail.txt", "a+") as mailfile:
            mailfile.write(content)
        return {"result": "sent {}".format(content)}


class Book(SAFRSBase, db.Model):
    """
        description: Book description
    """

    __tablename__ = "Books"
    id = Column(String, primary_key=True)
    name = Column(String, default="")
    user_id = Column(String, ForeignKey("Users.id"))
    user = db.relationship("User", back_populates="books")


class UserView(sqla.ModelView):
    pass


class BookView(sqla.ModelView):
github thomaxxl / safrs / examples / v1_examples / demo_json.py View on Github external
from flask import Flask, redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer, String
from safrs.db import SAFRSBase, documented_api_method
from safrs.jsonapi import SAFRSRestAPI, SAFRSJSONEncoder, Api
from flask_swagger_ui import get_swaggerui_blueprint
from flask_marshmallow import Marshmallow
from safrs.safrs_types import JSONType


app = Flask("demo_app")
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://", SQLALCHEMY_TRACK_MODIFICATIONS=False, DEBUG=True)
db = SQLAlchemy(app)

# Example sqla database object
class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    __tablename__ = "users"
    id = Column(String, primary_key=True)
    name = Column(String, default="")
    email = Column(String, default="")
    json = Column(JSONType, default={})

    # Following method is exposed through the REST API
    # This means it can be invoked with a HTTP POST
    @documented_api_method
    def send_mail(self, email):
        """
            description : Send an email
github thomaxxl / safrs / examples / v1_examples / demo_relationship_ext.py View on Github external
    @jsonapi_rpc(http_methods=["POST", "GET"])
    def send_mail(self, **args):
        """
        description : Send an email
        args:
            email:
                type : string
                example : test email
        """
        return {"result": args}

    startswith = startswith
    search = search


class Book(SAFRSBase, db.Model):
    """
        description: Book description
    """

    __tablename__ = "Books"
    id = db.Column(db.String, primary_key=True)
    name = db.Column(db.String, default="")
    user_id = db.Column(db.String, db.ForeignKey("Users.id"))
    user = db.relationship("User", back_populates="books")


if __name__ == "__main__":
    HOST = sys.argv[1] if len(sys.argv) > 1 else "0.0.0.0"
    PORT = 5000
    app = Flask("SAFRS Demo Application")
    CORS(app, origins="*", allow_headers=["Content-Type", "Authorization", "Access-Control-Allow-Credentials"], supports_credentials=True)
github thomaxxl / safrs / examples / v1_examples / demo_relationship_ext.py View on Github external
import sys
import logging
import builtins
from flask import Flask, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_swagger_ui import get_swaggerui_blueprint
from flask_cors import CORS
from safrs.db import SAFRSBase, jsonapi_rpc
from safrs.jsonapi import SAFRSJSONEncoder, Api, paginate, jsonapi_format_response, SAFRSFormattedResponse
from safrs.errors import ValidationError, GenericError
from safrs.api_methods import search, startswith

db = SQLAlchemy()

# Example sqla database object
class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    __tablename__ = "Users"
    id = db.Column(db.String, primary_key=True)
    name = db.Column(db.String, default="")
    email = db.Column(db.String, default="")
    books = db.relationship("Book", back_populates="user", lazy="dynamic")

    # Following method is exposed through the REST API
    # This means it can be invoked with a HTTP POST
    @classmethod
    @jsonapi_rpc(http_methods=["POST", "GET"])
    def send_mail(self, **args):
        """
github thomaxxl / safrs / examples / v1_examples / demo_flask_admin.py View on Github external
from flask_swagger_ui import get_swaggerui_blueprint
from flask_cors import CORS
from sqlalchemy import Column, String, ForeignKey
from safrs.db import SAFRSBase, documented_api_method
from safrs.jsonapi import SAFRSJSONEncoder, Api

from flask_admin import Admin
from flask_admin import BaseView
from flask_admin.contrib import sqla

db = SQLAlchemy()
# Needed because we don't want to implicitly commit when using flask-admin
SAFRSBase.db_commit = False

# Example sqla database object
class User(SAFRSBase, db.Model):
    """
        description: User description
    """

    __tablename__ = "Users"
    id = Column(String, primary_key=True)
    name = Column(String, default="")
    email = Column(String, default="")
    books = db.relationship("Book", back_populates="user", lazy="dynamic")

    # Following method is exposed through the REST API
    # This means it can be invoked with a HTTP POST
    @documented_api_method
    def send_mail(self, email):
        """
            description : Send an email
github thomaxxl / safrs / examples / v1_examples / demo_relationship_mysql.py View on Github external
description : Send an email
        args:
            email:
                type : string
                example : test email
        """
        return {"result": args}

    startswith = startswith
    search = search


User.id_type = get_id_type_mysql(DB_NAME, "Users", User)


class Book(SAFRSBase, db.Model):
    """
        description: Book description
    """

    __tablename__ = "Books"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(myString, default="")
    user_id = db.Column(db.Integer, db.ForeignKey("Users.id"))
    user = db.relationship("User", back_populates="books")


Book.id_type = get_id_type_mysql(DB_NAME, "Books", Book)


if __name__ == "__main__":
    HOST = sys.argv[1] if len(sys.argv) > 1 else "0.0.0.0"