Open-event-server: Deduplicate marshmallow schema by using marshmallow-sqlalchemy

Created on 25 Sep 2019  Â·  18Comments  Â·  Source: fossasia/open-event-server

There is a lot of repetition between sqlalchemy models and marshmallow schema

Use https://marshmallow-sqlalchemy.readthedocs.io to remove this duplication

This is a parent issue and new contributors can take an API component and send contributions converting their schemas

good-first-issue help-wanted open-for-all parent issue refactor

Most helpful comment

@kushthedude @iamareebjamal @prateekj117 I am working on the event schemas.

All 18 comments

Hi, I would like to work on this issue. This will be my first pull request here. Can you help me with where to start?

Take a look at codebase and marshmallow schema files. Read about the above mentioned library and tell which component will you start working on here

@iamareebjamal I am willing towork on any of the schemas but I am not able to figure out the repetitions. I see that models serialize the data. Can you give an example?

Can you please take a look at the library I have linked in the issue and read about its usage. The issue will become apparent

@iamareebjamal I went through the library. I need to rewrite the models and schemas using marshmallow-sqlalchemy but I am unable to find the repetiton. As per the documentation we still need to deserialize after the schema class. Sorry I am new but could you show me an example?

I would like to work on this issue @iamareebjamal @prateekj117

@kushthedude @iamareebjamal @prateekj117 I am working on the event schemas.

@iamareebjamal @prateekj117 taking this up

@iamareebjamal Hey! I'd like to take this issue.
So basically i just have to remove the entire marshmallow schema(repeated) and put the sqlalchemy model there
eg -> model = Event
under Meta class in EventSchemaPublic.
And similarly for other API components.
Right?

If it works, yes

Can I explore a bit and work on this, if there's still more to do?

@iamareebjamal :
Hi,
If this issue is till open, can I work on this ?

Yes

@iamareebjamal :

As per the latest update on: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/

For the model Book relationship with model Author, there are changes required in the Schema of Author and Book as well.

Models:

class Author(Base):
    __tablename__ = "authors"
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String, nullable=False)

    def __repr__(self):
        return "<Author(name={self.name!r})>".format(self=self)


class Book(Base):
    __tablename__ = "books"
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String)
    author_id = sa.Column(sa.Integer, sa.ForeignKey("authors.id"))
    author = relationship("Author", backref=backref("books"))

Schema:

class AuthorSchema(SQLAlchemyAutoSchema):
    class Meta:
        model = Author
        include_relationships = True # 
        load_instance = True


class BookSchema(SQLAlchemyAutoSchema):
    class Meta:
        model = Book
        include_fk = True #
        load_instance = True

Hey would like to contribute to this

Hey, I want to work on this. Can I contribute?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SaptakS picture SaptakS  Â·  3Comments

Masquerade0097 picture Masquerade0097  Â·  3Comments

aditya1702 picture aditya1702  Â·  4Comments

Aju100 picture Aju100  Â·  4Comments

CosmicCoder96 picture CosmicCoder96  Â·  4Comments