Skip to content

Use Pydantic models instead of reading from raw JSON objects #45

@gcarvellas

Description

@gcarvellas

Currently in a lot of parts of the code (Ex: all of the database files), we take the data directly from JSONs and pass it around into the controller or managers. Instead of passing this into the controllers, managers, it would be nice to pass the JSON into a pydantic model instead.

Example:

In database/users.py, the _new_user() function looks like this:

    @classmethod
    def _new_user(cls, _id: str, username: str, vendor_type: str) -> JSONDict:
        return {
            "_id": _id,
            "username": username,
            "contracts": [],
            "group": Groups.CUSTOMER,
            "vendor_type": vendor_type,
            "roles": []
        }

Instead we can define a model and use that

class MongoUser(BaseModel):
   _id: str
   username: str
   contracts: List<str>
   ...


    @classmethod
    def _new_user(cls, _id: str, username: str, vendor_type: str) -> JSONDict:
        return MongoUser(...)

Metadata

Metadata

Assignees

Labels

cleanupCleaning up code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions