class Publisher(BaseModel): """ description: Publisher description --- demonstrate custom (de)serialization in __init__ and to_dict """ __tablename__ = "Publishers" allow_client_generated_ids = True id = db.Column(db.Integer, primary_key=True) # Integer pk instead of str name = db.Column(db.String, default="") books = db.relationship("Book", back_populates="publisher", lazy="dynamic") #books = db.relationship("Book", back_populates="publisher") duplicate = duplicate unexposed_books = db.relationship("UnexpBook", back_populates="publisher", lazy="dynamic") data = db.Column(db.JSON, default = {1:1}) def __init__(self, *args, **kwargs): custom_field = kwargs.pop("custom_field", None) super().__init__(self, **kwargs) def to_dict(self): result = SAFRSBase.to_dict(self) result["custom_field"] = "some customization" return result @classmethod def _s_filter(cls, arg): """ Sample custom filtering, override this method to implement custom filtering using the sqlalchemy orm """ return cls.query.filter_by(name=arg)
class Book(BaseModel): """ description: Book description """ __tablename__ = "Books" id = db.Column(db.String, primary_key=True) title = db.Column(db.String, default="") reader_id = db.Column(db.String, db.ForeignKey("People.id")) author_id = db.Column(db.String, db.ForeignKey("People.id")) publisher_id = db.Column(db.Integer, db.ForeignKey("Publishers.id")) publisher = db.relationship("Publisher", back_populates="books") reviews = db.relationship( "Review", backref="book", cascade="save-update, merge, delete, delete-orphan")
class SubThing(BaseModel): __tablename__ = "subthing" _s_auto_commit = True id = db.Column(db.String, primary_key=True, server_default=func.uuid_generate_v1()) name = db.Column(db.String, nullable=False) thing_id = db.Column(db.String, db.ForeignKey("thing.id")) thing = db.relationship("Thing", foreign_keys=thing_id)
class Book2(SAFRSBase, db.Model): """ description: Book description """ __tablename__ = "Books2" id = db.Column(db.String, primary_key=True) name = db.Column(db.String, default="") user_id = db.Column(db.String, db.ForeignKey("Users2.id")) user = db.relationship("User", back_populates="books")
class User(SAFRSBase, db.Model): """ description: User description """ __tablename__ = "Users2" id = db.Column(db.String, primary_key=True) name = db.Column(db.String, default="") email = db.Column(db.String, default="") books = db.relationship("Book2", back_populates="user", lazy="dynamic")
class UnexpBook(db.Model): """ description: Book description """ __tablename__ = "UnexpBooks" #id = db.Column(db.Integer, primary_key=True, auto_increment=True) id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, default="") publisher_id = db.Column(db.Integer, db.ForeignKey("Publishers.id")) publisher = db.relationship("Publisher", back_populates="unexposed_books")
class Person(BaseModel): """ description: People description """ __tablename__ = "People" id = db.Column(db.String, primary_key=True) name = db.Column(db.String, default="") email = db.Column(db.String, default="") comment = db.Column(db.Text, default="") dob = db.Column(db.Date) books_read = db.relationship( "Book", backref="reader", foreign_keys=[Book.reader_id], cascade="save-update, merge", ) books_written = db.relationship("Book", backref="author", foreign_keys=[Book.author_id]) reviews = db.relationship("Review", backref="reader") password = db.Column(db.Text, default="") exclude_attrs = ["password"] # Following methods are exposed through the REST API @jsonapi_rpc(http_methods=["POST"]) def send_mail(self, email): """ description : Send an email args: email: test email parameters: - name : my_query_string_param default : my_value """ content = "Mail to {} : {}\n".format(self.name, email) with open("/tmp/mail.txt", "a+") as mailfile: mailfile.write(content) return {"result": "sent {}".format(content)} @classmethod @jsonapi_rpc(http_methods=["GET", "POST"]) def my_rpc(cls, *args, **kwargs): """ description : Generate and return a jsonapi-formatted response pageable: false parameters: - name : my_query_string_param default : my_value args: email: test email """ print(args) print(kwargs) response = SAFRSFormattedResponse() try: instances = cls.query links, instances, count = paginate(instances) data = [item for item in instances] meta = {"args": args, "kwargs": kwargs} errors = None response.response = jsonapi_format_response( data, meta, links, errors, count) except Exception as exc: safrs.log.exception(exc) return response