示例#1
0
class UserModel(DB.Model):
    """ User model has user related functionality such as saving and retrieving from db"""

    __tablename__ = "user"

    id = DB.Column(DB.Integer, primary_key=True)
    email = DB.Column(DB.String(80))
    password = DB.Column(DB.String(80))

    def __init__(self, email: str, password: str):
        self.email = email
        self.password = password

    def save_to_db(self):
        """ Saves (UserModel) object to database"""
        DB.session.add(self)
        DB.session.commit()

    def to_json(self) -> dict:
        """ returns dict representation of (UserModel)"""
        return {"id": self.id, "email": self.email, "password": self.password}

    @classmethod
    def find_by_email(cls, email: str) -> "UserModel":
        """Gets user with given email from database
        :param email(String) email of the user
        """
        return cls.query.filter_by(email=email).first() or None

    @classmethod
    def find_by_id(cls, _id: int) -> "UserModel":
        """Gets user with given id from database
        :param _id(int) id of the user
        """
        return cls.query.filter_by(id=_id).first() or None
示例#2
0
class TodoModel(DB.Model):
    """Todo model has all todo related functionality such as saving and retrieving from db"""

    __tablename__ = "todo"

    id = DB.Column(DB.Integer, primary_key=True)
    title = DB.Column(DB.String(256), nullable=False)
    desc = DB.Column(DB.String(256), nullable=True)
    status = DB.Column(DB.Boolean, nullable=False)
    created_at = DB.Column(DB.DateTime, default=datetime.utcnow())
    updated_at = DB.Column(DB.DateTime)
    owner = DB.Column(DB.Integer, DB.ForeignKey("user.id"))
    user = DB.relationship("UserModel")

    def __init__(self, title: str, desc: str, status: bool, owner: int):
        self.title = title
        self.desc = desc
        self.status = status
        self.owner = owner

    def to_json(self) -> dict:
        """ returns dict representation of the object"""
        return {
            "id": self.id,
            "title": self.title,
            "desc": self.desc,
            "status": self.status,
            "owner": self.owner,
        }

    def save_to_db(self):
        """Save (TodoModel) into db"""
        DB.session.add(self)
        DB.session.commit()

    def delete_from_db(self):
        """Delete (TodoModel) from db"""
        DB.session.delete(self)
        DB.session.commit()

    @classmethod
    def find_by_user(cls, user_id: str) -> "TodoModel":
        """return all todo items where owner is user_id"""
        return cls.query.filter_by(owner=user_id).all()

    @classmethod
    def find_by_id(cls, _id: int) -> "TodoModel":
        """returns one todo by id"""
        return cls.query.filter_by(id=_id).first()